UNPKG

@coursebuilder/core

Version:

Core package for Course Builder

112 lines (110 loc) 3.4 kB
import { FULL_PRICE_COUPON_REDEEMED_EVENT } from "./chunk-KJUGUROM.js"; import { sendServerEmail } from "./chunk-ZJOFKIX6.js"; import { z } from "./chunk-JLNB6NRA.js"; import { __name } from "./chunk-VLQXSCFN.js"; // src/lib/actions/redeem.ts async function redeem(request, cookies, options) { if (!options.adapter) throw new Error("Adapter not found"); const { email: baseEmail, couponId, sendEmail = true, productIds } = z.object({ email: z.string().email(), couponId: z.string().optional(), sendEmail: z.boolean().optional().default(true), productIds: z.array(z.string()).optional() }).parse(request.body); if (!baseEmail) throw new Error(`invalid-email-${baseEmail}`); const currentUser = await options.adapter.getUserByEmail(baseEmail); const createdPurchase = await options.adapter.redeemFullPriceCoupon({ email: baseEmail, couponId, productIds, currentUserId: currentUser?.id, redeemingProductId: productIds?.[0] }); if (createdPurchase) { const { purchase, redeemingForCurrentUser } = createdPurchase; if (redeemingForCurrentUser) { if (purchase) { const product = await options.adapter.getProduct(purchase.productId); if (product) { try { await options.inngest.send({ name: FULL_PRICE_COUPON_REDEEMED_EVENT, data: { purchaseId: purchase.id, productType: product.type || "self-paced" }, user: { id: purchase.userId, email: baseEmail } }); } catch (e) { console.log("error sending inngest event for current user redemption", e); throw new Error("unable-to-send-inngest-event"); } } } } else if (purchase?.userId && sendEmail) { const user = await options.adapter.getUserById(purchase.userId); if (!user) throw new Error(`unable-to-find-user-with-id-${purchase.id}`); const emailProvider = options.providers.find((p) => p.type === "email"); const product = await options.adapter.getProduct(purchase.productId); if (product) { try { await options.inngest.send({ name: FULL_PRICE_COUPON_REDEEMED_EVENT, data: { purchaseId: purchase.id, productType: product.type || "self-paced" }, user }); } catch (e) { console.log("error", e); throw new Error("unable-to-send-inngest-event"); } } try { await sendServerEmail({ email: user.email, baseUrl: options.baseUrl, callbackUrl: `${options.baseUrl}/welcome?purchaseId=${purchase.id}`, adapter: options.adapter, authOptions: options.authConfig, emailProvider: emailProvider ? emailProvider : void 0 }); } catch (e) { console.log("error", e); throw new Error("unable-to-send-email"); } } return { body: { purchase, redeemingForCurrentUser }, headers: { "Content-Type": "application/json" }, cookies }; } else { throw new Error(`coupon-is-invalid-${couponId}`); } } __name(redeem, "redeem"); export { redeem }; //# sourceMappingURL=chunk-32RI6ONG.js.map