import em from './em' import { chunk, isEmpty, isFunction } from './helpers' import { compare as jsonPatchCompare } from './json-patch' import { HistoryModel } from './model' import { omitDeep as omit } from './omit-deep' import type { HydratedDocument, MongooseError, Types } from 'mongoose' import type { Metadata, PatchContext, PatchEvent, PluginOptions, User } from './types' const isPatchHistoryEnabled = (opts: PluginOptions, context: PatchContext): boolean => { return !opts.patchHistoryDisabled && !context.ignorePatchHistory } const applyOmit = (object: Partial, opts: PluginOptions): Partial => { return opts.omit ? omit(object, opts.omit) : object } const replacer = (_key: string, value: unknown): unknown => (typeof value === 'bigint' ? value.toString() : value) export const getJsonOmit = (opts: PluginOptions, doc: HydratedDocument): Partial => { // NOSONAR — structuredClone cannot handle mongoose documents (they contain non-cloneable methods) return applyOmit(JSON.parse(JSON.stringify(doc, replacer)) as Partial, opts) } export const getObjectOmit = (opts: PluginOptions, doc: HydratedDocument): Partial => { return applyOmit(isFunction(doc?.toObject) ? (doc.toObject() as Partial) : doc, opts) } const getOptionalField = async (fn: ((doc: HydratedDocument) => Promise | R) | undefined, doc?: HydratedDocument): Promise => { if (isFunction(fn)) { return await fn(doc as HydratedDocument) } return undefined } export const getUser = async (opts: PluginOptions, doc?: HydratedDocument): Promise => getOptionalField(opts.getUser, doc) export const getReason = async (opts: PluginOptions, doc?: HydratedDocument): Promise => getOptionalField(opts.getReason, doc) export const getMetadata = async (opts: PluginOptions, doc?: HydratedDocument): Promise => getOptionalField(opts.getMetadata, doc) export const getValue = (item: PromiseSettledResult): T | undefined => { return item.status === 'fulfilled' ? item.value : undefined } export const getData = async (opts: PluginOptions, doc?: HydratedDocument): Promise<[User | undefined, string | undefined, Metadata | undefined]> => { return Promise.allSettled([getUser(opts, doc), getReason(opts, doc), getMetadata(opts, doc)]).then(([user, reason, metadata]) => { return [getValue(user), getValue(reason), getValue(metadata)] }) } export const emitEvent = (context: PatchContext, event: string | undefined, data: PatchEvent): void => { if (event && !context.ignoreEvent) { try { em.emit(event, data) } catch { // Listener errors must not crash patch history recording } } } export const bulkPatch = async (opts: PluginOptions, context: PatchContext, eventKey: 'eventCreated' | 'eventDeleted', docsKey: 'createdDocs' | 'deletedDocs'): Promise => { const history = isPatchHistoryEnabled(opts, context) const event = opts[eventKey] const docs = context[docsKey] const key = eventKey === 'eventCreated' ? 'doc' : 'oldDoc' if (isEmpty(docs) || !docs || (!event && !history)) return const chunks = chunk(docs, 1000) for (const batch of chunks) { const bulk = [] for (const doc of batch) { const omitted = getObjectOmit(opts, doc) emitEvent(context, event, { [key]: omitted }) if (history) { const [user, reason, metadata] = await getData(opts, doc) bulk.push({ insertOne: { document: { op: context.op, modelName: context.modelName, collectionName: context.collectionName, collectionId: doc._id as Types.ObjectId, doc: omitted, version: 0, ...(user !== undefined && { user }), ...(reason !== undefined && { reason }), ...(metadata !== undefined && { metadata }), }, }, }) } } if (history && !isEmpty(bulk)) { const onError = opts.onError ?? console.error await HistoryModel.bulkWrite(bulk, { ordered: false }).catch((error: MongooseError) => { onError(error) }) } } } export const createPatch = async (opts: PluginOptions, context: PatchContext): Promise => { await bulkPatch(opts, context, 'eventCreated', 'createdDocs') } export const updatePatch = async (opts: PluginOptions, context: PatchContext, current: HydratedDocument, original: HydratedDocument): Promise => { const history = isPatchHistoryEnabled(opts, context) const currentObject = getJsonOmit(opts, current) const originalObject = getJsonOmit(opts, original) if (isEmpty(originalObject) || isEmpty(currentObject)) return const patch = jsonPatchCompare(originalObject, currentObject, true) if (isEmpty(patch)) return emitEvent(context, opts.eventUpdated, { oldDoc: original, doc: current, patch }) if (history) { let version = 0 const lastHistory = await HistoryModel.findOne({ collectionId: original._id as Types.ObjectId }) .sort('-version') .exec() if (lastHistory) { version = lastHistory.version + 1 } const [user, reason, metadata] = await getData(opts, current) const onError = opts.onError ?? console.error await HistoryModel.create({ op: context.op, modelName: context.modelName, collectionName: context.collectionName, collectionId: original._id as Types.ObjectId, patch, version, ...(user !== undefined && { user }), ...(reason !== undefined && { reason }), ...(metadata !== undefined && { metadata }), }).catch((error: MongooseError) => { onError(error) }) } } export const deletePatch = async (opts: PluginOptions, context: PatchContext): Promise => { await bulkPatch(opts, context, 'eventDeleted', 'deletedDocs') }