From 3216b04517939636892d740b59616accc58f00dd Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sun, 6 Oct 2024 07:21:35 +0000 Subject: [PATCH] refactor: avoid duplicate code block by moving logic to a method --- .../src/controllers/api-tokens.controller.ts | 5 ++++- .../src/controllers/org-tokens.controller.ts | 5 ++++- packages/nocodb/src/models/Integration.ts | 13 ++++++++----- packages/nocodb/src/models/Source.ts | 16 +++++++--------- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/nocodb/src/controllers/api-tokens.controller.ts b/packages/nocodb/src/controllers/api-tokens.controller.ts index 4cba808fce..1768d3b5e2 100644 --- a/packages/nocodb/src/controllers/api-tokens.controller.ts +++ b/packages/nocodb/src/controllers/api-tokens.controller.ts @@ -51,7 +51,10 @@ export class ApiTokensController { '/api/v2/meta/bases/:baseId/api-tokens/:tokenId', ]) @Acl('baseApiTokenDelete') - async apiTokenDelete(@Req() req: NcRequest, @Param('tokenId') tokenId: string) { + async apiTokenDelete( + @Req() req: NcRequest, + @Param('tokenId') tokenId: string, + ) { return await this.apiTokensService.apiTokenDelete({ tokenId, user: req['user'], diff --git a/packages/nocodb/src/controllers/org-tokens.controller.ts b/packages/nocodb/src/controllers/org-tokens.controller.ts index 80957aa1f9..be600a63f1 100644 --- a/packages/nocodb/src/controllers/org-tokens.controller.ts +++ b/packages/nocodb/src/controllers/org-tokens.controller.ts @@ -61,7 +61,10 @@ export class OrgTokensController { // allowedRoles: [OrgUserRoles.SUPER], blockApiTokenAccess: true, }) - async apiTokenDelete(@Req() req: NcRequest, @Param('tokenId') tokenId: string) { + async apiTokenDelete( + @Req() req: NcRequest, + @Param('tokenId') tokenId: string, + ) { await this.orgTokensService.apiTokenDelete({ tokenId, user: req['user'], diff --git a/packages/nocodb/src/models/Integration.ts b/packages/nocodb/src/models/Integration.ts index fef0660f42..60f4f16675 100644 --- a/packages/nocodb/src/models/Integration.ts +++ b/packages/nocodb/src/models/Integration.ts @@ -41,6 +41,11 @@ export default class Integration implements IntegrationType { return integration && new Integration(integration); } + protected static encryptConfigIfRequired(obj: Record) { + obj.config = encryptPropIfRequired({ data: obj }); + obj.is_encrypted = isEncryptionRequired(); + } + public static async createIntegration( integration: IntegrationType & { workspaceId?: string; @@ -63,11 +68,7 @@ export default class Integration implements IntegrationType { 'is_encrypted', ]); - insertObj.config = encryptPropIfRequired({ - data: insertObj, - }); - - insertObj.is_encrypted = isEncryptionRequired(); + this.encryptConfigIfRequired(insertObj); if ('meta' in insertObj) { insertObj.meta = stringifyMetaProp(insertObj); @@ -138,6 +139,8 @@ export default class Integration implements IntegrationType { data: updateObj, }); updateObj.is_encrypted = isEncryptionRequired(); + + this.encryptConfigIfRequired(updateObj); } // type property is undefined even if not provided diff --git a/packages/nocodb/src/models/Source.ts b/packages/nocodb/src/models/Source.ts index 7d6b934363..1181113e4d 100644 --- a/packages/nocodb/src/models/Source.ts +++ b/packages/nocodb/src/models/Source.ts @@ -62,6 +62,11 @@ export default class Source implements SourceType { return source && new Source(source); } + protected static encryptConfigIfRequired(obj: Record) { + obj.config = encryptPropIfRequired({ data: obj }); + obj.is_encrypted = isEncryptionRequired(); + } + public static async createBase( context: NcContext, source: SourceType & { @@ -91,11 +96,7 @@ export default class Source implements SourceType { 'is_encrypted', ]); - insertObj.config = encryptPropIfRequired({ - data: insertObj, - }); - - insertObj.is_encrypted = isEncryptionRequired(); + this.encryptConfigIfRequired(insertObj); if ('meta' in insertObj) { insertObj.meta = stringifyMetaProp(insertObj); @@ -158,10 +159,7 @@ export default class Source implements SourceType { ]); if (updateObj.config) { - updateObj.config = encryptPropIfRequired({ - data: updateObj, - }); - updateObj.is_encrypted = isEncryptionRequired(); + this.encryptConfigIfRequired(updateObj); } // type property is undefined even if not provided