strapi-plugin-bulk-updater 1.5.6 → 1.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -993,87 +993,111 @@ function formatEntryRecord(entry) {
993
993
  }
994
994
  return formatted;
995
995
  }
996
- function extractStageName(val) {
997
- if (val === void 0 || val === null) return "";
998
- if (typeof val === "object") {
999
- const candidateProps = [
1000
- "strapi_stage",
1001
- "strapi_stage.name",
1002
- "strapiStage",
1003
- "Strapi Stage",
1004
- "strapi-stage",
1005
- "stage",
1006
- "Stage",
1007
- "review_stage"
1008
- ];
1009
- for (const prop of candidateProps) {
1010
- if (val[prop] !== void 0 && val[prop] !== null) {
1011
- const fromProp = extractStageName(val[prop]);
1012
- if (fromProp) return fromProp;
1013
- }
996
+ function isRowInEditingStage$1(row) {
997
+ if (!row || typeof row !== "object") return false;
998
+ const candidates = [
999
+ row.strapi_stage,
1000
+ row["strapi_stage"],
1001
+ row.strapiStage,
1002
+ row["Strapi Stage"],
1003
+ row["strapi-stage"],
1004
+ row.stage,
1005
+ row.Stage,
1006
+ row.review_stage,
1007
+ row.workflow_stage
1008
+ ];
1009
+ if (row.status && typeof row.status === "string") {
1010
+ const s = row.status.trim().toLowerCase();
1011
+ if (s === "editing" || s === "in review" || s === "in_review" || s === "pending review" || s === "pending_review") {
1012
+ candidates.push(row.status);
1014
1013
  }
1015
- return String(val.name || val.title || val.status || val.label || "").trim();
1016
1014
  }
1017
- if (typeof val === "string") {
1018
- const trimmed = val.trim();
1019
- if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
1020
- try {
1021
- const parsed = JSON.parse(trimmed);
1022
- if (parsed && typeof parsed === "object") {
1023
- const fromParsed = String(parsed.name || parsed.title || parsed.status || parsed.label || "").trim();
1024
- if (fromParsed) return fromParsed;
1015
+ for (const c of candidates) {
1016
+ if (c !== void 0 && c !== null) {
1017
+ let stageName = "";
1018
+ if (typeof c === "object") {
1019
+ stageName = String(c.name || c.title || c.status || c.documentId || c.id || "").trim();
1020
+ } else if (typeof c === "string") {
1021
+ const trimmed = c.trim();
1022
+ if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
1023
+ try {
1024
+ const parsed = JSON.parse(trimmed);
1025
+ if (typeof parsed === "object" && parsed !== null) {
1026
+ stageName = String(parsed.name || parsed.title || parsed.status || parsed.documentId || parsed.id || "").trim();
1027
+ }
1028
+ } catch {
1029
+ }
1030
+ }
1031
+ if (!stageName) {
1032
+ stageName = trimmed;
1033
+ }
1034
+ } else {
1035
+ stageName = String(c).trim();
1036
+ }
1037
+ if (stageName) {
1038
+ const s = stageName.toLowerCase();
1039
+ if (s === "editing" || s === "in review" || s === "in_review" || s === "pending review" || s === "pending_review") {
1040
+ return true;
1025
1041
  }
1026
- } catch {
1027
1042
  }
1028
1043
  }
1029
- return trimmed;
1030
1044
  }
1031
- return String(val).trim();
1032
- }
1033
- function isStageApproved(stageVal) {
1034
- const name = extractStageName(stageVal);
1035
- return name.toLowerCase() === "approved";
1036
- }
1037
- function isStageEditingOrPending(stageVal) {
1038
- const name = extractStageName(stageVal).toLowerCase();
1039
- return name === "editing" || name === "in review" || name === "in_review" || name === "pending review" || name === "pending_review";
1045
+ return false;
1040
1046
  }
1041
- function inspectPreUpdateStatus(entry) {
1047
+ function resolveRecordPublicationStatus(entry, publishedRecord, isDraftAndPublish) {
1048
+ if (isDraftAndPublish === false) {
1049
+ return { status: "published", hadPublishedVersion: true };
1050
+ }
1042
1051
  if (!entry || typeof entry !== "object") {
1043
- return { hadPublishedVersion: false, isModified: false };
1052
+ return { status: "unknown", hadPublishedVersion: false };
1044
1053
  }
1045
- const isModified = entry.status === "modified";
1046
- const isPublished = entry.status === "published" || Boolean(entry.publishedAt);
1047
- const hadPublishedVersion = isModified || isPublished;
1048
- return { hadPublishedVersion, isModified };
1054
+ if (typeof entry.status === "string" && entry.status.trim()) {
1055
+ const s = entry.status.trim().toLowerCase();
1056
+ if (s === "published") {
1057
+ return { status: "published", hadPublishedVersion: true };
1058
+ }
1059
+ if (s === "modified") {
1060
+ return { status: "modified", hadPublishedVersion: true };
1061
+ }
1062
+ if (s === "draft") {
1063
+ return { status: "draft", hadPublishedVersion: false };
1064
+ }
1065
+ }
1066
+ const pub = publishedRecord || (entry.publishedAt ? entry : null);
1067
+ if (!pub) {
1068
+ return { status: "draft", hadPublishedVersion: false };
1069
+ }
1070
+ const draftUpdatedTime = new Date(entry.updatedAt || entry.updated_at || 0).getTime();
1071
+ const pubUpdatedTime = new Date(pub.updatedAt || pub.updated_at || 0).getTime();
1072
+ const pubPublishedTime = new Date(pub.publishedAt || pub.published_at || 0).getTime();
1073
+ const lastPublishedTime = Math.max(
1074
+ isNaN(pubUpdatedTime) ? 0 : pubUpdatedTime,
1075
+ isNaN(pubPublishedTime) ? 0 : pubPublishedTime
1076
+ );
1077
+ if (isNaN(draftUpdatedTime) || lastPublishedTime === 0) {
1078
+ if (pub.publishedAt || pub.published_at) {
1079
+ return { status: "published", hadPublishedVersion: true };
1080
+ }
1081
+ return { status: "unknown", hadPublishedVersion: false };
1082
+ }
1083
+ if (draftUpdatedTime > lastPublishedTime) {
1084
+ return { status: "modified", hadPublishedVersion: true };
1085
+ }
1086
+ return { status: "published", hadPublishedVersion: true };
1049
1087
  }
1050
- function determineShouldPublish({
1088
+ function evaluateShouldPublish({
1051
1089
  isDraftAndPublish,
1052
1090
  hadPublishedVersion,
1053
- isModified = false,
1054
- stageValue,
1055
- publish = true,
1091
+ isEditing,
1092
+ publishOnApproval = true,
1056
1093
  publishStrategy = "safe",
1057
1094
  unreviewedConsent = false
1058
1095
  }) {
1059
- if (!isDraftAndPublish || !publish) {
1060
- return false;
1061
- }
1062
- if (!hadPublishedVersion) {
1063
- return false;
1064
- }
1065
- const isApproved = isStageApproved(stageValue);
1066
- const isEditingOrPending = isStageEditingOrPending(stageValue);
1067
- if (isApproved) {
1068
- return true;
1069
- }
1070
- if (isEditingOrPending) {
1071
- return false;
1072
- }
1073
- if (publishStrategy === "all" || unreviewedConsent) {
1074
- return true;
1075
- }
1076
- return hadPublishedVersion && !isModified;
1096
+ if (!isDraftAndPublish) return false;
1097
+ if (!hadPublishedVersion) return false;
1098
+ if (!publishOnApproval) return false;
1099
+ if (isEditing) return false;
1100
+ return true;
1077
1101
  }
1078
1102
  async function populateDocumentLifecycleStatus(strapi, uid, entries) {
1079
1103
  if (!entries || !Array.isArray(entries) || entries.length === 0) {
@@ -1714,18 +1738,50 @@ async function executeBatchInternal(strapi, {
1714
1738
  }
1715
1739
  const targets = Array.from(targetsMap.values());
1716
1740
  const CHUNK_SIZE = 25;
1741
+ const publishedCounterparts = /* @__PURE__ */ new Map();
1742
+ const uidsWithDraftAndPublish = /* @__PURE__ */ new Set();
1743
+ const docIdsByUid = /* @__PURE__ */ new Map();
1744
+ for (const target of targets) {
1745
+ if (target.uid && target.documentId) {
1746
+ const ct = strapi.contentTypes[target.uid];
1747
+ if (ct?.options?.draftAndPublish) {
1748
+ uidsWithDraftAndPublish.add(target.uid);
1749
+ if (!docIdsByUid.has(target.uid)) {
1750
+ docIdsByUid.set(target.uid, []);
1751
+ }
1752
+ docIdsByUid.get(target.uid).push(target.documentId);
1753
+ }
1754
+ }
1755
+ }
1756
+ for (const colUid of uidsWithDraftAndPublish) {
1757
+ const docIds = docIdsByUid.get(colUid) || [];
1758
+ if (docIds.length > 0) {
1759
+ try {
1760
+ const pubDocs = await strapi.documents(colUid).findMany({
1761
+ status: "published",
1762
+ filters: { documentId: { $in: docIds } },
1763
+ fields: ["documentId", "updatedAt", "publishedAt", "locale"],
1764
+ limit: Math.max(docIds.length, 100)
1765
+ });
1766
+ for (const pub of pubDocs || []) {
1767
+ if (pub?.documentId) {
1768
+ publishedCounterparts.set(`${colUid}@@@${pub.documentId}`, pub);
1769
+ if (pub.locale) {
1770
+ publishedCounterparts.set(`${colUid}@@@${pub.documentId}@@@${pub.locale}`, pub);
1771
+ }
1772
+ }
1773
+ }
1774
+ } catch {
1775
+ }
1776
+ }
1777
+ }
1717
1778
  const documentLoader = /* @__PURE__ */ new Map();
1718
1779
  const getDocumentDeduplicated = (uid, documentId) => {
1719
1780
  const loaderKey = `${uid}@@@${documentId}`;
1720
1781
  if (!documentLoader.has(loaderKey)) {
1721
- const ct = strapi.contentTypes[uid];
1722
- const findOneArgs = { documentId };
1723
- if (ct?.attributes?.strapi_stage) {
1724
- findOneArgs.populate = ["strapi_stage"];
1725
- }
1726
1782
  documentLoader.set(
1727
1783
  loaderKey,
1728
- strapi.documents(uid).findOne(findOneArgs)
1784
+ strapi.documents(uid).findOne({ documentId })
1729
1785
  );
1730
1786
  }
1731
1787
  return documentLoader.get(loaderKey);
@@ -1776,29 +1832,56 @@ async function executeBatchInternal(strapi, {
1776
1832
  targetIndexes,
1777
1833
  targetComponentUid
1778
1834
  });
1779
- await strapi.documents(target.uid).update({
1780
- documentId: target.documentId,
1781
- data: {
1782
- [rootKey]: updatedRoot
1783
- }
1784
- });
1785
1835
  const isDraftAndPublishComp = Boolean(contentType?.options?.draftAndPublish);
1786
- const { hadPublishedVersion: hadPubComp, isModified: isModComp } = inspectPreUpdateStatus(currentEntry);
1787
- const targetStageComp = attribute.toLowerCase() === "strapi_stage" || attribute.toLowerCase() === "stage" ? value : currentEntry?.strapi_stage ?? currentEntry?.stage ?? target?.strapi_stage ?? target?.stage;
1788
- const shouldPublishComp = determineShouldPublish({
1836
+ const candLocaleComp = target.locale || currentEntry?.locale;
1837
+ let pubRecordComp = candLocaleComp && publishedCounterparts.get(`${target.uid}@@@${target.documentId}@@@${candLocaleComp}`) || publishedCounterparts.get(`${target.uid}@@@${target.documentId}`);
1838
+ if (!pubRecordComp && isDraftAndPublishComp && target.documentId) {
1839
+ try {
1840
+ const singlePub = await strapi.documents(target.uid).findMany({
1841
+ status: "published",
1842
+ filters: { documentId: target.documentId },
1843
+ fields: ["documentId", "updatedAt", "publishedAt", "locale"],
1844
+ limit: 1
1845
+ });
1846
+ if (singlePub && singlePub.length > 0) {
1847
+ pubRecordComp = singlePub[0];
1848
+ publishedCounterparts.set(`${target.uid}@@@${target.documentId}`, pubRecordComp);
1849
+ }
1850
+ } catch {
1851
+ }
1852
+ }
1853
+ const inspectionTargetComp = target.status || target.publishedAt ? { ...currentEntry, status: target.status, publishedAt: target.publishedAt } : currentEntry;
1854
+ const { hadPublishedVersion: hadPubComp } = resolveRecordPublicationStatus(
1855
+ inspectionTargetComp,
1856
+ pubRecordComp,
1857
+ isDraftAndPublishComp
1858
+ );
1859
+ const isEditingComp = isRowInEditingStage$1(inspectionTargetComp) || isRowInEditingStage$1(target);
1860
+ const shouldPublishComp = evaluateShouldPublish({
1789
1861
  isDraftAndPublish: isDraftAndPublishComp,
1790
1862
  hadPublishedVersion: hadPubComp,
1791
- isModified: isModComp,
1792
- stageValue: targetStageComp,
1793
- publish: Boolean(publishOnApproval),
1863
+ isEditing: isEditingComp,
1864
+ publishOnApproval: Boolean(publishOnApproval),
1794
1865
  publishStrategy,
1795
1866
  unreviewedConsent
1796
1867
  });
1868
+ const updateArgsComp = {
1869
+ documentId: target.documentId,
1870
+ data: {
1871
+ [rootKey]: updatedRoot
1872
+ }
1873
+ };
1874
+ if (Boolean(contentType?.pluginOptions?.i18n?.localized) && candLocaleComp && candLocaleComp !== "null" && candLocaleComp !== "undefined") {
1875
+ updateArgsComp.locale = candLocaleComp;
1876
+ }
1877
+ await strapi.documents(target.uid).update(updateArgsComp);
1797
1878
  if (shouldPublishComp) {
1798
1879
  try {
1799
- const pubArgs = { documentId: target.documentId };
1800
- if (target.locale || currentEntry?.locale) {
1801
- pubArgs.locale = target.locale || currentEntry?.locale;
1880
+ const pubArgs = {
1881
+ documentId: target.documentId
1882
+ };
1883
+ if (Boolean(contentType?.pluginOptions?.i18n?.localized) && candLocaleComp && candLocaleComp !== "null" && candLocaleComp !== "undefined") {
1884
+ pubArgs.locale = candLocaleComp;
1802
1885
  }
1803
1886
  await strapi.documents(target.uid).publish(pubArgs);
1804
1887
  } catch (publishErr) {
@@ -1886,29 +1969,56 @@ async function executeBatchInternal(strapi, {
1886
1969
  } else if (mode === "disconnect") {
1887
1970
  relationPayload = { disconnect: targetDocIds };
1888
1971
  }
1889
- await strapi.documents(target.uid).update({
1890
- documentId: target.documentId,
1891
- data: {
1892
- [attribute]: relationPayload
1893
- }
1894
- });
1895
1972
  const isDraftAndPublishRel = Boolean(contentType?.options?.draftAndPublish);
1896
- const { hadPublishedVersion: hadPubRel, isModified: isModRel } = inspectPreUpdateStatus(currentEntry);
1897
- const targetStageRel = attribute.toLowerCase() === "strapi_stage" || attribute.toLowerCase() === "stage" ? value : currentEntry?.strapi_stage ?? currentEntry?.stage ?? target?.strapi_stage ?? target?.stage;
1898
- const shouldPublishRel = determineShouldPublish({
1973
+ const candLocaleRel = target.locale || currentEntry?.locale;
1974
+ let pubRecordRel = candLocaleRel && publishedCounterparts.get(`${target.uid}@@@${target.documentId}@@@${candLocaleRel}`) || publishedCounterparts.get(`${target.uid}@@@${target.documentId}`);
1975
+ if (!pubRecordRel && isDraftAndPublishRel && target.documentId) {
1976
+ try {
1977
+ const singlePub = await strapi.documents(target.uid).findMany({
1978
+ status: "published",
1979
+ filters: { documentId: target.documentId },
1980
+ fields: ["documentId", "updatedAt", "publishedAt", "locale"],
1981
+ limit: 1
1982
+ });
1983
+ if (singlePub && singlePub.length > 0) {
1984
+ pubRecordRel = singlePub[0];
1985
+ publishedCounterparts.set(`${target.uid}@@@${target.documentId}`, pubRecordRel);
1986
+ }
1987
+ } catch {
1988
+ }
1989
+ }
1990
+ const inspectionTargetRel = target.status || target.publishedAt ? { ...currentEntry, status: target.status, publishedAt: target.publishedAt } : currentEntry;
1991
+ const { hadPublishedVersion: hadPubRel } = resolveRecordPublicationStatus(
1992
+ inspectionTargetRel,
1993
+ pubRecordRel,
1994
+ isDraftAndPublishRel
1995
+ );
1996
+ const isEditingRel = isRowInEditingStage$1(inspectionTargetRel) || isRowInEditingStage$1(target);
1997
+ const shouldPublishRel = evaluateShouldPublish({
1899
1998
  isDraftAndPublish: isDraftAndPublishRel,
1900
1999
  hadPublishedVersion: hadPubRel,
1901
- isModified: isModRel,
1902
- stageValue: targetStageRel,
1903
- publish: Boolean(publishOnApproval),
2000
+ isEditing: isEditingRel,
2001
+ publishOnApproval: Boolean(publishOnApproval),
1904
2002
  publishStrategy,
1905
2003
  unreviewedConsent
1906
2004
  });
2005
+ const updateArgsRel = {
2006
+ documentId: target.documentId,
2007
+ data: {
2008
+ [attribute]: relationPayload
2009
+ }
2010
+ };
2011
+ if (Boolean(contentType?.pluginOptions?.i18n?.localized) && candLocaleRel && candLocaleRel !== "null" && candLocaleRel !== "undefined") {
2012
+ updateArgsRel.locale = candLocaleRel;
2013
+ }
2014
+ await strapi.documents(target.uid).update(updateArgsRel);
1907
2015
  if (shouldPublishRel) {
1908
2016
  try {
1909
- const pubArgs = { documentId: target.documentId };
1910
- if (target.locale || currentEntry?.locale) {
1911
- pubArgs.locale = target.locale || currentEntry?.locale;
2017
+ const pubArgs = {
2018
+ documentId: target.documentId
2019
+ };
2020
+ if (Boolean(contentType?.pluginOptions?.i18n?.localized) && candLocaleRel && candLocaleRel !== "null" && candLocaleRel !== "undefined") {
2021
+ pubArgs.locale = candLocaleRel;
1912
2022
  }
1913
2023
  await strapi.documents(target.uid).publish(pubArgs);
1914
2024
  } catch (publishErr) {
@@ -1942,29 +2052,56 @@ async function executeBatchInternal(strapi, {
1942
2052
  if (regexObj && typeof currentVal === "string") {
1943
2053
  newValue = currentVal.replace(regexObj, value);
1944
2054
  }
1945
- await strapi.documents(target.uid).update({
1946
- documentId: target.documentId,
1947
- data: {
1948
- [attribute]: newValue
1949
- }
1950
- });
1951
2055
  const isDraftAndPublishScalar = Boolean(contentType?.options?.draftAndPublish);
1952
- const { hadPublishedVersion: hadPubScalar, isModified: isModScalar } = inspectPreUpdateStatus(currentEntry);
1953
- const targetStageScalar = attribute.toLowerCase() === "strapi_stage" || attribute.toLowerCase() === "stage" ? value : currentEntry?.strapi_stage ?? currentEntry?.stage ?? target?.strapi_stage ?? target?.stage;
1954
- const shouldPublishScalar = determineShouldPublish({
2056
+ const candLocaleScalar = target.locale || currentEntry?.locale;
2057
+ let pubRecordScalar = candLocaleScalar && publishedCounterparts.get(`${target.uid}@@@${target.documentId}@@@${candLocaleScalar}`) || publishedCounterparts.get(`${target.uid}@@@${target.documentId}`);
2058
+ if (!pubRecordScalar && isDraftAndPublishScalar && target.documentId) {
2059
+ try {
2060
+ const singlePub = await strapi.documents(target.uid).findMany({
2061
+ status: "published",
2062
+ filters: { documentId: target.documentId },
2063
+ fields: ["documentId", "updatedAt", "publishedAt", "locale"],
2064
+ limit: 1
2065
+ });
2066
+ if (singlePub && singlePub.length > 0) {
2067
+ pubRecordScalar = singlePub[0];
2068
+ publishedCounterparts.set(`${target.uid}@@@${target.documentId}`, pubRecordScalar);
2069
+ }
2070
+ } catch {
2071
+ }
2072
+ }
2073
+ const inspectionTargetScalar = target.status || target.publishedAt ? { ...currentEntry, status: target.status, publishedAt: target.publishedAt } : currentEntry;
2074
+ const { hadPublishedVersion: hadPubScalar } = resolveRecordPublicationStatus(
2075
+ inspectionTargetScalar,
2076
+ pubRecordScalar,
2077
+ isDraftAndPublishScalar
2078
+ );
2079
+ const isEditingScalar = isRowInEditingStage$1(inspectionTargetScalar) || isRowInEditingStage$1(target);
2080
+ const shouldPublishScalar = evaluateShouldPublish({
1955
2081
  isDraftAndPublish: isDraftAndPublishScalar,
1956
2082
  hadPublishedVersion: hadPubScalar,
1957
- isModified: isModScalar,
1958
- stageValue: targetStageScalar,
1959
- publish: Boolean(publishOnApproval),
2083
+ isEditing: isEditingScalar,
2084
+ publishOnApproval: Boolean(publishOnApproval),
1960
2085
  publishStrategy,
1961
2086
  unreviewedConsent
1962
2087
  });
2088
+ const updateArgsScalar = {
2089
+ documentId: target.documentId,
2090
+ data: {
2091
+ [attribute]: newValue
2092
+ }
2093
+ };
2094
+ if (Boolean(contentType?.pluginOptions?.i18n?.localized) && candLocaleScalar && candLocaleScalar !== "null" && candLocaleScalar !== "undefined") {
2095
+ updateArgsScalar.locale = candLocaleScalar;
2096
+ }
2097
+ await strapi.documents(target.uid).update(updateArgsScalar);
1963
2098
  if (shouldPublishScalar) {
1964
2099
  try {
1965
- const pubArgs = { documentId: target.documentId };
1966
- if (target.locale || currentEntry?.locale) {
1967
- pubArgs.locale = target.locale || currentEntry?.locale;
2100
+ const pubArgs = {
2101
+ documentId: target.documentId
2102
+ };
2103
+ if (Boolean(contentType?.pluginOptions?.i18n?.localized) && candLocaleScalar && candLocaleScalar !== "null" && candLocaleScalar !== "undefined") {
2104
+ pubArgs.locale = candLocaleScalar;
1968
2105
  }
1969
2106
  await strapi.documents(target.uid).publish(pubArgs);
1970
2107
  } catch (publishErr) {
@@ -5383,10 +5520,6 @@ function isRowInEditingStage(row) {
5383
5520
  }
5384
5521
  return false;
5385
5522
  }
5386
- function isRowInApprovedStage(row) {
5387
- if (!row || typeof row !== "object") return false;
5388
- return isStageApproved(row);
5389
- }
5390
5523
  function extractDetailedErrorMessage(err) {
5391
5524
  if (!err) return "Error processing row";
5392
5525
  const detailsErrors = err.details?.errors || err.error?.details?.errors;
@@ -5583,7 +5716,6 @@ async function findExistingDocument(strapi, uid, targetDocId, docLocale) {
5583
5716
  try {
5584
5717
  const queryArgs = { documentId: targetDocId };
5585
5718
  if (effectiveLocale) queryArgs.locale = effectiveLocale;
5586
- if (ct?.attributes?.strapi_stage) queryArgs.populate = ["strapi_stage"];
5587
5719
  const found = await strapi.documents(uid).findOne(queryArgs);
5588
5720
  if (found) return found;
5589
5721
  } catch {
@@ -5954,6 +6086,27 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
5954
6086
  const attrs = ct.attributes || {};
5955
6087
  const { mode = "auto", publish = true, publishStrategy = "safe", user } = options;
5956
6088
  const isDraftAndPublish = Boolean(ct.options?.draftAndPublish);
6089
+ const docIdsInChunk = rows.map((r) => extractDocId(r)).filter(Boolean);
6090
+ const publishedCounterparts = /* @__PURE__ */ new Map();
6091
+ if (isDraftAndPublish && docIdsInChunk.length > 0) {
6092
+ try {
6093
+ const pubDocs = await strapi.documents(uid).findMany({
6094
+ status: "published",
6095
+ filters: { documentId: { $in: docIdsInChunk } },
6096
+ fields: ["documentId", "updatedAt", "publishedAt", "locale"],
6097
+ limit: Math.max(docIdsInChunk.length, 100)
6098
+ });
6099
+ for (const pub of pubDocs || []) {
6100
+ if (pub?.documentId) {
6101
+ publishedCounterparts.set(String(pub.documentId), pub);
6102
+ if (pub.locale) {
6103
+ publishedCounterparts.set(`${pub.documentId}@@@${pub.locale}`, pub);
6104
+ }
6105
+ }
6106
+ }
6107
+ } catch {
6108
+ }
6109
+ }
5957
6110
  const stats = {
5958
6111
  total: rows.length,
5959
6112
  created: 0,
@@ -6030,14 +6183,33 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
6030
6183
  };
6031
6184
  if (effectiveLocale) updateArgs.locale = effectiveLocale;
6032
6185
  await strapi.documents(uid).update(updateArgs);
6033
- const { hadPublishedVersion, isModified } = inspectPreUpdateStatus(existing);
6034
- const stageVal = row.strapi_stage ?? row["strapi_stage"] ?? row.stage ?? existing?.strapi_stage;
6035
- const shouldPublish = determineShouldPublish({
6186
+ let pubRecord = effectiveLocale && publishedCounterparts.get(`${resolvedDocId}@@@${effectiveLocale}`) || publishedCounterparts.get(String(resolvedDocId));
6187
+ if (!pubRecord && isDraftAndPublish && resolvedDocId) {
6188
+ try {
6189
+ const singlePub = await strapi.documents(uid).findMany({
6190
+ status: "published",
6191
+ filters: { documentId: resolvedDocId },
6192
+ fields: ["documentId", "updatedAt", "publishedAt", "locale"],
6193
+ limit: 1
6194
+ });
6195
+ if (singlePub && singlePub.length > 0) {
6196
+ pubRecord = singlePub[0];
6197
+ publishedCounterparts.set(String(resolvedDocId), pubRecord);
6198
+ }
6199
+ } catch {
6200
+ }
6201
+ }
6202
+ const inspectionExisting = row.status || row.publishedAt ? { ...existing, status: row.status, publishedAt: row.publishedAt } : existing;
6203
+ const { hadPublishedVersion } = resolveRecordPublicationStatus(
6204
+ inspectionExisting,
6205
+ pubRecord,
6206
+ isDraftAndPublish
6207
+ );
6208
+ const shouldPublish = evaluateShouldPublish({
6036
6209
  isDraftAndPublish,
6037
6210
  hadPublishedVersion,
6038
- isModified,
6039
- stageValue: stageVal,
6040
- publish: Boolean(publish),
6211
+ isEditing,
6212
+ publishOnApproval: Boolean(publish),
6041
6213
  publishStrategy,
6042
6214
  unreviewedConsent: options.unreviewedConsent
6043
6215
  });
@@ -6094,14 +6266,33 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
6094
6266
  };
6095
6267
  if (effectiveLocale) updateArgs.locale = effectiveLocale;
6096
6268
  await strapi.documents(uid).update(updateArgs);
6097
- const { hadPublishedVersion, isModified } = inspectPreUpdateStatus(existing);
6098
- const stageVal = row.strapi_stage ?? row["strapi_stage"] ?? row.stage ?? existing?.strapi_stage;
6099
- const shouldPublish = determineShouldPublish({
6269
+ let pubRecord = effectiveLocale && publishedCounterparts.get(`${resolvedDocId}@@@${effectiveLocale}`) || publishedCounterparts.get(String(resolvedDocId));
6270
+ if (!pubRecord && isDraftAndPublish && resolvedDocId) {
6271
+ try {
6272
+ const singlePub = await strapi.documents(uid).findMany({
6273
+ status: "published",
6274
+ filters: { documentId: resolvedDocId },
6275
+ fields: ["documentId", "updatedAt", "publishedAt", "locale"],
6276
+ limit: 1
6277
+ });
6278
+ if (singlePub && singlePub.length > 0) {
6279
+ pubRecord = singlePub[0];
6280
+ publishedCounterparts.set(String(resolvedDocId), pubRecord);
6281
+ }
6282
+ } catch {
6283
+ }
6284
+ }
6285
+ const inspectionExisting = row.status || row.publishedAt ? { ...existing, status: row.status, publishedAt: row.publishedAt } : existing;
6286
+ const { hadPublishedVersion } = resolveRecordPublicationStatus(
6287
+ inspectionExisting,
6288
+ pubRecord,
6289
+ isDraftAndPublish
6290
+ );
6291
+ const shouldPublish = evaluateShouldPublish({
6100
6292
  isDraftAndPublish,
6101
6293
  hadPublishedVersion,
6102
- isModified,
6103
- stageValue: stageVal,
6104
- publish: Boolean(publish),
6294
+ isEditing,
6295
+ publishOnApproval: Boolean(publish),
6105
6296
  publishStrategy,
6106
6297
  unreviewedConsent: options.unreviewedConsent
6107
6298
  });
@@ -6145,8 +6336,7 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
6145
6336
  if (effectiveLocale) createArgs.locale = effectiveLocale;
6146
6337
  const created = await strapi.documents(uid).create(createArgs);
6147
6338
  const newDocId = created?.documentId || docId;
6148
- const isApprovedStage = isRowInApprovedStage(row);
6149
- const shouldPublishNew = !isEditing && !isApprovedStage && isDraftAndPublish && Boolean(publish) && (publishStrategy === "all" || options.unreviewedConsent ? true : false);
6339
+ const shouldPublishNew = !isEditing && isDraftAndPublish && Boolean(publish) && (publishStrategy === "all" || options.unreviewedConsent ? true : false);
6150
6340
  if (shouldPublishNew && newDocId) {
6151
6341
  try {
6152
6342
  const pubArgs = { documentId: newDocId };
@@ -6201,14 +6391,33 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
6201
6391
  };
6202
6392
  if (effectiveLocale) updateArgs.locale = effectiveLocale;
6203
6393
  await strapi.documents(uid).update(updateArgs);
6204
- const { hadPublishedVersion, isModified } = inspectPreUpdateStatus(existing);
6205
- const stageVal = row.strapi_stage ?? row["strapi_stage"] ?? row.stage ?? existing?.strapi_stage;
6206
- const shouldPublish = determineShouldPublish({
6394
+ let pubRecord = effectiveLocale && publishedCounterparts.get(`${resolvedDocId}@@@${effectiveLocale}`) || publishedCounterparts.get(String(resolvedDocId));
6395
+ if (!pubRecord && isDraftAndPublish && resolvedDocId) {
6396
+ try {
6397
+ const singlePub = await strapi.documents(uid).findMany({
6398
+ status: "published",
6399
+ filters: { documentId: resolvedDocId },
6400
+ fields: ["documentId", "updatedAt", "publishedAt", "locale"],
6401
+ limit: 1
6402
+ });
6403
+ if (singlePub && singlePub.length > 0) {
6404
+ pubRecord = singlePub[0];
6405
+ publishedCounterparts.set(String(resolvedDocId), pubRecord);
6406
+ }
6407
+ } catch {
6408
+ }
6409
+ }
6410
+ const inspectionExisting = row.status || row.publishedAt ? { ...existing, status: row.status, publishedAt: row.publishedAt } : existing;
6411
+ const { hadPublishedVersion } = resolveRecordPublicationStatus(
6412
+ inspectionExisting,
6413
+ pubRecord,
6414
+ isDraftAndPublish
6415
+ );
6416
+ const shouldPublish = evaluateShouldPublish({
6207
6417
  isDraftAndPublish,
6208
6418
  hadPublishedVersion,
6209
- isModified,
6210
- stageValue: stageVal,
6211
- publish: Boolean(publish),
6419
+ isEditing,
6420
+ publishOnApproval: Boolean(publish),
6212
6421
  publishStrategy,
6213
6422
  unreviewedConsent: options.unreviewedConsent
6214
6423
  });
@@ -6252,8 +6461,7 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
6252
6461
  if (effectiveLocale) createArgs.locale = effectiveLocale;
6253
6462
  const created = await strapi.documents(uid).create(createArgs);
6254
6463
  const newDocId = created?.documentId || docId;
6255
- const isApprovedStage = isRowInApprovedStage(row);
6256
- const shouldPublishNew = !isEditing && !isApprovedStage && isDraftAndPublish && Boolean(publish) && (publishStrategy === "all" || options.unreviewedConsent ? true : false);
6464
+ const shouldPublishNew = !isEditing && isDraftAndPublish && Boolean(publish) && (publishStrategy === "all" || options.unreviewedConsent ? true : false);
6257
6465
  if (shouldPublishNew && newDocId) {
6258
6466
  try {
6259
6467
  const pubArgs = { documentId: newDocId };
@@ -6853,7 +7061,7 @@ const csvService = ({ strapi }) => ({
6853
7061
  const docId = extractDocId(row);
6854
7062
  const rowLocale = extractLocale(row);
6855
7063
  const effectiveLocale = getEffectiveLocale(ct, rowLocale);
6856
- isRowInEditingStage(row);
7064
+ const isEditing = isRowInEditingStage(row);
6857
7065
  let rowHasError = false;
6858
7066
  let existing = null;
6859
7067
  if (docId) {
@@ -6861,27 +7069,21 @@ const csvService = ({ strapi }) => ({
6861
7069
  }
6862
7070
  if (existing && isDraftAndPublish) {
6863
7071
  lifecycleStats.totalChecked++;
6864
- const { hadPublishedVersion, isModified } = inspectPreUpdateStatus(existing);
6865
- const stageVal = row.strapi_stage ?? row["strapi_stage"] ?? row.stage ?? existing?.strapi_stage;
6866
- const willPublish = determineShouldPublish({
6867
- isDraftAndPublish,
6868
- hadPublishedVersion,
6869
- isModified,
6870
- stageValue: stageVal,
6871
- publish: true,
6872
- publishStrategy: options.publishStrategy || "safe",
6873
- unreviewedConsent: options.unreviewedConsent
6874
- });
6875
- if (willPublish) {
7072
+ const inspectionExisting = row.status || row.publishedAt ? { ...existing, status: row.status, publishedAt: row.publishedAt } : existing;
7073
+ const { status: preStatus, hadPublishedVersion } = resolveRecordPublicationStatus(
7074
+ inspectionExisting,
7075
+ void 0,
7076
+ isDraftAndPublish
7077
+ );
7078
+ const isApproved = !isEditing && hadPublishedVersion;
7079
+ if (isApproved) {
6876
7080
  lifecycleStats.published++;
6877
- } else if (hadPublishedVersion && (isModified || isRowInEditingStage(row))) {
7081
+ } else if (preStatus === "modified" || hadPublishedVersion && isEditing) {
6878
7082
  lifecycleStats.modified++;
6879
7083
  lifecycleStats.unreviewedCount++;
6880
- } else if (!hadPublishedVersion) {
7084
+ } else {
6881
7085
  lifecycleStats.draft++;
6882
7086
  lifecycleStats.unreviewedCount++;
6883
- } else {
6884
- lifecycleStats.published++;
6885
7087
  }
6886
7088
  }
6887
7089
  if (mode === "update") {