-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a feature to copy entries to workbooks (#9)
- Loading branch information
Showing
11 changed files
with
370 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import {ServiceArgs} from '../types'; | ||
import {makeSchemaValidator} from '../../../components/validation-schema-compiler'; | ||
import Utils, {logInfo, makeUserId} from '../../../utils'; | ||
import {crossSyncCopiedJoinedEntryRevisions} from '../workbook'; | ||
import {transaction} from 'objection'; | ||
import {getPrimary} from '../utils'; | ||
import {copyToWorkbook} from '../../entry/actions'; | ||
import {JoinedEntryRevisionColumns} from '../../../db/presentations'; | ||
|
||
export type CopyEntriesToWorkbookParams = { | ||
entryIds: string[]; | ||
workbookId: string; | ||
}; | ||
|
||
const validateArgs = makeSchemaValidator({ | ||
type: 'object', | ||
required: ['entryIds', 'workbookId'], | ||
properties: { | ||
entryIds: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
}, | ||
}, | ||
workbookId: { | ||
type: 'string', | ||
}, | ||
}, | ||
}); | ||
|
||
export const copyEntriesToWorkbook = async ( | ||
{ctx, trx, skipValidation = false, skipCheckPermissions = false}: ServiceArgs, | ||
args: CopyEntriesToWorkbookParams, | ||
) => { | ||
if (!skipValidation) { | ||
validateArgs(args); | ||
} | ||
|
||
const {entryIds, workbookId: targetWorkbookId} = args; | ||
const {user} = ctx.get('info'); | ||
const updatedBy = makeUserId(user.userId); | ||
|
||
logInfo(ctx, 'COPY_ENTRIES_TO_WORKBOOK_START', { | ||
entryIds: entryIds.map((entryId) => Utils.encodeId(entryId)), | ||
workbookId: Utils.encodeId(targetWorkbookId), | ||
copiedBy: updatedBy, | ||
}); | ||
|
||
await transaction(getPrimary(trx), async (transactionTrx) => { | ||
const copiedJoinedEntryRevisions = await copyToWorkbook(ctx, { | ||
entryIds, | ||
destinationWorkbookId: targetWorkbookId, | ||
trxOverride: transactionTrx, | ||
skipLinkSync: true, | ||
skipWorkbookPermissionsCheck: skipCheckPermissions, | ||
resolveNameCollisions: true, | ||
}); | ||
|
||
const filteredCopiedJoinedEntryRevisions = copiedJoinedEntryRevisions.filter( | ||
(item) => item.newJoinedEntryRevision !== undefined, | ||
) as { | ||
newJoinedEntryRevision: JoinedEntryRevisionColumns; | ||
oldEntryId: string; | ||
}[]; | ||
|
||
await crossSyncCopiedJoinedEntryRevisions({ | ||
copiedJoinedEntryRevisions: filteredCopiedJoinedEntryRevisions, | ||
ctx, | ||
trx: transactionTrx, | ||
}); | ||
}); | ||
|
||
logInfo(ctx, 'COPY_ENTRIES_TO_WORKBOOK_FINISH'); | ||
|
||
return { | ||
workbookId: targetWorkbookId, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Utils from '../../../../utils'; | ||
|
||
export const resolveEntriesNameCollisions = ({ | ||
existingEntries, | ||
addingEntries, | ||
}: { | ||
existingEntries: Array<{entryId: string; displayKey: Nullable<string>}>; | ||
addingEntries: Array<{entryId: string; displayKey: Nullable<string>}>; | ||
}) => { | ||
const mapCollisions = new Map<string, number>(); | ||
|
||
existingEntries.forEach(({displayKey}) => { | ||
if (displayKey) { | ||
const entryName = Utils.getNameByKey({key: displayKey}); | ||
const entryNameWithoutCopy = Utils.getNameWithoutCopyNumber(entryName); | ||
const preparedEntryName = entryNameWithoutCopy.toLowerCase(); | ||
const copyNameNumber = Utils.getCopyNumber(entryName); | ||
|
||
if (copyNameNumber > 0) { | ||
mapCollisions.set( | ||
preparedEntryName, | ||
Math.max(mapCollisions.get(preparedEntryName) ?? 0, copyNameNumber) + 1, | ||
); | ||
} else { | ||
mapCollisions.set( | ||
preparedEntryName, | ||
(mapCollisions.get(preparedEntryName) ?? 0) + 1, | ||
); | ||
} | ||
} | ||
}); | ||
|
||
const addingNames = new Map<string, string>(); | ||
|
||
addingEntries.forEach((entry) => { | ||
let collisionCount: number; | ||
|
||
const {entryId, displayKey} = entry; | ||
|
||
const entryName = Utils.getNameByKey({key: displayKey}); | ||
const copyNameNumber = Utils.getCopyNumber(entryName); | ||
const entryNameWithoutCopy = Utils.getNameWithoutCopyNumber(entryName); | ||
const preparedEntryName = entryNameWithoutCopy.toLowerCase(); | ||
|
||
if (copyNameNumber > 0) { | ||
collisionCount = Math.max(mapCollisions.get(preparedEntryName) ?? 0, copyNameNumber); | ||
} else { | ||
collisionCount = mapCollisions.get(preparedEntryName) ?? 0; | ||
} | ||
|
||
const newEntryName = Utils.setCopyNumber(entryNameWithoutCopy, collisionCount); | ||
|
||
mapCollisions.set(preparedEntryName, collisionCount + 1); | ||
|
||
addingNames.set(entryId, newEntryName); | ||
}); | ||
|
||
return addingNames; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.