Skip to content

Commit

Permalink
fix: available stock of addons
Browse files Browse the repository at this point in the history
  • Loading branch information
TextC0de committed Nov 3, 2024
1 parent ec02343 commit 4185fd9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
12 changes: 12 additions & 0 deletions src/schema/purchaseOrder/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,18 @@ export const clearExpiredPurchaseOrders = async ({
),
)
.returning();

await DB.update(userTicketAddonsSchema)
.set({
approvalStatus: UserTicketAddonApprovalStatus.CANCELLED,
})
.where(
inArray(
userTicketAddonsSchema.purchaseOrderId,
expiredOrders.map((po) => po.id),
),
)
.returning();
}

return expiredOrders;
Expand Down
28 changes: 4 additions & 24 deletions src/schema/ticketAddons/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,15 @@ builder.objectType(AddonRef, {
return null;
}

const userTickets = await DB.query.userTicketsSchema.findMany({
where: (ut, { eq, and, inArray }) =>
and(
eq(ut.ticketTemplateId, root.id),
inArray(ut.approvalStatus, [
"approved",
"not_required",
"pending",
"gifted",
"gift_accepted",
"transfer_accepted",
"transfer_pending",
]),
),
});

if (userTickets.length === 0) {
return root.totalStock;
}

const userTicketAddons = await DB.query.userTicketAddonsSchema.findMany(
{
where: (ut, ops) =>
ops.and(
ops.eq(ut.addonId, root.id),
ops.inArray(
ut.userTicketId,
userTickets.map((ut) => ut.id),
),
ops.inArray(ut.approvalStatus, [
UserTicketAddonApprovalStatus.APPROVED,
UserTicketAddonApprovalStatus.PENDING,
]),
),
},
);
Expand Down

0 comments on commit 4185fd9

Please sign in to comment.