{"version":3,"file":"first-published-at.mjs","sources":["../../src/migrations/first-published-at.ts"],"sourcesContent":["import { contentTypes as contentTypesUtils } from '@strapi/utils';\nimport _ from 'lodash';\nimport { Input } from './draft-publish';\n\ninterface ContentTypeData {\n  id: number;\n  documentId: string;\n  publishedAt: Date;\n  firstPublishedAt: Date;\n  locale: string;\n}\n\nconst enableFirstPublishedAt = async ({ oldContentTypes, contentTypes }: Input) => {\n  if (!oldContentTypes) {\n    return;\n  }\n\n  return strapi.db.transaction(async (trx) => {\n    for (const uid in contentTypes) {\n      if (!oldContentTypes[uid]) {\n        continue;\n      }\n\n      const contentType = contentTypes[uid];\n\n      if (!contentTypesUtils.hasFirstPublishedAtField(contentType)) {\n        continue;\n      }\n\n      if (!contentType.attributes?.firstPublishedAt) {\n        continue;\n      }\n\n      const content: ContentTypeData[] = await strapi.db\n        .queryBuilder(uid)\n        .select('*')\n        .transacting(trx)\n        .execute();\n\n      // Process content types in pairs: draft and published.\n      // If only one exist, which means the value is not published yet and we can ignore it\n      const groupedContent = _.groupBy(content, (item) => `${item.documentId}-${item.locale}`);\n\n      for (const items of Object.values(groupedContent)) {\n        // If there is only one item, which means nothing is published yet for this locale\n        if (items.length <= 1) {\n          continue;\n        }\n\n        // If firstPublishedAt is already present, do not do anything\n        if (items[0].firstPublishedAt != null && items[1].firstPublishedAt != null) {\n          continue;\n        }\n\n        const publishedContent = items.filter((item) => item.publishedAt != null).at(0);\n        if (!publishedContent) {\n          continue;\n        }\n\n        await strapi.db\n          .queryBuilder(uid)\n          .update({\n            firstPublishedAt: new Date(publishedContent.publishedAt),\n          })\n          .where({\n            documentId: publishedContent.documentId,\n            locale: publishedContent.locale,\n          })\n          .transacting(trx)\n          .execute();\n      }\n    }\n  });\n};\n\nexport { enableFirstPublishedAt as enable };\n"],"names":["enableFirstPublishedAt","oldContentTypes","contentTypes","strapi","db","transaction","trx","uid","contentType","contentTypesUtils","hasFirstPublishedAtField","attributes","firstPublishedAt","content","queryBuilder","select","transacting","execute","groupedContent","_","groupBy","item","documentId","locale","items","Object","values","length","publishedContent","filter","publishedAt","at","update","Date","where"],"mappings":";;;AAYA,MAAMA,yBAAyB,OAAO,EAAEC,eAAe,gBAAEC,cAAY,EAAS,GAAA;AAC5E,IAAA,IAAI,CAACD,eAAAA,EAAiB;AACpB,QAAA;AACF,IAAA;AAEA,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,WAAAA,GAAcN,cAAY,CAACK,GAAAA,CAAI;AAErC,YAAA,IAAI,CAACE,YAAAA,CAAkBC,wBAAwB,CAACF,WAAAA,CAAAA,EAAc;AAC5D,gBAAA;AACF,YAAA;AAEA,YAAA,IAAI,CAACA,WAAAA,CAAYG,UAAU,EAAEC,gBAAAA,EAAkB;AAC7C,gBAAA;AACF,YAAA;AAEA,YAAA,MAAMC,OAAAA,GAA6B,MAAMV,MAAAA,CAAOC,EAAE,CAC/CU,YAAY,CAACP,GAAAA,CAAAA,CACbQ,MAAM,CAAC,GAAA,CAAA,CACPC,WAAW,CAACV,KACZW,OAAO,EAAA;;;AAIV,YAAA,MAAMC,cAAAA,GAAiBC,CAAAA,CAAEC,OAAO,CAACP,SAAS,CAACQ,IAAAA,GAAS,CAAA,EAAGA,IAAAA,CAAKC,UAAU,CAAC,CAAC,EAAED,IAAAA,CAAKE,MAAM,CAAA,CAAE,CAAA;AAEvF,YAAA,KAAK,MAAMC,KAAAA,IAASC,MAAAA,CAAOC,MAAM,CAACR,cAAAA,CAAAA,CAAiB;;gBAEjD,IAAIM,KAAAA,CAAMG,MAAM,IAAI,CAAA,EAAG;AACrB,oBAAA;AACF,gBAAA;;AAGA,gBAAA,IAAIH,KAAK,CAAC,CAAA,CAAE,CAACZ,gBAAgB,IA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