Skip to content

Commit

Permalink
fix(core): Prevent fulfillments with too great a quantity
Browse files Browse the repository at this point in the history
Relates to #2329
  • Loading branch information
michaelbromley committed Aug 7, 2023
1 parent 6f48ee2 commit 579459a
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ export class OrderService {
ctx: RequestContext,
input: FulfillOrderInput,
) {
const linesToBeFulfilled = await this.connection
const existingFulfillmentLines = await this.connection
.getRepository(ctx, FulfillmentLine)
.createQueryBuilder('fulfillmentLine')
.leftJoinAndSelect('fulfillmentLine.orderLine', 'orderLine')
Expand All @@ -1244,13 +1244,25 @@ export class OrderService {
.andWhere('fulfillment.state != :state', { state: 'Cancelled' })
.getMany();

for (const lineToBeFulfilled of linesToBeFulfilled) {
const unfulfilledQuantity = lineToBeFulfilled.orderLine.quantity - lineToBeFulfilled.quantity;
const lineInput = input.lines.find(l =>
idsAreEqual(l.orderLineId, lineToBeFulfilled.orderLine.id),
for (const inputLine of input.lines) {
const existingFulfillmentLine = existingFulfillmentLines.find(l =>
idsAreEqual(l.orderLineId, inputLine.orderLineId),
);
if (unfulfilledQuantity < (lineInput?.quantity ?? 0)) {
return true;
if (existingFulfillmentLine) {
const unfulfilledQuantity =
existingFulfillmentLine.orderLine.quantity - existingFulfillmentLine.quantity;
if (unfulfilledQuantity < inputLine.quantity) {
return true;
}
} else {
const orderLine = await this.connection.getEntityOrThrow(
ctx,
OrderLine,
inputLine.orderLineId,
);
if (orderLine.quantity < inputLine.quantity) {
return true;
}
}
}
return false;
Expand Down

0 comments on commit 579459a

Please sign in to comment.