UNPKG

@coursebuilder/core

Version:

Core package for Course Builder

132 lines (130 loc) 5.21 kB
import { PurchaseInfoSchema, PurchaseMetadata } from "./chunk-EGXM3TQ6.js"; import { EXISTING_BULK_COUPON, INDIVIDUAL_TO_BULK_UPGRADE, NEW_BULK_COUPON, NEW_INDIVIDUAL_PURCHASE } from "./chunk-IXARSSGX.js"; import { __name } from "./chunk-VLQXSCFN.js"; // src/lib/pricing/stripe-purchase-utils.ts import { first, isEmpty, sortBy } from "@coursebuilder/nodash"; async function parsePurchaseInfoFromCheckoutSession(checkoutSession, courseBuilderAdapter) { const { customer, line_items, payment_intent, metadata } = checkoutSession; const { email, name, id: stripeCustomerId } = customer; const lineItem = first(line_items?.data); const stripePrice = lineItem.price; const quantity = lineItem.quantity || 1; const stripeProduct = stripePrice?.product; const { latest_charge } = payment_intent; let stripeCharge = latest_charge; const stripeChargeId = stripeCharge?.id; const stripeChargeAmount = stripeCharge?.amount || 0; const discount = first(lineItem.discounts); const stripeCouponId = discount?.discount.coupon.id; const parsedMetadata = metadata ? PurchaseMetadata.parse(metadata) : void 0; const purchaseType = await determinePurchaseType({ chargeIdentifier: stripeChargeId, email, courseBuilderAdapter }); const info = { customerIdentifier: stripeCustomerId, email, name, productIdentifier: stripeProduct.id, product: stripeProduct, chargeIdentifier: stripeChargeId, couponIdentifier: stripeCouponId, quantity, chargeAmount: stripeChargeAmount, metadata: parsedMetadata, purchaseType }; return PurchaseInfoSchema.parse(info); } __name(parsePurchaseInfoFromCheckoutSession, "parsePurchaseInfoFromCheckoutSession"); async function determinePurchaseType(options) { const { email, chargeIdentifier, courseBuilderAdapter } = options; const { getUserByEmail, getPurchasesForUser, getPurchaseForStripeCharge } = courseBuilderAdapter; try { const user = !!email && await getUserByEmail?.(email); const { id: userId } = user || {}; console.log("Debug - userId:", userId); const checkoutSessionPurchase = await getPurchaseForStripeCharge(chargeIdentifier); console.log("Debug - checkoutSessionPurchase:", checkoutSessionPurchase); if (!userId || !checkoutSessionPurchase?.id) { console.log("Debug - Missing data, throwing error"); throw new Error("Missing userId or purchase for checkout session"); } const allUserPurchases = await getPurchasesForUser(userId); console.log("Debug - allUserPurchases:", allUserPurchases); const { type, data } = summarizePurchases(checkoutSessionPurchase, allUserPurchases); console.log("Debug - summarized type:", type); console.log("Debug - summarized data:", data); if (type === "BULK") { const { otherTeamBulkPurchases, otherIndividualPurchases, focalBulkPurchaseIsChronologicallyFirst } = data.focalProduct; if (isEmpty(otherTeamBulkPurchases) || focalBulkPurchaseIsChronologicallyFirst) { if (isEmpty(otherIndividualPurchases)) { return NEW_BULK_COUPON; } else { return INDIVIDUAL_TO_BULK_UPGRADE; } } else { return EXISTING_BULK_COUPON; } } return NEW_INDIVIDUAL_PURCHASE; } catch (e) { console.error("Error in determinePurchaseType:", e); return NEW_INDIVIDUAL_PURCHASE; } } __name(determinePurchaseType, "determinePurchaseType"); var summarizePurchases = /* @__PURE__ */ __name((focalPurchase, allUserPurchases) => { const focalProductId = focalPurchase.productId; const purchaseIsBulk = Boolean(focalPurchase.bulkCoupon?.id); const focalProductPurchases = allUserPurchases.filter((purchase) => purchase.productId === focalProductId); const bulkPurchases = focalProductPurchases.filter((purchase) => Boolean(purchase.bulkCoupon?.id)); const otherIndividualPurchases = focalProductPurchases.filter((purchase) => isEmpty(purchase.bulkCoupon) && isEmpty(purchase.redeemedBulkCouponId) && purchase.id !== focalPurchase.id); if (purchaseIsBulk) { const teamBulkPurchases = bulkPurchases.filter((purchase) => match(purchase.bulkCoupon.id, focalPurchase.bulkCoupon?.id)); const otherTeamBulkPurchases = teamBulkPurchases.filter((purchase) => purchase.id !== focalPurchase.id); const focalBulkPurchaseIsChronologicallyFirst = isEmpty(teamBulkPurchases) || sortBy(teamBulkPurchases, "createdAt")[0].id === focalPurchase.id; return { type: "BULK", data: { focalProduct: { productId: focalProductId, focalPurchase, otherTeamBulkPurchases, otherIndividualPurchases, focalBulkPurchaseIsChronologicallyFirst } } }; } else { return { type: "INDIVIDUAL", data: { focalProduct: { productId: focalProductId, focalPurchase, otherIndividualPurchases } } }; } }, "summarizePurchases"); var match = /* @__PURE__ */ __name((value1, value2) => { return Boolean(value1 && value2 && value1 === value2); }, "match"); export { parsePurchaseInfoFromCheckoutSession, determinePurchaseType }; //# sourceMappingURL=chunk-MUVDMT7G.js.map