Skip to content

Commit

Permalink
feat(tenant-management): add db details
Browse files Browse the repository at this point in the history
add db details

BREAKING CHANGE:
yes

43
  • Loading branch information
Tyagi-Sunny committed Sep 25, 2024
1 parent d85099d commit f18580d
Show file tree
Hide file tree
Showing 11 changed files with 342 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20240925102459-add-table-tenant-configs-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20240925102459-add-table-tenant-configs-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop table main.tenant_configs;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE TABLE IF NOT EXISTS main.tenant_configs
(
id uuid NOT NULL DEFAULT (md5(((random())::text || (clock_timestamp())::text)))::uuid,
config_key character varying(100) COLLATE pg_catalog."default" NOT NULL,
config_value jsonb NOT NULL,
created_on timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
modified_on timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
created_by integer,
modified_by integer,
deleted boolean NOT NULL DEFAULT false,
tenant_id uuid NOT NULL,
deleted_by uuid,
deleted_on timestamp with time zone,
CONSTRAINT pk_tenant_configs_id PRIMARY KEY (id),
CONSTRAINT fk_tenant_configs_tenants FOREIGN KEY (tenant_id)
REFERENCES main.tenants (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
4 changes: 4 additions & 0 deletions services/tenant-management-service/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
TenantOnboardDTO,
VerifyLeadResponseDTO,
WebhookDTO,
TenantConfig,
} from './models';
import {
AddressRepository,
Expand All @@ -70,6 +71,7 @@ import {
ResourceRepository,
TenantRepository,
WebhookSecretRepository,
TenantConfigRepository,
} from './repositories';
import {LeadTokenVerifierProvider, SystemUserProvider} from './providers';
import {
Expand Down Expand Up @@ -120,6 +122,7 @@ export class TenantManagementServiceComponent implements Component {
ResourceRepository,
TenantRepository,
WebhookSecretRepository,
TenantConfigRepository
];

this.models = [
Expand All @@ -136,6 +139,7 @@ export class TenantManagementServiceComponent implements Component {
TenantOnboardDTO,
VerifyLeadResponseDTO,
WebhookDTO,
TenantConfig
];

this.controllers = [
Expand Down
2 changes: 2 additions & 0 deletions services/tenant-management-service/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export * from './lead-tenant.controller';
export * from './tenant.controller';
export * from './webhook.controller';
export * from './invoice.controller';
export * from './tenant-config.controller';
export * from './tenant-config-tenant.controller';
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
repository,
} from '@loopback/repository';
import {
param,
get,
getModelSchemaRef,
} from '@loopback/rest';
import {
TenantConfig,
Tenant,
} from '../models';
import {TenantConfigRepository} from '../repositories';

export class TenantConfigTenantController {
constructor(
@repository(TenantConfigRepository)
public tenantConfigRepository: TenantConfigRepository,
) { }

@get('/tenant-configs/{id}/tenant', {
responses: {
'200': {
description: 'Tenant belonging to TenantConfig',
content: {
'application/json': {
schema: getModelSchemaRef(Tenant),
},
},
},
},
})
async getTenant(
@param.path.string('id') id: typeof TenantConfig.prototype.id,
): Promise<Tenant> {
return this.tenantConfigRepository.tenant(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import {
Count,
CountSchema,
Filter,
FilterExcludingWhere,
repository,
Where,
} from '@loopback/repository';
import {
post,
param,
get,
getModelSchemaRef,
patch,
put,
del,
requestBody,
response,
} from '@loopback/rest';
import {TenantConfig} from '../models';
import {TenantConfigRepository} from '../repositories';

export class TenantConfigController {
constructor(
@repository(TenantConfigRepository)
public tenantConfigRepository : TenantConfigRepository,
) {}

@post('/tenant-configs')
@response(200, {
description: 'TenantConfig model instance',
content: {'application/json': {schema: getModelSchemaRef(TenantConfig)}},
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(TenantConfig, {
title: 'NewTenantConfig',
exclude: ['id'],
}),
},
},
})
tenantConfig: Omit<TenantConfig, 'id'>,
): Promise<TenantConfig> {
return this.tenantConfigRepository.create(tenantConfig);
}

@get('/tenant-configs/count')
@response(200, {
description: 'TenantConfig model count',
content: {'application/json': {schema: CountSchema}},
})
async count(
@param.where(TenantConfig) where?: Where<TenantConfig>,
): Promise<Count> {
return this.tenantConfigRepository.count(where);
}

@get('/tenant-configs')
@response(200, {
description: 'Array of TenantConfig model instances',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(TenantConfig, {includeRelations: true}),
},
},
},
})
async find(
@param.filter(TenantConfig) filter?: Filter<TenantConfig>,
): Promise<TenantConfig[]> {
return this.tenantConfigRepository.find(filter);
}

@patch('/tenant-configs')
@response(200, {
description: 'TenantConfig PATCH success count',
content: {'application/json': {schema: CountSchema}},
})
async updateAll(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(TenantConfig, {partial: true}),
},
},
})
tenantConfig: TenantConfig,
@param.where(TenantConfig) where?: Where<TenantConfig>,
): Promise<Count> {
return this.tenantConfigRepository.updateAll(tenantConfig, where);
}

