80 lines
2.4 KiB
TypeScript
80 lines
2.4 KiB
TypeScript
import type { Coupon, Prisma } from '../generated/prisma/client';
|
|
import type { Principal } from '../identity/identity.types';
|
|
import { AppError } from '../common/errors/app-error';
|
|
import { minor, discountFor } from '../checkout/money';
|
|
import {
|
|
campaignCouponContext,
|
|
type PricedCartLine,
|
|
} from './campaign-coupon-policy';
|
|
|
|
export function assertCoupon(
|
|
coupon: Coupon | null,
|
|
currency: string,
|
|
subtotal: bigint,
|
|
now: Date,
|
|
uses: number,
|
|
userUses: number,
|
|
): asserts coupon is Coupon {
|
|
if (!coupon) throw new AppError('COUPON_INELIGIBLE', 'COUPON_UNKNOWN');
|
|
const failures: [boolean, string][] = [
|
|
[!coupon.active, 'COUPON_DISABLED'],
|
|
[coupon.currency !== currency, 'COUPON_CURRENCY'],
|
|
[coupon.startsAt > now, 'COUPON_NOT_STARTED'],
|
|
[coupon.endsAt <= now, 'COUPON_EXPIRED'],
|
|
[minor(coupon.minimumSubtotal.toString()) > subtotal, 'COUPON_MINIMUM'],
|
|
[uses >= coupon.maxUses, 'COUPON_TOTAL_LIMIT'],
|
|
[userUses >= coupon.perUserLimit, 'COUPON_USER_LIMIT'],
|
|
];
|
|
const failure = failures.find(([failed]) => failed);
|
|
if (failure) throw new AppError('COUPON_INELIGIBLE', failure[1]);
|
|
}
|
|
export async function priceCoupon(
|
|
tx: Prisma.TransactionClient,
|
|
actor: Principal,
|
|
code: string | undefined,
|
|
currency: string,
|
|
subtotal: bigint,
|
|
now: Date,
|
|
lines: readonly PricedCartLine[] = [],
|
|
) {
|
|
if (!code) return { discount: 0n, coupon: null, campaignSnapshot: null };
|
|
const coupon = await tx.coupon.findUnique({
|
|
where: {
|
|
organizationId_code: { organizationId: actor.organizationId, code },
|
|
},
|
|
});
|
|
const { eligibleSubtotal, campaignSnapshot } = await campaignCouponContext(
|
|
tx,
|
|
actor,
|
|
coupon,
|
|
lines,
|
|
subtotal,
|
|
now,
|
|
);
|
|
const where = {
|
|
couponId: coupon?.id ?? '00000000-0000-0000-0000-000000000000',
|
|
OR: [
|
|
{ status: 'PENDING_PAYMENT' as const, expiresAt: { gt: now } },
|
|
{
|
|
status: {
|
|
in: ['CONFIRMED' as const, 'SHIPPED' as const, 'DELIVERED' as const],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
const uses = await tx.order.count({ where });
|
|
const userUses = await tx.order.count({
|
|
where: { ...where, userId: actor.userId },
|
|
});
|
|
assertCoupon(coupon, currency, eligibleSubtotal, now, uses, userUses);
|
|
return {
|
|
coupon,
|
|
campaignSnapshot,
|
|
discount: discountFor(eligibleSubtotal, {
|
|
kind: coupon.kind,
|
|
amount: coupon.amount?.toString() ?? null,
|
|
percentBps: coupon.percentBps,
|
|
}),
|
|
};
|
|
}
|