{"version":3,"sources":["../../src/duplicateDocument/index.ts"],"sourcesContent":["import type { SanitizedCollectionConfig } from '../collections/config/types.js'\nimport type { FindOneArgs } from '../database/types.js'\nimport type { JsonObject, PayloadRequest } from '../types/index.js'\n\nimport { executeAccess } from '../auth/executeAccess.js'\nimport { hasWhereAccessResult } from '../auth/types.js'\nimport { combineQueries } from '../database/combineQueries.js'\nimport { Forbidden } from '../errors/Forbidden.js'\nimport { NotFound } from '../errors/NotFound.js'\nimport { afterRead } from '../fields/hooks/afterRead/index.js'\nimport { beforeDuplicate } from '../fields/hooks/beforeDuplicate/index.js'\nimport { deepCopyObjectSimple } from '../utilities/deepCopyObject.js'\nimport { filterDataToSelectedLocales } from '../utilities/filterDataToSelectedLocales.js'\nimport { getLatestCollectionVersion } from '../versions/getLatestCollectionVersion.js'\n\ntype GetDuplicateDocumentArgs = {\n collectionConfig: SanitizedCollectionConfig\n draftArg?: boolean\n id: number | string\n overrideAccess?: boolean\n req: PayloadRequest\n selectedLocales?: string[]\n}\nexport const getDuplicateDocumentData = async ({\n id,\n collectionConfig,\n draftArg,\n overrideAccess,\n req,\n selectedLocales,\n}: GetDuplicateDocumentArgs): Promise<{\n duplicatedFromDoc: JsonObject\n duplicatedFromDocWithLocales: JsonObject\n}> => {\n const { payload } = req\n // /////////////////////////////////////\n // Read Access\n // /////////////////////////////////////\n\n const accessResults = !overrideAccess\n ? await executeAccess({ id, req }, collectionConfig.access.read)\n : true\n const hasWherePolicy = hasWhereAccessResult(accessResults)\n\n // /////////////////////////////////////\n // Retrieve document\n // /////////////////////////////////////\n const findOneArgs: FindOneArgs = {\n collection: collectionConfig.slug,\n locale: req.locale!,\n req,\n where: combineQueries({ id: { equals: id } }, accessResults),\n }\n\n let duplicatedFromDocWithLocales = await getLatestCollectionVersion({\n id,\n config: collectionConfig,\n payload,\n query: findOneArgs,\n req,\n })\n\n if (selectedLocales && selectedLocales.length > 0 && duplicatedFromDocWithLocales) {\n duplicatedFromDocWithLocales = filterDataToSelectedLocales({\n configBlockReferences: payload.config.blocks,\n docWithLocales: duplicatedFromDocWithLocales,\n fields: collectionConfig.fields,\n selectedLocales,\n })\n }\n\n if (!duplicatedFromDocWithLocales && !hasWherePolicy) {\n throw new NotFound(req.t)\n }\n if (!duplicatedFromDocWithLocales && hasWherePolicy) {\n throw new Forbidden(req.t)\n }\n\n // remove the createdAt timestamp and rely on the db to set it\n if ('createdAt' in duplicatedFromDocWithLocales) {\n delete duplicatedFromDocWithLocales.createdAt\n }\n // remove the id and rely on the db to set it\n if ('id' in duplicatedFromDocWithLocales) {\n delete duplicatedFromDocWithLocales.id\n }\n\n duplicatedFromDocWithLocales = await beforeDuplicate({\n id,\n collection: collectionConfig,\n context: req.context,\n doc: duplicatedFromDocWithLocales,\n overrideAccess: overrideAccess!,\n req,\n })\n\n const duplicatedFromDoc = await afterRead({\n collection: collectionConfig,\n context: req.context,\n depth: 0,\n doc: deepCopyObjectSimple(duplicatedFromDocWithLocales),\n draft: draftArg!,\n fallbackLocale: null,\n global: null,\n locale: req.locale!,\n overrideAccess: true,\n req,\n showHiddenFields: true,\n })\n\n return { duplicatedFromDoc, duplicatedFromDocWithLocales }\n}\n"],"names":["executeAccess","hasWhereAccessResult","combineQueries","Forbidden","NotFound","afterRead","beforeDuplicate","deepCopyObjectSimple","filterDataToSelectedLocales","getLatestCollectionVersion","getDuplicateDocumentData","id","collectionConfig","draftArg","overrideAccess","req","selectedLocales","payload","accessResults","access","read","hasWherePolicy","findOneArgs","collection","slug","locale","where","equals","duplicatedFromDocWithLocales","config","query","length","configBlockReferences","blocks","docWithLocales","fields","t","createdAt","context","doc","duplicatedFromDoc","depth","draft","fallbackLocale","global","showHiddenFields"],"mappings":"AAIA,SAASA,aAAa,QAAQ,2BAA0B;AACxD,SAASC,oBAAoB,QAAQ,mBAAkB;AACvD,SAASC,cAAc,QAAQ,gCAA+B;AAC9D,SAASC,SAAS,QAAQ,yBAAwB;AAClD,SAASC,QAAQ,QAAQ,wBAAuB;AAChD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