Skip to content

Commit

Permalink
add flag isMigrateCopiedEntries to copyToWorkbook (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey-weber authored Nov 24, 2023
1 parent e57b3bd commit 48304e5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 57 deletions.
127 changes: 71 additions & 56 deletions src/services/entry/actions/copy-to-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface Params {
skipLinkSync?: boolean;
skipWorkbookPermissionsCheck?: boolean;
resolveNameCollisions?: boolean;
isMigrateCopiedEntries?: boolean;
}

export const validateParams = makeSchemaValidator({
Expand All @@ -50,6 +51,9 @@ export const validateParams = makeSchemaValidator({
resolveNameCollisions: {
type: 'boolean',
},
isMigrateCopiedEntries: {
type: 'boolean',
},
},
});

Expand All @@ -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', {
Expand Down Expand Up @@ -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 =
Expand All @@ -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,
});
Expand Down Expand Up @@ -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<WorkbookModel>[] =
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<WorkbookModel> = 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,
});
}

Expand Down
7 changes: 6 additions & 1 deletion src/services/new/entry/copy-entries-to-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {JoinedEntryRevisionColumns} from '../../../db/presentations';
export type CopyEntriesToWorkbookParams = {
entryIds: string[];
workbookId: string;
isMigrateCopiedEntries?: boolean;
};

const validateArgs = makeSchemaValidator({
Expand All @@ -25,6 +26,9 @@ const validateArgs = makeSchemaValidator({
workbookId: {
type: 'string',
},
isMigrateCopiedEntries: {
type: 'boolean',
},
},
});

Expand All @@ -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);

Expand All @@ -54,6 +58,7 @@ export const copyEntriesToWorkbook = async (
skipLinkSync: true,
skipWorkbookPermissionsCheck: skipCheckPermissions,
resolveNameCollisions: true,
isMigrateCopiedEntries,
});

const filteredCopiedJoinedEntryRevisions = copiedJoinedEntryRevisions.filter(
Expand Down

0 comments on commit 48304e5

Please sign in to comment.