diff --git a/src/services/entry/actions/copy-to-workbook.ts b/src/services/entry/actions/copy-to-workbook.ts index d6684db1..26823042 100644 --- a/src/services/entry/actions/copy-to-workbook.ts +++ b/src/services/entry/actions/copy-to-workbook.ts @@ -25,6 +25,7 @@ interface Params { skipLinkSync?: boolean; skipWorkbookPermissionsCheck?: boolean; resolveNameCollisions?: boolean; + isMigrateCopiedEntries?: boolean; } export const validateParams = makeSchemaValidator({ @@ -50,6 +51,9 @@ export const validateParams = makeSchemaValidator({ resolveNameCollisions: { type: 'boolean', }, + isMigrateCopiedEntries: { + type: 'boolean', + }, }, }); @@ -64,6 +68,7 @@ export const copyToWorkbook = async (ctx: CTX, params: Params) => { skipLinkSync, skipWorkbookPermissionsCheck = false, resolveNameCollisions, + isMigrateCopiedEntries, } = params; logInfo(ctx, 'COPY_ENTRY_TO_WORKBOOK_CALL', { @@ -115,15 +120,30 @@ export const copyToWorkbook = async (ctx: CTX, params: Params) => { }); } - if (joinedEntryRevision.workbookId === null) { - throw new AppError( - `Entry ${Utils.encodeId( - joinedEntryRevision.entryId, - )} doesn't have a workbookId and cannot be copied to a workbook.`, - { - code: US_ERRORS.ENTRY_WITHOUT_WORKBOOK_ID_COPY_DENIED, - }, - ); + if (!isMigrateCopiedEntries) { + if (joinedEntryRevision.workbookId === null) { + throw new AppError( + `Entry ${Utils.encodeId( + joinedEntryRevision.entryId, + )} doesn't have a workbookId and cannot be copied to a workbook.`, + { + code: US_ERRORS.ENTRY_WITHOUT_WORKBOOK_ID_COPY_DENIED, + }, + ); + } + + if (workbookId === undefined) { + workbookId = joinedEntryRevision.workbookId; + } else if (joinedEntryRevision.workbookId !== workbookId) { + throw new AppError( + `Copying entries from different workbooks is denied – ${Utils.encodeId( + workbookId, + )} and ${Utils.encodeId(joinedEntryRevision.workbookId)}`, + { + code: US_ERRORS.ENTRIES_WITH_DIFFERENT_WORKBOOK_IDS_COPY_DENIED, + }, + ); + } } const isFileConnection = @@ -140,22 +160,9 @@ export const copyToWorkbook = async (ctx: CTX, params: Params) => { }, ); } - - if (workbookId === undefined) { - workbookId = joinedEntryRevision.workbookId; - } else if (joinedEntryRevision.workbookId !== workbookId) { - throw new AppError( - `Copying entries from different workbooks is denied – ${Utils.encodeId( - workbookId, - )} and ${Utils.encodeId(joinedEntryRevision.workbookId)}`, - { - code: US_ERRORS.ENTRIES_WITH_DIFFERENT_WORKBOOK_IDS_COPY_DENIED, - }, - ); - } }); - if (workbookId === undefined) { + if (workbookId === undefined && !isMigrateCopiedEntries) { throw new AppError(`Entries don't have a workbookId and cannot be copied to a workbook.`, { code: US_ERRORS.ENTRY_WITHOUT_WORKBOOK_ID_COPY_DENIED, }); @@ -235,48 +242,56 @@ export const copyToWorkbook = async (ctx: CTX, params: Params) => { if (!skipWorkbookPermissionsCheck) { const workbookTargetTrx = trxOverride ?? WorkbookModel.replica; + const {Workbook} = registry.common.classes.get(); - const [originWorkbookModel, destinationWorkbookModel]: Optional[] = - await Promise.all([ - WorkbookModel.query(workbookTargetTrx) - .findById(workbookId) - .timeout(WorkbookModel.DEFAULT_QUERY_TIMEOUT), - WorkbookModel.query(workbookTargetTrx) - .findById(destinationWorkbookId) - .timeout(WorkbookModel.DEFAULT_QUERY_TIMEOUT), - ]); - - if (originWorkbookModel === undefined || destinationWorkbookModel === undefined) { - throw new AppError('Workbook not exists', { - code: US_ERRORS.WORKBOOK_NOT_EXISTS, - }); - } + const destinationWorkbookModel: Optional = await WorkbookModel.query( + workbookTargetTrx, + ) + .findById(destinationWorkbookId) + .timeout(WorkbookModel.DEFAULT_QUERY_TIMEOUT); - if (tenantIdOverride === undefined && originWorkbookModel.tenantId !== tenantId) { - throw new AppError('Workbook not exists', { - code: US_ERRORS.WORKBOOK_NOT_EXISTS, - }); - } - const {Workbook} = registry.common.classes.get(); + if (workbookId) { + const originWorkbookModel = await WorkbookModel.query(workbookTargetTrx) + .findById(workbookId) + .timeout(WorkbookModel.DEFAULT_QUERY_TIMEOUT); - if (tenantIdOverride === undefined) { - const originWorkbook = new Workbook({ - ctx, - model: originWorkbookModel, - }); + if (originWorkbookModel === undefined) { + throw new AppError('Workbook not exists', { + code: US_ERRORS.WORKBOOK_NOT_EXISTS, + }); + } - let originWorkbookParentIds: string[] = []; + if (tenantIdOverride === undefined && originWorkbookModel.tenantId !== tenantId) { + throw new AppError('Workbook not exists', { + code: US_ERRORS.WORKBOOK_NOT_EXISTS, + }); + } - if (originWorkbook.model.collectionId !== null) { - originWorkbookParentIds = await getParentIds({ + if (tenantIdOverride === undefined) { + const originWorkbook = new Workbook({ ctx, - collectionId: originWorkbook.model.collectionId, + model: originWorkbookModel, + }); + + let originWorkbookParentIds: string[] = []; + + if (originWorkbook.model.collectionId !== null) { + originWorkbookParentIds = await getParentIds({ + ctx, + collectionId: originWorkbook.model.collectionId, + }); + } + + await originWorkbook.checkPermission({ + parentIds: originWorkbookParentIds, + permission: WorkbookPermission.Copy, }); } + } - await originWorkbook.checkPermission({ - parentIds: originWorkbookParentIds, - permission: WorkbookPermission.Copy, + if (destinationWorkbookModel === undefined) { + throw new AppError('Workbook not exists', { + code: US_ERRORS.WORKBOOK_NOT_EXISTS, }); } diff --git a/src/services/new/entry/copy-entries-to-workbook.ts b/src/services/new/entry/copy-entries-to-workbook.ts index ceda9d88..8e402ae6 100644 --- a/src/services/new/entry/copy-entries-to-workbook.ts +++ b/src/services/new/entry/copy-entries-to-workbook.ts @@ -10,6 +10,7 @@ import {JoinedEntryRevisionColumns} from '../../../db/presentations'; export type CopyEntriesToWorkbookParams = { entryIds: string[]; workbookId: string; + isMigrateCopiedEntries?: boolean; }; const validateArgs = makeSchemaValidator({ @@ -25,6 +26,9 @@ const validateArgs = makeSchemaValidator({ workbookId: { type: 'string', }, + isMigrateCopiedEntries: { + type: 'boolean', + }, }, }); @@ -36,7 +40,7 @@ export const copyEntriesToWorkbook = async ( validateArgs(args); } - const {entryIds, workbookId: targetWorkbookId} = args; + const {entryIds, workbookId: targetWorkbookId, isMigrateCopiedEntries} = args; const {user} = ctx.get('info'); const updatedBy = makeUserId(user.userId); @@ -54,6 +58,7 @@ export const copyEntriesToWorkbook = async ( skipLinkSync: true, skipWorkbookPermissionsCheck: skipCheckPermissions, resolveNameCollisions: true, + isMigrateCopiedEntries, }); const filteredCopiedJoinedEntryRevisions = copiedJoinedEntryRevisions.filter(