Open source CapCut alternative.
TypeScript
fix(site): give the promotion test send a working claim link
David Le24f9c3aee750
diff --git a/site/src/lib/email/kinds.ts b/site/src/lib/email/kinds.tsindex da879137..92f7fd79 100644--- a/site/src/lib/email/kinds.ts+++ b/site/src/lib/email/kinds.ts@@ -12,7 +12,7 @@ import { buildWelcomeEmail, recordWelcomeSent } from "@/lib/email/send-welcome";import { isMarketingUnsubscribed } from "@/lib/email/unsubscribe";import { buildReplyForwardEmail, replyForwardPayloadSchema } from "@/lib/marketing/forward-reply";import { UnknownPlaceholderError } from "@/lib/marketing/placeholders";-import { SAMPLE_CLAIM_URL, promotionOfferOf } from "@/lib/marketing/promotionOfferInput";+import { promotionOfferOf } from "@/lib/marketing/promotionOfferInput";import { buildPromotionEmail, promotionCopyOf } from "@/lib/marketing/promotions";import { buildOutreachEmail, outreachPayloadSchema, recordOutreachSent } from "@/lib/marketing/send-outreach";import { prisma } from "@/lib/prisma";@@ -125,6 +125,25 @@ async function abandonPromotion(promotionId: string, error: Error): Promise<voidawait prisma.promotion.update({ data: { status: "draft" }, where: { id: promotionId } });}+// A promotion with a credit offer mails each person their own claim link,+// backed by an offer row that the real send and the test send share.+async function promotionEmailFor(promotion: Awaited<ReturnType<typeof promotionRowOf>>, user: EmailUser) {+ const offer = promotionOfferOf(promotion.creditOffer);+ const claimUrl = offer+ ? (+ await createPromotionCreditOffer({+ promotionId: promotion.id,+ userId: user.id,+ amountDollars: offer.dollars,+ claimWindowDays: offer.claimWindowDays,+ expiresAfterDays: offer.expiresAfterDays,+ offeredByUserId: promotion.actorUserId,+ })+ ).claimUrl+ : undefined;+ return buildPromotionEmail(promotionCopyOf(promotion), user, claimUrl);+}+const promotion = define({quota: "bulk",payload: promotionPayloadSchema,@@ -133,33 +152,20 @@ const promotion = define({// The opt-out is read again right before the send, so someone who// unsubscribed after the segment was resolved is skipped.if (!user || (await isMarketingUnsubscribed(user.id))) return null;- const offer = promotionOfferOf(promotion.creditOffer);- const claimUrl = offer- ? (- await createPromotionCreditOffer({- promotionId,- userId: user.id,- amountDollars: offer.dollars,- claimWindowDays: offer.claimWindowDays,- expiresAfterDays: offer.expiresAfterDays,- offeredByUserId: promotion.actorUserId,- })- ).claimUrl- : undefined;- return buildPromotionEmail(promotionCopyOf(promotion), user, claimUrl);+ return promotionEmailFor(promotion, user);},onAbandon: async ({ promotionId }, _row, error) => abandonPromotion(promotionId, error),});// The saved copy mailed to the operator, placeholders filled with their own-// account and a sample claim link where the copy has one.+// account. A credit offer gives them their own working claim link.const promotionTest = define({quota: "manual",payload: promotionPayloadSchema,build: async ({ promotionId }, row) => {const [user, promotion] = await Promise.all([userOf(row), promotionRowOf(promotionId)]);if (!user) return null;- return buildPromotionEmail(promotionCopyOf(promotion), user, promotion.creditOffer ? SAMPLE_CLAIM_URL : undefined);+ return promotionEmailFor(promotion, user);},});diff --git a/site/src/lib/marketing/promotionOfferInput.ts b/site/src/lib/marketing/promotionOfferInput.tsindex 3c2ae27f..b68a5487 100644--- a/site/src/lib/marketing/promotionOfferInput.ts+++ b/site/src/lib/marketing/promotionOfferInput.ts@@ -8,7 +8,6 @@ export const promotionOfferSchema = z.object({export type PromotionOffer = z.infer<typeof promotionOfferSchema>;export const CLAIM_URL_PLACEHOLDER = "{{claimUrl}}";-export const SAMPLE_CLAIM_URL = "https://example.com/preview-credit-offer";export function promotionOfferOf(value: unknown): PromotionOffer | null {return value == null ? null : promotionOfferSchema.parse(value);diff --git a/site/src/lib/marketing/promotionSave.ts b/site/src/lib/marketing/promotionSave.tsindex 812c19f0..e08e6b6d 100644--- a/site/src/lib/marketing/promotionSave.ts+++ b/site/src/lib/marketing/promotionSave.ts@@ -1,5 +1,4 @@import { Prisma } from "@/generated/prisma/client";-import { SAMPLE_CLAIM_URL } from "@/lib/marketing/promotionOfferInput";import { invalidResponse } from "@/lib/config/experimentList";import { UnknownPlaceholderError } from "@/lib/marketing/placeholders";import { renderPromotion, SAMPLE_RECIPIENT } from "@/lib/marketing/promotionCopy";@@ -7,11 +6,12 @@ import type { PromotionInput } from "@/lib/marketing/promotionInput";// What a save of a promotion shares between create and edit: the copy is// rendered for a stand-in recipient, so a placeholder typo is refused at the-// save and never reaches a send; and the row data the input maps to.+// save and never reaches a send; and the row data the input maps to. The+// claim link is minted per person at the send, so the check fills it blank.export function copyIssue(input: PromotionInput): Response | null {try {- renderPromotion(input, SAMPLE_RECIPIENT, input.creditOffer ? SAMPLE_CLAIM_URL : undefined);+ renderPromotion(input, SAMPLE_RECIPIENT, input.creditOffer ? "" : undefined);return null;} catch (error) {if (error instanceof UnknownPlaceholderError) {