import Inject, { RegisterScoped } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import EntityAccessError from "@entity-access/entity-access/dist/common/EntityAccessError.js"; import StoreJournalCoupon from "../../model/entities/StoreJournalCoupon.js"; import { StoreJournalLineItem } from "../../model/entities/StoreJournalLineItem.js"; @RegisterScoped export default class CouponService { @Inject db: SocialMailContext; async apply(orderCoupon: StoreJournalCoupon, entry = this.db.changeSet.getEntry(orderCoupon)) { const { db } = this; if (orderCoupon.lineItemID) { await db.storeJournalLineItems.where(orderCoupon, (x, p) => x.lineItemID === p.lineItemID) .include((x) => [ x.itemPrice, x.item, x.journal]) .firstOrFail(); } else { await db.storeJournals.where(orderCoupon, (x, p) => x.journalID === p.orderID) .include((x) => [ x.lineItems.forEach((i) => [i.itemPrice, i.item]) ]) .firstOrFail(); } const { lineItem, order = lineItem.journal, couponCode } = orderCoupon; const { storeID } = order; const coupon = (await db.storeCoupons .where({ storeID, couponCode }, (x, p) => x.storeID === p.storeID && x.code === p.couponCode && x.active === true ) .first()) ?? EntityAccessError.throw(`Coupon "${couponCode}" not found`); let applicableItems = order.lineItems; if (!orderCoupon.couponID) { orderCoupon.couponID = coupon.couponID; orderCoupon.coupon = coupon; } if (coupon.itemID) { applicableItems = order.lineItems.filter((i) => i.itemID === coupon.itemID); if (!applicableItems.length) { throw new EntityAccessError(`Coupon "${couponCode}" not applicable for any items in the order.`); } } if (coupon.minQuantity > 1) { applicableItems = applicableItems.filter((i) => i.quantity > coupon.minQuantity); if (!applicableItems.length) { throw new EntityAccessError(`Coupon "${couponCode}" can only be applied.`); } } if (coupon.maxQuantity > 1) { applicableItems = applicableItems.filter((i) => i.quantity < coupon.maxQuantity); if (!applicableItems.length) { throw new EntityAccessError(`Coupon "${couponCode}" can only be applied.`); } } if (coupon.maxOrderAmount > 0) { applicableItems = applicableItems.filter((i) => i.priceAfterDiscount < coupon.maxOrderAmount); if (!applicableItems.length) { throw new EntityAccessError(`Coupon "${couponCode}" can only be applied for max of ${coupon.maxOrderAmount} amount.`); } } if (coupon.minOrderAmount > 0) { applicableItems = applicableItems.filter((i) => i.priceAfterDiscount > coupon.minOrderAmount); if (!applicableItems.length) { throw new EntityAccessError(`Coupon "${couponCode}" can only be applied for min of ${coupon.maxOrderAmount} amount.`); } } return applicableItems; } async compute(lineItem: StoreJournalLineItem) { // set couponDiscount amount const coupons = lineItem.coupons ?? lineItem.journal.orderCoupons; if (!coupons?.length) { return; } lineItem.couponDiscount = 0; for (const coupon of coupons) { if (!coupon.coupon) { await this.db.changeSet.getEntry(coupon).loadNavigationAsync((x) => x.coupon); } switch(coupon.coupon.type) { case "discount": lineItem.couponDiscount += lineItem.priceAfterDiscount * Number(coupon.coupon.amount); break; case "subtract": lineItem.couponDiscount += Number(coupon.coupon.amount); break; } } } }