From afd73c02a017510709f1e40822b0d56088e77e83 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 1 Mar 2023 11:52:19 +0530 Subject: [PATCH] refactor: sharedBaseApi hander Signed-off-by: Pranav C --- packages/nocodb/src/lib/services/index.ts | 1 + .../src/lib/services/sharedBaseService.ts | 108 ++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 packages/nocodb/src/lib/services/sharedBaseService.ts diff --git a/packages/nocodb/src/lib/services/index.ts b/packages/nocodb/src/lib/services/index.ts index 13696db0ce..7efb0a2e74 100644 --- a/packages/nocodb/src/lib/services/index.ts +++ b/packages/nocodb/src/lib/services/index.ts @@ -19,3 +19,4 @@ export * as viewColumnService from './viewColumnService'; export * as metaDiffService from './metaDiffService'; export * as mapViewService from './mapViewService'; export * as modelVisibilityService from './modelVisibilityService'; +export * as sharedBaseService from './sharedBaseService'; diff --git a/packages/nocodb/src/lib/services/sharedBaseService.ts b/packages/nocodb/src/lib/services/sharedBaseService.ts new file mode 100644 index 0000000000..637a9e322c --- /dev/null +++ b/packages/nocodb/src/lib/services/sharedBaseService.ts @@ -0,0 +1,108 @@ +import { Tele } from 'nc-help'; +import { v4 as uuidv4 } from 'uuid'; +import Project from '../models/Project'; +import { NcError } from '../meta/helpers/catchError'; +// todo: load from config +const config = { + dashboardPath: '/nc', +}; + +export async function createSharedBaseLink(param:{ + projectId: string; + roles: string; + password: string; + siteUrl: string; +}): Promise { + const project = await Project.get(param.projectId); + + let roles = param?.roles; + if (!roles || (roles !== 'editor' && roles !== 'viewer')) { + roles = 'viewer'; + } + + if (!project) { + NcError.badRequest('Invalid project id'); + } + + const data: any = { + uuid: uuidv4(), + password: param?.password, + roles, + }; + + await Project.update(project.id, data); + + data.url = `${param.siteUrl}${config.dashboardPath}#/nc/base/${data.uuid}`; + delete data.password; + Tele.emit('evt', { evt_type: 'sharedBase:generated-link' }); + return data; +} + +export async function updateSharedBaseLink(param: { + projectId: string; + roles: string; + password: string; + siteUrl: string; +}): Promise { + const project = await Project.get(param.projectId); + + let roles = param.roles; + if (!roles || (roles !== 'editor' && roles !== 'viewer')) { + roles = 'viewer'; + } + + if (!project) { + NcError.badRequest('Invalid project id'); + } + const data: any = { + uuid: project.uuid || uuidv4(), + password: param.password, + roles, + }; + + await Project.update(project.id, data); + + data.url = `${param.siteUrl}${config.dashboardPath}#/nc/base/${data.uuid}`; + delete data.password; + Tele.emit('evt', { evt_type: 'sharedBase:generated-link' }); + return data; +} + +export async function disableSharedBaseLink(param:{ + projectId: string; +}): Promise { + const project = await Project.get(param.projectId); + + if (!project) { + NcError.badRequest('Invalid project id'); + } + const data: any = { + uuid: null, + }; + + await Project.update(project.id, data); + + Tele.emit('evt', { evt_type: 'sharedBase:disable-link' }); + + return { uuid: null } +} + +export async function getSharedBaseLink(param:{ + projectId: string; + siteUrl: string; +}): Promise { + const project = await Project.get(param.projectId); + + if (!project) { + NcError.badRequest('Invalid project id'); + } + const data: any = { + uuid: project.uuid, + roles: project.roles, + }; + if (data.uuid) + data.url = `${param.siteUrl}${config.dashboardPath}#/nc/base/${data.shared_base_id}`; + + return data; +} +