strapi-plugin-bulk-updater 1.5.5 → 1.5.7

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,6 +993,151 @@ 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 === "string") {
999
+ const trimmed = val.trim();
1000
+ if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
1001
+ try {
1002
+ const parsed = JSON.parse(trimmed);
1003
+ if (parsed && typeof parsed === "object") {
1004
+ const fromParsed = extractStageName(parsed);
1005
+ if (fromParsed) return fromParsed;
1006
+ }
1007
+ } catch {
1008
+ }
1009
+ }
1010
+ return trimmed;
1011
+ }
1012
+ if (typeof val === "number") {
1013
+ return String(val);
1014
+ }
1015
+ if (typeof val === "object") {
1016
+ const candidateProps = [
1017
+ "strapi_stage",
1018
+ "strapi_stage.name",
1019
+ "strapi_stage[name]",
1020
+ "strapi_stage.title",
1021
+ "strapiStage",
1022
+ "Strapi Stage",
1023
+ "strapi-stage",
1024
+ "strapi_stage_name",
1025
+ "stage",
1026
+ "Stage",
1027
+ "stage.name",
1028
+ "stage[name]",
1029
+ "stage_name",
1030
+ "Stage Name",
1031
+ "review_stage",
1032
+ "review_stage.name",
1033
+ "review_stage[name]",
1034
+ "Review Stage",
1035
+ "reviewStage",
1036
+ "workflow_stage",
1037
+ "workflow_stage.name",
1038
+ "Workflow Stage",
1039
+ "workflowStage"
1040
+ ];
1041
+ for (const prop of candidateProps) {
1042
+ if (val[prop] !== void 0 && val[prop] !== null) {
1043
+ const fromProp = extractStageName(val[prop]);
1044
+ if (fromProp) return fromProp;
1045
+ }
1046
+ }
1047
+ const keys = Object.keys(val);
1048
+ for (const k of keys) {
1049
+ const norm = k.toLowerCase().replace(/[\s_\-\.\[\]]+/g, "");
1050
+ if (norm === "strapistage" || norm === "strapistagename" || norm === "stagename" || norm === "reviewstage" || norm === "reviewstagename" || norm === "workflowstage" || norm === "workflowstagename") {
1051
+ if (val[k] !== void 0 && val[k] !== null) {
1052
+ const fromNorm = extractStageName(val[k]);
1053
+ if (fromNorm) return fromNorm;
1054
+ }
1055
+ }
1056
+ }
1057
+ if (val.documentId && !val.color && !val.workflow) {
1058
+ return "";
1059
+ }
1060
+ const candidateStageValues = [
1061
+ val.name,
1062
+ val.title,
1063
+ val.label,
1064
+ val.value,
1065
+ val.stageName,
1066
+ val.stage_name,
1067
+ val.status
1068
+ ];
1069
+ for (const sv of candidateStageValues) {
1070
+ if (sv !== void 0 && sv !== null && typeof sv === "string" && sv.trim()) {
1071
+ return sv.trim();
1072
+ }
1073
+ }
1074
+ }
1075
+ return "";
1076
+ }
1077
+ function isStageApproved(stageVal) {
1078
+ const name = extractStageName(stageVal);
1079
+ return name.toLowerCase() === "approved";
1080
+ }
1081
+ function isStageEditingOrPending(stageVal) {
1082
+ const name = extractStageName(stageVal).toLowerCase();
1083
+ return name === "editing" || name === "in review" || name === "in_review" || name === "pending review" || name === "pending_review";
1084
+ }
1085
+ function inspectPreUpdateStatus(entry, publishedCounterpart) {
1086
+ if (!entry || typeof entry !== "object") {
1087
+ return { hadPublishedVersion: false, isModified: false };
1088
+ }
1089
+ const status = String(entry.status || "").toLowerCase();
1090
+ if (status === "published") {
1091
+ return { hadPublishedVersion: true, isModified: false };
1092
+ }
1093
+ if (status === "modified" || status === "modified draft") {
1094
+ return { hadPublishedVersion: true, isModified: true };
1095
+ }
1096
+ if (status === "draft") {
1097
+ return { hadPublishedVersion: false, isModified: false };
1098
+ }
1099
+ if (publishedCounterpart) {
1100
+ const draftUpdatedTime = new Date(entry.updatedAt || entry.updated_at || 0).getTime();
1101
+ const pubUpdatedTime = new Date(publishedCounterpart.updatedAt || publishedCounterpart.updated_at || 0).getTime();
1102
+ const pubPublishedTime = new Date(publishedCounterpart.publishedAt || publishedCounterpart.published_at || 0).getTime();
1103
+ const lastPublishedTime = Math.max(
1104
+ isNaN(pubUpdatedTime) ? 0 : pubUpdatedTime,
1105
+ isNaN(pubPublishedTime) ? 0 : pubPublishedTime
1106
+ );
1107
+ const isModified = !isNaN(draftUpdatedTime) && lastPublishedTime > 0 && draftUpdatedTime > lastPublishedTime;
1108
+ return { hadPublishedVersion: true, isModified };
1109
+ }
1110
+ const hasPubAt = Boolean(entry.publishedAt || entry.published_at);
1111
+ return { hadPublishedVersion: hasPubAt, isModified: false };
1112
+ }
1113
+ function determineShouldPublish({
1114
+ isDraftAndPublish,
1115
+ hadPublishedVersion,
1116
+ isModified = false,
1117
+ stageValue,
1118
+ publish = true,
1119
+ publishStrategy = "safe",
1120
+ unreviewedConsent = false
1121
+ }) {
1122
+ if (!isDraftAndPublish || !publish) {
1123
+ return false;
1124
+ }
1125
+ if (!hadPublishedVersion) {
1126
+ return false;
1127
+ }
1128
+ const isApproved = isStageApproved(stageValue);
1129
+ const isEditingOrPending = isStageEditingOrPending(stageValue);
1130
+ if (isApproved) {
1131
+ return true;
1132
+ }
1133
+ if (isEditingOrPending) {
1134
+ return false;
1135
+ }
1136
+ if (publishStrategy === "all" || unreviewedConsent) {
1137
+ return true;
1138
+ }
1139
+ return hadPublishedVersion && !isModified;
1140
+ }
996
1141
  async function populateDocumentLifecycleStatus(strapi, uid, entries) {
997
1142
  if (!entries || !Array.isArray(entries) || entries.length === 0) {
998
1143
  return entries;
@@ -1636,15 +1781,45 @@ async function executeBatchInternal(strapi, {
1636
1781
  const getDocumentDeduplicated = (uid, documentId) => {
1637
1782
  const loaderKey = `${uid}@@@${documentId}`;
1638
1783
  if (!documentLoader.has(loaderKey)) {
1784
+ strapi.contentTypes[uid];
1785
+ const findOneArgs = { documentId, populate: ["strapi_stage", "stage"] };
1639
1786
  documentLoader.set(
1640
1787
  loaderKey,
1641
- strapi.documents(uid).findOne({ documentId })
1788
+ strapi.documents(uid).findOne(findOneArgs)
1642
1789
  );
1643
1790
  }
1644
1791
  return documentLoader.get(loaderKey);
1645
1792
  };
1646
1793
  for (let i = 0; i < targets.length; i += CHUNK_SIZE) {
1647
1794
  const chunk = targets.slice(i, i + CHUNK_SIZE);
1795
+ const chunkByUid = /* @__PURE__ */ new Map();
1796
+ for (const t of chunk) {
1797
+ if (t.uid && t.documentId) {
1798
+ if (!chunkByUid.has(t.uid)) chunkByUid.set(t.uid, []);
1799
+ chunkByUid.get(t.uid).push(t.documentId);
1800
+ }
1801
+ }
1802
+ const publishedCounterparts = /* @__PURE__ */ new Map();
1803
+ for (const [colUid, docIds] of chunkByUid.entries()) {
1804
+ const colCt = strapi.contentTypes[colUid];
1805
+ if (colCt?.options?.draftAndPublish && docIds.length > 0) {
1806
+ try {
1807
+ const pubList = await strapi.documents(colUid).findMany({
1808
+ status: "published",
1809
+ filters: { documentId: { $in: docIds } },
1810
+ fields: ["documentId", "updatedAt", "publishedAt"],
1811
+ limit: docIds.length
1812
+ });
1813
+ for (const pub of pubList || []) {
1814
+ if (pub?.documentId) {
1815
+ publishedCounterparts.set(`${colUid}@@@${pub.documentId}`, pub);
1816
+ }
1817
+ }
1818
+ } catch (err) {
1819
+ strapi.log.warn(`[bulk-updater] Failed to pre-fetch published status for ${colUid}:`, err);
1820
+ }
1821
+ }
1822
+ }
1648
1823
  const chunkPromises = chunk.map(async (target) => {
1649
1824
  const contentType = strapi.contentTypes[target.uid];
1650
1825
  const displayName = contentType?.info?.displayName || target.uid;
@@ -1696,13 +1871,42 @@ async function executeBatchInternal(strapi, {
1696
1871
  }
1697
1872
  });
1698
1873
  const isDraftAndPublishComp = Boolean(contentType?.options?.draftAndPublish);
1699
- const isApprovedComp = isDraftAndPublishComp && Boolean(currentEntry?.publishedAt) && currentEntry?.status !== "modified";
1700
- const shouldPublishComp = isDraftAndPublishComp && Boolean(publishOnApproval) && (publishStrategy === "all" ? true : isApprovedComp);
1701
- if (shouldPublishComp) {
1874
+ let pubRecordComp = publishedCounterparts.get(`${target.uid}@@@${target.documentId}`);
1875
+ if (!pubRecordComp && isDraftAndPublishComp && target.documentId) {
1702
1876
  try {
1703
- await strapi.documents(target.uid).publish({
1704
- documentId: target.documentId
1877
+ const singlePub = await strapi.documents(target.uid).findMany({
1878
+ status: "published",
1879
+ filters: { documentId: target.documentId },
1880
+ fields: ["documentId", "updatedAt", "publishedAt"],
1881
+ limit: 1
1705
1882
  });
1883
+ if (singlePub && singlePub.length > 0) {
1884
+ pubRecordComp = singlePub[0];
1885
+ publishedCounterparts.set(`${target.uid}@@@${target.documentId}`, pubRecordComp);
1886
+ }
1887
+ } catch {
1888
+ }
1889
+ }
1890
+ const inspectionTargetComp = target.status || target.publishedAt ? { ...currentEntry, status: target.status, publishedAt: target.publishedAt } : currentEntry;
1891
+ const { hadPublishedVersion: hadPubComp, isModified: isModComp } = inspectPreUpdateStatus(inspectionTargetComp, pubRecordComp);
1892
+ const targetStageComp = attribute.toLowerCase() === "strapi_stage" || attribute.toLowerCase() === "stage" ? value : currentEntry?.strapi_stage ?? currentEntry?.stage ?? target?.strapi_stage ?? target?.stage;
1893
+ const shouldPublishComp = determineShouldPublish({
1894
+ isDraftAndPublish: isDraftAndPublishComp,
1895
+ hadPublishedVersion: hadPubComp,
1896
+ isModified: isModComp,
1897
+ stageValue: targetStageComp,
1898
+ publish: Boolean(publishOnApproval),
1899
+ publishStrategy,
1900
+ unreviewedConsent
1901
+ });
1902
+ if (shouldPublishComp) {
1903
+ try {
1904
+ const pubArgs = { documentId: target.documentId };
1905
+ const candLoc = target.locale || currentEntry?.locale;
1906
+ if (Boolean(contentType?.pluginOptions?.i18n?.localized) && candLoc && candLoc !== "null" && candLoc !== "undefined") {
1907
+ pubArgs.locale = candLoc;
1908
+ }
1909
+ await strapi.documents(target.uid).publish(pubArgs);
1706
1910
  } catch (publishErr) {
1707
1911
  strapi.log.warn(`[bulk-updater] Failed to auto-publish ${target.uid}/${target.documentId}:`, publishErr);
1708
1912
  }
@@ -1712,7 +1916,7 @@ async function executeBatchInternal(strapi, {
1712
1916
  uid: target.uid,
1713
1917
  documentId: target.documentId,
1714
1918
  userId: currentUser?.id,
1715
- statusOverride: shouldPublishComp ? "published" : isDraftAndPublishComp ? "draft" : void 0
1919
+ statusOverride: shouldPublishComp ? "published" : isDraftAndPublishComp ? hadPubComp ? "modified" : "draft" : void 0
1716
1920
  });
1717
1921
  const dzDiffs = isRootDz && affectedMatches.length > 0 ? affectedMatches.map((m) => ({
1718
1922
  index: m.index,
@@ -1795,13 +1999,42 @@ async function executeBatchInternal(strapi, {
1795
1999
  }
1796
2000
  });
1797
2001
  const isDraftAndPublishRel = Boolean(contentType?.options?.draftAndPublish);
1798
- const isApprovedRel = isDraftAndPublishRel && Boolean(currentEntry?.publishedAt) && currentEntry?.status !== "modified";
1799
- const shouldPublishRel = isDraftAndPublishRel && Boolean(publishOnApproval) && (publishStrategy === "all" ? true : isApprovedRel);
1800
- if (shouldPublishRel) {
2002
+ let pubRecordRel = publishedCounterparts.get(`${target.uid}@@@${target.documentId}`);
2003
+ if (!pubRecordRel && isDraftAndPublishRel && target.documentId) {
1801
2004
  try {
1802
- await strapi.documents(target.uid).publish({
1803
- documentId: target.documentId
2005
+ const singlePub = await strapi.documents(target.uid).findMany({
2006
+ status: "published",
2007
+ filters: { documentId: target.documentId },
2008
+ fields: ["documentId", "updatedAt", "publishedAt"],
2009
+ limit: 1
1804
2010
  });
2011
+ if (singlePub && singlePub.length > 0) {
2012
+ pubRecordRel = singlePub[0];
2013
+ publishedCounterparts.set(`${target.uid}@@@${target.documentId}`, pubRecordRel);
2014
+ }
2015
+ } catch {
2016
+ }
2017
+ }
2018
+ const inspectionTargetRel = target.status || target.publishedAt ? { ...currentEntry, status: target.status, publishedAt: target.publishedAt } : currentEntry;
2019
+ const { hadPublishedVersion: hadPubRel, isModified: isModRel } = inspectPreUpdateStatus(inspectionTargetRel, pubRecordRel);
2020
+ const targetStageRel = attribute.toLowerCase() === "strapi_stage" || attribute.toLowerCase() === "stage" ? value : currentEntry?.strapi_stage ?? currentEntry?.stage ?? target?.strapi_stage ?? target?.stage;
2021
+ const shouldPublishRel = determineShouldPublish({
2022
+ isDraftAndPublish: isDraftAndPublishRel,
2023
+ hadPublishedVersion: hadPubRel,
2024
+ isModified: isModRel,
2025
+ stageValue: targetStageRel,
2026
+ publish: Boolean(publishOnApproval),
2027
+ publishStrategy,
2028
+ unreviewedConsent
2029
+ });
2030
+ if (shouldPublishRel) {
2031
+ try {
2032
+ const pubArgs = { documentId: target.documentId };
2033
+ const candLoc = target.locale || currentEntry?.locale;
2034
+ if (Boolean(contentType?.pluginOptions?.i18n?.localized) && candLoc && candLoc !== "null" && candLoc !== "undefined") {
2035
+ pubArgs.locale = candLoc;
2036
+ }
2037
+ await strapi.documents(target.uid).publish(pubArgs);
1805
2038
  } catch (publishErr) {
1806
2039
  strapi.log.warn(`[bulk-updater] Failed to auto-publish ${target.uid}/${target.documentId}:`, publishErr);
1807
2040
  }
@@ -1814,7 +2047,7 @@ async function executeBatchInternal(strapi, {
1814
2047
  uid: target.uid,
1815
2048
  documentId: target.documentId,
1816
2049
  userId: currentUser?.id,
1817
- statusOverride: shouldPublishRel ? "published" : isDraftAndPublishRel ? "draft" : void 0
2050
+ statusOverride: shouldPublishRel ? "published" : isDraftAndPublishRel ? hadPubRel ? "modified" : "draft" : void 0
1818
2051
  });
1819
2052
  return {
1820
2053
  uid: target.uid,
@@ -1840,13 +2073,42 @@ async function executeBatchInternal(strapi, {
1840
2073
  }
1841
2074
  });
1842
2075
  const isDraftAndPublishScalar = Boolean(contentType?.options?.draftAndPublish);
1843
- const isApprovedScalar = isDraftAndPublishScalar && Boolean(currentEntry?.publishedAt) && currentEntry?.status !== "modified";
1844
- const shouldPublishScalar = isDraftAndPublishScalar && Boolean(publishOnApproval) && (publishStrategy === "all" ? true : isApprovedScalar);
1845
- if (shouldPublishScalar) {
2076
+ let pubRecordScalar = publishedCounterparts.get(`${target.uid}@@@${target.documentId}`);
2077
+ if (!pubRecordScalar && isDraftAndPublishScalar && target.documentId) {
1846
2078
  try {
1847
- await strapi.documents(target.uid).publish({
1848
- documentId: target.documentId
2079
+ const singlePub = await strapi.documents(target.uid).findMany({
2080
+ status: "published",
2081
+ filters: { documentId: target.documentId },
2082
+ fields: ["documentId", "updatedAt", "publishedAt"],
2083
+ limit: 1
1849
2084
  });
2085
+ if (singlePub && singlePub.length > 0) {
2086
+ pubRecordScalar = singlePub[0];
2087
+ publishedCounterparts.set(`${target.uid}@@@${target.documentId}`, pubRecordScalar);
2088
+ }
2089
+ } catch {
2090
+ }
2091
+ }
2092
+ const inspectionTargetScalar = target.status || target.publishedAt ? { ...currentEntry, status: target.status, publishedAt: target.publishedAt } : currentEntry;
2093
+ const { hadPublishedVersion: hadPubScalar, isModified: isModScalar } = inspectPreUpdateStatus(inspectionTargetScalar, pubRecordScalar);
2094
+ const targetStageScalar = attribute.toLowerCase() === "strapi_stage" || attribute.toLowerCase() === "stage" ? value : currentEntry?.strapi_stage ?? currentEntry?.stage ?? target?.strapi_stage ?? target?.stage;
2095
+ const shouldPublishScalar = determineShouldPublish({
2096
+ isDraftAndPublish: isDraftAndPublishScalar,
2097
+ hadPublishedVersion: hadPubScalar,
2098
+ isModified: isModScalar,
2099
+ stageValue: targetStageScalar,
2100
+ publish: Boolean(publishOnApproval),
2101
+ publishStrategy,
2102
+ unreviewedConsent
2103
+ });
2104
+ if (shouldPublishScalar) {
2105
+ try {
2106
+ const pubArgs = { documentId: target.documentId };
2107
+ const candLoc = target.locale || currentEntry?.locale;
2108
+ if (Boolean(contentType?.pluginOptions?.i18n?.localized) && candLoc && candLoc !== "null" && candLoc !== "undefined") {
2109
+ pubArgs.locale = candLoc;
2110
+ }
2111
+ await strapi.documents(target.uid).publish(pubArgs);
1850
2112
  } catch (publishErr) {
1851
2113
  strapi.log.warn(`[bulk-updater] Failed to auto-publish ${target.uid}/${target.documentId}:`, publishErr);
1852
2114
  }
@@ -1855,7 +2117,7 @@ async function executeBatchInternal(strapi, {
1855
2117
  uid: target.uid,
1856
2118
  documentId: target.documentId,
1857
2119
  userId: currentUser?.id,
1858
- statusOverride: shouldPublishScalar ? "published" : isDraftAndPublishScalar ? "draft" : void 0
2120
+ statusOverride: shouldPublishScalar ? "published" : isDraftAndPublishScalar ? hadPubScalar ? "modified" : "draft" : void 0
1859
2121
  });
1860
2122
  return {
1861
2123
  uid: target.uid,
@@ -5218,50 +5480,30 @@ function getEffectiveLocale(ct, rowLocale) {
5218
5480
  }
5219
5481
  return trimmed;
5220
5482
  }
5221
- function isRowInEditingStage(row) {
5222
- if (!row || typeof row !== "object") return false;
5223
- const candidates = [
5224
- row.strapi_stage,
5225
- row["strapi_stage"],
5226
- row.strapiStage,
5227
- row["Strapi Stage"],
5228
- row["strapi-stage"],
5229
- row.stage,
5230
- row.Stage,
5231
- row.status,
5232
- row.Status
5233
- ];
5234
- for (const c of candidates) {
5235
- if (c !== void 0 && c !== null) {
5236
- let stageName = "";
5237
- if (typeof c === "object") {
5238
- stageName = String(c.name || c.title || c.status || c.documentId || c.id || "").trim();
5239
- } else if (typeof c === "string") {
5240
- const trimmed = c.trim();
5241
- if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
5242
- try {
5243
- const parsed = JSON.parse(trimmed);
5244
- if (typeof parsed === "object" && parsed !== null) {
5245
- stageName = String(parsed.name || parsed.title || parsed.status || parsed.documentId || parsed.id || "").trim();
5246
- }
5247
- } catch {
5248
- }
5249
- }
5250
- if (!stageName) {
5251
- stageName = trimmed;
5252
- }
5253
- } else {
5254
- stageName = String(c).trim();
5255
- }
5256
- if (stageName) {
5257
- const s = stageName.toLowerCase();
5258
- if (s === "editing" || s === "in review" || s === "in_review" || s === "pending review" || s === "pending_review") {
5259
- return true;
5260
- }
5261
- }
5483
+ function resolveStageValue(row, existing) {
5484
+ if (row && typeof row === "object") {
5485
+ const stageNameFromRow = extractStageName(row);
5486
+ if (stageNameFromRow) return stageNameFromRow;
5487
+ }
5488
+ if (existing && typeof existing === "object") {
5489
+ if (existing.strapi_stage !== void 0 && existing.strapi_stage !== null) {
5490
+ return existing.strapi_stage;
5491
+ }
5492
+ if (existing.stage !== void 0 && existing.stage !== null) {
5493
+ return existing.stage;
5262
5494
  }
5495
+ const stageNameFromExisting = extractStageName(existing);
5496
+ if (stageNameFromExisting) return stageNameFromExisting;
5263
5497
  }
5264
- return false;
5498
+ return void 0;
5499
+ }
5500
+ function isRowInEditingStage(row, existing) {
5501
+ const stage = resolveStageValue(row, existing);
5502
+ return isStageEditingOrPending(stage);
5503
+ }
5504
+ function isRowInApprovedStage(row, existing) {
5505
+ const stage = resolveStageValue(row, existing);
5506
+ return isStageApproved(stage);
5265
5507
  }
5266
5508
  function extractDetailedErrorMessage(err) {
5267
5509
  if (!err) return "Error processing row";
@@ -5459,6 +5701,7 @@ async function findExistingDocument(strapi, uid, targetDocId, docLocale) {
5459
5701
  try {
5460
5702
  const queryArgs = { documentId: targetDocId };
5461
5703
  if (effectiveLocale) queryArgs.locale = effectiveLocale;
5704
+ queryArgs.populate = ["strapi_stage", "stage"];
5462
5705
  const found = await strapi.documents(uid).findOne(queryArgs);
5463
5706
  if (found) return found;
5464
5707
  } catch {
@@ -5838,6 +6081,25 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
5838
6081
  errors: [],
5839
6082
  diffs: []
5840
6083
  };
6084
+ const docIdsInChunk = rows.map((r) => extractDocId(r)).filter(Boolean);
6085
+ const publishedCounterparts = /* @__PURE__ */ new Map();
6086
+ if (isDraftAndPublish && docIdsInChunk.length > 0) {
6087
+ try {
6088
+ const pubList = await strapi.documents(uid).findMany({
6089
+ status: "published",
6090
+ filters: { documentId: { $in: docIdsInChunk } },
6091
+ fields: ["documentId", "updatedAt", "publishedAt"],
6092
+ limit: docIdsInChunk.length
6093
+ });
6094
+ for (const pub of pubList || []) {
6095
+ if (pub?.documentId) {
6096
+ publishedCounterparts.set(String(pub.documentId), pub);
6097
+ }
6098
+ }
6099
+ } catch (err) {
6100
+ strapi.log.warn(`[csv] Failed to pre-fetch published status for CSV chunk:`, err);
6101
+ }
6102
+ }
5841
6103
  for (let i = 0; i < rows.length; i++) {
5842
6104
  const rowNumber = startRowNumber + i;
5843
6105
  const row = rows[i];
@@ -5905,12 +6167,40 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
5905
6167
  };
5906
6168
  if (effectiveLocale) updateArgs.locale = effectiveLocale;
5907
6169
  await strapi.documents(uid).update(updateArgs);
5908
- const isApproved = !isEditing && isDraftAndPublish && Boolean(existing?.publishedAt) && existing?.status !== "modified";
5909
- const shouldPublish = !isEditing && isDraftAndPublish && Boolean(publish) && (publishStrategy === "all" || options.unreviewedConsent ? true : isApproved);
6170
+ let pubRecord = publishedCounterparts.get(String(resolvedDocId));
6171
+ if (!pubRecord && isDraftAndPublish && resolvedDocId) {
6172
+ try {
6173
+ const singlePub = await strapi.documents(uid).findMany({
6174
+ status: "published",
6175
+ filters: { documentId: resolvedDocId },
6176
+ fields: ["documentId", "updatedAt", "publishedAt"],
6177
+ limit: 1
6178
+ });
6179
+ if (singlePub && singlePub.length > 0) {
6180
+ pubRecord = singlePub[0];
6181
+ publishedCounterparts.set(String(resolvedDocId), pubRecord);
6182
+ }
6183
+ } catch {
6184
+ }
6185
+ }
6186
+ const { hadPublishedVersion, isModified } = inspectPreUpdateStatus(existing, pubRecord);
6187
+ const stageVal = resolveStageValue(row, existing);
6188
+ const shouldPublish = determineShouldPublish({
6189
+ isDraftAndPublish,
6190
+ hadPublishedVersion,
6191
+ isModified,
6192
+ stageValue: stageVal,
6193
+ publish: Boolean(publish),
6194
+ publishStrategy,
6195
+ unreviewedConsent: options.unreviewedConsent
6196
+ });
5910
6197
  if (shouldPublish) {
5911
6198
  try {
5912
6199
  const pubArgs = { documentId: resolvedDocId };
5913
- if (effectiveLocale) pubArgs.locale = effectiveLocale;
6200
+ const isLoc = Boolean(ct?.pluginOptions?.i18n?.localized);
6201
+ if (isLoc && effectiveLocale && effectiveLocale !== "null" && effectiveLocale !== "undefined") {
6202
+ pubArgs.locale = effectiveLocale;
6203
+ }
5914
6204
  await strapi.documents(uid).publish(pubArgs);
5915
6205
  } catch (pubErr) {
5916
6206
  strapi.log.warn(`[bulk-updater] Auto-publish failed for ${resolvedDocId}:`, pubErr);
@@ -5921,7 +6211,7 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
5921
6211
  uid,
5922
6212
  documentId: resolvedDocId,
5923
6213
  userId: user?.id,
5924
- statusOverride: shouldPublish ? "published" : isDraftAndPublish ? existing?.publishedAt ? "modified" : "draft" : void 0
6214
+ statusOverride: shouldPublish ? "published" : isDraftAndPublish ? hadPublishedVersion ? "modified" : "draft" : void 0
5925
6215
  });
5926
6216
  } catch {
5927
6217
  }
@@ -5960,12 +6250,38 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
5960
6250
  };
5961
6251
  if (effectiveLocale) updateArgs.locale = effectiveLocale;
5962
6252
  await strapi.documents(uid).update(updateArgs);
5963
- const isApproved = !isEditing && isDraftAndPublish && Boolean(existing?.publishedAt) && existing?.status !== "modified";
5964
- const shouldPublish = !isEditing && isDraftAndPublish && Boolean(publish) && (publishStrategy === "all" || options.unreviewedConsent ? true : isApproved);
6253
+ let pubRecord = publishedCounterparts.get(String(resolvedDocId));
6254
+ if (!pubRecord && isDraftAndPublish && resolvedDocId) {
6255
+ try {
6256
+ const singlePub = await strapi.documents(uid).findMany({
6257
+ status: "published",
6258
+ filters: { documentId: resolvedDocId },
6259
+ fields: ["documentId", "updatedAt", "publishedAt"],
6260
+ limit: 1
6261
+ });
6262
+ if (singlePub && singlePub.length > 0) {
6263
+ pubRecord = singlePub[0];
6264
+ publishedCounterparts.set(String(resolvedDocId), pubRecord);
6265
+ }
6266
+ } catch {
6267
+ }
6268
+ }
6269
+ const { hadPublishedVersion, isModified } = inspectPreUpdateStatus(existing, pubRecord);
6270
+ const stageVal = resolveStageValue(row, existing);
6271
+ const shouldPublish = determineShouldPublish({
6272
+ isDraftAndPublish,
6273
+ hadPublishedVersion,
6274
+ isModified,
6275
+ stageValue: stageVal,
6276
+ publish: Boolean(publish),
6277
+ publishStrategy,
6278
+ unreviewedConsent: options.unreviewedConsent
6279
+ });
5965
6280
  if (shouldPublish) {
5966
6281
  try {
5967
6282
  const pubArgs = { documentId: resolvedDocId };
5968
- if (effectiveLocale) pubArgs.locale = effectiveLocale;
6283
+ const isLoc = Boolean(ct?.pluginOptions?.i18n?.localized);
6284
+ if (isLoc && effectiveLocale && effectiveLocale !== "null" && effectiveLocale !== "undefined") pubArgs.locale = effectiveLocale;
5969
6285
  await strapi.documents(uid).publish(pubArgs);
5970
6286
  } catch (pubErr) {
5971
6287
  strapi.log.warn(`[bulk-updater] Auto-publish failed for ${resolvedDocId}:`, pubErr);
@@ -5976,7 +6292,7 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
5976
6292
  uid,
5977
6293
  documentId: resolvedDocId,
5978
6294
  userId: user?.id,
5979
- statusOverride: shouldPublish ? "published" : isDraftAndPublish ? existing?.publishedAt ? "modified" : "draft" : void 0
6295
+ statusOverride: shouldPublish ? "published" : isDraftAndPublish ? hadPublishedVersion ? "modified" : "draft" : void 0
5980
6296
  });
5981
6297
  } catch {
5982
6298
  }
@@ -6002,11 +6318,13 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
6002
6318
  if (effectiveLocale) createArgs.locale = effectiveLocale;
6003
6319
  const created = await strapi.documents(uid).create(createArgs);
6004
6320
  const newDocId = created?.documentId || docId;
6005
- const shouldPublishNew = !isEditing && isDraftAndPublish && Boolean(publish) && (publishStrategy === "all" || options.unreviewedConsent ? true : false);
6321
+ const isApprovedStage = isRowInApprovedStage(row);
6322
+ const shouldPublishNew = !isEditing && !isApprovedStage && isDraftAndPublish && Boolean(publish) && (publishStrategy === "all" || options.unreviewedConsent ? true : false);
6006
6323
  if (shouldPublishNew && newDocId) {
6007
6324
  try {
6008
6325
  const pubArgs = { documentId: newDocId };
6009
- if (effectiveLocale) pubArgs.locale = effectiveLocale;
6326
+ const isLoc = Boolean(ct?.pluginOptions?.i18n?.localized);
6327
+ if (isLoc && effectiveLocale && effectiveLocale !== "null" && effectiveLocale !== "undefined") pubArgs.locale = effectiveLocale;
6010
6328
  await strapi.documents(uid).publish(pubArgs);
6011
6329
  } catch (pubErr) {
6012
6330
  strapi.log.warn(`[bulk-updater] Auto-publish failed for ${newDocId}:`, pubErr);
@@ -6057,12 +6375,38 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
6057
6375
  };
6058
6376
  if (effectiveLocale) updateArgs.locale = effectiveLocale;
6059
6377
  await strapi.documents(uid).update(updateArgs);
6060
- const isApproved = !isEditing && isDraftAndPublish && Boolean(existing?.publishedAt) && existing?.status !== "modified";
6061
- const shouldPublish = !isEditing && isDraftAndPublish && Boolean(publish) && (publishStrategy === "all" || options.unreviewedConsent ? true : isApproved);
6378
+ let pubRecord = publishedCounterparts.get(String(resolvedDocId));
6379
+ if (!pubRecord && isDraftAndPublish && resolvedDocId) {
6380
+ try {
6381
+ const singlePub = await strapi.documents(uid).findMany({
6382
+ status: "published",
6383
+ filters: { documentId: resolvedDocId },
6384
+ fields: ["documentId", "updatedAt", "publishedAt"],
6385
+ limit: 1
6386
+ });
6387
+ if (singlePub && singlePub.length > 0) {
6388
+ pubRecord = singlePub[0];
6389
+ publishedCounterparts.set(String(resolvedDocId), pubRecord);
6390
+ }
6391
+ } catch {
6392
+ }
6393
+ }
6394
+ const { hadPublishedVersion, isModified } = inspectPreUpdateStatus(existing, pubRecord);
6395
+ const stageVal = resolveStageValue(row, existing);
6396
+ const shouldPublish = determineShouldPublish({
6397
+ isDraftAndPublish,
6398
+ hadPublishedVersion,
6399
+ isModified,
6400
+ stageValue: stageVal,
6401
+ publish: Boolean(publish),
6402
+ publishStrategy,
6403
+ unreviewedConsent: options.unreviewedConsent
6404
+ });
6062
6405
  if (shouldPublish) {
6063
6406
  try {
6064
6407
  const pubArgs = { documentId: resolvedDocId };
6065
- if (effectiveLocale) pubArgs.locale = effectiveLocale;
6408
+ const isLoc = Boolean(ct?.pluginOptions?.i18n?.localized);
6409
+ if (isLoc && effectiveLocale && effectiveLocale !== "null" && effectiveLocale !== "undefined") pubArgs.locale = effectiveLocale;
6066
6410
  await strapi.documents(uid).publish(pubArgs);
6067
6411
  } catch (pubErr) {
6068
6412
  strapi.log.warn(`[bulk-updater] Auto-publish failed for ${resolvedDocId}:`, pubErr);
@@ -6073,7 +6417,7 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
6073
6417
  uid,
6074
6418
  documentId: resolvedDocId,
6075
6419
  userId: user?.id,
6076
- statusOverride: shouldPublish ? "published" : isDraftAndPublish ? existing?.publishedAt ? "modified" : "draft" : void 0
6420
+ statusOverride: shouldPublish ? "published" : isDraftAndPublish ? hadPublishedVersion ? "modified" : "draft" : void 0
6077
6421
  });
6078
6422
  } catch {
6079
6423
  }
@@ -6099,11 +6443,13 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
6099
6443
  if (effectiveLocale) createArgs.locale = effectiveLocale;
6100
6444
  const created = await strapi.documents(uid).create(createArgs);
6101
6445
  const newDocId = created?.documentId || docId;
6102
- const shouldPublishNew = !isEditing && isDraftAndPublish && Boolean(publish) && (publishStrategy === "all" || options.unreviewedConsent ? true : false);
6446
+ const isApprovedStage = isRowInApprovedStage(row);
6447
+ const shouldPublishNew = !isEditing && !isApprovedStage && isDraftAndPublish && Boolean(publish) && (publishStrategy === "all" || options.unreviewedConsent ? true : false);
6103
6448
  if (shouldPublishNew && newDocId) {
6104
6449
  try {
6105
6450
  const pubArgs = { documentId: newDocId };
6106
- if (effectiveLocale) pubArgs.locale = effectiveLocale;
6451
+ const isLoc = Boolean(ct?.pluginOptions?.i18n?.localized);
6452
+ if (isLoc && effectiveLocale && effectiveLocale !== "null" && effectiveLocale !== "undefined") pubArgs.locale = effectiveLocale;
6107
6453
  await strapi.documents(uid).publish(pubArgs);
6108
6454
  } catch (pubErr) {
6109
6455
  strapi.log.warn(`[bulk-updater] Auto-publish failed for ${newDocId}:`, pubErr);
@@ -6691,6 +7037,25 @@ const csvService = ({ strapi }) => ({
6691
7037
  draft: 0,
6692
7038
  unreviewedCount: 0
6693
7039
  };
7040
+ const docIdsInPreflight = rows.map((r) => extractDocId(r)).filter(Boolean);
7041
+ const publishedCounterparts = /* @__PURE__ */ new Map();
7042
+ if (isDraftAndPublish && docIdsInPreflight.length > 0) {
7043
+ try {
7044
+ const pubList = await strapi.documents(uid).findMany({
7045
+ status: "published",
7046
+ filters: { documentId: { $in: docIdsInPreflight } },
7047
+ fields: ["documentId", "updatedAt", "publishedAt"],
7048
+ limit: docIdsInPreflight.length
7049
+ });
7050
+ for (const pub of pubList || []) {
7051
+ if (pub?.documentId) {
7052
+ publishedCounterparts.set(String(pub.documentId), pub);
7053
+ }
7054
+ }
7055
+ } catch (err) {
7056
+ strapi.log.warn(`[csv] Failed to pre-fetch published status for CSV preflight:`, err);
7057
+ }
7058
+ }
6694
7059
  const errors = [];
6695
7060
  const warnings = [];
6696
7061
  for (let i = 0; i < rows.length; i++) {
@@ -6699,7 +7064,7 @@ const csvService = ({ strapi }) => ({
6699
7064
  const docId = extractDocId(row);
6700
7065
  const rowLocale = extractLocale(row);
6701
7066
  const effectiveLocale = getEffectiveLocale(ct, rowLocale);
6702
- const isEditing = isRowInEditingStage(row);
7067
+ isRowInEditingStage(row);
6703
7068
  let rowHasError = false;
6704
7069
  let existing = null;
6705
7070
  if (docId) {
@@ -6707,15 +7072,44 @@ const csvService = ({ strapi }) => ({
6707
7072
  }
6708
7073
  if (existing && isDraftAndPublish) {
6709
7074
  lifecycleStats.totalChecked++;
6710
- const isApproved = !isEditing && Boolean(existing.publishedAt) && existing.status !== "modified";
6711
- if (isApproved) {
7075
+ const targetDocId = existing.documentId || docId;
7076
+ let pubRecord = publishedCounterparts.get(String(targetDocId));
7077
+ if (!pubRecord && isDraftAndPublish && targetDocId) {
7078
+ try {
7079
+ const singlePub = await strapi.documents(uid).findMany({
7080
+ status: "published",
7081
+ filters: { documentId: targetDocId },
7082
+ fields: ["documentId", "updatedAt", "publishedAt"],
7083
+ limit: 1
7084
+ });
7085
+ if (singlePub && singlePub.length > 0) {
7086
+ pubRecord = singlePub[0];
7087
+ publishedCounterparts.set(String(targetDocId), pubRecord);
7088
+ }
7089
+ } catch {
7090
+ }
7091
+ }
7092
+ const { hadPublishedVersion, isModified } = inspectPreUpdateStatus(existing, pubRecord);
7093
+ const stageVal = resolveStageValue(row, existing);
7094
+ const willPublish = determineShouldPublish({
7095
+ isDraftAndPublish,
7096
+ hadPublishedVersion,
7097
+ isModified,
7098
+ stageValue: stageVal,
7099
+ publish: true,
7100
+ publishStrategy: options.publishStrategy || "safe",
7101
+ unreviewedConsent: options.unreviewedConsent
7102
+ });
7103
+ if (willPublish) {
6712
7104
  lifecycleStats.published++;
6713
- } else if (existing.status === "modified" || existing.publishedAt && isEditing) {
7105
+ } else if (hadPublishedVersion && (isModified || isRowInEditingStage(row))) {
6714
7106
  lifecycleStats.modified++;
6715
7107
  lifecycleStats.unreviewedCount++;
6716
- } else {
7108
+ } else if (!hadPublishedVersion) {
6717
7109
  lifecycleStats.draft++;
6718
7110
  lifecycleStats.unreviewedCount++;
7111
+ } else {
7112
+ lifecycleStats.published++;
6719
7113
  }
6720
7114
  }
6721
7115
  if (mode === "update") {