Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update invoicePrefix

Steven Tey7a0d51c59ab9
diff --git a/apps/web/lib/actions/partners/create-program.ts b/apps/web/lib/actions/partners/create-program.ts
index 6d91b6e339..51172b7126 100644
--- a/apps/web/lib/actions/partners/create-program.ts
+++ b/apps/web/lib/actions/partners/create-program.ts
@@ -9,7 +9,7 @@ import { programDataSchema } from "@/lib/zod/schemas/program-onboarding";
import { sendEmail } from "@dub/email";
import { PartnerInvite } from "@dub/email/templates/partner-invite";
import { prisma } from "@dub/prisma";
-import { nanoid, R2_URL } from "@dub/utils";
+import { generateRandomString, nanoid, R2_URL } from "@dub/utils";
import { Program, Project, User } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
@@ -18,7 +18,10 @@ export const createProgram = async ({
workspace,
user,
}: {
- workspace: Pick<Project, "id" | "slug" | "plan" | "store" | "webhookEnabled">;
+ workspace: Pick<
+ Project,
+ "id" | "slug" | "plan" | "store" | "webhookEnabled" | "invoicePrefix"
+ >;
user: Pick<User, "id">;
}) => {
const store = workspace.store as Record<string, any>;
@@ -132,6 +135,10 @@ export const createProgram = async ({
...store,
programOnboarding: undefined,
},
+ // if the workspace doesn't have an invoice prefix, generate one
+ ...(!workspace.invoicePrefix && {
+ invoicePrefix: generateRandomString(8),
+ }),
},
}),
prisma.program.update({
diff --git a/apps/web/scripts/backfill-invoice-numbers.ts b/apps/web/scripts/backfill-invoice-numbers.ts
index 6e6ad7b246..9c918ebea8 100644
--- a/apps/web/scripts/backfill-invoice-numbers.ts
+++ b/apps/web/scripts/backfill-invoice-numbers.ts
@@ -1,14 +1,15 @@
import { prisma } from "@dub/prisma";
+import { generateRandomString } from "@dub/utils";
import "dotenv-flow/config";
async function main() {
const workspaces = await prisma.project.findMany({
where: {
- programs: {
- some: {},
- },
+ partnersEnabled: true,
},
select: {
+ id: true,
+ slug: true,
invoicePrefix: true,
invoices: {
select: {
@@ -23,10 +24,24 @@ async function main() {
const updatedInvoices = await Promise.all(
workspaces.flatMap(async (workspace) => {
+ // no need to update workspaces that already have an invoice prefix
+ if (workspace.invoicePrefix) {
+ return [];
+ }
+
+ const newWorkspace = await prisma.project.update({
+ where: { id: workspace.id },
+ data: { invoicePrefix: generateRandomString(8) },
+ });
+
+ console.log(
+ `Updated workspace ${workspace.slug} to have invoice prefix ${newWorkspace.invoicePrefix}`,
+ );
+
return workspace.invoices.map(async (invoice, idx) => {
return prisma.invoice.update({
where: { id: invoice.id },
- data: { number: `${workspace.invoicePrefix}-000${idx + 1}` },
+ data: { number: `${newWorkspace.invoicePrefix}-000${idx + 1}` },
select: { id: true, number: true, createdAt: true },
});
});