{"version":3,"file":"draft-publish.mjs","sources":["../../src/migrations/draft-publish.ts"],"sourcesContent":["import { contentTypes as contentTypesUtils, async } from '@strapi/utils';\nimport type { Schema } from '@strapi/types';\n\nimport { getBatchToDiscard } from './database/5.0.0-discard-drafts';\n\nexport interface Input {\n  oldContentTypes: Record<string, Schema.ContentType>;\n  contentTypes: Record<string, Schema.ContentType>;\n}\n\n/**\n * Enable draft and publish for content types.\n *\n * Draft and publish disabled content types will have their entries published,\n * this migration clones those entries as drafts.\n *\n * TODO: Clone components, dynamic zones and relations\n */\nconst enableDraftAndPublish = async ({ oldContentTypes, contentTypes }: Input) => {\n  if (!oldContentTypes) {\n    return;\n  }\n\n  // run the after content types migrations\n  return strapi.db.transaction(async (trx) => {\n    for (const uid in contentTypes) {\n      if (!oldContentTypes[uid]) {\n        continue;\n      }\n\n      const oldContentType = oldContentTypes[uid];\n      const contentType = contentTypes[uid];\n\n      // if d&p was enabled set publishedAt to eq createdAt\n      if (\n        !contentTypesUtils.hasDraftAndPublish(oldContentType) &&\n        contentTypesUtils.hasDraftAndPublish(contentType)\n      ) {\n        const discardDraft = async (entry: { documentId: string; locale: string }) =>\n          strapi\n            .documents(uid as any)\n            // Discard draft by referencing the documentId and locale\n            .discardDraft({ documentId: entry.documentId, locale: entry.locale });\n\n        /**\n         * Load a batch of entries (batched to prevent loading millions of rows at once ),\n         * and discard them using the document service.\n         */\n        for await (const batch of getBatchToDiscard({ db: strapi.db, trx, uid })) {\n          await async.map(batch, discardDraft, { concurrency: 10 });\n        }\n      }\n    }\n  });\n};\n\nconst disableDraftAndPublish = async ({ oldContentTypes, contentTypes }: Input) => {\n  if (!oldContentTypes) {\n    return;\n  }\n\n  for (const uid in contentTypes) {\n    if (!oldContentTypes[uid]) {\n      continue;\n    }\n\n    const oldContentType = oldContentTypes[uid];\n    const contentType = contentTypes[uid];\n\n    // if d&p was disabled remove unpublish content before sync\n    if (\n      contentTypesUtils.hasDraftAndPublish(oldContentType) &&\n      !contentTypesUtils.hasDraftAndPublish(contentType)\n    ) {\n      await strapi.db?.queryBuilder(uid).delete().where({ published_at: null }).execute();\n    }\n  }\n};\n\nexport { enableDraftAndPublish as enable, disableDraftAndPublish as disable };\n"],"names":["enableDraftAndPublish","oldContentTypes","contentTypes","strapi","db","transaction","trx","uid","oldContentType","contentType","contentTypesUtils","hasDraftAndPublish","discardDraft","entry","documents","documentId","locale","batch","getBatchToDiscard","async","map","concurrency","disableDraftAndPublish","queryBuilder","delete","where","published_at","execute"],"mappings":";;;AAUA;;;;;;;AAOC,UACKA,qBAAwB,GAAA,OAAO,EAAEC,eAAe,gBAAEC,cAAY,EAAS,GAAA;AAC3E,IAAA,IAAI,CAACD,eAAiB,EAAA;AACpB,QAAA;AACF;;AAGA,IAAA,OAAOE,MAAOC,CAAAA,EAAE,CAACC,WAAW,CAAC,OAAOC,GAAAA,GAAAA;QAClC,IAAK,MAAMC,OAAOL,cAAc,CAAA;AAC9B,YAAA,IAAI,CAACD,eAAe,CAACM,GAAAA,CAAI,EAAE;AACzB,gBAAA;AACF;YAEA,MAAMC,cAAAA,GAAiBP,eAAe,CAACM,GAAI,CAAA;YAC3C,MAAME,WAAAA,GAAcP,cAAY,CAACK,GAAI,CAAA;;YAGrC,IACE,CAACG,aAAkBC,kBAAkB,CAACH,mBACtCE,YAAkBC,CAAAA,kBAAkB,CAACF,WACrC,CAAA,EAAA;AACA,gBAAA,MAAMG,eAAe,OAAOC,KAAAA,GAC1BV,OACGW,SAAS,CAACP,IACX;AACCK,qBAAAA,YAAY,CAAC;AAAEG,wBAAAA,UAAAA,EAAYF,MAAME,UAAU;AAAEC,wBAAAA,MAAAA,EAAQH,MAAMG;AAAO,qBAAA,CAAA;AAEvE;;;YAIA,WAAW,MAAMC,KAAAA,IAASC,iBAAkB,CAAA;AAAEd,oBAAAA,EAAAA,EAAID,OAAOC,EAAE;AAAEE,oBAAAA,GAAAA;AAAKC,oBAAAA;iBAAQ,CAAA,CAAA;AACxE,oBAAA,MAAMY,K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