{"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,qBAAAA,GAAwB,OAAO,EAAEC,eAAe,gBAAEC,cAAY,EAAS,GAAA;AAC3E,IAAA,IAAI,CAACD,eAAAA,EAAiB;AACpB,QAAA;AACF,IAAA;;AAGA,IAAA,OAAOE,MAAAA,CAAOC,EAAE,CAACC,WAAW,CAAC,OAAOC,GAAAA,GAAAA;QAClC,IAAK,MAAMC,OAAOL,cAAAA,CAAc;AAC9B,YAAA,IAAI,CAACD,eAAe,CAACM,GAAAA,CAAI,EAAE;AACzB,gBAAA;AACF,YAAA;YAEA,MAAMC,cAAAA,GAAiBP,eAAe,CAACM,GAAAA,CAAI;YAC3C,MAAME,WAAAA,GAAcP,cAAY,CAACK,GAAAA,CAAI;;YAGrC,IACE,CAACG,aAAkBC,kBAAkB,CAACH,mBACtCE,YAAAA,CAAkBC,kBAAkB,CAACF,WAAAA,CAAAA,EACrC;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,iBAAAA,CAAkB;AAAEd,oBAAAA,EAAAA,EAAID,OAAOC,EAAE;AAAEE,oBAAAA,GAAAA;AAAKC,oBAAAA;iBAAI,CAAA,CAAI;A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