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.

Consolidate program resource schemas and simplify actions

Kiran K57fd089777e9
diff --git a/apps/web/lib/actions/partners/program-resources/add-program-resource.ts b/apps/web/lib/actions/partners/program-resources/add-program-resource.ts
index f166dc1dd0..9df5b73578 100644
--- a/apps/web/lib/actions/partners/program-resources/add-program-resource.ts
+++ b/apps/web/lib/actions/partners/program-resources/add-program-resource.ts
@@ -27,11 +27,9 @@ export const addProgramResourceAction = authActionClient
const programId = getDefaultProgramIdOrThrow(workspace);
- // Verify the program exists and belongs to the workspace
- const program = await prisma.program.findUnique({
+ const program = await prisma.program.findUniqueOrThrow({
where: {
id: programId,
- workspaceId: workspace.id,
},
select: {
id: true,
@@ -39,8 +37,6 @@ export const addProgramResourceAction = authActionClient
},
});
- if (!program) throw new Error("Program not found");
-
const currentResources = (program.resources as any) || {
logos: [],
colors: [],
diff --git a/apps/web/lib/actions/partners/program-resources/delete-program-resource.ts b/apps/web/lib/actions/partners/program-resources/delete-program-resource.ts
index 07ce4cebca..5b64654130 100644
--- a/apps/web/lib/actions/partners/program-resources/delete-program-resource.ts
+++ b/apps/web/lib/actions/partners/program-resources/delete-program-resource.ts
@@ -4,21 +4,13 @@ import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-progr
import { prisma } from "@/lib/prisma";
import { storage } from "@/lib/storage";
import {
- PROGRAM_RESOURCE_TYPES,
+ deleteProgramResourceSchema,
programResourcesSchema,
} from "@/lib/zod/schemas/program-resources";
import { R2_URL } from "@dub/utils";
-import * as z from "zod/v4";
import { authActionClient } from "../../safe-action";
import { throwIfNoPermission } from "../../throw-if-no-permission";
-// Schema for deleting a program resource
-const deleteProgramResourceSchema = z.object({
- workspaceId: z.string(),
- resourceType: z.enum(PROGRAM_RESOURCE_TYPES),
- resourceId: z.string(),
-});
-
export const deleteProgramResourceAction = authActionClient
.inputSchema(deleteProgramResourceSchema)
.action(async ({ ctx, parsedInput }) => {
@@ -32,11 +24,9 @@ export const deleteProgramResourceAction = authActionClient
const programId = getDefaultProgramIdOrThrow(workspace);
- // Verify the program exists and belongs to the workspace
- const program = await prisma.program.findUnique({
+ const program = await prisma.program.findUniqueOrThrow({
where: {
id: programId,
- workspaceId: workspace.id,
},
select: {
id: true,
@@ -44,8 +34,9 @@ export const deleteProgramResourceAction = authActionClient
},
});
- if (!program) throw new Error("Program not found");
- if (!program.resources) throw new Error("Program resources not found");
+ if (!program.resources) {
+ throw new Error("Program resources not found");
+ }
// Create a copy of the current resources to update
const updatedResources = { ...(program.resources as any) };
diff --git a/apps/web/lib/actions/partners/program-resources/update-program-resource.ts b/apps/web/lib/actions/partners/program-resources/update-program-resource.ts
index 41f36b34d5..198e42fd80 100644
--- a/apps/web/lib/actions/partners/program-resources/update-program-resource.ts
+++ b/apps/web/lib/actions/partners/program-resources/update-program-resource.ts
@@ -28,11 +28,9 @@ export const updateProgramResourceAction = authActionClient
const programId = getDefaultProgramIdOrThrow(workspace);
- // Verify the program exists and belongs to the workspace
- const program = await prisma.program.findUnique({
+ const program = await prisma.program.findUniqueOrThrow({
where: {
id: programId,
- workspaceId: workspace.id,
},
select: {
id: true,
@@ -40,8 +38,9 @@ export const updateProgramResourceAction = authActionClient
},
});
- if (!program) throw new Error("Program not found");
- if (!program.resources) throw new Error("Program resources not found");
+ if (!program.resources) {
+ throw new Error("Program resources not found.");
+ }
const currentResources = program.resources as any;
const updatedResources = { ...currentResources };
@@ -127,7 +126,7 @@ export const updateProgramResourceAction = authActionClient
id: program.id,
},
data: {
- resources: programResourcesSchema.parse(updatedResources) as any,
+ resources: programResourcesSchema.parse(updatedResources),
},
});
@@ -144,8 +143,4 @@ export const updateProgramResourceAction = authActionClient
);
}
}
-
- return {
- success: true,
- };
});
diff --git a/apps/web/lib/zod/schemas/program-resources.ts b/apps/web/lib/zod/schemas/program-resources.ts
index 787bf2135f..3d12a41b34 100644
--- a/apps/web/lib/zod/schemas/program-resources.ts
+++ b/apps/web/lib/zod/schemas/program-resources.ts
@@ -1,11 +1,6 @@
import * as z from "zod/v4";
-export const PROGRAM_RESOURCE_TYPES = [
- "logo",
- "file",
- "color",
- "link",
-] as const;
+const PROGRAM_RESOURCE_TYPES = ["logo", "file", "color", "link"] as const;
export type ProgramResourceType = (typeof PROGRAM_RESOURCE_TYPES)[number];
@@ -23,7 +18,7 @@ export const programResourceColorSchema = z.object({
});
// Allow http(s) and mailto links, but block dangerous schemes (e.g. javascript:, data:)
-export const programResourceLinkUrlSchema = z.url({
+const programResourceLinkUrlSchema = z.url({
protocol: /^(https?|mailto)$/,
});
@@ -43,7 +38,6 @@ export const programResourcesSchema = z.object({
export type ProgramResourceFile = z.infer<typeof programResourceFileSchema>;
export type ProgramResourceColor = z.infer<typeof programResourceColorSchema>;
export type ProgramResourceLink = z.infer<typeof programResourceLinkSchema>;
-export type ProgramResources = z.infer<typeof programResourcesSchema>;
const baseResourceInputSchema = z.object({
workspaceId: z.string(),
@@ -60,14 +54,17 @@ const logoResourceInputSchema = baseResourceInputSchema.extend({
resourceType: z.literal("logo"),
...fileResourceInputFields,
});
+
const fileResourceInputSchema = baseResourceInputSchema.extend({
resourceType: z.literal("file"),
...fileResourceInputFields,
});
+
const colorResourceInputSchema = baseResourceInputSchema.extend({
resourceType: z.literal("color"),
color: z.string(),
});
+
const linkResourceInputSchema = baseResourceInputSchema.extend({
resourceType: z.literal("link"),
url: programResourceLinkUrlSchema,
@@ -99,3 +96,9 @@ export const updateProgramResourceSchema = z.discriminatedUnion(
.extend({ resourceId: z.string() }),
],
);
+
+export const deleteProgramResourceSchema = z.object({
+ workspaceId: z.string(),
+ resourceType: z.enum(PROGRAM_RESOURCE_TYPES),
+ resourceId: z.string(),
+});