The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add payouts logs
Steven Teyeaea9ed072ea
diff --git a/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts b/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.tsindex 5b9571691d..457a1d26c1 100644--- a/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts+++ b/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts@@ -16,7 +16,7 @@ import { resend } from "@dub/email/resend";import { VARIANT_TO_FROM_MAP } from "@dub/email/resend/constants";import PartnerPayoutConfirmed from "@dub/email/templates/partner-payout-confirmed";import { prisma } from "@dub/prisma";-import { chunk, log } from "@dub/utils";+import { chunk, currencyFormatter, log } from "@dub/utils";import { Program, Project } from "@prisma/client";const paymentMethodToCurrency = {@@ -111,60 +111,60 @@ export async function confirmPayouts({const paymentMethod = await stripe.paymentMethods.retrieve(paymentMethodId);- // Create the invoice for the payouts- const newInvoice = await prisma.$transaction(async (tx) => {- const payoutFee = calculatePayoutFeeForMethod({- paymentMethod: paymentMethod.type,- payoutFee: workspace.payoutFee,- });-- if (!payoutFee) {- throw new Error("Failed to calculate payout fee.");- }-- console.info(- `Using payout fee of ${payoutFee} for payment method ${paymentMethod.type}`,- );+ const payoutFee = calculatePayoutFeeForMethod({+ paymentMethod: paymentMethod.type,+ payoutFee: workspace.payoutFee,+ });- const currency = paymentMethodToCurrency[paymentMethod.type] || "usd";- const totalFee = Math.round(payoutAmount * payoutFee);- const total = payoutAmount + totalFee;- let convertedTotal = total;+ if (!payoutFee) {+ throw new Error("Failed to calculate payout fee.");+ }- // convert the amount to EUR/CAD if the payment method is sepa_debit or acss_debit- if (["sepa_debit", "acss_debit"].includes(paymentMethod.type)) {- const fxQuote = await createFxQuote({- fromCurrency: currency,- toCurrency: "usd",- });+ console.info(+ `Using payout fee of ${payoutFee} for payment method ${paymentMethod.type}`,+ );- const exchangeRate = fxQuote.rates[currency].exchange_rate;+ const currency = paymentMethodToCurrency[paymentMethod.type] || "usd";+ const totalFee = Math.round(payoutAmount * payoutFee);+ const total = payoutAmount + totalFee;+ let convertedTotal = total;- // if Stripe's FX rate is not available, throw an error- if (!exchangeRate || exchangeRate <= 0) {- throw new Error(- `Failed to get exchange rate from Stripe for ${currency}.`,- );- }+ // convert the amount to EUR/CAD if the payment method is sepa_debit or acss_debit+ if (["sepa_debit", "acss_debit"].includes(paymentMethod.type)) {+ const fxQuote = await createFxQuote({+ fromCurrency: currency,+ toCurrency: "usd",+ });- convertedTotal = Math.round(- (total / exchangeRate) * (1 + FOREX_MARKUP_RATE),- );+ const exchangeRate = fxQuote.rates[currency].exchange_rate;- console.log(- `Currency conversion: ${total} usd -> ${convertedTotal} ${currency} using exchange rate ${exchangeRate}.`,+ // if Stripe's FX rate is not available, throw an error+ if (!exchangeRate || exchangeRate <= 0) {+ throw new Error(+ `Failed to get exchange rate from Stripe for ${currency}.`,);}- // Generate the next invoice number- const totalInvoices = await tx.invoice.count({- where: {- workspaceId: workspace.id,- },- });- const paddedNumber = String(totalInvoices + 1).padStart(4, "0");- const invoiceNumber = `${workspace.invoicePrefix}-${paddedNumber}`;+ convertedTotal = Math.round(+ (total / exchangeRate) * (1 + FOREX_MARKUP_RATE),+ );++ console.log(+ `Currency conversion: ${total} usd -> ${convertedTotal} ${currency} using exchange rate ${exchangeRate}.`,+ );+ }++ // Generate the next invoice number+ const totalInvoices = await prisma.invoice.count({+ where: {+ workspaceId: workspace.id,+ },+ });+ const paddedNumber = String(totalInvoices + 1).padStart(4, "0");+ const invoiceNumber = `${workspace.invoicePrefix}-${paddedNumber}`;+ // Create the invoice for the payouts+ const newInvoice = await prisma.$transaction(async (tx) => {const invoice = await tx.invoice.create({data: {id: createId({ prefix: "inv_" }),@@ -221,6 +221,11 @@ export async function confirmPayouts({return invoice;});+ await log({+ message: `*${program.name}* just sent a payout of *${currencyFormatter(payoutAmount / 100)}* :money_with_wings: \n\n Fees earned: *${currencyFormatter(totalFee / 100)} (${payoutFee * 100}%)* :money_mouth_face:`,+ type: "payouts",+ });+// Send emails to all the partners involved in the payouts if the payout method is Direct Debit// This is because Direct Debit takes 4 business days to process, so we want to give partners a heads upif (diff --git a/packages/utils/src/functions/log.ts b/packages/utils/src/functions/log.tsindex a80154f464..c6f776cdb3 100644--- a/packages/utils/src/functions/log.ts+++ b/packages/utils/src/functions/log.ts@@ -1,9 +1,9 @@const logTypeToEnv = {alerts: process.env.DUB_SLACK_HOOK_ALERTS,cron: process.env.DUB_SLACK_HOOK_CRON,- links: process.env.DUB_SLACK_HOOK_LINKS,- subscribers: process.env.DUB_SLACK_HOOK_SUBSCRIBERS,errors: process.env.DUB_SLACK_HOOK_ERRORS,+ links: process.env.DUB_SLACK_HOOK_LINKS,+ payouts: process.env.DUB_SLACK_HOOK_PAYOUTS,};export const log = async ({@@ -12,20 +12,12 @@ export const log = async ({mention = false,}: {message: string;- type: "alerts" | "cron" | "links" | "subscribers" | "errors";+ type: "alerts" | "cron" | "errors" | "links" | "payouts";mention?: boolean;}) => {- if (- process.env.NODE_ENV === "development" ||- !process.env.DUB_SLACK_HOOK_ALERTS ||- !process.env.DUB_SLACK_HOOK_CRON ||- !process.env.DUB_SLACK_HOOK_LINKS ||- !process.env.DUB_SLACK_HOOK_SUBSCRIBERS ||- !process.env.DUB_SLACK_HOOK_ERRORS- ) {- console.log(message);- }/* Log a message to the console */+ console.log(message);+const HOOK = logTypeToEnv[type];if (!HOOK) return;try {