106 lines
3.1 KiB
TypeScript
106 lines
3.1 KiB
TypeScript
import { minor, decimal } from '../checkout/money';
|
|
import { AppError } from '../common/errors/app-error';
|
|
import type { Coupon, Prisma } from '../generated/prisma/client';
|
|
import type { Principal } from '../identity/identity.types';
|
|
import { campaignStatus } from '../campaigns/campaign-policy';
|
|
|
|
export const campaignCouponSelect = {
|
|
code: true,
|
|
currency: true,
|
|
kind: true,
|
|
amount: true,
|
|
percentBps: true,
|
|
minimumSubtotal: true,
|
|
startsAt: true,
|
|
endsAt: true,
|
|
active: true,
|
|
} as const;
|
|
export function assertCampaignCouponWindow(
|
|
campaign: { startsAt: Date; endsAt: Date },
|
|
coupon: { startsAt: Date; endsAt: Date },
|
|
) {
|
|
if (coupon.startsAt < campaign.startsAt || coupon.endsAt > campaign.endsAt)
|
|
throw new AppError('CAMPAIGN_COUPON_WINDOW');
|
|
}
|
|
export type CampaignCouponOffer = Prisma.CouponGetPayload<{
|
|
select: typeof campaignCouponSelect;
|
|
}>;
|
|
|
|
export type PricedCartLine = { variantId: string; lineTotal: string };
|
|
export function campaignEligibleSubtotal(
|
|
lines: readonly PricedCartLine[],
|
|
eligible: ReadonlySet<string>,
|
|
) {
|
|
return lines.reduce(
|
|
(sum, line) =>
|
|
sum + (eligible.has(line.variantId) ? minor(line.lineTotal) : 0n),
|
|
0n,
|
|
);
|
|
}
|
|
|
|
export async function campaignCouponContext(
|
|
tx: Prisma.TransactionClient,
|
|
actor: Principal,
|
|
coupon: Coupon | null,
|
|
lines: readonly PricedCartLine[],
|
|
subtotal: bigint,
|
|
now: Date,
|
|
) {
|
|
if (!coupon?.campaignId)
|
|
return { eligibleSubtotal: subtotal, campaignSnapshot: null };
|
|
const campaign = await tx.seasonalCampaign.findFirst({
|
|
where: { id: coupon.campaignId, organizationId: actor.organizationId },
|
|
select: {
|
|
id: true,
|
|
slug: true,
|
|
collectionId: true,
|
|
state: true,
|
|
startsAt: true,
|
|
endsAt: true,
|
|
collection: { select: { kind: true } },
|
|
},
|
|
});
|
|
if (
|
|
!campaign ||
|
|
campaignStatus(campaign, now) !== 'ACTIVE' ||
|
|
campaign.collection.kind !== 'COLLECTION'
|
|
)
|
|
throw new AppError('COUPON_CAMPAIGN_INELIGIBLE');
|
|
const variants = await tx.productVariant.findMany({
|
|
where: {
|
|
organizationId: actor.organizationId,
|
|
id: { in: lines.map((line) => line.variantId) },
|
|
active: true,
|
|
product: {
|
|
status: 'PUBLISHED',
|
|
groups: {
|
|
some: {
|
|
groupId: campaign.collectionId,
|
|
organizationId: actor.organizationId,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
select: { id: true },
|
|
});
|
|
const eligible = new Set(variants.map((variant) => variant.id));
|
|
const eligibleSubtotal = campaignEligibleSubtotal(lines, eligible);
|
|
if (eligibleSubtotal === 0n) throw new AppError('COUPON_CAMPAIGN_INELIGIBLE');
|
|
return {
|
|
eligibleSubtotal,
|
|
campaignSnapshot: {
|
|
id: campaign.id,
|
|
slug: campaign.slug,
|
|
collectionId: campaign.collectionId,
|
|
startsAt: campaign.startsAt.toISOString(),
|
|
endsAt: campaign.endsAt.toISOString(),
|
|
couponStartsAt: coupon.startsAt.toISOString(),
|
|
couponEndsAt: coupon.endsAt.toISOString(),
|
|
eligibleSubtotal: decimal(eligibleSubtotal),
|
|
eligibleVariantIds: lines
|
|
.filter((line) => eligible.has(line.variantId))
|
|
.map((line) => line.variantId),
|
|
},
|
|
};
|
|
}
|