strapi-plugin-bulk-updater 1.5.0 → 1.5.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.
@@ -1113,14 +1113,15 @@ function discoverComponentAttributes(strapi, compUid, prefix, currentDepth, maxD
1113
1113
  }
1114
1114
  function parseDynamicZonePath(path) {
1115
1115
  if (!path) return { cleanPath: "", rootKey: "", subPath: "" };
1116
- const bracketMatch = path.match(/^([^.[]+)\[([^\]]+)\]\.(.+)$/);
1116
+ const bracketMatch = path.match(/^([^.[]+)\[([^\]]+)\](?:\.(.+))?$/);
1117
1117
  if (bracketMatch) {
1118
1118
  const rootKey = bracketMatch[1];
1119
- const selector = bracketMatch[2];
1120
- const subPath = bracketMatch[3];
1121
- const cleanPath = `${rootKey}.${subPath}`;
1122
- if (/^\d+$/.test(selector)) {
1123
- return { cleanPath, rootKey, subPath, targetIndex: parseInt(selector, 10) };
1119
+ const selector = bracketMatch[2].trim();
1120
+ const subPath = bracketMatch[3] || "";
1121
+ const cleanPath = subPath ? `${rootKey}.${subPath}` : rootKey;
1122
+ const numMatch = selector.match(/^#?(\d+)/);
1123
+ if (numMatch) {
1124
+ return { cleanPath, rootKey, subPath, targetIndex: parseInt(numMatch[1], 10) };
1124
1125
  }
1125
1126
  return { cleanPath, rootKey, subPath, targetComponentUid: selector };
1126
1127
  }
@@ -4273,42 +4274,74 @@ const auditService = ({ strapi }) => ({
4273
4274
  for (const item of diffs) {
4274
4275
  if (item.status === "updated" && item.uid && item.documentId && item.attribute) {
4275
4276
  try {
4277
+ const ct = strapi.contentTypes[item.uid];
4278
+ if (!ct) {
4279
+ throw new Error(`Content type "${item.uid}" does not exist in schema.`);
4280
+ }
4276
4281
  const isRel = item.oldValue && typeof item.oldValue === "object" && item.oldValue.isRelation;
4277
4282
  let rollbackPayload;
4278
4283
  if (isRel) {
4279
4284
  const oldDocIds = Array.isArray(item.oldValue.documentIds) ? item.oldValue.documentIds : [];
4280
4285
  rollbackPayload = { set: oldDocIds };
4281
4286
  } else {
4282
- rollbackPayload = item.oldValue;
4287
+ rollbackPayload = item.oldValue ?? null;
4283
4288
  }
4284
- if (item.attribute.includes(".")) {
4285
- const [parentKey, childKey] = item.attribute.split(".");
4286
- const ct2 = strapi.contentTypes[item.uid];
4287
- if (ct2?.attributes?.[parentKey]?.type === "component") {
4288
- const currentDoc = await strapi.documents(item.uid).findOne({
4289
- documentId: item.documentId,
4290
- populate: [parentKey]
4291
- });
4292
- const currentParent = currentDoc?.[parentKey] || {};
4293
- const restoredParent = {
4294
- ...currentParent,
4295
- [childKey]: rollbackPayload
4296
- };
4297
- await strapi.documents(item.uid).update({
4298
- documentId: item.documentId,
4299
- data: {
4300
- [parentKey]: restoredParent
4301
- }
4302
- });
4289
+ const parsed = parseDynamicZonePath(item.attribute);
4290
+ const rootKey = parsed.rootKey;
4291
+ const rootAttrDef = ct.attributes?.[rootKey];
4292
+ const isComponent = rootAttrDef?.type === "component";
4293
+ const isDynamicZone = rootAttrDef?.type === "dynamiczone";
4294
+ const isNested = item.attribute.includes(".") || item.attribute.includes("[") || isComponent || isDynamicZone || isDeepComponentField(ct, strapi.components, item.attribute);
4295
+ let wasPublished = false;
4296
+ if (isNested && (isComponent || isDynamicZone || rootAttrDef)) {
4297
+ const populateOption = buildDeepPopulate$1(ct.attributes, strapi.components);
4298
+ const findOneArgs = { documentId: item.documentId };
4299
+ if (populateOption) {
4300
+ findOneArgs.populate = populateOption;
4301
+ }
4302
+ const currentDoc = await strapi.documents(item.uid).findOne(findOneArgs);
4303
+ if (!currentDoc) {
4304
+ throw new Error(`Document "${item.documentId}" not found in collection "${item.uid}".`);
4305
+ }
4306
+ wasPublished = Boolean(currentDoc?.publishedAt);
4307
+ const isRootDz = isDynamicZone;
4308
+ const existingRoot = currentDoc?.[rootKey] || (isRootDz ? [] : {});
4309
+ let restoredRoot;
4310
+ if (Array.isArray(item.dynamicZoneDiffs) && item.dynamicZoneDiffs.length > 0) {
4311
+ let workingRoot = existingRoot;
4312
+ for (const dzDiff of item.dynamicZoneDiffs) {
4313
+ const parsedDz = parseDynamicZonePath(dzDiff.attribute || item.attribute);
4314
+ const dzParts = parsedDz.cleanPath.split(".").slice(1);
4315
+ const targetIndexes = dzDiff.index !== void 0 ? [dzDiff.index] : parsedDz.targetIndex !== void 0 ? [parsedDz.targetIndex] : void 0;
4316
+ workingRoot = setDeepProperty(workingRoot, dzParts, dzDiff.oldValue ?? null, {
4317
+ targetIndexes,
4318
+ targetComponentUid: dzDiff.componentUid || parsedDz.targetComponentUid,
4319
+ strapi
4320
+ });
4321
+ }
4322
+ restoredRoot = workingRoot;
4303
4323
  } else {
4304
- await strapi.documents(item.uid).update({
4305
- documentId: item.documentId,
4306
- data: {
4307
- [item.attribute]: rollbackPayload
4308
- }
4324
+ const parts = parsed.cleanPath.split(".");
4325
+ const pathParts = parts.slice(1);
4326
+ const targetIndexes = parsed.targetIndex !== void 0 ? [parsed.targetIndex] : void 0;
4327
+ const targetComponentUid = parsed.targetComponentUid;
4328
+ restoredRoot = setDeepProperty(existingRoot, pathParts, rollbackPayload, {
4329
+ targetIndexes,
4330
+ targetComponentUid,
4331
+ strapi
4309
4332
  });
4310
4333
  }
4334
+ await strapi.documents(item.uid).update({
4335
+ documentId: item.documentId,
4336
+ data: {
4337
+ [rootKey]: restoredRoot
4338
+ }
4339
+ });
4311
4340
  } else {
4341
+ const currentDoc = await strapi.documents(item.uid).findOne({
4342
+ documentId: item.documentId
4343
+ });
4344
+ wasPublished = Boolean(currentDoc?.publishedAt);
4312
4345
  await strapi.documents(item.uid).update({
4313
4346
  documentId: item.documentId,
4314
4347
  data: {
@@ -4316,13 +4349,13 @@ const auditService = ({ strapi }) => ({
4316
4349
  }
4317
4350
  });
4318
4351
  }
4319
- const ct = strapi.contentTypes[item.uid];
4320
- if (ct?.options?.draftAndPublish) {
4352
+ if (ct?.options?.draftAndPublish && wasPublished) {
4321
4353
  try {
4322
4354
  await strapi.documents(item.uid).publish({
4323
4355
  documentId: item.documentId
4324
4356
  });
4325
- } catch {
4357
+ } catch (publishErr) {
4358
+ strapi.log.warn(`[bulk-updater] Failed to auto-publish rollback for ${item.uid}/${item.documentId}:`, publishErr?.message);
4326
4359
  }
4327
4360
  }
4328
4361
  try {
@@ -4330,7 +4363,7 @@ const auditService = ({ strapi }) => ({
4330
4363
  uid: item.uid,
4331
4364
  documentId: item.documentId,
4332
4365
  userId,
4333
- statusOverride: ct?.options?.draftAndPublish ? "published" : void 0
4366
+ statusOverride: ct?.options?.draftAndPublish ? wasPublished ? "published" : "draft" : void 0
4334
4367
  });
4335
4368
  } catch {
4336
4369
  }