Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/api-plugin-orders/src/mutations/cancelOrderItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export default async function cancelOrderItem(context, input) {
const order = await Orders.findOne({ _id: orderId });
if (!order) throw new ReactionError("not-found", "Order not found");

// Track updatedAt for optimistic concurrency to prevent parallel
// cancelOrderItem calls from overwriting each other's changes
const orderUpdatedAt = order.updatedAt;

await context.validatePermissions(`reaction:legacy:orders:${order._id}`, "cancel:item", {
shopId: order.shopId,
owner: order.accountId
Expand Down Expand Up @@ -172,8 +176,16 @@ export default async function cancelOrderItem(context, input) {

OrderSchema.validate(modifier, { modifier: true });

// Use optimistic concurrency with updatedAt to prevent parallel
// cancelOrderItem calls from overwriting each other's changes.
// If the order doesn't have an updatedAt (e.g. legacy documents),
// fall back to update without the concurrency check.
const query = { _id: orderId };
if (orderUpdatedAt) {
query.updatedAt = orderUpdatedAt;
}
const { modifiedCount, value: updatedOrder } = await Orders.findOneAndUpdate(
{ _id: orderId },
query,
modifier,
{ returnOriginal: false }
);
Expand Down