From f18580d6944edf142031abb8a29e3cc633ff5959 Mon Sep 17 00:00:00 2001 From: Tyagi-Sunny Date: Wed, 25 Sep 2024 16:16:27 +0530 Subject: [PATCH] feat(tenant-management): add db details add db details BREAKING CHANGE: yes 43 --- ...20240925102459-add-table-tenant-configs.js | 53 +++++++ ...25102459-add-table-tenant-configs-down.sql | 1 + ...0925102459-add-table-tenant-configs-up.sql | 19 +++ .../src/component.ts | 4 + .../src/controllers/index.ts | 2 + .../tenant-config-tenant.controller.ts | 38 +++++ .../controllers/tenant-config.controller.ts | 150 ++++++++++++++++++ .../src/models/index.ts | 1 + .../src/models/tenant-config.model.ts | 46 ++++++ .../src/repositories/index.ts | 1 + .../repositories/tenant-config.repository.ts | 27 ++++ 11 files changed, 342 insertions(+) create mode 100644 services/tenant-management-service/migrations/pg/migrations/20240925102459-add-table-tenant-configs.js create mode 100644 services/tenant-management-service/migrations/pg/migrations/sqls/20240925102459-add-table-tenant-configs-down.sql create mode 100644 services/tenant-management-service/migrations/pg/migrations/sqls/20240925102459-add-table-tenant-configs-up.sql create mode 100644 services/tenant-management-service/src/controllers/tenant-config-tenant.controller.ts create mode 100644 services/tenant-management-service/src/controllers/tenant-config.controller.ts create mode 100644 services/tenant-management-service/src/models/tenant-config.model.ts create mode 100644 services/tenant-management-service/src/repositories/tenant-config.repository.ts diff --git a/services/tenant-management-service/migrations/pg/migrations/20240925102459-add-table-tenant-configs.js b/services/tenant-management-service/migrations/pg/migrations/20240925102459-add-table-tenant-configs.js new file mode 100644 index 0000000..2052c51 --- /dev/null +++ b/services/tenant-management-service/migrations/pg/migrations/20240925102459-add-table-tenant-configs.js @@ -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 +}; diff --git a/services/tenant-management-service/migrations/pg/migrations/sqls/20240925102459-add-table-tenant-configs-down.sql b/services/tenant-management-service/migrations/pg/migrations/sqls/20240925102459-add-table-tenant-configs-down.sql new file mode 100644 index 0000000..3284c15 --- /dev/null +++ b/services/tenant-management-service/migrations/pg/migrations/sqls/20240925102459-add-table-tenant-configs-down.sql @@ -0,0 +1 @@ +drop table main.tenant_configs; \ No newline at end of file diff --git a/services/tenant-management-service/migrations/pg/migrations/sqls/20240925102459-add-table-tenant-configs-up.sql b/services/tenant-management-service/migrations/pg/migrations/sqls/20240925102459-add-table-tenant-configs-up.sql new file mode 100644 index 0000000..1e36149 --- /dev/null +++ b/services/tenant-management-service/migrations/pg/migrations/sqls/20240925102459-add-table-tenant-configs-up.sql @@ -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 +) \ No newline at end of file diff --git a/services/tenant-management-service/src/component.ts b/services/tenant-management-service/src/component.ts index 33fc487..8f57e7b 100644 --- a/services/tenant-management-service/src/component.ts +++ b/services/tenant-management-service/src/component.ts @@ -60,6 +60,7 @@ import { TenantOnboardDTO, VerifyLeadResponseDTO, WebhookDTO, + TenantConfig, } from './models'; import { AddressRepository, @@ -70,6 +71,7 @@ import { ResourceRepository, TenantRepository, WebhookSecretRepository, + TenantConfigRepository, } from './repositories'; import {LeadTokenVerifierProvider, SystemUserProvider} from './providers'; import { @@ -120,6 +122,7 @@ export class TenantManagementServiceComponent implements Component { ResourceRepository, TenantRepository, WebhookSecretRepository, + TenantConfigRepository ]; this.models = [ @@ -136,6 +139,7 @@ export class TenantManagementServiceComponent implements Component { TenantOnboardDTO, VerifyLeadResponseDTO, WebhookDTO, + TenantConfig ]; this.controllers = [ diff --git a/services/tenant-management-service/src/controllers/index.ts b/services/tenant-management-service/src/controllers/index.ts index 8406aab..90a9866 100644 --- a/services/tenant-management-service/src/controllers/index.ts +++ b/services/tenant-management-service/src/controllers/index.ts @@ -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'; diff --git a/services/tenant-management-service/src/controllers/tenant-config-tenant.controller.ts b/services/tenant-management-service/src/controllers/tenant-config-tenant.controller.ts new file mode 100644 index 0000000..fe79d87 --- /dev/null +++ b/services/tenant-management-service/src/controllers/tenant-config-tenant.controller.ts @@ -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 { + return this.tenantConfigRepository.tenant(id); + } +} diff --git a/services/tenant-management-service/src/controllers/tenant-config.controller.ts b/services/tenant-management-service/src/controllers/tenant-config.controller.ts new file mode 100644 index 0000000..b1bbcdf --- /dev/null +++ b/services/tenant-management-service/src/controllers/tenant-config.controller.ts @@ -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, + ): Promise { + 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, + ): Promise { + 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, + ): Promise { + 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, + ): Promise { + 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 + ): Promise { + 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 { + 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 { + 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 { + await this.tenantConfigRepository.deleteById(id); + } +} diff --git a/services/tenant-management-service/src/models/index.ts b/services/tenant-management-service/src/models/index.ts index 02b5768..10cb796 100644 --- a/services/tenant-management-service/src/models/index.ts +++ b/services/tenant-management-service/src/models/index.ts @@ -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'; \ No newline at end of file diff --git a/services/tenant-management-service/src/models/tenant-config.model.ts b/services/tenant-management-service/src/models/tenant-config.model.ts new file mode 100644 index 0000000..815d64d --- /dev/null +++ b/services/tenant-management-service/src/models/tenant-config.model.ts @@ -0,0 +1,46 @@ +import {Entity, model, property, belongsTo} from '@loopback/repository'; +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) { + super(data); + } +} diff --git a/services/tenant-management-service/src/repositories/index.ts b/services/tenant-management-service/src/repositories/index.ts index 5c5157a..543778d 100644 --- a/services/tenant-management-service/src/repositories/index.ts +++ b/services/tenant-management-service/src/repositories/index.ts @@ -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'; \ No newline at end of file diff --git a/services/tenant-management-service/src/repositories/tenant-config.repository.ts b/services/tenant-management-service/src/repositories/tenant-config.repository.ts new file mode 100644 index 0000000..34c158b --- /dev/null +++ b/services/tenant-management-service/src/repositories/tenant-config.repository.ts @@ -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; + + constructor( + @inject.getter(SYSTEM_USER) + public readonly getCurrentUser: Getter, + @inject(`datasources.${TenantManagementDbSourceName}`) + dataSource: juggler.DataSource, @repository.getter('TenantRepository') protected tenantRepositoryGetter: Getter, + ) { + super(TenantConfig, dataSource,getCurrentUser); + this.tenant = this.createBelongsToAccessorFor('tenant', tenantRepositoryGetter,); + this.registerInclusionResolver('tenant', this.tenant.inclusionResolver); + } +}