Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 43 additions & 51 deletions apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,61 +166,53 @@ export async function confirmPayouts({
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_" }),
number: invoiceNumber,
programId: program.id,
workspaceId: workspace.id,
amount: payoutAmount,
fee: totalFee,
total,
},
});

if (!invoice) {
throw new Error("Failed to create payout invoice.");
}
const invoice = await prisma.invoice.create({
data: {
id: createId({ prefix: "inv_" }),
number: invoiceNumber,
programId: program.id,
workspaceId: workspace.id,
amount: payoutAmount,
fee: totalFee,
total,
},
});

await stripe.paymentIntents.create({
amount: convertedTotal,
customer: workspace.stripeId!,
payment_method_types: [paymentMethod.type],
payment_method: paymentMethod.id,
currency,
confirmation_method: "automatic",
confirm: true,
transfer_group: invoice.id,
statement_descriptor: "Dub Partners",
description: `Dub Partners payout invoice (${invoice.id})`,
});
await stripe.paymentIntents.create({
amount: convertedTotal,
customer: workspace.stripeId!,
payment_method_types: [paymentMethod.type],
payment_method: paymentMethod.id,
currency,
confirmation_method: "automatic",
confirm: true,
transfer_group: invoice.id,
statement_descriptor: "Dub Partners",
description: `Dub Partners payout invoice (${invoice.id})`,
});

await tx.payout.updateMany({
where: {
id: {
in: payouts.map((p) => p.id),
},
},
data: {
invoiceId: invoice.id,
status: "processing",
userId,
await prisma.payout.updateMany({
where: {
id: {
in: payouts.map((p) => p.id),
},
});
},
data: {
invoiceId: invoice.id,
status: "processing",
userId,
},
});

await tx.project.update({
where: {
id: workspace.id,
},
data: {
payoutsUsage: {
increment: payoutAmount,
},
await prisma.project.update({
where: {
id: workspace.id,
},
data: {
payoutsUsage: {
increment: payoutAmount,
},
});

return invoice;
},
});

await log({
Expand All @@ -231,7 +223,7 @@ export async function confirmPayouts({
// 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 up
if (
newInvoice &&
invoice &&
DIRECT_DEBIT_PAYMENT_METHOD_TYPES.includes(paymentMethod.type)
) {
if (!resend) {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/(ee)/api/cron/payouts/confirm/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function POST(req: Request) {
} catch (error) {
await log({
message: `Error confirming payouts for program: ${error.message}`,
type: "cron",
type: "errors",
});

return handleAndReturnErrorResponse(error);
Expand Down
120 changes: 61 additions & 59 deletions apps/web/app/(ee)/app.dub.co/invoices/[invoiceId]/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -317,68 +317,70 @@ export const GET = withSession(async ({ session, params }) => {
</View>
</View>

<View style={tw("mb-6 border border-neutral-200 rounded-xl")}>
<View style={tw("flex-row border-neutral-200 border-b")}>
<Text
style={tw("w-2/6 p-3.5 text-sm font-medium text-neutral-700")}
>
Partner
</Text>
<Text
style={tw("w-2/6 p-3.5 text-sm font-medium text-neutral-700")}
>
Period
</Text>
<Text
style={tw("w-1/6 p-3.5 text-sm font-medium text-neutral-700")}
>
Amount
</Text>
</View>

{invoice.payouts.map((payout, index) => (
<View
key={index}
style={tw(
`flex-row text-sm font-medium text-neutral-700 border-neutral-200 items-center ${index + 1 === invoice.payouts.length ? "" : "border-b"}`,
)}
>
<View style={tw("flex-row items-center gap-2 w-2/6 p-3.5")}>
<Image
src={
payout.partner.image ??
`${OG_AVATAR_URL}${payout.partner.name}`
}
style={tw("w-5 h-5 rounded-full")}
/>
<Text>{payout.partner.name}</Text>
</View>
<Text style={tw("w-2/6 p-3.5")}>
{payout.periodStart && payout.periodEnd ? (
<>
{formatDate(payout.periodStart, {
month: "short",
year:
new Date(payout.periodStart).getFullYear() ===
new Date(payout.periodEnd).getFullYear()
? undefined
: "numeric",
})}
-{formatDate(payout.periodEnd, { month: "short" })}
</>
) : (
"-"
)}
{invoice.payouts.length > 0 && (
<View style={tw("mb-6 border border-neutral-200 rounded-xl")}>
<View style={tw("flex-row border-neutral-200 border-b")}>
<Text
style={tw("w-2/6 p-3.5 text-sm font-medium text-neutral-700")}
>
Partner
</Text>
<Text style={tw("w-1/6 p-3.5")}>
{currencyFormatter(payout.amount / 100, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}
<Text
style={tw("w-2/6 p-3.5 text-sm font-medium text-neutral-700")}
>
Period
</Text>
<Text
style={tw("w-1/6 p-3.5 text-sm font-medium text-neutral-700")}
>
Amount
</Text>
</View>
))}
</View>

{invoice.payouts.map((payout, index) => (
<View
key={index}
style={tw(
`flex-row text-sm font-medium text-neutral-700 border-neutral-200 items-center ${index + 1 === invoice.payouts.length ? "" : "border-b"}`,
)}
>
<View style={tw("flex-row items-center gap-2 w-2/6 p-3.5")}>
<Image
src={
payout.partner.image ??
`${OG_AVATAR_URL}${payout.partner.name}`
}
style={tw("w-5 h-5 rounded-full")}
/>
<Text>{payout.partner.name}</Text>
</View>
<Text style={tw("w-2/6 p-3.5")}>
{payout.periodStart && payout.periodEnd ? (
<>
{formatDate(payout.periodStart, {
month: "short",
year:
new Date(payout.periodStart).getFullYear() ===
new Date(payout.periodEnd).getFullYear()
? undefined
: "numeric",
})}
-{formatDate(payout.periodEnd, { month: "short" })}
</>
) : (
"-"
)}
</Text>
<Text style={tw("w-1/6 p-3.5")}>
{currencyFormatter(payout.amount / 100, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}
</Text>
</View>
))}
</View>
)}

<View
style={tw(
Expand Down