strapi-plugin-bulk-updater 1.6.1 → 1.6.2
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.
- package/dist/_chunks/{BulkUpdater-DTJLdJJy.js → BulkUpdater-Y09TGkcC.js} +46 -6
- package/dist/_chunks/BulkUpdater-Y09TGkcC.js.map +1 -0
- package/dist/_chunks/{BulkUpdater-xj4wH_iI.mjs → BulkUpdater-e7N4vMse.mjs} +47 -7
- package/dist/_chunks/BulkUpdater-e7N4vMse.mjs.map +1 -0
- package/dist/admin/index.js +1 -1
- package/dist/admin/index.mjs +1 -1
- package/dist/server/index.js +1543 -1264
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +1543 -1264
- package/dist/server/index.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/_chunks/BulkUpdater-DTJLdJJy.js.map +0 -1
- package/dist/_chunks/BulkUpdater-xj4wH_iI.mjs.map +0 -1
package/dist/server/index.js
CHANGED
|
@@ -661,8 +661,8 @@ const csvController = ({ strapi }) => ({
|
|
|
661
661
|
async exportCollection(ctx) {
|
|
662
662
|
try {
|
|
663
663
|
const { uid } = ctx.params;
|
|
664
|
-
const { search, attribute, searchAttribute, filterOperator, filterQuery, limit, documentIds } = {
|
|
665
|
-
...ctx.query,
|
|
664
|
+
const { search, attribute, searchAttribute, filterOperator, filterQuery, limit, documentIds, exportMode } = {
|
|
665
|
+
...ctx.query || {},
|
|
666
666
|
...ctx.request?.body || {}
|
|
667
667
|
};
|
|
668
668
|
const csvService2 = strapi.plugin("bulk-updater").service("csv");
|
|
@@ -677,7 +677,8 @@ const csvController = ({ strapi }) => ({
|
|
|
677
677
|
filterOperator: filterOperator ? String(filterOperator) : void 0,
|
|
678
678
|
filterQuery: filterQuery !== void 0 ? String(filterQuery) : void 0,
|
|
679
679
|
limit: limit ? Number(limit) : void 0,
|
|
680
|
-
documentIds: parsedDocIds
|
|
680
|
+
documentIds: parsedDocIds,
|
|
681
|
+
exportMode: exportMode ? String(exportMode) : void 0
|
|
681
682
|
});
|
|
682
683
|
ctx.body = {
|
|
683
684
|
success: true,
|
|
@@ -1733,7 +1734,15 @@ function discoverSchema(strapi, uid) {
|
|
|
1733
1734
|
};
|
|
1734
1735
|
}
|
|
1735
1736
|
const EXPLICIT_CLEAR_TOKENS = /* @__PURE__ */ new Set(["__CLEAR__", "__NULL__"]);
|
|
1737
|
+
const EXPLICIT_EMPTY_STRING_TOKEN = "__EMPTY__";
|
|
1738
|
+
const EXPLICIT_DELETE_KEY_TOKEN = "__DELETE__";
|
|
1736
1739
|
const FORBIDDEN_OBJECT_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
1740
|
+
function normalizeExportMode(raw) {
|
|
1741
|
+
const val = String(raw || "").trim().toLowerCase();
|
|
1742
|
+
if (val === "metafields") return "metafields";
|
|
1743
|
+
if (val === "topbarconfig") return "topbarconfig";
|
|
1744
|
+
return "all";
|
|
1745
|
+
}
|
|
1737
1746
|
function isPlainObject(val) {
|
|
1738
1747
|
if (val === null || typeof val !== "object" || Array.isArray(val)) return false;
|
|
1739
1748
|
if (val instanceof Date || val instanceof RegExp) return false;
|
|
@@ -1760,37 +1769,39 @@ function deepCloneValue(val) {
|
|
|
1760
1769
|
}
|
|
1761
1770
|
return val;
|
|
1762
1771
|
}
|
|
1763
|
-
function
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
return { exists: false, value: void 0 };
|
|
1772
|
+
function deepEqualValues(a, b) {
|
|
1773
|
+
if (a === b) return true;
|
|
1774
|
+
if (a === null || b === null || a === void 0 || b === void 0) return false;
|
|
1775
|
+
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
|
1776
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
1777
|
+
if (a.length !== b.length) return false;
|
|
1778
|
+
for (let i = 0; i < a.length; i++) {
|
|
1779
|
+
if (!deepEqualValues(a[i], b[i])) return false;
|
|
1772
1780
|
}
|
|
1773
|
-
|
|
1774
|
-
|
|
1781
|
+
return true;
|
|
1782
|
+
}
|
|
1783
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
1784
|
+
const keysA = Object.keys(a).filter((k) => !FORBIDDEN_OBJECT_KEYS.has(k));
|
|
1785
|
+
const keysB = Object.keys(b).filter((k) => !FORBIDDEN_OBJECT_KEYS.has(k));
|
|
1786
|
+
if (keysA.length !== keysB.length) return false;
|
|
1787
|
+
for (const k of keysA) {
|
|
1788
|
+
if (!Object.prototype.hasOwnProperty.call(b, k)) return false;
|
|
1789
|
+
if (!deepEqualValues(a[k], b[k])) return false;
|
|
1775
1790
|
}
|
|
1776
|
-
|
|
1791
|
+
return true;
|
|
1777
1792
|
}
|
|
1778
|
-
return
|
|
1793
|
+
return false;
|
|
1779
1794
|
}
|
|
1780
|
-
function
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
}
|
|
1791
|
-
const leafKey = pathSegments[pathSegments.length - 1];
|
|
1792
|
-
if (!isSafePathSegment(leafKey)) return;
|
|
1793
|
-
cursor[leafKey] = value;
|
|
1795
|
+
function isSafePropKey(segment) {
|
|
1796
|
+
return Boolean(segment) && !FORBIDDEN_OBJECT_KEYS.has(segment) && !segment.includes(".");
|
|
1797
|
+
}
|
|
1798
|
+
function resolveTargetObjectField(schema, exportMode) {
|
|
1799
|
+
if (exportMode === "all") return void 0;
|
|
1800
|
+
const keys = Object.keys(schema.fields || {});
|
|
1801
|
+
const exactMatch = keys.find((k) => k === exportMode);
|
|
1802
|
+
if (exactMatch) return exactMatch;
|
|
1803
|
+
const normalizedTarget = exportMode.toLowerCase().replace(/[_-]/g, "");
|
|
1804
|
+
return keys.find((k) => k.toLowerCase().replace(/[_-]/g, "") === normalizedTarget);
|
|
1794
1805
|
}
|
|
1795
1806
|
function classifyLeafType(val) {
|
|
1796
1807
|
if (val === null) return "null";
|
|
@@ -1812,146 +1823,67 @@ function needsTypeAnnotation(val) {
|
|
|
1812
1823
|
}
|
|
1813
1824
|
return false;
|
|
1814
1825
|
}
|
|
1815
|
-
function
|
|
1816
|
-
const leafPaths = [];
|
|
1817
|
-
const seenJoined = /* @__PURE__ */ new Set();
|
|
1818
|
-
let hasAnyNonNullValue = false;
|
|
1819
|
-
for (const val of values) {
|
|
1820
|
-
if (val === null || val === void 0) continue;
|
|
1821
|
-
hasAnyNonNullValue = true;
|
|
1822
|
-
if (!isPlainObject(val)) {
|
|
1823
|
-
return { canFlatten: false, paths: [] };
|
|
1824
|
-
}
|
|
1825
|
-
}
|
|
1826
|
-
if (!hasAnyNonNullValue) {
|
|
1827
|
-
return { canFlatten: false, paths: [] };
|
|
1828
|
-
}
|
|
1829
|
-
const walk = (node, relSegments, seenObjects) => {
|
|
1830
|
-
if (relSegments.length > 25 || seenObjects.has(node)) return;
|
|
1831
|
-
seenObjects.add(node);
|
|
1832
|
-
for (const [key, child] of Object.entries(node)) {
|
|
1833
|
-
if (!isSafePathSegment(key) || key.includes(".")) {
|
|
1834
|
-
continue;
|
|
1835
|
-
}
|
|
1836
|
-
const nextRel = [...relSegments, key];
|
|
1837
|
-
const fullPath = [...baseSegments, ...nextRel].join(".");
|
|
1838
|
-
if (blockedRootNames.has(fullPath)) continue;
|
|
1839
|
-
if (isPlainObject(child)) {
|
|
1840
|
-
const childKeys = Object.keys(child).filter((ck) => isSafePathSegment(ck) && !ck.includes("."));
|
|
1841
|
-
if (childKeys.length === 0) {
|
|
1842
|
-
if (!seenJoined.has(fullPath)) {
|
|
1843
|
-
seenJoined.add(fullPath);
|
|
1844
|
-
leafPaths.push(nextRel);
|
|
1845
|
-
}
|
|
1846
|
-
} else {
|
|
1847
|
-
if (seenJoined.has(fullPath)) {
|
|
1848
|
-
seenJoined.delete(fullPath);
|
|
1849
|
-
const idx = leafPaths.findIndex((p) => [...baseSegments, ...p].join(".") === fullPath);
|
|
1850
|
-
if (idx >= 0) leafPaths.splice(idx, 1);
|
|
1851
|
-
}
|
|
1852
|
-
walk(child, nextRel, seenObjects);
|
|
1853
|
-
}
|
|
1854
|
-
} else {
|
|
1855
|
-
if (!seenJoined.has(fullPath)) {
|
|
1856
|
-
seenJoined.add(fullPath);
|
|
1857
|
-
leafPaths.push(nextRel);
|
|
1858
|
-
}
|
|
1859
|
-
}
|
|
1860
|
-
}
|
|
1861
|
-
seenObjects.delete(node);
|
|
1862
|
-
};
|
|
1863
|
-
for (const val of values) {
|
|
1864
|
-
if (isPlainObject(val)) {
|
|
1865
|
-
walk(val, [], /* @__PURE__ */ new WeakSet());
|
|
1866
|
-
}
|
|
1867
|
-
}
|
|
1868
|
-
return { canFlatten: leafPaths.length > 0, paths: leafPaths };
|
|
1869
|
-
}
|
|
1870
|
-
function collectComponentExportColumns(strapi, compUid, baseSegments, documents, blockedRootNames, visitedComps) {
|
|
1871
|
-
const comp = strapi.components?.[compUid];
|
|
1872
|
-
if (!comp?.attributes || visitedComps.has(compUid)) {
|
|
1873
|
-
return [];
|
|
1874
|
-
}
|
|
1875
|
-
const nextVisited = new Set(visitedComps);
|
|
1876
|
-
nextVisited.add(compUid);
|
|
1877
|
-
const rootKey = baseSegments[0];
|
|
1826
|
+
function collectFirstLevelObjectColumns(strapi, rootKey, field, documents, blockedNames) {
|
|
1878
1827
|
const result = [];
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
const
|
|
1882
|
-
if (
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
attrDef.component,
|
|
1891
|
-
nextSegments,
|
|
1892
|
-
documents,
|
|
1893
|
-
blockedRootNames,
|
|
1894
|
-
nextVisited
|
|
1895
|
-
);
|
|
1896
|
-
if (nested.length > 0) {
|
|
1897
|
-
result.push(...nested);
|
|
1898
|
-
} else {
|
|
1828
|
+
const seenProps = /* @__PURE__ */ new Set();
|
|
1829
|
+
if (field.category === "component_single" && field.componentUid) {
|
|
1830
|
+
const comp = strapi.components?.[field.componentUid];
|
|
1831
|
+
if (comp?.attributes) {
|
|
1832
|
+
for (const [subKey, subDef] of Object.entries(comp.attributes)) {
|
|
1833
|
+
if (!isSafePropKey(subKey)) continue;
|
|
1834
|
+
const norm = subKey.toLowerCase().replace(/[\s_-]/g, "");
|
|
1835
|
+
if (SYSTEM_ATTRIBUTES.has(norm) || subKey === "id" || subKey === "__component") continue;
|
|
1836
|
+
const header = `${rootKey}.${subKey}`;
|
|
1837
|
+
if (blockedNames.has(header) || seenProps.has(subKey)) continue;
|
|
1838
|
+
seenProps.add(subKey);
|
|
1899
1839
|
result.push({
|
|
1900
1840
|
header,
|
|
1901
|
-
kind: "
|
|
1841
|
+
kind: "flattened_first_level",
|
|
1902
1842
|
rootKey,
|
|
1903
|
-
|
|
1904
|
-
|
|
1843
|
+
propKey: subKey,
|
|
1844
|
+
field,
|
|
1845
|
+
componentSubDef: subDef
|
|
1905
1846
|
});
|
|
1906
1847
|
}
|
|
1907
|
-
}
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
kind: "flattened_component_leaf",
|
|
1916
|
-
rootKey,
|
|
1917
|
-
pathSegments: fullSegments,
|
|
1918
|
-
componentSubDef: { type: "json_leaf" }
|
|
1919
|
-
});
|
|
1920
|
-
}
|
|
1921
|
-
} else {
|
|
1922
|
-
result.push({
|
|
1923
|
-
header,
|
|
1924
|
-
kind: "flattened_component_leaf",
|
|
1925
|
-
rootKey,
|
|
1926
|
-
pathSegments: nextSegments,
|
|
1927
|
-
componentSubDef: attrDef
|
|
1928
|
-
});
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
for (const doc of documents) {
|
|
1851
|
+
const obj = doc?.[rootKey];
|
|
1852
|
+
if (obj === null || obj === void 0) continue;
|
|
1853
|
+
if (!isPlainObject(obj)) {
|
|
1854
|
+
if (field.category === "json") {
|
|
1855
|
+
return [];
|
|
1929
1856
|
}
|
|
1930
|
-
|
|
1857
|
+
continue;
|
|
1858
|
+
}
|
|
1859
|
+
for (const propKey of Object.keys(obj)) {
|
|
1860
|
+
if (!isSafePropKey(propKey)) continue;
|
|
1861
|
+
const header = `${rootKey}.${propKey}`;
|
|
1862
|
+
if (blockedNames.has(header) || seenProps.has(propKey)) continue;
|
|
1863
|
+
seenProps.add(propKey);
|
|
1931
1864
|
result.push({
|
|
1932
1865
|
header,
|
|
1933
|
-
kind: "
|
|
1866
|
+
kind: "flattened_first_level",
|
|
1934
1867
|
rootKey,
|
|
1935
|
-
|
|
1936
|
-
|
|
1868
|
+
propKey,
|
|
1869
|
+
field
|
|
1937
1870
|
});
|
|
1938
1871
|
}
|
|
1939
1872
|
}
|
|
1940
1873
|
return result;
|
|
1941
1874
|
}
|
|
1942
|
-
function buildExportColumnPlan(strapi, uid, documents,
|
|
1943
|
-
const schema =
|
|
1875
|
+
function buildExportColumnPlan(strapi, uid, documents, options = {}) {
|
|
1876
|
+
const schema = options.schema || discoverSchema(strapi, uid);
|
|
1877
|
+
const exportMode = normalizeExportMode(options.exportMode);
|
|
1878
|
+
const targetObjectField = resolveTargetObjectField(schema, exportMode);
|
|
1944
1879
|
const topLevelNames = new Set(schema.allExportKeys);
|
|
1945
1880
|
const columns = [];
|
|
1946
|
-
const flattenedJsonRoots = /* @__PURE__ */ new Set();
|
|
1947
|
-
const flattenedComponentRoots = /* @__PURE__ */ new Set();
|
|
1948
1881
|
for (const key of schema.allExportKeys) {
|
|
1949
1882
|
if (key === "documentId" || key === "locale") {
|
|
1950
1883
|
columns.push({
|
|
1951
1884
|
header: key,
|
|
1952
1885
|
kind: "system",
|
|
1953
|
-
rootKey: key
|
|
1954
|
-
pathSegments: [key]
|
|
1886
|
+
rootKey: key
|
|
1955
1887
|
});
|
|
1956
1888
|
continue;
|
|
1957
1889
|
}
|
|
@@ -1960,121 +1892,99 @@ function buildExportColumnPlan(strapi, uid, documents, schemaOverride) {
|
|
|
1960
1892
|
columns.push({
|
|
1961
1893
|
header: key,
|
|
1962
1894
|
kind: "field",
|
|
1963
|
-
rootKey: key
|
|
1964
|
-
pathSegments: [key]
|
|
1895
|
+
rootKey: key
|
|
1965
1896
|
});
|
|
1966
1897
|
continue;
|
|
1967
1898
|
}
|
|
1968
|
-
if (
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
field
|
|
1981
|
-
});
|
|
1899
|
+
if (exportMode !== "all" && targetObjectField && key === targetObjectField) {
|
|
1900
|
+
if (field.category === "json" || field.category === "component_single") {
|
|
1901
|
+
const firstLevelCols = collectFirstLevelObjectColumns(
|
|
1902
|
+
strapi,
|
|
1903
|
+
key,
|
|
1904
|
+
field,
|
|
1905
|
+
documents,
|
|
1906
|
+
topLevelNames
|
|
1907
|
+
);
|
|
1908
|
+
if (firstLevelCols.length > 0) {
|
|
1909
|
+
columns.push(...firstLevelCols);
|
|
1910
|
+
continue;
|
|
1982
1911
|
}
|
|
1983
|
-
continue;
|
|
1984
|
-
}
|
|
1985
|
-
}
|
|
1986
|
-
if (field.category === "component_single" && field.componentUid) {
|
|
1987
|
-
const compColumns = collectComponentExportColumns(
|
|
1988
|
-
strapi,
|
|
1989
|
-
field.componentUid,
|
|
1990
|
-
[key],
|
|
1991
|
-
documents,
|
|
1992
|
-
topLevelNames,
|
|
1993
|
-
/* @__PURE__ */ new Set()
|
|
1994
|
-
);
|
|
1995
|
-
if (compColumns.length > 0) {
|
|
1996
|
-
flattenedComponentRoots.add(key);
|
|
1997
|
-
columns.push(...compColumns);
|
|
1998
|
-
continue;
|
|
1999
1912
|
}
|
|
2000
1913
|
}
|
|
2001
1914
|
columns.push({
|
|
2002
1915
|
header: key,
|
|
2003
1916
|
kind: "field",
|
|
2004
1917
|
rootKey: key,
|
|
2005
|
-
pathSegments: [key],
|
|
2006
1918
|
field
|
|
2007
1919
|
});
|
|
2008
1920
|
}
|
|
2009
1921
|
return {
|
|
1922
|
+
exportMode,
|
|
1923
|
+
targetObjectField,
|
|
2010
1924
|
columns,
|
|
2011
|
-
headers: columns.map((c) => c.header)
|
|
2012
|
-
flattenedJsonRoots,
|
|
2013
|
-
flattenedComponentRoots
|
|
1925
|
+
headers: columns.map((c) => c.header)
|
|
2014
1926
|
};
|
|
2015
1927
|
}
|
|
2016
1928
|
function extractRowFlattenedMetadata(plan, doc) {
|
|
2017
|
-
const
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
]);
|
|
2023
|
-
for (const rootKey of flattenedRoots) {
|
|
2024
|
-
const val = doc?.[rootKey];
|
|
2025
|
-
if (val !== null && val !== void 0) {
|
|
2026
|
-
applicableFields.push(rootKey);
|
|
2027
|
-
if (plan.flattenedJsonRoots.has(rootKey) && isPlainObject(val) && Object.keys(val).length === 0) {
|
|
2028
|
-
flatTypes[rootKey] = "object";
|
|
2029
|
-
}
|
|
2030
|
-
}
|
|
1929
|
+
const out = {
|
|
1930
|
+
exportMode: plan.exportMode
|
|
1931
|
+
};
|
|
1932
|
+
if (plan.exportMode === "all" || !plan.targetObjectField) {
|
|
1933
|
+
return out;
|
|
2031
1934
|
}
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
1935
|
+
const rootKey = plan.targetObjectField;
|
|
1936
|
+
out.targetObjectField = rootKey;
|
|
1937
|
+
const hasOwn = doc && typeof doc === "object" && Object.prototype.hasOwnProperty.call(doc, rootKey);
|
|
1938
|
+
const rawVal = hasOwn ? doc[rootKey] : void 0;
|
|
1939
|
+
if (!hasOwn || rawVal === void 0) {
|
|
1940
|
+
out.targetObjectState = "absent";
|
|
1941
|
+
out.applicableFields = [];
|
|
1942
|
+
return out;
|
|
2039
1943
|
}
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
out.applicableFields =
|
|
1944
|
+
if (rawVal === null) {
|
|
1945
|
+
out.targetObjectState = "null";
|
|
1946
|
+
out.applicableFields = [];
|
|
1947
|
+
return out;
|
|
2043
1948
|
}
|
|
2044
|
-
if (
|
|
2045
|
-
|
|
1949
|
+
if (isPlainObject(rawVal)) {
|
|
1950
|
+
const keys = Object.keys(rawVal);
|
|
1951
|
+
out.targetObjectState = keys.length === 0 ? "empty_object" : "present";
|
|
1952
|
+
out.applicableFields = [rootKey];
|
|
1953
|
+
const flatTypes = {};
|
|
1954
|
+
for (const col of plan.columns) {
|
|
1955
|
+
if (col.kind !== "flattened_first_level" || !col.propKey) continue;
|
|
1956
|
+
if (!Object.prototype.hasOwnProperty.call(rawVal, col.propKey)) continue;
|
|
1957
|
+
const leafVal = rawVal[col.propKey];
|
|
1958
|
+
if (leafVal !== void 0 && needsTypeAnnotation(leafVal)) {
|
|
1959
|
+
flatTypes[col.header] = classifyLeafType(leafVal);
|
|
1960
|
+
}
|
|
1961
|
+
}
|
|
1962
|
+
if (Object.keys(flatTypes).length > 0) {
|
|
1963
|
+
out.flatTypes = flatTypes;
|
|
1964
|
+
}
|
|
1965
|
+
return out;
|
|
2046
1966
|
}
|
|
1967
|
+
out.targetObjectState = "present";
|
|
1968
|
+
out.applicableFields = [rootKey];
|
|
2047
1969
|
return out;
|
|
2048
1970
|
}
|
|
2049
|
-
function serializeFlattenedLeafValue(val) {
|
|
2050
|
-
if (val === void 0 || val === null) return "";
|
|
2051
|
-
if (val instanceof Date) return val.toISOString();
|
|
2052
|
-
if (Array.isArray(val) || isPlainObject(val)) {
|
|
2053
|
-
return JSON.stringify(val);
|
|
2054
|
-
}
|
|
2055
|
-
if (typeof val === "boolean") return val ? "true" : "false";
|
|
2056
|
-
if (typeof val === "number") return Number.isFinite(val) ? String(val) : "";
|
|
2057
|
-
return String(val);
|
|
2058
|
-
}
|
|
2059
1971
|
function serializePlannedCell(col, doc, serializeFieldFallback) {
|
|
2060
1972
|
if (col.kind === "system") {
|
|
2061
1973
|
if (col.header === "documentId") return doc?.documentId || doc?.id || "";
|
|
2062
1974
|
if (col.header === "locale") return doc?.locale || "";
|
|
2063
1975
|
return doc?.[col.header] ?? "";
|
|
2064
1976
|
}
|
|
2065
|
-
if (col.kind === "
|
|
2066
|
-
const
|
|
2067
|
-
if (!
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
const { exists, value } = getValueAtPath(doc, col.pathSegments);
|
|
2073
|
-
if (!exists || value === void 0 || value === null) return "";
|
|
1977
|
+
if (col.kind === "flattened_first_level" && col.propKey) {
|
|
1978
|
+
const rootObj = doc?.[col.rootKey];
|
|
1979
|
+
if (!isPlainObject(rootObj) || !Object.prototype.hasOwnProperty.call(rootObj, col.propKey)) {
|
|
1980
|
+
return "";
|
|
1981
|
+
}
|
|
1982
|
+
const val = rootObj[col.propKey];
|
|
1983
|
+
if (val === void 0 || val === null) return "";
|
|
2074
1984
|
const subDef = col.componentSubDef || {};
|
|
2075
1985
|
const subType = String(subDef.type || "").toLowerCase();
|
|
2076
1986
|
if (subType === "media") {
|
|
2077
|
-
return serializeFieldFallback(
|
|
1987
|
+
return serializeFieldFallback(val, {
|
|
2078
1988
|
key: col.header,
|
|
2079
1989
|
label: col.header,
|
|
2080
1990
|
category: subDef.multiple ? "media_to_many" : "media_single",
|
|
@@ -2084,7 +1994,7 @@ function serializePlannedCell(col, doc, serializeFieldFallback) {
|
|
|
2084
1994
|
}
|
|
2085
1995
|
if (subType === "relation") {
|
|
2086
1996
|
const isToMany = String(subDef.relation || "").toLowerCase().endsWith("many");
|
|
2087
|
-
return serializeFieldFallback(
|
|
1997
|
+
return serializeFieldFallback(val, {
|
|
2088
1998
|
key: col.header,
|
|
2089
1999
|
label: col.header,
|
|
2090
2000
|
category: isToMany ? "relation_to_many" : "relation_to_one",
|
|
@@ -2092,13 +2002,13 @@ function serializePlannedCell(col, doc, serializeFieldFallback) {
|
|
|
2092
2002
|
relationType: subDef.relation
|
|
2093
2003
|
});
|
|
2094
2004
|
}
|
|
2095
|
-
if (
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
if (subType === "blocks" || subType === "json") {
|
|
2099
|
-
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
2005
|
+
if (val instanceof Date) return val.toISOString();
|
|
2006
|
+
if (Array.isArray(val) || isPlainObject(val)) {
|
|
2007
|
+
return JSON.stringify(val);
|
|
2100
2008
|
}
|
|
2101
|
-
|
|
2009
|
+
if (typeof val === "boolean") return val ? "true" : "false";
|
|
2010
|
+
if (typeof val === "number") return Number.isFinite(val) ? String(val) : "";
|
|
2011
|
+
return String(val);
|
|
2102
2012
|
}
|
|
2103
2013
|
const rawValue = doc?.[col.rootKey];
|
|
2104
2014
|
if (col.field) {
|
|
@@ -2106,197 +2016,272 @@ function serializePlannedCell(col, doc, serializeFieldFallback) {
|
|
|
2106
2016
|
}
|
|
2107
2017
|
return rawValue;
|
|
2108
2018
|
}
|
|
2109
|
-
function
|
|
2110
|
-
if (
|
|
2111
|
-
|
|
2112
|
-
const trimmed = rawCell.trim();
|
|
2113
|
-
if (EXPLICIT_CLEAR_TOKENS.has(trimmed)) {
|
|
2114
|
-
return null;
|
|
2019
|
+
function inspectRowMetadataMode(rawRow) {
|
|
2020
|
+
if (!rawRow || typeof rawRow !== "object") {
|
|
2021
|
+
return { mode: "legacy" };
|
|
2115
2022
|
}
|
|
2116
|
-
|
|
2117
|
-
|
|
2023
|
+
const rawMetaCell = rawRow["__bulk_updater_metadata"] !== void 0 ? rawRow["__bulk_updater_metadata"] : rawRow["_bulk_updater_metadata"];
|
|
2024
|
+
if (rawMetaCell === void 0 || rawMetaCell === null || String(rawMetaCell).trim() === "") {
|
|
2025
|
+
return { mode: "legacy" };
|
|
2118
2026
|
}
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2027
|
+
let parsed = rawMetaCell;
|
|
2028
|
+
if (typeof rawMetaCell === "string") {
|
|
2029
|
+
try {
|
|
2030
|
+
parsed = JSON.parse(rawMetaCell);
|
|
2031
|
+
} catch {
|
|
2032
|
+
return {
|
|
2033
|
+
mode: "legacy",
|
|
2034
|
+
error: "Corrupted __bulk_updater_metadata cell: invalid JSON."
|
|
2035
|
+
};
|
|
2123
2036
|
}
|
|
2124
2037
|
}
|
|
2125
|
-
if (
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2038
|
+
if (!isPlainObject(parsed)) {
|
|
2039
|
+
return {
|
|
2040
|
+
mode: "legacy",
|
|
2041
|
+
error: "Corrupted __bulk_updater_metadata cell: expected JSON object."
|
|
2042
|
+
};
|
|
2129
2043
|
}
|
|
2130
|
-
if (
|
|
2131
|
-
|
|
2044
|
+
if (parsed.exportMode === void 0) {
|
|
2045
|
+
return {
|
|
2046
|
+
mode: "legacy",
|
|
2047
|
+
applicableFields: Array.isArray(parsed.applicableFields) ? parsed.applicableFields : void 0,
|
|
2048
|
+
flatTypes: isPlainObject(parsed.flatTypes) ? parsed.flatTypes : void 0
|
|
2049
|
+
};
|
|
2050
|
+
}
|
|
2051
|
+
const rawMode = String(parsed.exportMode).trim().toLowerCase();
|
|
2052
|
+
if (rawMode !== "all" && rawMode !== "metafields" && rawMode !== "topbarconfig") {
|
|
2053
|
+
return {
|
|
2054
|
+
mode: "legacy",
|
|
2055
|
+
error: `Unsupported CSV exportMode "${parsed.exportMode}" in metadata.`
|
|
2056
|
+
};
|
|
2057
|
+
}
|
|
2058
|
+
return {
|
|
2059
|
+
mode: rawMode,
|
|
2060
|
+
targetObjectField: typeof parsed.targetObjectField === "string" && parsed.targetObjectField.trim() ? parsed.targetObjectField.trim() : void 0,
|
|
2061
|
+
targetObjectState: parsed.targetObjectState,
|
|
2062
|
+
applicableFields: Array.isArray(parsed.applicableFields) ? parsed.applicableFields : void 0,
|
|
2063
|
+
flatTypes: isPlainObject(parsed.flatTypes) ? parsed.flatTypes : void 0
|
|
2064
|
+
};
|
|
2065
|
+
}
|
|
2066
|
+
function detectCsvBatchExportMode(rows) {
|
|
2067
|
+
const seenModes = /* @__PURE__ */ new Set();
|
|
2068
|
+
for (let i = 0; i < rows.length; i++) {
|
|
2069
|
+
const info = inspectRowMetadataMode(rows[i]);
|
|
2070
|
+
if (info.error) {
|
|
2071
|
+
return { mode: "legacy", error: `Row ${i + 2}: ${info.error}` };
|
|
2072
|
+
}
|
|
2073
|
+
seenModes.add(info.mode);
|
|
2074
|
+
}
|
|
2075
|
+
if (seenModes.size > 1) {
|
|
2076
|
+
return {
|
|
2077
|
+
mode: "legacy",
|
|
2078
|
+
error: `Inconsistent CSV export modes detected across rows (${Array.from(seenModes).join(", ")}). Re-export the file cleanly before importing.`
|
|
2079
|
+
};
|
|
2080
|
+
}
|
|
2081
|
+
const first = Array.from(seenModes)[0];
|
|
2082
|
+
return { mode: first || "legacy" };
|
|
2083
|
+
}
|
|
2084
|
+
function parseFirstLevelCellValue(rawCell, annotatedType, existingPropValue) {
|
|
2085
|
+
if (rawCell === null || rawCell === void 0) return { value: null };
|
|
2086
|
+
if (typeof rawCell !== "string") return { value: rawCell };
|
|
2087
|
+
const trimmed = rawCell.trim();
|
|
2088
|
+
if (EXPLICIT_CLEAR_TOKENS.has(trimmed)) {
|
|
2089
|
+
return { value: null };
|
|
2090
|
+
}
|
|
2091
|
+
if (trimmed === EXPLICIT_EMPTY_STRING_TOKEN) {
|
|
2092
|
+
return { value: "" };
|
|
2093
|
+
}
|
|
2094
|
+
if (annotatedType === "string") {
|
|
2095
|
+
return { value: rawCell };
|
|
2096
|
+
}
|
|
2097
|
+
if (annotatedType === "object" || isPlainObject(existingPropValue)) {
|
|
2098
|
+
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
|
|
2132
2099
|
try {
|
|
2133
|
-
return JSON.parse(trimmed);
|
|
2100
|
+
return { value: JSON.parse(trimmed) };
|
|
2134
2101
|
} catch {
|
|
2102
|
+
return {
|
|
2103
|
+
value: rawCell,
|
|
2104
|
+
error: `Invalid JSON object value: "${rawCell}"`
|
|
2105
|
+
};
|
|
2135
2106
|
}
|
|
2136
2107
|
}
|
|
2137
2108
|
}
|
|
2138
|
-
if (annotatedType === "
|
|
2139
|
-
if (trimmed.startsWith("
|
|
2109
|
+
if (annotatedType === "array" || Array.isArray(existingPropValue)) {
|
|
2110
|
+
if (trimmed.startsWith("[")) {
|
|
2140
2111
|
try {
|
|
2141
|
-
|
|
2112
|
+
const parsed = JSON.parse(trimmed);
|
|
2113
|
+
return { value: parsed };
|
|
2142
2114
|
} catch {
|
|
2115
|
+
return {
|
|
2116
|
+
value: rawCell,
|
|
2117
|
+
error: `Invalid JSON array value: "${rawCell}"`
|
|
2118
|
+
};
|
|
2143
2119
|
}
|
|
2144
2120
|
}
|
|
2145
2121
|
}
|
|
2146
|
-
if (typeof
|
|
2122
|
+
if (annotatedType === "number" || typeof existingPropValue === "number") {
|
|
2123
|
+
if (/^-?(?:0|[1-9]\d*)(?:\.\d+)?$/.test(trimmed)) {
|
|
2124
|
+
const num = Number(trimmed);
|
|
2125
|
+
if (Number.isFinite(num)) return { value: num };
|
|
2126
|
+
}
|
|
2127
|
+
}
|
|
2128
|
+
if (annotatedType === "boolean" || typeof existingPropValue === "boolean") {
|
|
2129
|
+
const low = trimmed.toLowerCase();
|
|
2130
|
+
if (low === "true") return { value: true };
|
|
2131
|
+
if (low === "false") return { value: false };
|
|
2132
|
+
}
|
|
2133
|
+
if (typeof existingPropValue === "string") {
|
|
2147
2134
|
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
|
|
2148
2135
|
try {
|
|
2149
|
-
return JSON.parse(trimmed);
|
|
2136
|
+
return { value: JSON.parse(trimmed) };
|
|
2150
2137
|
} catch {
|
|
2151
2138
|
}
|
|
2152
2139
|
}
|
|
2153
|
-
return rawCell;
|
|
2140
|
+
return { value: rawCell };
|
|
2154
2141
|
}
|
|
2155
|
-
if (trimmed === "null") return null;
|
|
2156
|
-
if (trimmed === "true") return true;
|
|
2157
|
-
if (trimmed === "false") return false;
|
|
2142
|
+
if (trimmed === "null") return { value: null };
|
|
2143
|
+
if (trimmed === "true") return { value: true };
|
|
2144
|
+
if (trimmed === "false") return { value: false };
|
|
2158
2145
|
if (/^-?(?:0|[1-9]\d*)(?:\.\d+)?$/.test(trimmed)) {
|
|
2159
2146
|
const num = Number(trimmed);
|
|
2160
|
-
if (Number.isFinite(num)) return num;
|
|
2147
|
+
if (Number.isFinite(num)) return { value: num };
|
|
2161
2148
|
}
|
|
2162
2149
|
if (trimmed.startsWith("[") && trimmed.endsWith("]") || trimmed.startsWith("{") && trimmed.endsWith("}")) {
|
|
2163
2150
|
try {
|
|
2164
|
-
return JSON.parse(trimmed);
|
|
2151
|
+
return { value: JSON.parse(trimmed) };
|
|
2165
2152
|
} catch {
|
|
2166
2153
|
}
|
|
2167
2154
|
}
|
|
2168
|
-
return rawCell;
|
|
2155
|
+
return { value: rawCell };
|
|
2169
2156
|
}
|
|
2170
|
-
function
|
|
2157
|
+
function reconstructTargetedFirstLevelObject(params) {
|
|
2171
2158
|
const {
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2159
|
+
targetAttr,
|
|
2160
|
+
exportMode,
|
|
2161
|
+
existingDoc,
|
|
2162
|
+
docIdForError,
|
|
2163
|
+
rawRow,
|
|
2177
2164
|
rowMeta,
|
|
2178
|
-
|
|
2165
|
+
isAttrRequired = false
|
|
2179
2166
|
} = params;
|
|
2180
|
-
const
|
|
2181
|
-
const
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
if (
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
if (
|
|
2188
|
-
|
|
2167
|
+
const prefix = `${targetAttr}.`;
|
|
2168
|
+
const firstLevelEntries = [];
|
|
2169
|
+
const invalidNestedColumns = [];
|
|
2170
|
+
for (const [colKey, val] of Object.entries(rawRow)) {
|
|
2171
|
+
if (!colKey.startsWith(prefix)) continue;
|
|
2172
|
+
const propKey = colKey.slice(prefix.length);
|
|
2173
|
+
if (!propKey) continue;
|
|
2174
|
+
if (propKey.includes(".")) {
|
|
2175
|
+
if (val !== void 0 && val !== null && String(val) !== "") {
|
|
2176
|
+
invalidNestedColumns.push(colKey);
|
|
2177
|
+
}
|
|
2178
|
+
continue;
|
|
2189
2179
|
}
|
|
2190
|
-
if (
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2180
|
+
if (!isSafePropKey(propKey)) continue;
|
|
2181
|
+
firstLevelEntries.push([colKey, propKey, val]);
|
|
2182
|
+
}
|
|
2183
|
+
if (invalidNestedColumns.length > 0) {
|
|
2184
|
+
return {
|
|
2185
|
+
shouldSet: false,
|
|
2186
|
+
value: void 0,
|
|
2187
|
+
errors: [
|
|
2188
|
+
`Deeply nested column(s) [${invalidNestedColumns.join(", ")}] are not valid in "${exportMode}" one-level targeted mode.`
|
|
2189
|
+
]
|
|
2190
|
+
};
|
|
2191
|
+
}
|
|
2192
|
+
const directRawVal = targetAttr in rawRow ? rawRow[targetAttr] : void 0;
|
|
2193
|
+
const directTrimmed = typeof directRawVal === "string" ? directRawVal.trim() : "";
|
|
2194
|
+
const hasNonEmptyFirstLevelEdit = firstLevelEntries.some(
|
|
2195
|
+
([, , val]) => val !== void 0 && val !== null && String(val) !== ""
|
|
2196
|
+
);
|
|
2197
|
+
if (existingDoc) {
|
|
2198
|
+
const existingObj = existingDoc[targetAttr];
|
|
2199
|
+
const hasObjectOnRecord = isPlainObject(existingObj);
|
|
2200
|
+
if (!hasObjectOnRecord) {
|
|
2201
|
+
const attemptedDirectEdit = directRawVal !== void 0 && directRawVal !== null && directTrimmed !== "";
|
|
2202
|
+
if (!hasNonEmptyFirstLevelEdit && !attemptedDirectEdit) {
|
|
2203
|
+
return { shouldSet: false, value: void 0, errors: [] };
|
|
2204
|
+
}
|
|
2205
|
+
if (!isAttrRequired) {
|
|
2206
|
+
const recordRef = docIdForError || existingDoc.documentId || existingDoc.id || "record";
|
|
2207
|
+
return {
|
|
2208
|
+
shouldSet: false,
|
|
2209
|
+
value: void 0,
|
|
2210
|
+
errors: [
|
|
2211
|
+
`Field "${targetAttr}" is not applicable or absent on document "${recordRef}". Targeted "${exportMode}" import will not create a new "${targetAttr}" object automatically.`
|
|
2212
|
+
]
|
|
2213
|
+
};
|
|
2195
2214
|
}
|
|
2196
2215
|
}
|
|
2197
|
-
|
|
2216
|
+
} else {
|
|
2217
|
+
if (!hasNonEmptyFirstLevelEdit && (!directTrimmed || directTrimmed === "")) {
|
|
2218
|
+
return { shouldSet: false, value: void 0, errors: [] };
|
|
2219
|
+
}
|
|
2198
2220
|
}
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2221
|
+
const base = isPlainObject(existingDoc?.[targetAttr]) ? deepCloneValue(existingDoc[targetAttr]) : {};
|
|
2222
|
+
const errors = [];
|
|
2223
|
+
let appliedAnyModification = false;
|
|
2224
|
+
if (firstLevelEntries.length === 0 && directRawVal !== void 0 && directRawVal !== null && directTrimmed !== "") {
|
|
2202
2225
|
if (EXPLICIT_CLEAR_TOKENS.has(directTrimmed) || directTrimmed === "null") {
|
|
2203
|
-
return { shouldSet: true, value: null };
|
|
2226
|
+
return { shouldSet: true, value: null, errors: [] };
|
|
2204
2227
|
}
|
|
2205
2228
|
try {
|
|
2206
2229
|
const parsedDirect = typeof directRawVal === "string" ? JSON.parse(directRawVal) : directRawVal;
|
|
2207
2230
|
if (isPlainObject(parsedDirect)) {
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2231
|
+
return {
|
|
2232
|
+
shouldSet: true,
|
|
2233
|
+
value: { ...base, ...deepCloneValue(parsedDirect) },
|
|
2234
|
+
errors: []
|
|
2235
|
+
};
|
|
2212
2236
|
}
|
|
2237
|
+
return { shouldSet: true, value: parsedDirect, errors: [] };
|
|
2213
2238
|
} catch {
|
|
2214
|
-
return {
|
|
2239
|
+
return {
|
|
2240
|
+
shouldSet: false,
|
|
2241
|
+
value: void 0,
|
|
2242
|
+
errors: [`Column "${targetAttr}" contains invalid JSON: "${directRawVal}"`]
|
|
2243
|
+
};
|
|
2215
2244
|
}
|
|
2216
2245
|
}
|
|
2217
|
-
for (const [
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
const relPathStr = fullKey.slice(prefix.length);
|
|
2221
|
-
const relSegments = relPathStr.split(".").filter(Boolean);
|
|
2222
|
-
if (relSegments.length === 0 || relSegments.some((s) => !isSafePathSegment(s))) {
|
|
2223
|
-
continue;
|
|
2224
|
-
}
|
|
2225
|
-
const strCell = rawCell === null || rawCell === void 0 ? "" : String(rawCell);
|
|
2246
|
+
for (const [colKey, propKey, rawCell] of firstLevelEntries) {
|
|
2247
|
+
if (rawCell === void 0 || rawCell === null) continue;
|
|
2248
|
+
const strCell = String(rawCell);
|
|
2226
2249
|
const trimmed = strCell.trim();
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
hasEffectiveChange = true;
|
|
2232
|
-
continue;
|
|
2233
|
-
}
|
|
2234
|
-
if (strCell === "") {
|
|
2235
|
-
if (hasExistingPlainObj && annotatedType && (annotatedType === "string" || annotatedType === "null" || annotatedType === "object")) {
|
|
2236
|
-
const existingAtPath = getValueAtPath(existingVal, relSegments);
|
|
2237
|
-
if (existingAtPath.exists) {
|
|
2238
|
-
if (annotatedType === "object" && isPlainObject(existingAtPath.value)) {
|
|
2239
|
-
if (!isPartialUpdate) hasEffectiveChange = true;
|
|
2240
|
-
} else if (annotatedType === "null" && existingAtPath.value === null) {
|
|
2241
|
-
if (!isPartialUpdate) hasEffectiveChange = true;
|
|
2242
|
-
} else if (annotatedType === "string" && existingAtPath.value === "") {
|
|
2243
|
-
if (!isPartialUpdate) hasEffectiveChange = true;
|
|
2244
|
-
}
|
|
2245
|
-
}
|
|
2250
|
+
if (trimmed === EXPLICIT_DELETE_KEY_TOKEN) {
|
|
2251
|
+
if (Object.prototype.hasOwnProperty.call(base, propKey)) {
|
|
2252
|
+
delete base[propKey];
|
|
2253
|
+
appliedAnyModification = true;
|
|
2246
2254
|
}
|
|
2247
2255
|
continue;
|
|
2248
2256
|
}
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
setValueAtPath(baseObj, relSegments, parsedVal);
|
|
2253
|
-
hasEffectiveChange = true;
|
|
2254
|
-
}
|
|
2255
|
-
if (!hasEffectiveChange) {
|
|
2256
|
-
if (!isPartialUpdate && hasExistingPlainObj && (rowMeta?.applicableFields?.includes(attrName) || rowMeta?.flatTypes?.[attrName] === "object")) {
|
|
2257
|
-
return { shouldSet: true, value: deepCloneValue(existingVal) };
|
|
2258
|
-
}
|
|
2259
|
-
return { shouldSet: false, value: void 0 };
|
|
2260
|
-
}
|
|
2261
|
-
return { shouldSet: true, value: baseObj || {} };
|
|
2262
|
-
}
|
|
2263
|
-
function buildNestedComponentPatchFromDots(attrName, dotEntries) {
|
|
2264
|
-
const patch = {};
|
|
2265
|
-
let hasEffectiveEdits = false;
|
|
2266
|
-
const prefix = `${attrName}.`;
|
|
2267
|
-
for (const [fullKey, rawCell] of dotEntries) {
|
|
2268
|
-
if (!fullKey.startsWith(prefix)) continue;
|
|
2269
|
-
const relPathStr = fullKey.slice(prefix.length);
|
|
2270
|
-
const relSegments = relPathStr.split(".").filter(Boolean);
|
|
2271
|
-
if (relSegments.length === 0 || relSegments.some((s) => !isSafePathSegment(s))) {
|
|
2257
|
+
if (EXPLICIT_CLEAR_TOKENS.has(trimmed)) {
|
|
2258
|
+
base[propKey] = null;
|
|
2259
|
+
appliedAnyModification = true;
|
|
2272
2260
|
continue;
|
|
2273
2261
|
}
|
|
2274
|
-
if (
|
|
2262
|
+
if (strCell === "") {
|
|
2275
2263
|
continue;
|
|
2276
2264
|
}
|
|
2277
|
-
const
|
|
2278
|
-
const
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2265
|
+
const annotatedType = rowMeta.flatTypes?.[colKey];
|
|
2266
|
+
const existingPropValue = isPlainObject(existingDoc?.[targetAttr]) ? existingDoc[targetAttr][propKey] : void 0;
|
|
2267
|
+
const parsed = parseFirstLevelCellValue(strCell, annotatedType, existingPropValue);
|
|
2268
|
+
if (parsed.error) {
|
|
2269
|
+
errors.push(`Column "${colKey}": ${parsed.error}`);
|
|
2282
2270
|
continue;
|
|
2283
2271
|
}
|
|
2284
|
-
|
|
2285
|
-
|
|
2272
|
+
base[propKey] = parsed.value;
|
|
2273
|
+
appliedAnyModification = true;
|
|
2286
2274
|
}
|
|
2287
|
-
|
|
2288
|
-
}
|
|
2289
|
-
function resolveDottedComponentAttrDef(strapi, rootDef, segments) {
|
|
2290
|
-
if (!rootDef || segments.length === 0) return null;
|
|
2291
|
-
if (segments.length === 1) return rootDef;
|
|
2292
|
-
if (rootDef.type === "component" && !rootDef.repeatable && rootDef.component) {
|
|
2293
|
-
const comp = strapi.components?.[rootDef.component];
|
|
2294
|
-
if (!comp?.attributes) return null;
|
|
2295
|
-
const nextAttr = comp.attributes[segments[1]];
|
|
2296
|
-
if (!nextAttr) return null;
|
|
2297
|
-
return resolveDottedComponentAttrDef(strapi, nextAttr, segments.slice(1));
|
|
2275
|
+
if (errors.length > 0) {
|
|
2276
|
+
return { shouldSet: false, value: void 0, errors };
|
|
2298
2277
|
}
|
|
2299
|
-
|
|
2278
|
+
if (!appliedAnyModification) {
|
|
2279
|
+
return { shouldSet: false, value: void 0, errors: [] };
|
|
2280
|
+
}
|
|
2281
|
+
if (existingDoc && isPlainObject(existingDoc[targetAttr]) && deepEqualValues(base, existingDoc[targetAttr])) {
|
|
2282
|
+
return { shouldSet: false, value: void 0, errors: [] };
|
|
2283
|
+
}
|
|
2284
|
+
return { shouldSet: true, value: base, errors: [] };
|
|
2300
2285
|
}
|
|
2301
2286
|
function validateRecordSchemaConstraints(strapi, uid, existingDoc, payload, rawRow, options) {
|
|
2302
2287
|
const ct = strapi.contentTypes?.[uid];
|
|
@@ -2304,51 +2289,25 @@ function validateRecordSchemaConstraints(strapi, uid, existingDoc, payload, rawR
|
|
|
2304
2289
|
const attrs = ct.attributes || {};
|
|
2305
2290
|
const errors = [];
|
|
2306
2291
|
const warnings = [];
|
|
2307
|
-
const
|
|
2292
|
+
const isTargeted = options.detectedMode === "metafields" || options.detectedMode === "topbarconfig";
|
|
2308
2293
|
const isMissingRequiredValue = (val) => val === void 0 || val === null || typeof val === "string" && val.trim() === "";
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
if (rootAttr?.type === "component" && !rootAttr.repeatable) {
|
|
2318
|
-
const subDef = resolveDottedComponentAttrDef(strapi, rootAttr, segments);
|
|
2319
|
-
if (subDef) {
|
|
2320
|
-
if (subDef.type === "integer" || subDef.type === "biginteger") {
|
|
2321
|
-
if (!/^-?\d+$/.test(trimmed)) {
|
|
2322
|
-
errors.push(`Column "${colKey}" expects an integer, received: "${rawVal}"`);
|
|
2323
|
-
}
|
|
2324
|
-
} else if (subDef.type === "float" || subDef.type === "decimal") {
|
|
2325
|
-
if (isNaN(Number(trimmed))) {
|
|
2326
|
-
errors.push(`Column "${colKey}" expects a numeric float/decimal, received: "${rawVal}"`);
|
|
2327
|
-
}
|
|
2328
|
-
} else if (subDef.type === "enumeration" && Array.isArray(subDef.enum)) {
|
|
2329
|
-
if (!subDef.enum.includes(trimmed)) {
|
|
2330
|
-
warnings.push(
|
|
2331
|
-
`Column "${colKey}" value "${rawVal}" is not in defined enum options: [${subDef.enum.join(", ")}]`
|
|
2332
|
-
);
|
|
2333
|
-
}
|
|
2334
|
-
}
|
|
2294
|
+
if (!isTargeted || options.isCreate) {
|
|
2295
|
+
for (const [attrName, attrDef] of Object.entries(attrs)) {
|
|
2296
|
+
if (!attrDef?.required) continue;
|
|
2297
|
+
const norm = attrName.toLowerCase().replace(/[\s_-]/g, "");
|
|
2298
|
+
if (SYSTEM_ATTRIBUTES.has(norm)) continue;
|
|
2299
|
+
if (options.isCreate) {
|
|
2300
|
+
if (isMissingRequiredValue(payload[attrName])) {
|
|
2301
|
+
errors.push(`Missing mandatory field "${attrName}".`);
|
|
2335
2302
|
}
|
|
2303
|
+
} else if (attrName in payload && isMissingRequiredValue(payload[attrName])) {
|
|
2304
|
+
errors.push(`Mandatory field "${attrName}" cannot be empty or cleared.`);
|
|
2336
2305
|
}
|
|
2337
2306
|
}
|
|
2338
|
-
}
|
|
2339
|
-
|
|
2340
|
-
if (
|
|
2341
|
-
|
|
2342
|
-
if (SYSTEM_ATTRIBUTES.has(norm)) continue;
|
|
2343
|
-
if (options.isCreate) {
|
|
2344
|
-
if (applicableList && !applicableList.has(attrName) && payload[attrName] === void 0) {
|
|
2345
|
-
continue;
|
|
2346
|
-
}
|
|
2347
|
-
if (isMissingRequiredValue(payload[attrName])) {
|
|
2348
|
-
errors.push(`Missing mandatory field "${attrName}".`);
|
|
2349
|
-
}
|
|
2350
|
-
} else if (attrName in payload && isMissingRequiredValue(payload[attrName])) {
|
|
2351
|
-
errors.push(`Mandatory field "${attrName}" cannot be empty or cleared.`);
|
|
2307
|
+
} else if (options.targetObjectField) {
|
|
2308
|
+
const tDef = attrs[options.targetObjectField];
|
|
2309
|
+
if (tDef?.required && options.targetObjectField in payload && isMissingRequiredValue(payload[options.targetObjectField])) {
|
|
2310
|
+
errors.push(`Mandatory field "${options.targetObjectField}" cannot be empty or cleared.`);
|
|
2352
2311
|
}
|
|
2353
2312
|
}
|
|
2354
2313
|
const validateComponentRequired = (compUid, valueObj, pathPrefix) => {
|
|
@@ -2370,754 +2329,581 @@ function validateRecordSchemaConstraints(strapi, uid, existingDoc, payload, rawR
|
|
|
2370
2329
|
const nextCompVal = payload[attrName];
|
|
2371
2330
|
if (isPlainObject(nextCompVal)) {
|
|
2372
2331
|
validateComponentRequired(attrDef.component, nextCompVal, attrName);
|
|
2373
|
-
}
|
|
2332
|
+
}
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
if (isTargeted && options.targetObjectField) {
|
|
2336
|
+
const tDef = attrs[options.targetObjectField];
|
|
2337
|
+
if (tDef?.type === "component" && !tDef.repeatable && tDef.component && strapi.components?.[tDef.component]) {
|
|
2338
|
+
const compAttrs = strapi.components[tDef.component].attributes || {};
|
|
2339
|
+
const prefix = `${options.targetObjectField}.`;
|
|
2340
|
+
for (const [colKey, rawVal] of Object.entries(rawRow)) {
|
|
2341
|
+
if (!colKey.startsWith(prefix)) continue;
|
|
2342
|
+
const propKey = colKey.slice(prefix.length);
|
|
2343
|
+
const subDef = compAttrs[propKey];
|
|
2344
|
+
const trimmed = rawVal === void 0 || rawVal === null ? "" : String(rawVal).trim();
|
|
2345
|
+
if (!subDef || !trimmed || EXPLICIT_CLEAR_TOKENS.has(trimmed) || trimmed === EXPLICIT_DELETE_KEY_TOKEN || trimmed === EXPLICIT_EMPTY_STRING_TOKEN) {
|
|
2346
|
+
continue;
|
|
2347
|
+
}
|
|
2348
|
+
if (subDef.type === "integer" || subDef.type === "biginteger") {
|
|
2349
|
+
if (!/^-?\d+$/.test(trimmed)) {
|
|
2350
|
+
errors.push(`Column "${colKey}" expects an integer, received: "${rawVal}"`);
|
|
2351
|
+
}
|
|
2352
|
+
} else if (subDef.type === "float" || subDef.type === "decimal") {
|
|
2353
|
+
if (isNaN(Number(trimmed))) {
|
|
2354
|
+
errors.push(`Column "${colKey}" expects a numeric float/decimal, received: "${rawVal}"`);
|
|
2355
|
+
}
|
|
2356
|
+
} else if (subDef.type === "enumeration" && Array.isArray(subDef.enum)) {
|
|
2357
|
+
if (!subDef.enum.includes(trimmed)) {
|
|
2358
|
+
warnings.push(`Column "${colKey}" value "${rawVal}" is not in defined enum options: [${subDef.enum.join(", ")}]`);
|
|
2359
|
+
}
|
|
2360
|
+
}
|
|
2361
|
+
}
|
|
2374
2362
|
}
|
|
2375
2363
|
}
|
|
2376
2364
|
return { errors, warnings };
|
|
2377
2365
|
}
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
if (
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
if (/^[=+@-]/.test(str)) {
|
|
2392
|
-
str = `'${str}`;
|
|
2393
|
-
}
|
|
2394
|
-
if (str.includes(",") || str.includes('"') || str.includes("\n") || str.includes("\r")) {
|
|
2395
|
-
return `"${str.replace(/"/g, '""')}"`;
|
|
2366
|
+
function isValidDocumentId(val) {
|
|
2367
|
+
if (!val || typeof val !== "string") return false;
|
|
2368
|
+
const trimmed = val.trim();
|
|
2369
|
+
if (!trimmed) return false;
|
|
2370
|
+
if (/\s/.test(trimmed)) return false;
|
|
2371
|
+
return /^[a-zA-Z0-9_-]+$/.test(trimmed);
|
|
2372
|
+
}
|
|
2373
|
+
function parseNumeric(rawVal, isInteger) {
|
|
2374
|
+
if (rawVal === null || rawVal === void 0 || rawVal === "") return null;
|
|
2375
|
+
const cleanStr = String(rawVal).replace(/[$€£,\s]/g, "");
|
|
2376
|
+
if (isInteger) {
|
|
2377
|
+
const n = parseInt(cleanStr, 10);
|
|
2378
|
+
return isNaN(n) ? null : n;
|
|
2396
2379
|
}
|
|
2397
|
-
|
|
2380
|
+
const f = parseFloat(cleanStr);
|
|
2381
|
+
return isNaN(f) ? null : f;
|
|
2398
2382
|
}
|
|
2399
|
-
function
|
|
2400
|
-
if (
|
|
2401
|
-
|
|
2402
|
-
if (
|
|
2403
|
-
|
|
2383
|
+
function parseBoolean(rawVal) {
|
|
2384
|
+
if (rawVal === null || rawVal === void 0 || rawVal === "") return null;
|
|
2385
|
+
const s = String(rawVal).trim().toLowerCase();
|
|
2386
|
+
if (s === "true" || s === "1" || s === "yes" || s === "y" || s === "on") return true;
|
|
2387
|
+
if (s === "false" || s === "0" || s === "no" || s === "n" || s === "off") return false;
|
|
2388
|
+
return null;
|
|
2389
|
+
}
|
|
2390
|
+
async function resolveMediaIds(strapi, rawVal, isMultiple) {
|
|
2391
|
+
if (rawVal === null || rawVal === void 0 || rawVal === "") {
|
|
2392
|
+
return isMultiple ? [] : null;
|
|
2404
2393
|
}
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
const nextChar = text[i + 1];
|
|
2413
|
-
if (inQuotes) {
|
|
2414
|
-
if (char === '"') {
|
|
2415
|
-
if (nextChar === '"') {
|
|
2416
|
-
currentCell += '"';
|
|
2417
|
-
i++;
|
|
2418
|
-
} else {
|
|
2419
|
-
inQuotes = false;
|
|
2420
|
-
}
|
|
2421
|
-
} else {
|
|
2422
|
-
currentCell += char;
|
|
2423
|
-
}
|
|
2424
|
-
} else {
|
|
2425
|
-
if (char === '"' && !cellIsQuoted && currentCell.trim() === "") {
|
|
2426
|
-
inQuotes = true;
|
|
2427
|
-
cellIsQuoted = true;
|
|
2428
|
-
currentCell = "";
|
|
2429
|
-
} else if (char === ",") {
|
|
2430
|
-
currentRow.push(cellIsQuoted ? currentCell : currentCell.trim());
|
|
2431
|
-
currentCell = "";
|
|
2432
|
-
cellIsQuoted = false;
|
|
2433
|
-
} else if (char === "\r") {
|
|
2434
|
-
if (nextChar === "\n") {
|
|
2435
|
-
i++;
|
|
2436
|
-
}
|
|
2437
|
-
currentRow.push(cellIsQuoted ? currentCell : currentCell.trim());
|
|
2438
|
-
if (currentRow.length > 1 || currentRow.length === 1 && currentRow[0] !== "") {
|
|
2439
|
-
rows.push(currentRow);
|
|
2440
|
-
}
|
|
2441
|
-
currentRow = [];
|
|
2442
|
-
currentCell = "";
|
|
2443
|
-
cellIsQuoted = false;
|
|
2444
|
-
} else if (char === "\n") {
|
|
2445
|
-
currentRow.push(cellIsQuoted ? currentCell : currentCell.trim());
|
|
2446
|
-
if (currentRow.length > 1 || currentRow.length === 1 && currentRow[0] !== "") {
|
|
2447
|
-
rows.push(currentRow);
|
|
2448
|
-
}
|
|
2449
|
-
currentRow = [];
|
|
2450
|
-
currentCell = "";
|
|
2451
|
-
cellIsQuoted = false;
|
|
2452
|
-
} else {
|
|
2453
|
-
if (!(cellIsQuoted && !inQuotes && (char === " " || char === " "))) {
|
|
2454
|
-
currentCell += char;
|
|
2455
|
-
}
|
|
2394
|
+
let parsed = rawVal;
|
|
2395
|
+
if (typeof rawVal === "string") {
|
|
2396
|
+
const trimmed = rawVal.trim();
|
|
2397
|
+
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
|
|
2398
|
+
try {
|
|
2399
|
+
parsed = JSON.parse(trimmed);
|
|
2400
|
+
} catch {
|
|
2456
2401
|
}
|
|
2457
2402
|
}
|
|
2458
2403
|
}
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2404
|
+
const numericIds = [];
|
|
2405
|
+
const candidateLookups = [];
|
|
2406
|
+
const inspectItem = (item) => {
|
|
2407
|
+
if (item === null || item === void 0) return;
|
|
2408
|
+
if (typeof item === "number" && !isNaN(item)) {
|
|
2409
|
+
numericIds.push(item);
|
|
2410
|
+
return;
|
|
2463
2411
|
}
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
const matrix = parseCsvToMatrix(csvText);
|
|
2469
|
-
if (matrix.length < 2) return [];
|
|
2470
|
-
const headers = matrix[0].map((h) => h.trim());
|
|
2471
|
-
const records = [];
|
|
2472
|
-
for (let i = 1; i < matrix.length; i++) {
|
|
2473
|
-
const row = matrix[i];
|
|
2474
|
-
const record = {};
|
|
2475
|
-
headers.forEach((h, idx) => {
|
|
2476
|
-
let val = row[idx] !== void 0 ? row[idx] : "";
|
|
2477
|
-
if (typeof val === "string" && /^'[=+@-]/.test(val)) {
|
|
2478
|
-
val = val.substring(1);
|
|
2412
|
+
if (typeof item === "object") {
|
|
2413
|
+
if (typeof item.id === "number") {
|
|
2414
|
+
numericIds.push(item.id);
|
|
2415
|
+
return;
|
|
2479
2416
|
}
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
case "scalar": {
|
|
2492
|
-
if (value instanceof Date) {
|
|
2493
|
-
return value.toISOString();
|
|
2417
|
+
if (typeof item.id === "string" && /^\d+$/.test(item.id)) {
|
|
2418
|
+
numericIds.push(parseInt(item.id, 10));
|
|
2419
|
+
return;
|
|
2420
|
+
}
|
|
2421
|
+
if (item.documentId) {
|
|
2422
|
+
candidateLookups.push(String(item.documentId));
|
|
2423
|
+
return;
|
|
2424
|
+
}
|
|
2425
|
+
if (item.url) {
|
|
2426
|
+
candidateLookups.push(String(item.url));
|
|
2427
|
+
return;
|
|
2494
2428
|
}
|
|
2495
|
-
return String(value);
|
|
2496
2429
|
}
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2430
|
+
const str = String(item).trim();
|
|
2431
|
+
if (/^\d+$/.test(str)) {
|
|
2432
|
+
numericIds.push(parseInt(str, 10));
|
|
2433
|
+
} else if (str) {
|
|
2434
|
+
candidateLookups.push(str);
|
|
2500
2435
|
}
|
|
2501
|
-
|
|
2502
|
-
|
|
2436
|
+
};
|
|
2437
|
+
if (Array.isArray(parsed)) {
|
|
2438
|
+
for (const el of parsed) {
|
|
2439
|
+
inspectItem(el);
|
|
2503
2440
|
}
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2441
|
+
} else if (typeof parsed === "object" && parsed !== null) {
|
|
2442
|
+
inspectItem(parsed);
|
|
2443
|
+
} else if (typeof rawVal === "string" && (rawVal.includes(",") || rawVal.includes(";"))) {
|
|
2444
|
+
const parts = rawVal.split(/[;,]/).map((s) => s.trim()).filter(Boolean);
|
|
2445
|
+
for (const part of parts) {
|
|
2446
|
+
inspectItem(part);
|
|
2509
2447
|
}
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2448
|
+
} else {
|
|
2449
|
+
inspectItem(parsed);
|
|
2450
|
+
}
|
|
2451
|
+
if (candidateLookups.length > 0) {
|
|
2452
|
+
try {
|
|
2453
|
+
const fileQuery = strapi.db?.query("plugin::upload.file");
|
|
2454
|
+
if (fileQuery) {
|
|
2455
|
+
for (const candidate of candidateLookups) {
|
|
2456
|
+
const found = await fileQuery.findOne({
|
|
2457
|
+
where: {
|
|
2458
|
+
$or: [
|
|
2459
|
+
{ documentId: candidate },
|
|
2460
|
+
{ url: candidate },
|
|
2461
|
+
{ hash: candidate },
|
|
2462
|
+
{ name: candidate }
|
|
2463
|
+
]
|
|
2464
|
+
},
|
|
2465
|
+
select: ["id"]
|
|
2466
|
+
});
|
|
2467
|
+
if (found?.id) {
|
|
2468
|
+
numericIds.push(found.id);
|
|
2515
2469
|
}
|
|
2516
|
-
|
|
2517
|
-
});
|
|
2518
|
-
return JSON.stringify(cleaned);
|
|
2470
|
+
}
|
|
2519
2471
|
}
|
|
2520
|
-
|
|
2472
|
+
} catch {
|
|
2521
2473
|
}
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2474
|
+
}
|
|
2475
|
+
const uniqueNumericIds = Array.from(new Set(numericIds));
|
|
2476
|
+
if (isMultiple) {
|
|
2477
|
+
return uniqueNumericIds;
|
|
2478
|
+
}
|
|
2479
|
+
return uniqueNumericIds.length > 0 ? uniqueNumericIds[0] : null;
|
|
2480
|
+
}
|
|
2481
|
+
function resolveRelationPayload(rawVal, isToMany) {
|
|
2482
|
+
if (rawVal === null || rawVal === void 0 || rawVal === "") {
|
|
2483
|
+
return isToMany ? { set: [] } : { set: [] };
|
|
2484
|
+
}
|
|
2485
|
+
let parsed = rawVal;
|
|
2486
|
+
if (typeof rawVal === "string") {
|
|
2487
|
+
const trimmed = rawVal.trim();
|
|
2488
|
+
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
|
|
2489
|
+
try {
|
|
2490
|
+
parsed = JSON.parse(trimmed);
|
|
2491
|
+
} catch {
|
|
2529
2492
|
}
|
|
2530
|
-
return String(value);
|
|
2531
2493
|
}
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
const docId = item.documentId || item.id || "";
|
|
2537
|
-
return {
|
|
2538
|
-
documentId: String(docId),
|
|
2539
|
-
title: item.title || item.name || item.slug || void 0
|
|
2540
|
-
};
|
|
2541
|
-
}
|
|
2542
|
-
return { documentId: String(item) };
|
|
2543
|
-
});
|
|
2544
|
-
return JSON.stringify(list);
|
|
2545
|
-
}
|
|
2546
|
-
return "";
|
|
2494
|
+
}
|
|
2495
|
+
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
|
2496
|
+
if (parsed.set || parsed.connect || parsed.disconnect) {
|
|
2497
|
+
return parsed;
|
|
2547
2498
|
}
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
id: value.id,
|
|
2552
|
-
documentId: value.documentId,
|
|
2553
|
-
name: value.name,
|
|
2554
|
-
url: value.url,
|
|
2555
|
-
mime: value.mime
|
|
2556
|
-
});
|
|
2557
|
-
}
|
|
2558
|
-
return String(value);
|
|
2499
|
+
const singleDocId = parsed.documentId || parsed.id;
|
|
2500
|
+
if (singleDocId && isValidDocumentId(String(singleDocId))) {
|
|
2501
|
+
return isToMany ? { set: [String(singleDocId).trim()] } : { set: [String(singleDocId).trim()] };
|
|
2559
2502
|
}
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
}
|
|
2572
|
-
return file;
|
|
2573
|
-
});
|
|
2574
|
-
return JSON.stringify(list);
|
|
2503
|
+
}
|
|
2504
|
+
const docIds = [];
|
|
2505
|
+
if (Array.isArray(parsed)) {
|
|
2506
|
+
for (const item of parsed) {
|
|
2507
|
+
if (item && typeof item === "object") {
|
|
2508
|
+
const id = item.documentId || item.id;
|
|
2509
|
+
if (id && isValidDocumentId(String(id))) {
|
|
2510
|
+
docIds.push(String(id).trim());
|
|
2511
|
+
}
|
|
2512
|
+
} else if (isValidDocumentId(String(item))) {
|
|
2513
|
+
docIds.push(String(item).trim());
|
|
2575
2514
|
}
|
|
2576
|
-
return "";
|
|
2577
2515
|
}
|
|
2578
|
-
|
|
2579
|
-
|
|
2516
|
+
} else if (typeof rawVal === "string") {
|
|
2517
|
+
if (rawVal.includes(",") || rawVal.includes(";")) {
|
|
2518
|
+
const parts = rawVal.split(/[;,]/).map((s) => s.trim()).filter((p) => isValidDocumentId(p));
|
|
2519
|
+
docIds.push(...parts);
|
|
2520
|
+
} else if (isValidDocumentId(rawVal.trim())) {
|
|
2521
|
+
docIds.push(rawVal.trim());
|
|
2522
|
+
}
|
|
2580
2523
|
}
|
|
2524
|
+
const uniqueDocIds = Array.from(new Set(docIds));
|
|
2525
|
+
return { set: uniqueDocIds };
|
|
2581
2526
|
}
|
|
2582
|
-
function
|
|
2583
|
-
const
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
schemaVersion: CURRENT_SCHEMA_VERSION,
|
|
2594
|
-
contentType: uid,
|
|
2595
|
-
documentId: String(doc.documentId || doc.id || ""),
|
|
2596
|
-
locale: doc.locale || void 0,
|
|
2597
|
-
exportedAt,
|
|
2598
|
-
...flattenedMeta.applicableFields ? { applicableFields: flattenedMeta.applicableFields } : {},
|
|
2599
|
-
...flattenedMeta.flatTypes ? { flatTypes: flattenedMeta.flatTypes } : {}
|
|
2600
|
-
};
|
|
2601
|
-
rowValues.push(escapeCsvCell(metadata));
|
|
2602
|
-
for (const col of plan.columns) {
|
|
2603
|
-
const cellValue = serializePlannedCell(col, doc, serializeFieldValue);
|
|
2604
|
-
rowValues.push(escapeCsvCell(cellValue));
|
|
2527
|
+
async function resolveComponentPayload(strapi, compUid, rawVal, existingVal = {}) {
|
|
2528
|
+
const comp = strapi.components?.[compUid];
|
|
2529
|
+
if (!comp?.attributes) {
|
|
2530
|
+
return typeof rawVal === "object" ? rawVal : {};
|
|
2531
|
+
}
|
|
2532
|
+
let sourceObj = {};
|
|
2533
|
+
if (typeof rawVal === "string") {
|
|
2534
|
+
try {
|
|
2535
|
+
sourceObj = JSON.parse(rawVal);
|
|
2536
|
+
} catch {
|
|
2537
|
+
return existingVal || {};
|
|
2605
2538
|
}
|
|
2606
|
-
|
|
2539
|
+
} else if (typeof rawVal === "object" && rawVal !== null) {
|
|
2540
|
+
sourceObj = rawVal;
|
|
2541
|
+
} else {
|
|
2542
|
+
return existingVal || {};
|
|
2607
2543
|
}
|
|
2608
|
-
const
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2544
|
+
const result = { ...existingVal || {} };
|
|
2545
|
+
for (const [attrName, attrDef] of Object.entries(comp.attributes)) {
|
|
2546
|
+
if (SYSTEM_ATTRIBUTES.has(attrName.toLowerCase().replace(/[\s_-]/g, ""))) continue;
|
|
2547
|
+
if (!(attrName in sourceObj)) {
|
|
2548
|
+
continue;
|
|
2549
|
+
}
|
|
2550
|
+
const fieldVal = sourceObj[attrName];
|
|
2551
|
+
const type = String(attrDef.type || "").toLowerCase();
|
|
2552
|
+
if (type === "component") {
|
|
2553
|
+
if (attrDef.repeatable) {
|
|
2554
|
+
const list = Array.isArray(fieldVal) ? fieldVal : [];
|
|
2555
|
+
const cleanedList = [];
|
|
2556
|
+
for (const item of list) {
|
|
2557
|
+
const subCleaned = await resolveComponentPayload(strapi, attrDef.component, item, {});
|
|
2558
|
+
cleanedList.push(subCleaned);
|
|
2559
|
+
}
|
|
2560
|
+
result[attrName] = cleanedList;
|
|
2561
|
+
} else {
|
|
2562
|
+
result[attrName] = await resolveComponentPayload(
|
|
2563
|
+
strapi,
|
|
2564
|
+
attrDef.component,
|
|
2565
|
+
fieldVal,
|
|
2566
|
+
existingVal?.[attrName] || {}
|
|
2567
|
+
);
|
|
2568
|
+
}
|
|
2569
|
+
} else if (type === "relation") {
|
|
2570
|
+
const isToMany = String(attrDef.relation || "").toLowerCase().endsWith("many");
|
|
2571
|
+
result[attrName] = resolveRelationPayload(fieldVal, isToMany);
|
|
2572
|
+
} else if (type === "media") {
|
|
2573
|
+
result[attrName] = await resolveMediaIds(strapi, fieldVal, Boolean(attrDef.multiple));
|
|
2574
|
+
} else if (type === "integer" || type === "biginteger") {
|
|
2575
|
+
result[attrName] = parseNumeric(fieldVal, true);
|
|
2576
|
+
} else if (type === "float" || type === "decimal") {
|
|
2577
|
+
result[attrName] = parseNumeric(fieldVal, false);
|
|
2578
|
+
} else if (type === "boolean") {
|
|
2579
|
+
result[attrName] = parseBoolean(fieldVal);
|
|
2580
|
+
} else if (type === "json") {
|
|
2581
|
+
if (isPlainObject(fieldVal) && isPlainObject(existingVal?.[attrName])) {
|
|
2582
|
+
const deepMergePlain = (base, patch) => {
|
|
2583
|
+
if (!isPlainObject(base) || !isPlainObject(patch)) return patch;
|
|
2584
|
+
const next = deepCloneValue(base);
|
|
2585
|
+
for (const [k, v] of Object.entries(patch)) {
|
|
2586
|
+
if (isPlainObject(v) && isPlainObject(next[k])) {
|
|
2587
|
+
next[k] = deepMergePlain(next[k], v);
|
|
2588
|
+
} else {
|
|
2589
|
+
next[k] = parseFirstLevelCellValue(v, void 0, next[k]).value;
|
|
2590
|
+
}
|
|
2591
|
+
}
|
|
2592
|
+
return next;
|
|
2593
|
+
};
|
|
2594
|
+
result[attrName] = deepMergePlain(existingVal[attrName], fieldVal);
|
|
2595
|
+
} else if (isPlainObject(fieldVal)) {
|
|
2596
|
+
const parsedObj = {};
|
|
2597
|
+
for (const [k, v] of Object.entries(fieldVal)) {
|
|
2598
|
+
parsedObj[k] = isPlainObject(v) ? deepCloneValue(v) : parseFirstLevelCellValue(v).value;
|
|
2599
|
+
}
|
|
2600
|
+
result[attrName] = parsedObj;
|
|
2601
|
+
} else if (typeof fieldVal === "string") {
|
|
2602
|
+
try {
|
|
2603
|
+
result[attrName] = JSON.parse(fieldVal);
|
|
2604
|
+
} catch {
|
|
2605
|
+
result[attrName] = fieldVal;
|
|
2606
|
+
}
|
|
2607
|
+
} else {
|
|
2608
|
+
result[attrName] = fieldVal;
|
|
2609
|
+
}
|
|
2610
|
+
} else if (type === "blocks") {
|
|
2611
|
+
if (typeof fieldVal === "string") {
|
|
2612
|
+
try {
|
|
2613
|
+
result[attrName] = JSON.parse(fieldVal);
|
|
2614
|
+
} catch {
|
|
2615
|
+
result[attrName] = fieldVal;
|
|
2616
|
+
}
|
|
2617
|
+
} else {
|
|
2618
|
+
result[attrName] = fieldVal;
|
|
2619
|
+
}
|
|
2620
|
+
} else {
|
|
2621
|
+
result[attrName] = fieldVal === null || fieldVal === void 0 ? null : String(fieldVal);
|
|
2622
|
+
}
|
|
2621
2623
|
}
|
|
2622
|
-
|
|
2624
|
+
return result;
|
|
2625
|
+
}
|
|
2626
|
+
async function resolveDynamicZonePayload(strapi, allowedComponents, rawVal, existingVal = []) {
|
|
2627
|
+
let list = [];
|
|
2628
|
+
if (typeof rawVal === "string") {
|
|
2623
2629
|
try {
|
|
2624
|
-
|
|
2625
|
-
if (parsed && typeof parsed === "object") {
|
|
2626
|
-
return parsed;
|
|
2627
|
-
}
|
|
2630
|
+
list = JSON.parse(rawVal);
|
|
2628
2631
|
} catch {
|
|
2632
|
+
return existingVal || [];
|
|
2629
2633
|
}
|
|
2634
|
+
} else if (Array.isArray(rawVal)) {
|
|
2635
|
+
list = rawVal;
|
|
2636
|
+
} else {
|
|
2637
|
+
return existingVal || [];
|
|
2630
2638
|
}
|
|
2631
|
-
return
|
|
2639
|
+
if (!Array.isArray(list)) return [];
|
|
2640
|
+
const allowedSet = new Set(allowedComponents || []);
|
|
2641
|
+
const resolvedBlocks = [];
|
|
2642
|
+
for (const block of list) {
|
|
2643
|
+
if (!block || typeof block !== "object") continue;
|
|
2644
|
+
const compUid = block.__component;
|
|
2645
|
+
if (!compUid || typeof compUid !== "string") {
|
|
2646
|
+
continue;
|
|
2647
|
+
}
|
|
2648
|
+
if (allowedSet.size > 0 && !allowedSet.has(compUid)) {
|
|
2649
|
+
continue;
|
|
2650
|
+
}
|
|
2651
|
+
const resolvedAttrs = await resolveComponentPayload(strapi, compUid, block, {});
|
|
2652
|
+
resolvedAttrs.__component = compUid;
|
|
2653
|
+
resolvedBlocks.push(resolvedAttrs);
|
|
2654
|
+
}
|
|
2655
|
+
return resolvedBlocks;
|
|
2632
2656
|
}
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
const
|
|
2636
|
-
|
|
2637
|
-
const
|
|
2638
|
-
|
|
2657
|
+
const UNSAFE_PROP_NAMES = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
2658
|
+
function buildLegacyNestedPatchFromDots(attrName, dotEntries) {
|
|
2659
|
+
const patch = {};
|
|
2660
|
+
let hasEffectiveEdits = false;
|
|
2661
|
+
const prefix = `${attrName}.`;
|
|
2662
|
+
for (const [fullKey, rawVal] of dotEntries) {
|
|
2663
|
+
if (!fullKey.startsWith(prefix)) continue;
|
|
2664
|
+
if (rawVal === void 0 || rawVal === null || rawVal === "") continue;
|
|
2665
|
+
const segments = fullKey.slice(prefix.length).split(".").filter(Boolean);
|
|
2666
|
+
if (segments.length === 0 || segments.some((s) => UNSAFE_PROP_NAMES.has(s))) continue;
|
|
2667
|
+
const strVal = String(rawVal);
|
|
2668
|
+
const trimmed = strVal.trim();
|
|
2669
|
+
if (trimmed === EXPLICIT_DELETE_KEY_TOKEN) continue;
|
|
2670
|
+
const parsedValue = EXPLICIT_CLEAR_TOKENS.has(trimmed) ? null : trimmed === EXPLICIT_EMPTY_STRING_TOKEN ? "" : parseFirstLevelCellValue(strVal).value;
|
|
2671
|
+
let cursor = patch;
|
|
2672
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
2673
|
+
const seg = segments[i];
|
|
2674
|
+
if (!isPlainObject(cursor[seg])) {
|
|
2675
|
+
cursor[seg] = {};
|
|
2676
|
+
}
|
|
2677
|
+
cursor = cursor[seg];
|
|
2678
|
+
}
|
|
2679
|
+
cursor[segments[segments.length - 1]] = parsedValue;
|
|
2680
|
+
hasEffectiveEdits = true;
|
|
2681
|
+
}
|
|
2682
|
+
return { hasEffectiveEdits, patch };
|
|
2639
2683
|
}
|
|
2640
|
-
function
|
|
2641
|
-
const
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2684
|
+
async function prepareCsvRowMutation(strapi, uid, existingDoc, rawRow, options = {}) {
|
|
2685
|
+
const schema = discoverSchema(strapi, uid);
|
|
2686
|
+
const payload = {};
|
|
2687
|
+
const errors = [];
|
|
2688
|
+
const warnings = [];
|
|
2689
|
+
const isPartialUpdate = options.isPartialUpdate !== false;
|
|
2690
|
+
const isCreate = options.isCreate !== void 0 ? options.isCreate : !existingDoc;
|
|
2691
|
+
const unescapedRow = {};
|
|
2692
|
+
for (const [k, v] of Object.entries(rawRow || {})) {
|
|
2693
|
+
let cleanVal = v;
|
|
2694
|
+
if (typeof cleanVal === "string" && /^'[=+@-]/.test(cleanVal)) {
|
|
2695
|
+
cleanVal = cleanVal.substring(1);
|
|
2696
|
+
}
|
|
2697
|
+
unescapedRow[k] = cleanVal;
|
|
2646
2698
|
}
|
|
2647
|
-
|
|
2648
|
-
|
|
2699
|
+
const rowInfo = inspectRowMetadataMode(unescapedRow);
|
|
2700
|
+
if (rowInfo.error) {
|
|
2701
|
+
errors.push(rowInfo.error);
|
|
2649
2702
|
}
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2703
|
+
const detectedMode = rowInfo.mode;
|
|
2704
|
+
if (options.expectedExportMode && detectedMode !== options.expectedExportMode) {
|
|
2705
|
+
errors.push(
|
|
2706
|
+
`Row exportMode "${detectedMode}" conflicts with batch exportMode "${options.expectedExportMode}".`
|
|
2707
|
+
);
|
|
2653
2708
|
}
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2709
|
+
const isTargetedMode = detectedMode === "metafields" || detectedMode === "topbarconfig";
|
|
2710
|
+
const targetAttr = isTargetedMode ? rowInfo.targetObjectField || resolveTargetObjectField(schema, detectedMode) || detectedMode : void 0;
|
|
2711
|
+
if (isTargetedMode && targetAttr && !isCreate) {
|
|
2712
|
+
const targetField = schema.fields[targetAttr];
|
|
2713
|
+
if (!targetField || targetField.category !== "json" && targetField.category !== "component_single") {
|
|
2714
|
+
errors.push(
|
|
2715
|
+
`Target object field "${targetAttr}" for exportMode "${detectedMode}" was not found as an object/component attribute on "${uid}".`
|
|
2716
|
+
);
|
|
2717
|
+
return {
|
|
2718
|
+
payload,
|
|
2719
|
+
errors,
|
|
2720
|
+
warnings,
|
|
2721
|
+
detectedMode,
|
|
2722
|
+
targetObjectField: targetAttr,
|
|
2723
|
+
rowMeta: rowInfo
|
|
2724
|
+
};
|
|
2725
|
+
}
|
|
2726
|
+
const ctAttr = strapi.contentTypes?.[uid]?.attributes?.[targetAttr];
|
|
2727
|
+
const reconstructed = reconstructTargetedFirstLevelObject({
|
|
2728
|
+
targetAttr,
|
|
2729
|
+
exportMode: detectedMode,
|
|
2730
|
+
existingDoc,
|
|
2731
|
+
docIdForError: options.docIdForError,
|
|
2732
|
+
rawRow: unescapedRow,
|
|
2733
|
+
rowMeta: rowInfo,
|
|
2734
|
+
isAttrRequired: Boolean(ctAttr?.required)
|
|
2735
|
+
});
|
|
2736
|
+
if (reconstructed.errors.length > 0) {
|
|
2737
|
+
errors.push(...reconstructed.errors);
|
|
2738
|
+
}
|
|
2739
|
+
if (reconstructed.shouldSet) {
|
|
2740
|
+
if (targetField.category === "component_single" && targetField.componentUid && isPlainObject(reconstructed.value)) {
|
|
2741
|
+
payload[targetAttr] = await resolveComponentPayload(
|
|
2742
|
+
strapi,
|
|
2743
|
+
targetField.componentUid,
|
|
2744
|
+
reconstructed.value,
|
|
2745
|
+
existingDoc?.[targetAttr] || {}
|
|
2746
|
+
);
|
|
2747
|
+
} else {
|
|
2748
|
+
payload[targetAttr] = reconstructed.value;
|
|
2749
|
+
}
|
|
2750
|
+
}
|
|
2751
|
+
const schemaCheck2 = validateRecordSchemaConstraints(
|
|
2752
|
+
strapi,
|
|
2753
|
+
uid,
|
|
2754
|
+
existingDoc,
|
|
2755
|
+
payload,
|
|
2756
|
+
unescapedRow,
|
|
2757
|
+
{
|
|
2758
|
+
isCreate: false,
|
|
2759
|
+
detectedMode,
|
|
2760
|
+
targetObjectField: targetAttr
|
|
2761
|
+
}
|
|
2762
|
+
);
|
|
2763
|
+
errors.push(...schemaCheck2.errors);
|
|
2764
|
+
warnings.push(...schemaCheck2.warnings);
|
|
2668
2765
|
return {
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2766
|
+
payload,
|
|
2767
|
+
errors,
|
|
2768
|
+
warnings,
|
|
2769
|
+
detectedMode,
|
|
2770
|
+
targetObjectField: targetAttr,
|
|
2771
|
+
rowMeta: rowInfo
|
|
2673
2772
|
};
|
|
2674
2773
|
}
|
|
2675
|
-
const
|
|
2676
|
-
for (const
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
if (!existing.messages.includes(msg)) {
|
|
2683
|
-
existing.messages.push(msg);
|
|
2684
|
-
}
|
|
2685
|
-
} else {
|
|
2686
|
-
groupedByRow.set(rowNum, {
|
|
2687
|
-
row: rowNum,
|
|
2688
|
-
documentId: item.documentId,
|
|
2689
|
-
messages: [msg],
|
|
2690
|
-
originalRow: item.originalRow && typeof item.originalRow === "object" ? item.originalRow : {}
|
|
2691
|
-
});
|
|
2692
|
-
}
|
|
2693
|
-
}
|
|
2694
|
-
const orderedEntries = Array.from(groupedByRow.values()).sort((a, b) => a.row - b.row);
|
|
2695
|
-
if (orderedEntries.length === 0) {
|
|
2696
|
-
return {
|
|
2697
|
-
csvContent: "",
|
|
2698
|
-
headers: headers ? [...headers] : [],
|
|
2699
|
-
errorColumnName: "Error",
|
|
2700
|
-
errorCount: 0
|
|
2701
|
-
};
|
|
2702
|
-
}
|
|
2703
|
-
let baseHeaders = [];
|
|
2704
|
-
if (Array.isArray(headers) && headers.length > 0) {
|
|
2705
|
-
baseHeaders = [...headers];
|
|
2706
|
-
} else {
|
|
2707
|
-
const keySet = /* @__PURE__ */ new Set();
|
|
2708
|
-
for (const entry of orderedEntries) {
|
|
2709
|
-
for (const k of Object.keys(entry.originalRow || {})) {
|
|
2710
|
-
if (!keySet.has(k)) {
|
|
2711
|
-
keySet.add(k);
|
|
2712
|
-
baseHeaders.push(k);
|
|
2713
|
-
}
|
|
2714
|
-
}
|
|
2715
|
-
}
|
|
2716
|
-
}
|
|
2717
|
-
const errorColumnName = resolveErrorColumnName(baseHeaders);
|
|
2718
|
-
const finalHeaders = [...baseHeaders, errorColumnName];
|
|
2719
|
-
const lines = [];
|
|
2720
|
-
lines.push(finalHeaders.map(escapeCsvCell).join(","));
|
|
2721
|
-
for (const entry of orderedEntries) {
|
|
2722
|
-
const cells = baseHeaders.map((h) => {
|
|
2723
|
-
const val = entry.originalRow ? entry.originalRow[h] : "";
|
|
2724
|
-
return escapeCsvCell(val === void 0 || val === null ? "" : val);
|
|
2725
|
-
});
|
|
2726
|
-
cells.push(escapeCsvCell(entry.messages.join("; ")));
|
|
2727
|
-
lines.push(cells.join(","));
|
|
2728
|
-
}
|
|
2729
|
-
const csvContent = "\uFEFF" + lines.join("\r\n") + "\r\n";
|
|
2730
|
-
return {
|
|
2731
|
-
csvContent,
|
|
2732
|
-
headers: finalHeaders,
|
|
2733
|
-
errorColumnName,
|
|
2734
|
-
errorCount: orderedEntries.length
|
|
2735
|
-
};
|
|
2736
|
-
}
|
|
2737
|
-
function isValidDocumentId(val) {
|
|
2738
|
-
if (!val || typeof val !== "string") return false;
|
|
2739
|
-
const trimmed = val.trim();
|
|
2740
|
-
if (!trimmed) return false;
|
|
2741
|
-
if (/\s/.test(trimmed)) return false;
|
|
2742
|
-
return /^[a-zA-Z0-9_-]+$/.test(trimmed);
|
|
2743
|
-
}
|
|
2744
|
-
function parseNumeric(rawVal, isInteger) {
|
|
2745
|
-
if (rawVal === null || rawVal === void 0 || rawVal === "") return null;
|
|
2746
|
-
const cleanStr = String(rawVal).replace(/[$€£,\s]/g, "");
|
|
2747
|
-
if (isInteger) {
|
|
2748
|
-
const n = parseInt(cleanStr, 10);
|
|
2749
|
-
return isNaN(n) ? null : n;
|
|
2750
|
-
}
|
|
2751
|
-
const f = parseFloat(cleanStr);
|
|
2752
|
-
return isNaN(f) ? null : f;
|
|
2753
|
-
}
|
|
2754
|
-
function parseBoolean(rawVal) {
|
|
2755
|
-
if (rawVal === null || rawVal === void 0 || rawVal === "") return null;
|
|
2756
|
-
const s = String(rawVal).trim().toLowerCase();
|
|
2757
|
-
if (s === "true" || s === "1" || s === "yes" || s === "y" || s === "on") return true;
|
|
2758
|
-
if (s === "false" || s === "0" || s === "no" || s === "n" || s === "off") return false;
|
|
2759
|
-
return null;
|
|
2760
|
-
}
|
|
2761
|
-
async function resolveMediaIds(strapi, rawVal, isMultiple) {
|
|
2762
|
-
if (rawVal === null || rawVal === void 0 || rawVal === "") {
|
|
2763
|
-
return isMultiple ? [] : null;
|
|
2764
|
-
}
|
|
2765
|
-
let parsed = rawVal;
|
|
2766
|
-
if (typeof rawVal === "string") {
|
|
2767
|
-
const trimmed = rawVal.trim();
|
|
2768
|
-
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
|
|
2769
|
-
try {
|
|
2770
|
-
parsed = JSON.parse(trimmed);
|
|
2771
|
-
} catch {
|
|
2772
|
-
}
|
|
2774
|
+
const dotEntriesByAttr = {};
|
|
2775
|
+
for (const [k, v] of Object.entries(unescapedRow)) {
|
|
2776
|
+
const firstDot = k.indexOf(".");
|
|
2777
|
+
if (firstDot > 0) {
|
|
2778
|
+
const parent = k.slice(0, firstDot);
|
|
2779
|
+
if (!dotEntriesByAttr[parent]) dotEntriesByAttr[parent] = [];
|
|
2780
|
+
dotEntriesByAttr[parent].push([k, v]);
|
|
2773
2781
|
}
|
|
2774
2782
|
}
|
|
2775
|
-
const
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
if (
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
if (item.documentId) {
|
|
2793
|
-
candidateLookups.push(String(item.documentId));
|
|
2794
|
-
return;
|
|
2783
|
+
for (const [attrName, field] of Object.entries(schema.fields)) {
|
|
2784
|
+
const hasDirectKey = attrName in unescapedRow;
|
|
2785
|
+
const dotEntries = dotEntriesByAttr[attrName] || [];
|
|
2786
|
+
const hasDotKeys = dotEntries.length > 0;
|
|
2787
|
+
if (isTargetedMode && isCreate && targetAttr === attrName) {
|
|
2788
|
+
const ctAttr = strapi.contentTypes?.[uid]?.attributes?.[targetAttr];
|
|
2789
|
+
const reconstructed = reconstructTargetedFirstLevelObject({
|
|
2790
|
+
targetAttr,
|
|
2791
|
+
exportMode: detectedMode,
|
|
2792
|
+
existingDoc: null,
|
|
2793
|
+
docIdForError: options.docIdForError,
|
|
2794
|
+
rawRow: unescapedRow,
|
|
2795
|
+
rowMeta: rowInfo,
|
|
2796
|
+
isAttrRequired: Boolean(ctAttr?.required)
|
|
2797
|
+
});
|
|
2798
|
+
if (reconstructed.errors.length > 0) {
|
|
2799
|
+
errors.push(...reconstructed.errors);
|
|
2795
2800
|
}
|
|
2796
|
-
if (
|
|
2797
|
-
|
|
2798
|
-
|
|
2801
|
+
if (reconstructed.shouldSet) {
|
|
2802
|
+
if (field.category === "component_single" && field.componentUid && isPlainObject(reconstructed.value)) {
|
|
2803
|
+
payload[attrName] = await resolveComponentPayload(
|
|
2804
|
+
strapi,
|
|
2805
|
+
field.componentUid,
|
|
2806
|
+
reconstructed.value,
|
|
2807
|
+
{}
|
|
2808
|
+
);
|
|
2809
|
+
} else {
|
|
2810
|
+
payload[attrName] = reconstructed.value;
|
|
2811
|
+
}
|
|
2799
2812
|
}
|
|
2813
|
+
continue;
|
|
2800
2814
|
}
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
numericIds.push(parseInt(str, 10));
|
|
2804
|
-
} else if (str) {
|
|
2805
|
-
candidateLookups.push(str);
|
|
2806
|
-
}
|
|
2807
|
-
};
|
|
2808
|
-
if (Array.isArray(parsed)) {
|
|
2809
|
-
for (const el of parsed) {
|
|
2810
|
-
inspectItem(el);
|
|
2811
|
-
}
|
|
2812
|
-
} else if (typeof parsed === "object" && parsed !== null) {
|
|
2813
|
-
inspectItem(parsed);
|
|
2814
|
-
} else if (typeof rawVal === "string" && (rawVal.includes(",") || rawVal.includes(";"))) {
|
|
2815
|
-
const parts = rawVal.split(/[;,]/).map((s) => s.trim()).filter(Boolean);
|
|
2816
|
-
for (const part of parts) {
|
|
2817
|
-
inspectItem(part);
|
|
2815
|
+
if (!hasDirectKey && !hasDotKeys) {
|
|
2816
|
+
continue;
|
|
2818
2817
|
}
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
}
|
|
2838
|
-
|
|
2839
|
-
numericIds.push(found.id);
|
|
2818
|
+
const rawVal = hasDirectKey ? unescapedRow[attrName] : void 0;
|
|
2819
|
+
switch (field.category) {
|
|
2820
|
+
case "scalar": {
|
|
2821
|
+
if (rawVal === void 0 || rawVal === "" || rawVal === null) {
|
|
2822
|
+
payload[attrName] = null;
|
|
2823
|
+
} else if (field.rawType === "integer" || field.rawType === "biginteger") {
|
|
2824
|
+
payload[attrName] = parseNumeric(rawVal, true);
|
|
2825
|
+
} else if (field.rawType === "float" || field.rawType === "decimal") {
|
|
2826
|
+
payload[attrName] = parseNumeric(rawVal, false);
|
|
2827
|
+
} else if (field.rawType === "boolean") {
|
|
2828
|
+
payload[attrName] = parseBoolean(rawVal);
|
|
2829
|
+
} else if (field.rawType === "date" || field.rawType === "time" || field.rawType === "datetime" || field.rawType === "timestamp") {
|
|
2830
|
+
payload[attrName] = String(rawVal).trim() || null;
|
|
2831
|
+
} else if (field.rawType === "enumeration") {
|
|
2832
|
+
const trimmed = String(rawVal).trim();
|
|
2833
|
+
if (field.enumOptions && field.enumOptions.length > 0) {
|
|
2834
|
+
const match = field.enumOptions.find((o) => o.toLowerCase() === trimmed.toLowerCase());
|
|
2835
|
+
payload[attrName] = match || trimmed;
|
|
2836
|
+
} else {
|
|
2837
|
+
payload[attrName] = trimmed;
|
|
2840
2838
|
}
|
|
2839
|
+
} else {
|
|
2840
|
+
payload[attrName] = String(rawVal).trim();
|
|
2841
2841
|
}
|
|
2842
|
+
break;
|
|
2842
2843
|
}
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
if (typeof rawVal === "string") {
|
|
2858
|
-
const trimmed = rawVal.trim();
|
|
2859
|
-
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
|
|
2860
|
-
try {
|
|
2861
|
-
parsed = JSON.parse(trimmed);
|
|
2862
|
-
} catch {
|
|
2844
|
+
case "blocks": {
|
|
2845
|
+
if (rawVal === void 0 || rawVal === "" || rawVal === null) {
|
|
2846
|
+
payload[attrName] = [];
|
|
2847
|
+
} else {
|
|
2848
|
+
try {
|
|
2849
|
+
const parsed = JSON.parse(rawVal);
|
|
2850
|
+
payload[attrName] = Array.isArray(parsed) ? parsed : [parsed];
|
|
2851
|
+
} catch {
|
|
2852
|
+
payload[attrName] = [
|
|
2853
|
+
{ type: "paragraph", children: [{ type: "text", text: String(rawVal).trim() }] }
|
|
2854
|
+
];
|
|
2855
|
+
}
|
|
2856
|
+
}
|
|
2857
|
+
break;
|
|
2863
2858
|
}
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2859
|
+
case "json": {
|
|
2860
|
+
if (hasDotKeys) {
|
|
2861
|
+
const { hasEffectiveEdits, patch } = buildLegacyNestedPatchFromDots(
|
|
2862
|
+
attrName,
|
|
2863
|
+
dotEntries
|
|
2864
|
+
);
|
|
2865
|
+
if (hasEffectiveEdits) {
|
|
2866
|
+
const mergeDeep = (base, p) => {
|
|
2867
|
+
if (!isPlainObject(base) || !isPlainObject(p)) return p;
|
|
2868
|
+
const next = deepCloneValue(base);
|
|
2869
|
+
for (const [k, v] of Object.entries(p)) {
|
|
2870
|
+
if (isPlainObject(v) && isPlainObject(next[k])) {
|
|
2871
|
+
next[k] = mergeDeep(next[k], v);
|
|
2872
|
+
} else {
|
|
2873
|
+
next[k] = deepCloneValue(v);
|
|
2874
|
+
}
|
|
2875
|
+
}
|
|
2876
|
+
return next;
|
|
2877
|
+
};
|
|
2878
|
+
payload[attrName] = isPlainObject(existingDoc?.[attrName]) ? mergeDeep(existingDoc[attrName], patch) : patch;
|
|
2879
|
+
break;
|
|
2880
|
+
}
|
|
2882
2881
|
}
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
}
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
}
|
|
2903
|
-
let sourceObj = {};
|
|
2904
|
-
if (typeof rawVal === "string") {
|
|
2905
|
-
try {
|
|
2906
|
-
sourceObj = JSON.parse(rawVal);
|
|
2907
|
-
} catch {
|
|
2908
|
-
return existingVal || {};
|
|
2909
|
-
}
|
|
2910
|
-
} else if (typeof rawVal === "object" && rawVal !== null) {
|
|
2911
|
-
sourceObj = rawVal;
|
|
2912
|
-
} else {
|
|
2913
|
-
return existingVal || {};
|
|
2914
|
-
}
|
|
2915
|
-
const result = { ...existingVal || {} };
|
|
2916
|
-
for (const [attrName, attrDef] of Object.entries(comp.attributes)) {
|
|
2917
|
-
if (SYSTEM_ATTRIBUTES.has(attrName.toLowerCase().replace(/[\s_-]/g, ""))) continue;
|
|
2918
|
-
if (!(attrName in sourceObj)) {
|
|
2919
|
-
continue;
|
|
2920
|
-
}
|
|
2921
|
-
const fieldVal = sourceObj[attrName];
|
|
2922
|
-
const type = String(attrDef.type || "").toLowerCase();
|
|
2923
|
-
if (type === "component") {
|
|
2924
|
-
if (attrDef.repeatable) {
|
|
2925
|
-
const list = Array.isArray(fieldVal) ? fieldVal : [];
|
|
2926
|
-
const cleanedList = [];
|
|
2927
|
-
for (const item of list) {
|
|
2928
|
-
const subCleaned = await resolveComponentPayload(strapi, attrDef.component, item, {});
|
|
2929
|
-
cleanedList.push(subCleaned);
|
|
2930
|
-
}
|
|
2931
|
-
result[attrName] = cleanedList;
|
|
2932
|
-
} else {
|
|
2933
|
-
result[attrName] = await resolveComponentPayload(
|
|
2934
|
-
strapi,
|
|
2935
|
-
attrDef.component,
|
|
2936
|
-
fieldVal,
|
|
2937
|
-
existingVal?.[attrName] || {}
|
|
2938
|
-
);
|
|
2939
|
-
}
|
|
2940
|
-
} else if (type === "relation") {
|
|
2941
|
-
const isToMany = String(attrDef.relation || "").toLowerCase().endsWith("many");
|
|
2942
|
-
result[attrName] = resolveRelationPayload(fieldVal, isToMany);
|
|
2943
|
-
} else if (type === "media") {
|
|
2944
|
-
result[attrName] = await resolveMediaIds(strapi, fieldVal, Boolean(attrDef.multiple));
|
|
2945
|
-
} else if (type === "integer" || type === "biginteger") {
|
|
2946
|
-
result[attrName] = parseNumeric(fieldVal, true);
|
|
2947
|
-
} else if (type === "float" || type === "decimal") {
|
|
2948
|
-
result[attrName] = parseNumeric(fieldVal, false);
|
|
2949
|
-
} else if (type === "boolean") {
|
|
2950
|
-
result[attrName] = parseBoolean(fieldVal);
|
|
2951
|
-
} else if (type === "json") {
|
|
2952
|
-
if (isPlainObject(fieldVal) && isPlainObject(existingVal?.[attrName])) {
|
|
2953
|
-
const deepMergePlain = (base, patch) => {
|
|
2954
|
-
if (!isPlainObject(base) || !isPlainObject(patch)) return patch;
|
|
2955
|
-
const next = deepCloneValue(base);
|
|
2956
|
-
for (const [k, v] of Object.entries(patch)) {
|
|
2957
|
-
if (isPlainObject(v) && isPlainObject(next[k])) {
|
|
2958
|
-
next[k] = deepMergePlain(next[k], v);
|
|
2959
|
-
} else {
|
|
2960
|
-
next[k] = parseFlattenedLeafValue(v, void 0, next[k]);
|
|
2961
|
-
}
|
|
2962
|
-
}
|
|
2963
|
-
return next;
|
|
2964
|
-
};
|
|
2965
|
-
result[attrName] = deepMergePlain(existingVal[attrName], fieldVal);
|
|
2966
|
-
} else if (isPlainObject(fieldVal)) {
|
|
2967
|
-
const parsedObj = {};
|
|
2968
|
-
for (const [k, v] of Object.entries(fieldVal)) {
|
|
2969
|
-
parsedObj[k] = isPlainObject(v) ? deepCloneValue(v) : parseFlattenedLeafValue(v);
|
|
2970
|
-
}
|
|
2971
|
-
result[attrName] = parsedObj;
|
|
2972
|
-
} else if (typeof fieldVal === "string") {
|
|
2973
|
-
try {
|
|
2974
|
-
result[attrName] = JSON.parse(fieldVal);
|
|
2975
|
-
} catch {
|
|
2976
|
-
result[attrName] = fieldVal;
|
|
2977
|
-
}
|
|
2978
|
-
} else {
|
|
2979
|
-
result[attrName] = fieldVal;
|
|
2980
|
-
}
|
|
2981
|
-
} else if (type === "blocks") {
|
|
2982
|
-
if (typeof fieldVal === "string") {
|
|
2983
|
-
try {
|
|
2984
|
-
result[attrName] = JSON.parse(fieldVal);
|
|
2985
|
-
} catch {
|
|
2986
|
-
result[attrName] = fieldVal;
|
|
2987
|
-
}
|
|
2988
|
-
} else {
|
|
2989
|
-
result[attrName] = fieldVal;
|
|
2990
|
-
}
|
|
2991
|
-
} else {
|
|
2992
|
-
result[attrName] = fieldVal === null || fieldVal === void 0 ? null : String(fieldVal);
|
|
2993
|
-
}
|
|
2994
|
-
}
|
|
2995
|
-
return result;
|
|
2996
|
-
}
|
|
2997
|
-
async function resolveDynamicZonePayload(strapi, allowedComponents, rawVal, existingVal = []) {
|
|
2998
|
-
let list = [];
|
|
2999
|
-
if (typeof rawVal === "string") {
|
|
3000
|
-
try {
|
|
3001
|
-
list = JSON.parse(rawVal);
|
|
3002
|
-
} catch {
|
|
3003
|
-
return existingVal || [];
|
|
3004
|
-
}
|
|
3005
|
-
} else if (Array.isArray(rawVal)) {
|
|
3006
|
-
list = rawVal;
|
|
3007
|
-
} else {
|
|
3008
|
-
return existingVal || [];
|
|
3009
|
-
}
|
|
3010
|
-
if (!Array.isArray(list)) return [];
|
|
3011
|
-
const allowedSet = new Set(allowedComponents || []);
|
|
3012
|
-
const resolvedBlocks = [];
|
|
3013
|
-
for (const block of list) {
|
|
3014
|
-
if (!block || typeof block !== "object") continue;
|
|
3015
|
-
const compUid = block.__component;
|
|
3016
|
-
if (!compUid || typeof compUid !== "string") {
|
|
3017
|
-
continue;
|
|
3018
|
-
}
|
|
3019
|
-
if (allowedSet.size > 0 && !allowedSet.has(compUid)) {
|
|
3020
|
-
continue;
|
|
3021
|
-
}
|
|
3022
|
-
const resolvedAttrs = await resolveComponentPayload(strapi, compUid, block, {});
|
|
3023
|
-
resolvedAttrs.__component = compUid;
|
|
3024
|
-
resolvedBlocks.push(resolvedAttrs);
|
|
3025
|
-
}
|
|
3026
|
-
return resolvedBlocks;
|
|
3027
|
-
}
|
|
3028
|
-
async function buildDocumentUpdatePayload(strapi, uid, existingDoc, rawRow, options = {}) {
|
|
3029
|
-
const schema = discoverSchema(strapi, uid);
|
|
3030
|
-
const payload = {};
|
|
3031
|
-
const isPartialUpdate = options.isPartialUpdate !== false;
|
|
3032
|
-
const unescapedRow = {};
|
|
3033
|
-
for (const [k, v] of Object.entries(rawRow)) {
|
|
3034
|
-
let cleanVal = v;
|
|
3035
|
-
if (typeof cleanVal === "string" && /^'[=+@-]/.test(cleanVal)) {
|
|
3036
|
-
cleanVal = cleanVal.substring(1);
|
|
3037
|
-
}
|
|
3038
|
-
unescapedRow[k] = cleanVal;
|
|
3039
|
-
}
|
|
3040
|
-
const rowMeta = extractMetadataFromRow(unescapedRow);
|
|
3041
|
-
const dotEntriesByAttr = {};
|
|
3042
|
-
for (const [k, v] of Object.entries(unescapedRow)) {
|
|
3043
|
-
const firstDot = k.indexOf(".");
|
|
3044
|
-
if (firstDot > 0) {
|
|
3045
|
-
const parent = k.slice(0, firstDot);
|
|
3046
|
-
if (!dotEntriesByAttr[parent]) dotEntriesByAttr[parent] = [];
|
|
3047
|
-
dotEntriesByAttr[parent].push([k, v]);
|
|
3048
|
-
}
|
|
3049
|
-
}
|
|
3050
|
-
for (const [attrName, field] of Object.entries(schema.fields)) {
|
|
3051
|
-
const hasDirectKey = attrName in unescapedRow;
|
|
3052
|
-
const dotEntries = dotEntriesByAttr[attrName] || [];
|
|
3053
|
-
const hasDotKeys = dotEntries.length > 0;
|
|
3054
|
-
if (!hasDirectKey && !hasDotKeys) {
|
|
3055
|
-
continue;
|
|
3056
|
-
}
|
|
3057
|
-
const rawVal = hasDirectKey ? unescapedRow[attrName] : void 0;
|
|
3058
|
-
switch (field.category) {
|
|
3059
|
-
case "scalar": {
|
|
3060
|
-
if (rawVal === void 0 || rawVal === "" || rawVal === null) {
|
|
3061
|
-
payload[attrName] = null;
|
|
3062
|
-
} else if (field.rawType === "integer" || field.rawType === "biginteger") {
|
|
3063
|
-
payload[attrName] = parseNumeric(rawVal, true);
|
|
3064
|
-
} else if (field.rawType === "float" || field.rawType === "decimal") {
|
|
3065
|
-
payload[attrName] = parseNumeric(rawVal, false);
|
|
3066
|
-
} else if (field.rawType === "boolean") {
|
|
3067
|
-
payload[attrName] = parseBoolean(rawVal);
|
|
3068
|
-
} else if (field.rawType === "date" || field.rawType === "time" || field.rawType === "datetime" || field.rawType === "timestamp") {
|
|
3069
|
-
payload[attrName] = String(rawVal).trim() || null;
|
|
3070
|
-
} else if (field.rawType === "enumeration") {
|
|
3071
|
-
const trimmed = String(rawVal).trim();
|
|
3072
|
-
if (field.enumOptions && field.enumOptions.length > 0) {
|
|
3073
|
-
const match = field.enumOptions.find((o) => o.toLowerCase() === trimmed.toLowerCase());
|
|
3074
|
-
payload[attrName] = match || trimmed;
|
|
3075
|
-
} else {
|
|
3076
|
-
payload[attrName] = trimmed;
|
|
3077
|
-
}
|
|
3078
|
-
} else {
|
|
3079
|
-
payload[attrName] = String(rawVal).trim();
|
|
3080
|
-
}
|
|
3081
|
-
break;
|
|
3082
|
-
}
|
|
3083
|
-
case "blocks": {
|
|
3084
|
-
if (rawVal === void 0 || rawVal === "" || rawVal === null) {
|
|
3085
|
-
payload[attrName] = [];
|
|
3086
|
-
} else {
|
|
3087
|
-
try {
|
|
3088
|
-
const parsed = JSON.parse(rawVal);
|
|
3089
|
-
payload[attrName] = Array.isArray(parsed) ? parsed : [parsed];
|
|
3090
|
-
} catch {
|
|
3091
|
-
payload[attrName] = [
|
|
3092
|
-
{ type: "paragraph", children: [{ type: "text", text: String(rawVal).trim() }] }
|
|
3093
|
-
];
|
|
3094
|
-
}
|
|
3095
|
-
}
|
|
3096
|
-
break;
|
|
3097
|
-
}
|
|
3098
|
-
case "json": {
|
|
3099
|
-
const reconstructed = reconstructFlattenedJsonAttribute({
|
|
3100
|
-
attrName,
|
|
3101
|
-
existingVal: existingDoc?.[attrName],
|
|
3102
|
-
hasDirectKey,
|
|
3103
|
-
directRawVal: rawVal,
|
|
3104
|
-
dotEntries,
|
|
3105
|
-
rowMeta: rowMeta ? {
|
|
3106
|
-
applicableFields: rowMeta.applicableFields,
|
|
3107
|
-
flatTypes: rowMeta.flatTypes
|
|
3108
|
-
} : null,
|
|
3109
|
-
isPartialUpdate
|
|
3110
|
-
});
|
|
3111
|
-
if (reconstructed.shouldSet) {
|
|
3112
|
-
payload[attrName] = reconstructed.value;
|
|
3113
|
-
}
|
|
3114
|
-
break;
|
|
2882
|
+
if (hasDirectKey) {
|
|
2883
|
+
const trimmed = typeof rawVal === "string" ? rawVal.trim() : "";
|
|
2884
|
+
if (rawVal === void 0 || rawVal === null || rawVal === "") {
|
|
2885
|
+
if (!isPartialUpdate) {
|
|
2886
|
+
payload[attrName] = null;
|
|
2887
|
+
}
|
|
2888
|
+
} else if (EXPLICIT_CLEAR_TOKENS.has(trimmed) || trimmed === "null") {
|
|
2889
|
+
payload[attrName] = null;
|
|
2890
|
+
} else if (typeof rawVal === "string") {
|
|
2891
|
+
try {
|
|
2892
|
+
payload[attrName] = JSON.parse(rawVal);
|
|
2893
|
+
} catch {
|
|
2894
|
+
payload[attrName] = rawVal;
|
|
2895
|
+
}
|
|
2896
|
+
} else {
|
|
2897
|
+
payload[attrName] = rawVal;
|
|
2898
|
+
}
|
|
2899
|
+
}
|
|
2900
|
+
break;
|
|
3115
2901
|
}
|
|
3116
2902
|
case "component_single": {
|
|
3117
2903
|
if (field.componentUid) {
|
|
3118
2904
|
const directTrimmed = typeof rawVal === "string" ? rawVal.trim() : "";
|
|
3119
2905
|
if (hasDotKeys) {
|
|
3120
|
-
const { hasEffectiveEdits, patch } =
|
|
2906
|
+
const { hasEffectiveEdits, patch } = buildLegacyNestedPatchFromDots(
|
|
3121
2907
|
attrName,
|
|
3122
2908
|
dotEntries
|
|
3123
2909
|
);
|
|
@@ -3218,7 +3004,32 @@ async function buildDocumentUpdatePayload(strapi, uid, existingDoc, rawRow, opti
|
|
|
3218
3004
|
payload[attrName] = rawVal;
|
|
3219
3005
|
}
|
|
3220
3006
|
}
|
|
3221
|
-
|
|
3007
|
+
const schemaCheck = validateRecordSchemaConstraints(
|
|
3008
|
+
strapi,
|
|
3009
|
+
uid,
|
|
3010
|
+
existingDoc,
|
|
3011
|
+
payload,
|
|
3012
|
+
unescapedRow,
|
|
3013
|
+
{
|
|
3014
|
+
isCreate,
|
|
3015
|
+
detectedMode,
|
|
3016
|
+
targetObjectField: targetAttr
|
|
3017
|
+
}
|
|
3018
|
+
);
|
|
3019
|
+
errors.push(...schemaCheck.errors);
|
|
3020
|
+
warnings.push(...schemaCheck.warnings);
|
|
3021
|
+
return {
|
|
3022
|
+
payload,
|
|
3023
|
+
errors,
|
|
3024
|
+
warnings,
|
|
3025
|
+
detectedMode,
|
|
3026
|
+
targetObjectField: targetAttr,
|
|
3027
|
+
rowMeta: rowInfo
|
|
3028
|
+
};
|
|
3029
|
+
}
|
|
3030
|
+
async function buildDocumentUpdatePayload(strapi, uid, existingDoc, rawRow, options = {}) {
|
|
3031
|
+
const plan = await prepareCsvRowMutation(strapi, uid, existingDoc, rawRow, options);
|
|
3032
|
+
return plan.payload;
|
|
3222
3033
|
}
|
|
3223
3034
|
function formatComponentDisplayName(compUid) {
|
|
3224
3035
|
if (!compUid) return "Component";
|
|
@@ -8607,64 +8418,427 @@ function buildComponentPopulate(strapi, compUid, visited, depth, maxDepth) {
|
|
|
8607
8418
|
populate[attrName] = true;
|
|
8608
8419
|
}
|
|
8609
8420
|
}
|
|
8610
|
-
return Object.keys(populate).length > 0 ? populate : true;
|
|
8611
|
-
}
|
|
8612
|
-
function buildLosslessPopulate(strapi, uid, options = {}) {
|
|
8613
|
-
const maxDepth = options.maxDepth || 5;
|
|
8614
|
-
const schema = discoverSchema(strapi, uid);
|
|
8615
|
-
const populate = {};
|
|
8616
|
-
const visited = /* @__PURE__ */ new Set([uid]);
|
|
8617
|
-
for (const [fieldName, field] of Object.entries(schema.fields)) {
|
|
8618
|
-
switch (field.category) {
|
|
8619
|
-
case "component_single":
|
|
8620
|
-
case "component_repeatable": {
|
|
8621
|
-
if (field.componentUid) {
|
|
8622
|
-
const compPop = buildComponentPopulate(
|
|
8623
|
-
strapi,
|
|
8624
|
-
field.componentUid,
|
|
8625
|
-
visited,
|
|
8626
|
-
0,
|
|
8627
|
-
maxDepth
|
|
8628
|
-
);
|
|
8629
|
-
populate[fieldName] = typeof compPop === "object" && Object.keys(compPop).length > 0 ? { populate: compPop } : true;
|
|
8630
|
-
} else {
|
|
8631
|
-
populate[fieldName] = true;
|
|
8632
|
-
}
|
|
8633
|
-
break;
|
|
8634
|
-
}
|
|
8635
|
-
case "dynamiczone": {
|
|
8636
|
-
const dzComponents = field.dzComponents || [];
|
|
8637
|
-
if (dzComponents.length > 0) {
|
|
8638
|
-
const onPopulate = {};
|
|
8639
|
-
for (const compUid of dzComponents) {
|
|
8640
|
-
const compPop = buildComponentPopulate(
|
|
8641
|
-
strapi,
|
|
8642
|
-
compUid,
|
|
8643
|
-
visited,
|
|
8644
|
-
0,
|
|
8645
|
-
maxDepth
|
|
8646
|
-
);
|
|
8647
|
-
onPopulate[compUid] = typeof compPop === "object" && Object.keys(compPop).length > 0 ? { populate: compPop } : true;
|
|
8648
|
-
}
|
|
8649
|
-
populate[fieldName] = { on: onPopulate };
|
|
8650
|
-
} else {
|
|
8651
|
-
populate[fieldName] = { populate: "*" };
|
|
8421
|
+
return Object.keys(populate).length > 0 ? populate : true;
|
|
8422
|
+
}
|
|
8423
|
+
function buildLosslessPopulate(strapi, uid, options = {}) {
|
|
8424
|
+
const maxDepth = options.maxDepth || 5;
|
|
8425
|
+
const schema = discoverSchema(strapi, uid);
|
|
8426
|
+
const populate = {};
|
|
8427
|
+
const visited = /* @__PURE__ */ new Set([uid]);
|
|
8428
|
+
for (const [fieldName, field] of Object.entries(schema.fields)) {
|
|
8429
|
+
switch (field.category) {
|
|
8430
|
+
case "component_single":
|
|
8431
|
+
case "component_repeatable": {
|
|
8432
|
+
if (field.componentUid) {
|
|
8433
|
+
const compPop = buildComponentPopulate(
|
|
8434
|
+
strapi,
|
|
8435
|
+
field.componentUid,
|
|
8436
|
+
visited,
|
|
8437
|
+
0,
|
|
8438
|
+
maxDepth
|
|
8439
|
+
);
|
|
8440
|
+
populate[fieldName] = typeof compPop === "object" && Object.keys(compPop).length > 0 ? { populate: compPop } : true;
|
|
8441
|
+
} else {
|
|
8442
|
+
populate[fieldName] = true;
|
|
8443
|
+
}
|
|
8444
|
+
break;
|
|
8445
|
+
}
|
|
8446
|
+
case "dynamiczone": {
|
|
8447
|
+
const dzComponents = field.dzComponents || [];
|
|
8448
|
+
if (dzComponents.length > 0) {
|
|
8449
|
+
const onPopulate = {};
|
|
8450
|
+
for (const compUid of dzComponents) {
|
|
8451
|
+
const compPop = buildComponentPopulate(
|
|
8452
|
+
strapi,
|
|
8453
|
+
compUid,
|
|
8454
|
+
visited,
|
|
8455
|
+
0,
|
|
8456
|
+
maxDepth
|
|
8457
|
+
);
|
|
8458
|
+
onPopulate[compUid] = typeof compPop === "object" && Object.keys(compPop).length > 0 ? { populate: compPop } : true;
|
|
8459
|
+
}
|
|
8460
|
+
populate[fieldName] = { on: onPopulate };
|
|
8461
|
+
} else {
|
|
8462
|
+
populate[fieldName] = { populate: "*" };
|
|
8463
|
+
}
|
|
8464
|
+
break;
|
|
8465
|
+
}
|
|
8466
|
+
case "relation_to_one":
|
|
8467
|
+
case "relation_to_many": {
|
|
8468
|
+
populate[fieldName] = true;
|
|
8469
|
+
break;
|
|
8470
|
+
}
|
|
8471
|
+
case "media_single":
|
|
8472
|
+
case "media_to_many": {
|
|
8473
|
+
populate[fieldName] = true;
|
|
8474
|
+
break;
|
|
8475
|
+
}
|
|
8476
|
+
}
|
|
8477
|
+
}
|
|
8478
|
+
return Object.keys(populate).length > 0 ? populate : void 0;
|
|
8479
|
+
}
|
|
8480
|
+
const METADATA_COLUMN_KEY = "__bulk_updater_metadata";
|
|
8481
|
+
const CURRENT_SCHEMA_VERSION = 1;
|
|
8482
|
+
function escapeCsvCell(val) {
|
|
8483
|
+
if (val === null || val === void 0) return "";
|
|
8484
|
+
let str = "";
|
|
8485
|
+
if (val instanceof Date) {
|
|
8486
|
+
str = val.toISOString();
|
|
8487
|
+
} else if (typeof val === "object") {
|
|
8488
|
+
str = JSON.stringify(val);
|
|
8489
|
+
} else {
|
|
8490
|
+
str = String(val);
|
|
8491
|
+
}
|
|
8492
|
+
str = str.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
8493
|
+
if (/^[=+@-]/.test(str)) {
|
|
8494
|
+
str = `'${str}`;
|
|
8495
|
+
}
|
|
8496
|
+
if (str.includes(",") || str.includes('"') || str.includes("\n") || str.includes("\r")) {
|
|
8497
|
+
return `"${str.replace(/"/g, '""')}"`;
|
|
8498
|
+
}
|
|
8499
|
+
return str;
|
|
8500
|
+
}
|
|
8501
|
+
function parseCsvToMatrix(csvText) {
|
|
8502
|
+
if (!csvText) return [];
|
|
8503
|
+
let text = csvText;
|
|
8504
|
+
if (text.charCodeAt(0) === 65279) {
|
|
8505
|
+
text = text.slice(1);
|
|
8506
|
+
}
|
|
8507
|
+
const rows = [];
|
|
8508
|
+
let currentRow = [];
|
|
8509
|
+
let currentCell = "";
|
|
8510
|
+
let inQuotes = false;
|
|
8511
|
+
let cellIsQuoted = false;
|
|
8512
|
+
for (let i = 0; i < text.length; i++) {
|
|
8513
|
+
const char = text[i];
|
|
8514
|
+
const nextChar = text[i + 1];
|
|
8515
|
+
if (inQuotes) {
|
|
8516
|
+
if (char === '"') {
|
|
8517
|
+
if (nextChar === '"') {
|
|
8518
|
+
currentCell += '"';
|
|
8519
|
+
i++;
|
|
8520
|
+
} else {
|
|
8521
|
+
inQuotes = false;
|
|
8522
|
+
}
|
|
8523
|
+
} else {
|
|
8524
|
+
currentCell += char;
|
|
8525
|
+
}
|
|
8526
|
+
} else {
|
|
8527
|
+
if (char === '"' && !cellIsQuoted && currentCell.trim() === "") {
|
|
8528
|
+
inQuotes = true;
|
|
8529
|
+
cellIsQuoted = true;
|
|
8530
|
+
currentCell = "";
|
|
8531
|
+
} else if (char === ",") {
|
|
8532
|
+
currentRow.push(cellIsQuoted ? currentCell : currentCell.trim());
|
|
8533
|
+
currentCell = "";
|
|
8534
|
+
cellIsQuoted = false;
|
|
8535
|
+
} else if (char === "\r") {
|
|
8536
|
+
if (nextChar === "\n") {
|
|
8537
|
+
i++;
|
|
8538
|
+
}
|
|
8539
|
+
currentRow.push(cellIsQuoted ? currentCell : currentCell.trim());
|
|
8540
|
+
if (currentRow.length > 1 || currentRow.length === 1 && currentRow[0] !== "") {
|
|
8541
|
+
rows.push(currentRow);
|
|
8542
|
+
}
|
|
8543
|
+
currentRow = [];
|
|
8544
|
+
currentCell = "";
|
|
8545
|
+
cellIsQuoted = false;
|
|
8546
|
+
} else if (char === "\n") {
|
|
8547
|
+
currentRow.push(cellIsQuoted ? currentCell : currentCell.trim());
|
|
8548
|
+
if (currentRow.length > 1 || currentRow.length === 1 && currentRow[0] !== "") {
|
|
8549
|
+
rows.push(currentRow);
|
|
8550
|
+
}
|
|
8551
|
+
currentRow = [];
|
|
8552
|
+
currentCell = "";
|
|
8553
|
+
cellIsQuoted = false;
|
|
8554
|
+
} else {
|
|
8555
|
+
if (!(cellIsQuoted && !inQuotes && (char === " " || char === " "))) {
|
|
8556
|
+
currentCell += char;
|
|
8557
|
+
}
|
|
8558
|
+
}
|
|
8559
|
+
}
|
|
8560
|
+
}
|
|
8561
|
+
if (currentCell !== "" || cellIsQuoted || currentRow.length > 0) {
|
|
8562
|
+
currentRow.push(cellIsQuoted ? currentCell : currentCell.trim());
|
|
8563
|
+
if (currentRow.length > 1 || currentRow.length === 1 && currentRow[0] !== "") {
|
|
8564
|
+
rows.push(currentRow);
|
|
8565
|
+
}
|
|
8566
|
+
}
|
|
8567
|
+
return rows;
|
|
8568
|
+
}
|
|
8569
|
+
function parseCsvToRecords(csvText) {
|
|
8570
|
+
const matrix = parseCsvToMatrix(csvText);
|
|
8571
|
+
if (matrix.length < 2) return [];
|
|
8572
|
+
const headers = matrix[0].map((h) => h.trim());
|
|
8573
|
+
const records = [];
|
|
8574
|
+
for (let i = 1; i < matrix.length; i++) {
|
|
8575
|
+
const row = matrix[i];
|
|
8576
|
+
const record = {};
|
|
8577
|
+
headers.forEach((h, idx) => {
|
|
8578
|
+
let val = row[idx] !== void 0 ? row[idx] : "";
|
|
8579
|
+
if (typeof val === "string" && /^'[=+@-]/.test(val)) {
|
|
8580
|
+
val = val.substring(1);
|
|
8581
|
+
}
|
|
8582
|
+
record[h] = val;
|
|
8583
|
+
});
|
|
8584
|
+
records.push(record);
|
|
8585
|
+
}
|
|
8586
|
+
return records;
|
|
8587
|
+
}
|
|
8588
|
+
function serializeFieldValue(value, field) {
|
|
8589
|
+
if (value === null || value === void 0) {
|
|
8590
|
+
return "";
|
|
8591
|
+
}
|
|
8592
|
+
switch (field.category) {
|
|
8593
|
+
case "scalar": {
|
|
8594
|
+
if (value instanceof Date) {
|
|
8595
|
+
return value.toISOString();
|
|
8596
|
+
}
|
|
8597
|
+
return String(value);
|
|
8598
|
+
}
|
|
8599
|
+
case "blocks":
|
|
8600
|
+
case "json": {
|
|
8601
|
+
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
8602
|
+
}
|
|
8603
|
+
case "component_single": {
|
|
8604
|
+
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
8605
|
+
}
|
|
8606
|
+
case "component_repeatable": {
|
|
8607
|
+
if (Array.isArray(value)) {
|
|
8608
|
+
return JSON.stringify(value);
|
|
8609
|
+
}
|
|
8610
|
+
return typeof value === "object" ? JSON.stringify([value]) : "";
|
|
8611
|
+
}
|
|
8612
|
+
case "dynamiczone": {
|
|
8613
|
+
if (Array.isArray(value)) {
|
|
8614
|
+
const cleaned = value.map((block) => {
|
|
8615
|
+
if (block && typeof block === "object") {
|
|
8616
|
+
return { ...block };
|
|
8617
|
+
}
|
|
8618
|
+
return block;
|
|
8619
|
+
});
|
|
8620
|
+
return JSON.stringify(cleaned);
|
|
8621
|
+
}
|
|
8622
|
+
return "";
|
|
8623
|
+
}
|
|
8624
|
+
case "relation_to_one": {
|
|
8625
|
+
if (typeof value === "object" && value !== null) {
|
|
8626
|
+
const docId = value.documentId || value.id || "";
|
|
8627
|
+
return JSON.stringify({
|
|
8628
|
+
documentId: String(docId),
|
|
8629
|
+
title: value.title || value.name || value.slug || void 0
|
|
8630
|
+
});
|
|
8631
|
+
}
|
|
8632
|
+
return String(value);
|
|
8633
|
+
}
|
|
8634
|
+
case "relation_to_many": {
|
|
8635
|
+
if (Array.isArray(value)) {
|
|
8636
|
+
const list = value.map((item) => {
|
|
8637
|
+
if (typeof item === "object" && item !== null) {
|
|
8638
|
+
const docId = item.documentId || item.id || "";
|
|
8639
|
+
return {
|
|
8640
|
+
documentId: String(docId),
|
|
8641
|
+
title: item.title || item.name || item.slug || void 0
|
|
8642
|
+
};
|
|
8643
|
+
}
|
|
8644
|
+
return { documentId: String(item) };
|
|
8645
|
+
});
|
|
8646
|
+
return JSON.stringify(list);
|
|
8647
|
+
}
|
|
8648
|
+
return "";
|
|
8649
|
+
}
|
|
8650
|
+
case "media_single": {
|
|
8651
|
+
if (typeof value === "object" && value !== null) {
|
|
8652
|
+
return JSON.stringify({
|
|
8653
|
+
id: value.id,
|
|
8654
|
+
documentId: value.documentId,
|
|
8655
|
+
name: value.name,
|
|
8656
|
+
url: value.url,
|
|
8657
|
+
mime: value.mime
|
|
8658
|
+
});
|
|
8659
|
+
}
|
|
8660
|
+
return String(value);
|
|
8661
|
+
}
|
|
8662
|
+
case "media_to_many": {
|
|
8663
|
+
if (Array.isArray(value)) {
|
|
8664
|
+
const list = value.map((file) => {
|
|
8665
|
+
if (typeof file === "object" && file !== null) {
|
|
8666
|
+
return {
|
|
8667
|
+
id: file.id,
|
|
8668
|
+
documentId: file.documentId,
|
|
8669
|
+
name: file.name,
|
|
8670
|
+
url: file.url,
|
|
8671
|
+
mime: file.mime
|
|
8672
|
+
};
|
|
8673
|
+
}
|
|
8674
|
+
return file;
|
|
8675
|
+
});
|
|
8676
|
+
return JSON.stringify(list);
|
|
8677
|
+
}
|
|
8678
|
+
return "";
|
|
8679
|
+
}
|
|
8680
|
+
default:
|
|
8681
|
+
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
8682
|
+
}
|
|
8683
|
+
}
|
|
8684
|
+
function serializeDocumentsToCsv(strapi, uid, documents, options = {}) {
|
|
8685
|
+
const schema = options.schema || discoverSchema(strapi, uid);
|
|
8686
|
+
const exportMode = normalizeExportMode(options.exportMode);
|
|
8687
|
+
const plan = buildExportColumnPlan(strapi, uid, documents || [], { schema, exportMode });
|
|
8688
|
+
const headers = [METADATA_COLUMN_KEY, ...plan.headers];
|
|
8689
|
+
const lines = [];
|
|
8690
|
+
lines.push(headers.map(escapeCsvCell).join(","));
|
|
8691
|
+
const exportedAt = options.exportedAt || (/* @__PURE__ */ new Date()).toISOString();
|
|
8692
|
+
for (const doc of documents) {
|
|
8693
|
+
const rowValues = [];
|
|
8694
|
+
const flattenedMeta = extractRowFlattenedMetadata(plan, doc);
|
|
8695
|
+
const metadata = {
|
|
8696
|
+
schemaVersion: CURRENT_SCHEMA_VERSION,
|
|
8697
|
+
contentType: uid,
|
|
8698
|
+
documentId: String(doc.documentId || doc.id || ""),
|
|
8699
|
+
locale: doc.locale || void 0,
|
|
8700
|
+
exportedAt,
|
|
8701
|
+
...flattenedMeta.exportMode ? { exportMode: flattenedMeta.exportMode } : {},
|
|
8702
|
+
...flattenedMeta.targetObjectField ? { targetObjectField: flattenedMeta.targetObjectField } : {},
|
|
8703
|
+
...flattenedMeta.targetObjectState ? { targetObjectState: flattenedMeta.targetObjectState } : {},
|
|
8704
|
+
...flattenedMeta.applicableFields ? { applicableFields: flattenedMeta.applicableFields } : {},
|
|
8705
|
+
...flattenedMeta.flatTypes ? { flatTypes: flattenedMeta.flatTypes } : {}
|
|
8706
|
+
};
|
|
8707
|
+
rowValues.push(escapeCsvCell(metadata));
|
|
8708
|
+
for (const col of plan.columns) {
|
|
8709
|
+
const cellValue = serializePlannedCell(col, doc, serializeFieldValue);
|
|
8710
|
+
rowValues.push(escapeCsvCell(cellValue));
|
|
8711
|
+
}
|
|
8712
|
+
lines.push(rowValues.join(","));
|
|
8713
|
+
}
|
|
8714
|
+
const csvContent = "\uFEFF" + lines.join("\r\n") + "\r\n";
|
|
8715
|
+
return {
|
|
8716
|
+
csvContent,
|
|
8717
|
+
headers,
|
|
8718
|
+
count: documents.length
|
|
8719
|
+
};
|
|
8720
|
+
}
|
|
8721
|
+
function extractMetadataFromRow(row) {
|
|
8722
|
+
if (!row || typeof row !== "object") return null;
|
|
8723
|
+
const rawMeta = row[METADATA_COLUMN_KEY] || row["__bulk_updater_metadata"];
|
|
8724
|
+
if (!rawMeta) return null;
|
|
8725
|
+
if (typeof rawMeta === "object") {
|
|
8726
|
+
return rawMeta;
|
|
8727
|
+
}
|
|
8728
|
+
if (typeof rawMeta === "string") {
|
|
8729
|
+
try {
|
|
8730
|
+
const parsed = JSON.parse(rawMeta);
|
|
8731
|
+
if (parsed && typeof parsed === "object") {
|
|
8732
|
+
return parsed;
|
|
8733
|
+
}
|
|
8734
|
+
} catch {
|
|
8735
|
+
}
|
|
8736
|
+
}
|
|
8737
|
+
return null;
|
|
8738
|
+
}
|
|
8739
|
+
function sanitizeUserErrorMessage(msg) {
|
|
8740
|
+
if (msg === null || msg === void 0) return "Error processing row";
|
|
8741
|
+
const raw = typeof msg === "string" ? msg : String(msg);
|
|
8742
|
+
const cleanedLines = raw.split(/\r?\n/).filter((line) => !/^\s+at\s+.+(\(.+\)|:\d+:\d+)\s*$/.test(line)).map((line) => line.trim()).filter(Boolean);
|
|
8743
|
+
const result = cleanedLines.join(" ").trim();
|
|
8744
|
+
return result || "Error processing row";
|
|
8745
|
+
}
|
|
8746
|
+
function resolveErrorColumnName(existingHeaders) {
|
|
8747
|
+
const existingLower = new Set(
|
|
8748
|
+
existingHeaders.map((h) => String(h || "").trim().toLowerCase())
|
|
8749
|
+
);
|
|
8750
|
+
if (!existingLower.has("error")) {
|
|
8751
|
+
return "Error";
|
|
8752
|
+
}
|
|
8753
|
+
if (!existingLower.has("import error")) {
|
|
8754
|
+
return "Import Error";
|
|
8755
|
+
}
|
|
8756
|
+
let suffix = 2;
|
|
8757
|
+
while (existingLower.has(`import error (${suffix})`)) {
|
|
8758
|
+
suffix++;
|
|
8759
|
+
}
|
|
8760
|
+
return `Import Error (${suffix})`;
|
|
8761
|
+
}
|
|
8762
|
+
function formatErrorCsvFilename(collectionTypeOrUid, date = /* @__PURE__ */ new Date()) {
|
|
8763
|
+
const rawSegment = collectionTypeOrUid ? String(collectionTypeOrUid).split(".").pop() || String(collectionTypeOrUid) : "collection";
|
|
8764
|
+
const safeType = rawSegment.replace(/[^a-zA-Z0-9_-]+/g, "_").replace(/^_+|_+$/g, "") || "collection";
|
|
8765
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
8766
|
+
const stamp = `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}_${pad(
|
|
8767
|
+
date.getHours()
|
|
8768
|
+
)}${pad(date.getMinutes())}${pad(date.getSeconds())}`;
|
|
8769
|
+
return `error_${safeType}_${stamp}.csv`;
|
|
8770
|
+
}
|
|
8771
|
+
function buildErrorCsv(options) {
|
|
8772
|
+
const { headers, failedRows } = options || {};
|
|
8773
|
+
if (!Array.isArray(failedRows) || failedRows.length === 0) {
|
|
8774
|
+
return {
|
|
8775
|
+
csvContent: "",
|
|
8776
|
+
headers: headers ? [...headers] : [],
|
|
8777
|
+
errorColumnName: "Error",
|
|
8778
|
+
errorCount: 0
|
|
8779
|
+
};
|
|
8780
|
+
}
|
|
8781
|
+
const groupedByRow = /* @__PURE__ */ new Map();
|
|
8782
|
+
for (const item of failedRows) {
|
|
8783
|
+
if (!item || typeof item !== "object") continue;
|
|
8784
|
+
const rowNum = typeof item.row === "number" && !isNaN(item.row) ? item.row : groupedByRow.size + 2;
|
|
8785
|
+
const msg = sanitizeUserErrorMessage(item.error);
|
|
8786
|
+
const existing = groupedByRow.get(rowNum);
|
|
8787
|
+
if (existing) {
|
|
8788
|
+
if (!existing.messages.includes(msg)) {
|
|
8789
|
+
existing.messages.push(msg);
|
|
8790
|
+
}
|
|
8791
|
+
} else {
|
|
8792
|
+
groupedByRow.set(rowNum, {
|
|
8793
|
+
row: rowNum,
|
|
8794
|
+
documentId: item.documentId,
|
|
8795
|
+
messages: [msg],
|
|
8796
|
+
originalRow: item.originalRow && typeof item.originalRow === "object" ? item.originalRow : {}
|
|
8797
|
+
});
|
|
8798
|
+
}
|
|
8799
|
+
}
|
|
8800
|
+
const orderedEntries = Array.from(groupedByRow.values()).sort((a, b) => a.row - b.row);
|
|
8801
|
+
if (orderedEntries.length === 0) {
|
|
8802
|
+
return {
|
|
8803
|
+
csvContent: "",
|
|
8804
|
+
headers: headers ? [...headers] : [],
|
|
8805
|
+
errorColumnName: "Error",
|
|
8806
|
+
errorCount: 0
|
|
8807
|
+
};
|
|
8808
|
+
}
|
|
8809
|
+
let baseHeaders = [];
|
|
8810
|
+
if (Array.isArray(headers) && headers.length > 0) {
|
|
8811
|
+
baseHeaders = [...headers];
|
|
8812
|
+
} else {
|
|
8813
|
+
const keySet = /* @__PURE__ */ new Set();
|
|
8814
|
+
for (const entry of orderedEntries) {
|
|
8815
|
+
for (const k of Object.keys(entry.originalRow || {})) {
|
|
8816
|
+
if (!keySet.has(k)) {
|
|
8817
|
+
keySet.add(k);
|
|
8818
|
+
baseHeaders.push(k);
|
|
8652
8819
|
}
|
|
8653
|
-
break;
|
|
8654
|
-
}
|
|
8655
|
-
case "relation_to_one":
|
|
8656
|
-
case "relation_to_many": {
|
|
8657
|
-
populate[fieldName] = true;
|
|
8658
|
-
break;
|
|
8659
|
-
}
|
|
8660
|
-
case "media_single":
|
|
8661
|
-
case "media_to_many": {
|
|
8662
|
-
populate[fieldName] = true;
|
|
8663
|
-
break;
|
|
8664
8820
|
}
|
|
8665
8821
|
}
|
|
8666
8822
|
}
|
|
8667
|
-
|
|
8823
|
+
const errorColumnName = resolveErrorColumnName(baseHeaders);
|
|
8824
|
+
const finalHeaders = [...baseHeaders, errorColumnName];
|
|
8825
|
+
const lines = [];
|
|
8826
|
+
lines.push(finalHeaders.map(escapeCsvCell).join(","));
|
|
8827
|
+
for (const entry of orderedEntries) {
|
|
8828
|
+
const cells = baseHeaders.map((h) => {
|
|
8829
|
+
const val = entry.originalRow ? entry.originalRow[h] : "";
|
|
8830
|
+
return escapeCsvCell(val === void 0 || val === null ? "" : val);
|
|
8831
|
+
});
|
|
8832
|
+
cells.push(escapeCsvCell(entry.messages.join("; ")));
|
|
8833
|
+
lines.push(cells.join(","));
|
|
8834
|
+
}
|
|
8835
|
+
const csvContent = "\uFEFF" + lines.join("\r\n") + "\r\n";
|
|
8836
|
+
return {
|
|
8837
|
+
csvContent,
|
|
8838
|
+
headers: finalHeaders,
|
|
8839
|
+
errorColumnName,
|
|
8840
|
+
errorCount: orderedEntries.length
|
|
8841
|
+
};
|
|
8668
8842
|
}
|
|
8669
8843
|
function normalizeVal(val) {
|
|
8670
8844
|
if (val === null || val === void 0) return null;
|
|
@@ -9242,13 +9416,44 @@ function formatValueDisplay(val) {
|
|
|
9242
9416
|
}
|
|
9243
9417
|
async function executeUpdateMutation(strapi, ct, uid, existing, row, rowNumber, options, effectiveLocale, isEditing, isDraftAndPublish, publishedCounterparts, stats, actionLabel = "Updated") {
|
|
9244
9418
|
const resolvedDocId = existing.documentId;
|
|
9245
|
-
const
|
|
9419
|
+
const mutationPlan = await prepareCsvRowMutation(
|
|
9246
9420
|
strapi,
|
|
9247
9421
|
uid,
|
|
9248
9422
|
existing,
|
|
9249
9423
|
row,
|
|
9250
|
-
{
|
|
9424
|
+
{
|
|
9425
|
+
isPartialUpdate: options.mode === "update",
|
|
9426
|
+
isCreate: false,
|
|
9427
|
+
expectedExportMode: options.expectedExportMode,
|
|
9428
|
+
docIdForError: resolvedDocId
|
|
9429
|
+
}
|
|
9251
9430
|
);
|
|
9431
|
+
const updatePayload = mutationPlan.payload;
|
|
9432
|
+
if (mutationPlan.errors.length > 0) {
|
|
9433
|
+
const errMsg = mutationPlan.errors[0];
|
|
9434
|
+
stats.skipped++;
|
|
9435
|
+
if (stats.failed !== void 0) stats.failed++;
|
|
9436
|
+
recordFailedCsvRow(
|
|
9437
|
+
stats,
|
|
9438
|
+
rowNumber,
|
|
9439
|
+
row,
|
|
9440
|
+
errMsg,
|
|
9441
|
+
resolvedDocId,
|
|
9442
|
+
options.originalHeaders
|
|
9443
|
+
);
|
|
9444
|
+
stats.diffs.push({
|
|
9445
|
+
uid,
|
|
9446
|
+
collectionName: ct.info?.displayName || uid,
|
|
9447
|
+
documentId: resolvedDocId,
|
|
9448
|
+
label: existing.title || existing.name || `Entry #${resolvedDocId}`,
|
|
9449
|
+
attribute: mutationPlan.targetObjectField || "",
|
|
9450
|
+
oldValue: null,
|
|
9451
|
+
newValue: null,
|
|
9452
|
+
status: "error",
|
|
9453
|
+
message: errMsg
|
|
9454
|
+
});
|
|
9455
|
+
return;
|
|
9456
|
+
}
|
|
9252
9457
|
if (Object.keys(updatePayload).length === 0) {
|
|
9253
9458
|
stats.skipped++;
|
|
9254
9459
|
stats.diffs.push({
|
|
@@ -9300,46 +9505,6 @@ async function executeUpdateMutation(strapi, ct, uid, existing, row, rowNumber,
|
|
|
9300
9505
|
publishStrategy: options.publishStrategy || "safe",
|
|
9301
9506
|
unreviewedConsent: options.unreviewedConsent
|
|
9302
9507
|
});
|
|
9303
|
-
const rowMeta = extractMetadataFromRow(row);
|
|
9304
|
-
const schemaValidation = validateRecordSchemaConstraints(
|
|
9305
|
-
strapi,
|
|
9306
|
-
uid,
|
|
9307
|
-
existing,
|
|
9308
|
-
updatePayload,
|
|
9309
|
-
row,
|
|
9310
|
-
{
|
|
9311
|
-
isCreate: false,
|
|
9312
|
-
rowMeta: rowMeta ? {
|
|
9313
|
-
applicableFields: rowMeta.applicableFields,
|
|
9314
|
-
flatTypes: rowMeta.flatTypes
|
|
9315
|
-
} : null
|
|
9316
|
-
}
|
|
9317
|
-
);
|
|
9318
|
-
if (schemaValidation.errors.length > 0) {
|
|
9319
|
-
const errMsg = schemaValidation.errors[0];
|
|
9320
|
-
stats.skipped++;
|
|
9321
|
-
if (stats.failed !== void 0) stats.failed++;
|
|
9322
|
-
recordFailedCsvRow(
|
|
9323
|
-
stats,
|
|
9324
|
-
rowNumber,
|
|
9325
|
-
row,
|
|
9326
|
-
errMsg,
|
|
9327
|
-
resolvedDocId,
|
|
9328
|
-
options.originalHeaders
|
|
9329
|
-
);
|
|
9330
|
-
stats.diffs.push({
|
|
9331
|
-
uid,
|
|
9332
|
-
collectionName: ct.info?.displayName || uid,
|
|
9333
|
-
documentId: resolvedDocId,
|
|
9334
|
-
label: existing.title || existing.name || `Entry #${resolvedDocId}`,
|
|
9335
|
-
attribute: "",
|
|
9336
|
-
oldValue: null,
|
|
9337
|
-
newValue: null,
|
|
9338
|
-
status: "error",
|
|
9339
|
-
message: errMsg
|
|
9340
|
-
});
|
|
9341
|
-
return;
|
|
9342
|
-
}
|
|
9343
9508
|
const proposedEntity = deepMergeEntities(existing, updatePayload);
|
|
9344
9509
|
const valResult = await validateEntityQuotas(strapi, uid, proposedEntity);
|
|
9345
9510
|
if (!valResult.isValid) {
|
|
@@ -9422,30 +9587,21 @@ async function executeUpdateMutation(strapi, ct, uid, existing, row, rowNumber,
|
|
|
9422
9587
|
}
|
|
9423
9588
|
}
|
|
9424
9589
|
async function executeCreateMutation(strapi, ct, uid, docId, row, rowNumber, options, effectiveLocale, isEditing, isDraftAndPublish, stats) {
|
|
9425
|
-
const
|
|
9426
|
-
strapi,
|
|
9427
|
-
uid,
|
|
9428
|
-
null,
|
|
9429
|
-
row,
|
|
9430
|
-
{ isPartialUpdate: false }
|
|
9431
|
-
);
|
|
9432
|
-
const rowMeta = extractMetadataFromRow(row);
|
|
9433
|
-
const schemaValidation = validateRecordSchemaConstraints(
|
|
9590
|
+
const mutationPlan = await prepareCsvRowMutation(
|
|
9434
9591
|
strapi,
|
|
9435
9592
|
uid,
|
|
9436
9593
|
null,
|
|
9437
|
-
createPayload,
|
|
9438
9594
|
row,
|
|
9439
9595
|
{
|
|
9596
|
+
isPartialUpdate: false,
|
|
9440
9597
|
isCreate: true,
|
|
9441
|
-
|
|
9442
|
-
|
|
9443
|
-
flatTypes: rowMeta.flatTypes
|
|
9444
|
-
} : null
|
|
9598
|
+
expectedExportMode: options.expectedExportMode,
|
|
9599
|
+
docIdForError: docId || `row-${rowNumber}`
|
|
9445
9600
|
}
|
|
9446
9601
|
);
|
|
9447
|
-
|
|
9448
|
-
|
|
9602
|
+
const createPayload = mutationPlan.payload;
|
|
9603
|
+
if (mutationPlan.errors.length > 0) {
|
|
9604
|
+
const errMsg = mutationPlan.errors[0];
|
|
9449
9605
|
stats.skipped++;
|
|
9450
9606
|
if (stats.failed !== void 0) stats.failed++;
|
|
9451
9607
|
recordFailedCsvRow(
|
|
@@ -9571,6 +9727,40 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
|
|
|
9571
9727
|
failedRows: [],
|
|
9572
9728
|
diffs: []
|
|
9573
9729
|
};
|
|
9730
|
+
const batchModeInfo = detectCsvBatchExportMode(rows);
|
|
9731
|
+
if (batchModeInfo.error) {
|
|
9732
|
+
for (let i = 0; i < rows.length; i++) {
|
|
9733
|
+
const rowNumber = startRowNumber + i;
|
|
9734
|
+
const row = rows[i];
|
|
9735
|
+
const docId = extractDocId(row);
|
|
9736
|
+
stats.skipped++;
|
|
9737
|
+
if (stats.failed !== void 0) stats.failed++;
|
|
9738
|
+
recordFailedCsvRow(
|
|
9739
|
+
stats,
|
|
9740
|
+
rowNumber,
|
|
9741
|
+
row,
|
|
9742
|
+
batchModeInfo.error,
|
|
9743
|
+
docId || void 0,
|
|
9744
|
+
originalHeaders
|
|
9745
|
+
);
|
|
9746
|
+
stats.diffs.push({
|
|
9747
|
+
uid,
|
|
9748
|
+
collectionName: ct.info?.displayName || uid,
|
|
9749
|
+
documentId: docId || `(Row #${rowNumber})`,
|
|
9750
|
+
label: `Row #${rowNumber}`,
|
|
9751
|
+
attribute: "__bulk_updater_metadata",
|
|
9752
|
+
oldValue: null,
|
|
9753
|
+
newValue: null,
|
|
9754
|
+
status: "error",
|
|
9755
|
+
message: batchModeInfo.error
|
|
9756
|
+
});
|
|
9757
|
+
}
|
|
9758
|
+
return stats;
|
|
9759
|
+
}
|
|
9760
|
+
const rowOptions = {
|
|
9761
|
+
...options,
|
|
9762
|
+
expectedExportMode: batchModeInfo.mode
|
|
9763
|
+
};
|
|
9574
9764
|
for (let i = 0; i < rows.length; i++) {
|
|
9575
9765
|
const rowNumber = startRowNumber + i;
|
|
9576
9766
|
const row = rows[i];
|
|
@@ -9623,7 +9813,7 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
|
|
|
9623
9813
|
existing,
|
|
9624
9814
|
row,
|
|
9625
9815
|
rowNumber,
|
|
9626
|
-
|
|
9816
|
+
rowOptions,
|
|
9627
9817
|
effectiveLocale,
|
|
9628
9818
|
isEditing,
|
|
9629
9819
|
isDraftAndPublish,
|
|
@@ -9644,7 +9834,7 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
|
|
|
9644
9834
|
existing,
|
|
9645
9835
|
row,
|
|
9646
9836
|
rowNumber,
|
|
9647
|
-
|
|
9837
|
+
rowOptions,
|
|
9648
9838
|
effectiveLocale,
|
|
9649
9839
|
isEditing,
|
|
9650
9840
|
isDraftAndPublish,
|
|
@@ -9660,7 +9850,7 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
|
|
|
9660
9850
|
docId,
|
|
9661
9851
|
row,
|
|
9662
9852
|
rowNumber,
|
|
9663
|
-
|
|
9853
|
+
rowOptions,
|
|
9664
9854
|
effectiveLocale,
|
|
9665
9855
|
isEditing,
|
|
9666
9856
|
isDraftAndPublish,
|
|
@@ -9680,7 +9870,7 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
|
|
|
9680
9870
|
existing,
|
|
9681
9871
|
row,
|
|
9682
9872
|
rowNumber,
|
|
9683
|
-
|
|
9873
|
+
rowOptions,
|
|
9684
9874
|
effectiveLocale,
|
|
9685
9875
|
isEditing,
|
|
9686
9876
|
isDraftAndPublish,
|
|
@@ -9696,7 +9886,7 @@ async function processCsvRows(strapi, ct, uid, rows, startRowNumber, options) {
|
|
|
9696
9886
|
docId,
|
|
9697
9887
|
row,
|
|
9698
9888
|
rowNumber,
|
|
9699
|
-
|
|
9889
|
+
rowOptions,
|
|
9700
9890
|
effectiveLocale,
|
|
9701
9891
|
isEditing,
|
|
9702
9892
|
isDraftAndPublish,
|
|
@@ -9877,6 +10067,7 @@ const csvService = ({ strapi }) => ({
|
|
|
9877
10067
|
}
|
|
9878
10068
|
const schema = discoverSchema(strapi, uid);
|
|
9879
10069
|
const deepPopulate = buildLosslessPopulate(strapi, uid);
|
|
10070
|
+
const exportMode = normalizeExportMode(options.exportMode);
|
|
9880
10071
|
const filterMeta = buildCollectionQueryFilters(strapi, ct, {
|
|
9881
10072
|
attribute: options.attribute,
|
|
9882
10073
|
searchAttribute: options.searchAttribute || options.attribute,
|
|
@@ -9886,12 +10077,13 @@ const csvService = ({ strapi }) => ({
|
|
|
9886
10077
|
documentIds: options.documentIds
|
|
9887
10078
|
});
|
|
9888
10079
|
if (!filterMeta.colHasAttribute && filterMeta.normOp && filterMeta.normOp !== "none") {
|
|
9889
|
-
const emptySerialized = serializeDocumentsToCsv(strapi, uid, [], { schema });
|
|
10080
|
+
const emptySerialized = serializeDocumentsToCsv(strapi, uid, [], { schema, exportMode });
|
|
9890
10081
|
return {
|
|
9891
10082
|
filename: `${ct.info?.singularName || uid}_export_${(/* @__PURE__ */ new Date()).toISOString().slice(0, 10)}.csv`,
|
|
9892
10083
|
csvContent: emptySerialized.csvContent,
|
|
9893
10084
|
count: 0,
|
|
9894
|
-
fields: emptySerialized.headers
|
|
10085
|
+
fields: emptySerialized.headers,
|
|
10086
|
+
exportMode
|
|
9895
10087
|
};
|
|
9896
10088
|
}
|
|
9897
10089
|
const filters = filterMeta.filters;
|
|
@@ -10002,13 +10194,18 @@ const csvService = ({ strapi }) => ({
|
|
|
10002
10194
|
await new Promise((resolve) => setImmediate(resolve));
|
|
10003
10195
|
}
|
|
10004
10196
|
}
|
|
10005
|
-
const serialized = serializeDocumentsToCsv(strapi, uid, exportedDocs, {
|
|
10197
|
+
const serialized = serializeDocumentsToCsv(strapi, uid, exportedDocs, {
|
|
10198
|
+
exportedAt,
|
|
10199
|
+
schema,
|
|
10200
|
+
exportMode
|
|
10201
|
+
});
|
|
10006
10202
|
const filename = `${ct.info?.singularName || uid}_export_${(/* @__PURE__ */ new Date()).toISOString().slice(0, 10)}.csv`;
|
|
10007
10203
|
return {
|
|
10008
10204
|
filename,
|
|
10009
10205
|
csvContent: serialized.csvContent,
|
|
10010
10206
|
count: serialized.count,
|
|
10011
|
-
fields: serialized.headers
|
|
10207
|
+
fields: serialized.headers,
|
|
10208
|
+
exportMode
|
|
10012
10209
|
};
|
|
10013
10210
|
},
|
|
10014
10211
|
async exportViolationsCsv(ruleId) {
|
|
@@ -10383,6 +10580,28 @@ const csvService = ({ strapi }) => ({
|
|
|
10383
10580
|
};
|
|
10384
10581
|
const errors = [];
|
|
10385
10582
|
const warnings = [];
|
|
10583
|
+
const batchModeInfo = detectCsvBatchExportMode(rows);
|
|
10584
|
+
if (batchModeInfo.error) {
|
|
10585
|
+
errors.push({
|
|
10586
|
+
row: 1,
|
|
10587
|
+
error: batchModeInfo.error
|
|
10588
|
+
});
|
|
10589
|
+
return {
|
|
10590
|
+
success: true,
|
|
10591
|
+
detectedExportMode: "invalid",
|
|
10592
|
+
totalRows: rows.length,
|
|
10593
|
+
validRows: 0,
|
|
10594
|
+
estimatedUpdates: 0,
|
|
10595
|
+
estimatedCreates: 0,
|
|
10596
|
+
lifecycleStats,
|
|
10597
|
+
hasErrors: true,
|
|
10598
|
+
hasWarnings: false,
|
|
10599
|
+
errors,
|
|
10600
|
+
warnings: [],
|
|
10601
|
+
totalErrorCount: errors.length,
|
|
10602
|
+
totalWarningCount: 0
|
|
10603
|
+
};
|
|
10604
|
+
}
|
|
10386
10605
|
const docIdsInPreflight = rows.map((r) => extractDocId(r)).filter(Boolean);
|
|
10387
10606
|
const publishedCounterparts = /* @__PURE__ */ new Map();
|
|
10388
10607
|
if (isDraftAndPublish && docIdsInPreflight.length > 0) {
|
|
@@ -10545,49 +10764,46 @@ const csvService = ({ strapi }) => ({
|
|
|
10545
10764
|
}
|
|
10546
10765
|
}
|
|
10547
10766
|
}
|
|
10548
|
-
let proposedPayload = {};
|
|
10549
|
-
try {
|
|
10550
|
-
proposedPayload = await buildDocumentUpdatePayload(
|
|
10551
|
-
strapi,
|
|
10552
|
-
uid,
|
|
10553
|
-
existing,
|
|
10554
|
-
row,
|
|
10555
|
-
{ isPartialUpdate: mode === "update" }
|
|
10556
|
-
);
|
|
10557
|
-
} catch {
|
|
10558
|
-
}
|
|
10559
|
-
const rowMeta = extractMetadataFromRow(row);
|
|
10560
10767
|
const isRowCreate = mode === "create_or_override" ? !existing : !existing && mode !== "update";
|
|
10768
|
+
let proposedPayload = {};
|
|
10561
10769
|
if (!(mode === "update" && (!docId || !existing))) {
|
|
10562
|
-
|
|
10563
|
-
|
|
10564
|
-
|
|
10565
|
-
|
|
10566
|
-
|
|
10567
|
-
|
|
10568
|
-
|
|
10569
|
-
|
|
10570
|
-
|
|
10571
|
-
|
|
10572
|
-
|
|
10573
|
-
}
|
|
10770
|
+
try {
|
|
10771
|
+
const mutationPlan = await prepareCsvRowMutation(
|
|
10772
|
+
strapi,
|
|
10773
|
+
uid,
|
|
10774
|
+
existing,
|
|
10775
|
+
row,
|
|
10776
|
+
{
|
|
10777
|
+
isPartialUpdate: mode === "update",
|
|
10778
|
+
isCreate: isRowCreate,
|
|
10779
|
+
expectedExportMode: batchModeInfo.mode,
|
|
10780
|
+
docIdForError: docId || `row-${rowNumber}`
|
|
10781
|
+
}
|
|
10782
|
+
);
|
|
10783
|
+
proposedPayload = mutationPlan.payload;
|
|
10784
|
+
for (const errStr of mutationPlan.errors) {
|
|
10785
|
+
errors.push({
|
|
10786
|
+
row: rowNumber,
|
|
10787
|
+
documentId: docId || void 0,
|
|
10788
|
+
error: errStr
|
|
10789
|
+
});
|
|
10790
|
+
rowHasError = true;
|
|
10574
10791
|
}
|
|
10575
|
-
|
|
10576
|
-
|
|
10792
|
+
for (const warnStr of mutationPlan.warnings) {
|
|
10793
|
+
warnings.push({
|
|
10794
|
+
row: rowNumber,
|
|
10795
|
+
documentId: docId || void 0,
|
|
10796
|
+
warning: warnStr
|
|
10797
|
+
});
|
|
10798
|
+
}
|
|
10799
|
+
} catch (prepErr) {
|
|
10577
10800
|
errors.push({
|
|
10578
10801
|
row: rowNumber,
|
|
10579
10802
|
documentId: docId || void 0,
|
|
10580
|
-
error:
|
|
10803
|
+
error: prepErr?.message || "Failed to reconstruct CSV payload."
|
|
10581
10804
|
});
|
|
10582
10805
|
rowHasError = true;
|
|
10583
10806
|
}
|
|
10584
|
-
for (const warnStr of schemaValidation.warnings) {
|
|
10585
|
-
warnings.push({
|
|
10586
|
-
row: rowNumber,
|
|
10587
|
-
documentId: docId || void 0,
|
|
10588
|
-
warning: warnStr
|
|
10589
|
-
});
|
|
10590
|
-
}
|
|
10591
10807
|
}
|
|
10592
10808
|
if (activeRules.length > 0) {
|
|
10593
10809
|
const proposedEntity = existing ? deepMergeEntities(existing, proposedPayload) : proposedPayload;
|
|
@@ -10607,6 +10823,7 @@ const csvService = ({ strapi }) => ({
|
|
|
10607
10823
|
}
|
|
10608
10824
|
return {
|
|
10609
10825
|
success: true,
|
|
10826
|
+
detectedExportMode: batchModeInfo.mode,
|
|
10610
10827
|
totalRows: rows.length,
|
|
10611
10828
|
validRows,
|
|
10612
10829
|
estimatedUpdates,
|
|
@@ -10631,6 +10848,37 @@ const csvService = ({ strapi }) => ({
|
|
|
10631
10848
|
}
|
|
10632
10849
|
const attrs = ct.attributes || {};
|
|
10633
10850
|
const { mode = "auto" } = options;
|
|
10851
|
+
const batchModeInfo = detectCsvBatchExportMode(rows);
|
|
10852
|
+
if (batchModeInfo.error) {
|
|
10853
|
+
return {
|
|
10854
|
+
success: true,
|
|
10855
|
+
detectedExportMode: "invalid",
|
|
10856
|
+
summary: {
|
|
10857
|
+
total: rows.length,
|
|
10858
|
+
willUpdate: 0,
|
|
10859
|
+
willCreate: 0,
|
|
10860
|
+
unchanged: 0,
|
|
10861
|
+
skipped: rows.length
|
|
10862
|
+
},
|
|
10863
|
+
diffs: [
|
|
10864
|
+
{
|
|
10865
|
+
uid,
|
|
10866
|
+
collectionName: ct.info?.displayName || uid,
|
|
10867
|
+
documentId: "(batch)",
|
|
10868
|
+
label: "CSV Export Mode Check",
|
|
10869
|
+
attribute: "__bulk_updater_metadata",
|
|
10870
|
+
oldValue: null,
|
|
10871
|
+
newValue: null,
|
|
10872
|
+
oldDisplay: "—",
|
|
10873
|
+
newDisplay: "—",
|
|
10874
|
+
hasChange: false,
|
|
10875
|
+
status: "error",
|
|
10876
|
+
action: "skip",
|
|
10877
|
+
message: batchModeInfo.error
|
|
10878
|
+
}
|
|
10879
|
+
]
|
|
10880
|
+
};
|
|
10881
|
+
}
|
|
10634
10882
|
const diffs = [];
|
|
10635
10883
|
let willUpdateCount = 0;
|
|
10636
10884
|
let willCreateCount = 0;
|
|
@@ -10676,13 +10924,39 @@ const csvService = ({ strapi }) => ({
|
|
|
10676
10924
|
});
|
|
10677
10925
|
continue;
|
|
10678
10926
|
}
|
|
10679
|
-
const
|
|
10927
|
+
const isRowCreate = !existing;
|
|
10928
|
+
const mutationPlan = await prepareCsvRowMutation(
|
|
10680
10929
|
strapi,
|
|
10681
10930
|
uid,
|
|
10682
10931
|
existing,
|
|
10683
10932
|
row,
|
|
10684
|
-
{
|
|
10933
|
+
{
|
|
10934
|
+
isPartialUpdate: mode === "update",
|
|
10935
|
+
isCreate: isRowCreate,
|
|
10936
|
+
expectedExportMode: batchModeInfo.mode,
|
|
10937
|
+
docIdForError: docId || `row-${rowNumber}`
|
|
10938
|
+
}
|
|
10685
10939
|
);
|
|
10940
|
+
if (mutationPlan.errors.length > 0) {
|
|
10941
|
+
skippedCount++;
|
|
10942
|
+
diffs.push({
|
|
10943
|
+
uid,
|
|
10944
|
+
collectionName: ct.info?.displayName || uid,
|
|
10945
|
+
documentId: existing?.documentId || docId || `(Row #${rowNumber})`,
|
|
10946
|
+
label: existing?.title || existing?.name || existing?.slug || `Row #${rowNumber}`,
|
|
10947
|
+
attribute: mutationPlan.targetObjectField || "(validation)",
|
|
10948
|
+
oldValue: null,
|
|
10949
|
+
newValue: null,
|
|
10950
|
+
oldDisplay: "—",
|
|
10951
|
+
newDisplay: "—",
|
|
10952
|
+
hasChange: false,
|
|
10953
|
+
status: "error",
|
|
10954
|
+
action: "skip",
|
|
10955
|
+
message: mutationPlan.errors.join(" | ")
|
|
10956
|
+
});
|
|
10957
|
+
continue;
|
|
10958
|
+
}
|
|
10959
|
+
const payload = mutationPlan.payload;
|
|
10686
10960
|
if (existing) {
|
|
10687
10961
|
const resolvedDocId = existing.documentId || docId;
|
|
10688
10962
|
const entryLabel = existing.title || existing.name || existing.slug || `Entry #${resolvedDocId}`;
|
|
@@ -10785,6 +11059,7 @@ const csvService = ({ strapi }) => ({
|
|
|
10785
11059
|
}
|
|
10786
11060
|
return {
|
|
10787
11061
|
success: true,
|
|
11062
|
+
detectedExportMode: batchModeInfo.mode,
|
|
10788
11063
|
summary: {
|
|
10789
11064
|
total: rows.length,
|
|
10790
11065
|
willUpdate: willUpdateCount,
|
|
@@ -11262,6 +11537,8 @@ const index = {
|
|
|
11262
11537
|
};
|
|
11263
11538
|
exports.CURRENT_SCHEMA_VERSION = CURRENT_SCHEMA_VERSION;
|
|
11264
11539
|
exports.EXPLICIT_CLEAR_TOKENS = EXPLICIT_CLEAR_TOKENS;
|
|
11540
|
+
exports.EXPLICIT_DELETE_KEY_TOKEN = EXPLICIT_DELETE_KEY_TOKEN;
|
|
11541
|
+
exports.EXPLICIT_EMPTY_STRING_TOKEN = EXPLICIT_EMPTY_STRING_TOKEN;
|
|
11265
11542
|
exports.FORBIDDEN_OBJECT_KEYS = FORBIDDEN_OBJECT_KEYS;
|
|
11266
11543
|
exports.METADATA_COLUMN_KEY = METADATA_COLUMN_KEY;
|
|
11267
11544
|
exports.SYSTEM_ATTRIBUTES = SYSTEM_ATTRIBUTES;
|
|
@@ -11271,13 +11548,14 @@ exports.buildDocumentUpdatePayload = buildDocumentUpdatePayload;
|
|
|
11271
11548
|
exports.buildErrorCsv = buildErrorCsv;
|
|
11272
11549
|
exports.buildExportColumnPlan = buildExportColumnPlan;
|
|
11273
11550
|
exports.buildLosslessPopulate = buildLosslessPopulate;
|
|
11274
|
-
exports.buildNestedComponentPatchFromDots = buildNestedComponentPatchFromDots;
|
|
11275
11551
|
exports.buildRichAttributeMeta = buildRichAttributeMeta;
|
|
11276
11552
|
exports.classifyAttribute = classifyAttribute;
|
|
11277
11553
|
exports.coerceAndValidateFieldValue = coerceAndValidateFieldValue;
|
|
11278
11554
|
exports.deepCloneValue = deepCloneValue;
|
|
11555
|
+
exports.deepEqualValues = deepEqualValues;
|
|
11279
11556
|
exports.deepMergeEntities = deepMergeEntities;
|
|
11280
11557
|
exports.default = index;
|
|
11558
|
+
exports.detectCsvBatchExportMode = detectCsvBatchExportMode;
|
|
11281
11559
|
exports.discoverComponentFields = discoverComponentFields;
|
|
11282
11560
|
exports.discoverSchema = discoverSchema;
|
|
11283
11561
|
exports.enumerateOccurrences = enumerateOccurrences;
|
|
@@ -11287,27 +11565,28 @@ exports.extractMetadataFromRow = extractMetadataFromRow;
|
|
|
11287
11565
|
exports.extractRowFlattenedMetadata = extractRowFlattenedMetadata;
|
|
11288
11566
|
exports.formatErrorCsvFilename = formatErrorCsvFilename;
|
|
11289
11567
|
exports.getAllNestedValues = getAllNestedValues;
|
|
11290
|
-
exports.
|
|
11568
|
+
exports.inspectRowMetadataMode = inspectRowMetadataMode;
|
|
11291
11569
|
exports.isPlainObject = isPlainObject;
|
|
11292
11570
|
exports.isValidDocumentId = isValidDocumentId;
|
|
11571
|
+
exports.normalizeExportMode = normalizeExportMode;
|
|
11293
11572
|
exports.parseBoolean = parseBoolean;
|
|
11294
11573
|
exports.parseCsvToMatrix = parseCsvToMatrix;
|
|
11295
11574
|
exports.parseCsvToRecords = parseCsvToRecords;
|
|
11296
|
-
exports.
|
|
11575
|
+
exports.parseFirstLevelCellValue = parseFirstLevelCellValue;
|
|
11297
11576
|
exports.parseNumeric = parseNumeric;
|
|
11298
|
-
exports.
|
|
11577
|
+
exports.prepareCsvRowMutation = prepareCsvRowMutation;
|
|
11578
|
+
exports.reconstructTargetedFirstLevelObject = reconstructTargetedFirstLevelObject;
|
|
11299
11579
|
exports.resolveBatchTargets = resolveBatchTargets;
|
|
11300
11580
|
exports.resolveComponentPayload = resolveComponentPayload;
|
|
11301
11581
|
exports.resolveDynamicZonePayload = resolveDynamicZonePayload;
|
|
11302
11582
|
exports.resolveErrorColumnName = resolveErrorColumnName;
|
|
11303
11583
|
exports.resolveMediaIds = resolveMediaIds;
|
|
11304
11584
|
exports.resolveRelationPayload = resolveRelationPayload;
|
|
11585
|
+
exports.resolveTargetObjectField = resolveTargetObjectField;
|
|
11305
11586
|
exports.sanitizeUserErrorMessage = sanitizeUserErrorMessage;
|
|
11306
11587
|
exports.serializeDocumentsToCsv = serializeDocumentsToCsv;
|
|
11307
11588
|
exports.serializeFieldValue = serializeFieldValue;
|
|
11308
|
-
exports.serializeFlattenedLeafValue = serializeFlattenedLeafValue;
|
|
11309
11589
|
exports.serializePlannedCell = serializePlannedCell;
|
|
11310
|
-
exports.setValueAtPath = setValueAtPath;
|
|
11311
11590
|
exports.sortAuditRecordsDesc = sortAuditRecordsDesc;
|
|
11312
11591
|
exports.validateEntityQuotas = validateEntityQuotas;
|
|
11313
11592
|
exports.validateRecordSchemaConstraints = validateRecordSchemaConstraints;
|