@get('/tenant-configs/{id}')
@response(200, {
description: 'TenantConfig model instance',
content: {
'application/json': {
schema: getModelSchemaRef(TenantConfig, {includeRelations: true}),
},
},
})
async findById(
@param.path.string('id') id: string,
@param.filter(TenantConfig, {exclude: 'where'}) filter?: FilterExcludingWhere<TenantConfig>
): Promise<TenantConfig> {
return this.tenantConfigRepository.findById(id, filter);
}

@patch('/tenant-configs/{id}')
@response(204, {
description: 'TenantConfig PATCH success',
})
async updateById(
@param.path.string('id') id: string,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(TenantConfig, {partial: true}),
},
},
})
tenantConfig: TenantConfig,
): Promise<void> {
await this.tenantConfigRepository.updateById(id, tenantConfig);
}

@put('/tenant-configs/{id}')
@response(204, {
description: 'TenantConfig PUT success',
})
async replaceById(
@param.path.string('id') id: string,
@requestBody() tenantConfig: TenantConfig,
): Promise<void> {
await this.tenantConfigRepository.replaceById(id, tenantConfig);
}

@del('/tenant-configs/{id}')
@response(204, {
description: 'TenantConfig DELETE success',
})
async deleteById(@param.path.string('id') id: string): Promise<void> {
await this.tenantConfigRepository.deleteById(id);
}
}
1 change: 1 addition & 0 deletions services/tenant-management-service/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './resource.model';
export * from './invoice.model';
export * from './address.model';
export * from './lead-token.model';
export * from './tenant-config.model';
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {Entity, model, property, belongsTo} from '@loopback/repository';

Check failure on line 1 in services/tenant-management-service/src/models/tenant-config.model.ts

View workflow job for this annotation

GitHub Actions / npm_test

'Entity' is defined but never used
import { UserModifiableEntity } from '@sourceloop/core';
import {Tenant} from './tenant.model';

@model({
name: 'tenant_configs',
description: 'tenant_configs to save any tenant specific data related to idP'
})
export class TenantConfig extends UserModifiableEntity {
@property({
type: 'string',
id: true,
generated: true,
})
id: string;

@property({
type: 'string',
required: true,
name: 'config_key'
})
configKey: string;

@property({
type: 'object',
required: true,
name: 'config_value'
})
configValue: object;

@belongsTo(
() => Tenant,
{keyTo: 'id'},
{
type: 'string',
name: 'tenant_id',
description: 'id of the tenant this invoice is generated for',
required: true,
},
)
tenantId: string;

constructor(data?: Partial<TenantConfig>) {
super(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './resource.repository';
export * from './invoice.repository';
export * from './address.repository';
export * from './lead-token.repository';
export * from './tenant-config.repository';
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {Getter, inject} from '@loopback/core';
import {DefaultCrudRepository, juggler, repository, BelongsToAccessor} from '@loopback/repository';
import {TenantConfig, Tenant} from '../models';
import { DefaultUserModifyCrudRepository, IAuthUserWithPermissions } from '@sourceloop/core';
import { SYSTEM_USER } from '../keys';
import { TenantManagementDbSourceName } from '../types';
import {TenantRepository} from './tenant.repository';

export class TenantConfigRepository extends DefaultUserModifyCrudRepository<
TenantConfig,
typeof TenantConfig.prototype.id,
{}
> {

public readonly tenant: BelongsToAccessor<Tenant, typeof TenantConfig.prototype.id>;

constructor(
@inject.getter(SYSTEM_USER)
public readonly getCurrentUser: Getter<IAuthUserWithPermissions>,
@inject(`datasources.${TenantManagementDbSourceName}`)
dataSource: juggler.DataSource, @repository.getter('TenantRepository') protected tenantRepositoryGetter: Getter<TenantRepository>,
) {
super(TenantConfig, dataSource,getCurrentUser);
this.tenant = this.createBelongsToAccessorFor('tenant', tenantRepositoryGetter,);
this.registerInclusionResolver('tenant', this.tenant.inclusionResolver);
}
}

0 comments on commit f18580d

Please sign in to comment.