@open-mercato/core 0.10.1-develop.7787.1.96c7cfc66f → 0.10.1-develop.7793.1.9e0c43fdde

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.
@@ -9,6 +9,8 @@ import { invalidateCrudCache } from "@open-mercato/shared/lib/crud/cache";
9
9
  import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
10
10
  import { findOneWithDecryption, findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
11
11
  import { readSafeDecryptedString } from "../lib/decryptionSafety.js";
12
+ import { isInitialReplacementOrderApproval } from "../lib/replacementOrderUndo.js";
13
+ import { OPTIMISTIC_LOCK_HEADER_NAME } from "@open-mercato/shared/lib/crud/optimistic-lock-headers";
12
14
  import { E } from "../../../generated/entities.ids.generated.js";
13
15
  import {
14
16
  WarrantyClaim,
@@ -2298,19 +2300,26 @@ const SALES_ORDER_LINE_KINDS = /* @__PURE__ */ new Set(["product", "service", "s
2298
2300
  function readSalesOrderLineKind(value) {
2299
2301
  return typeof value === "string" && SALES_ORDER_LINE_KINDS.has(value) ? value : null;
2300
2302
  }
2301
- async function dispatchSalesOrderDelete(ctx, scope, replacementOrderId) {
2303
+ async function dispatchSalesOrderDelete(ctx, scope, replacementOrderId, expectedUpdatedAt) {
2302
2304
  const commandBus = ctx.container.resolve("commandBus");
2305
+ const dispatchContext = scrubbedDispatchContext(ctx);
2306
+ if (expectedUpdatedAt) {
2307
+ dispatchContext.request = new Request(ctx.request?.url ?? "http://localhost", {
2308
+ headers: { [OPTIMISTIC_LOCK_HEADER_NAME]: expectedUpdatedAt }
2309
+ });
2310
+ }
2303
2311
  await commandBus.execute(
2304
2312
  "sales.orders.delete",
2305
2313
  {
2306
2314
  input: { id: replacementOrderId, tenantId: scope.tenantId, organizationId: scope.organizationId },
2307
- ctx: scrubbedDispatchContext(ctx)
2315
+ ctx: dispatchContext
2308
2316
  }
2309
2317
  );
2310
2318
  }
2311
2319
  const createReplacementOrderCommand = {
2312
2320
  id: "warranty_claims.claim.create_replacement_order",
2313
2321
  isUndoable: true,
2322
+ redo: ({ input, ctx }) => createReplacementOrderCommand.execute({ ...input, updatedAt: void 0 }, ctx),
2314
2323
  prepare: async (rawInput, ctx) => {
2315
2324
  const input = parseCommandInput(claimCreateReplacementOrderSchema, rawInput);
2316
2325
  const scope = resolveScope(ctx, input);
@@ -2548,10 +2557,10 @@ const createReplacementOrderCommand = {
2548
2557
  }
2549
2558
  if (currentRow) {
2550
2559
  const currentUpdatedAt = currentRow.updated_at ? new Date(currentRow.updated_at).toISOString() : null;
2551
- if (!replacementOrder.updatedAt || !currentUpdatedAt || currentUpdatedAt !== replacementOrder.updatedAt) {
2560
+ if (!replacementOrder.updatedAt || !currentUpdatedAt || currentUpdatedAt !== replacementOrder.updatedAt && !await isInitialReplacementOrderApproval(ctx, scope, replacementOrder.id, replacementOrder.updatedAt, currentUpdatedAt)) {
2552
2561
  throw new CrudHttpError(409, { error: "warranty_claims.errors.replacementOrderChangedUndoAborted" });
2553
2562
  }
2554
- await dispatchSalesOrderDelete(ctx, scope, replacementOrder.id);
2563
+ await dispatchSalesOrderDelete(ctx, scope, replacementOrder.id, currentUpdatedAt);
2555
2564
  } else {
2556
2565
  replacementOrderBridgeLogger.info("warranty_claims.claim.create_replacement_order undo found replacement order already absent", {
2557
2566
  claimId: before.id,