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.

1.2 KB
Raw Blame
"use server";

import { prisma } from "@/lib/prisma";
import * as z from "zod/v4";
import { workspacePreferencesValueSchemas } from "../zod/schemas/workspace-preferences";
import { authActionClient } from "./safe-action";

const schema = z.object({
  workspaceId: z.string(),
  key: z.string(),
  value: z.any(),
});

// Update a user's preferences for a workspace
export const updateWorkspacePreferences = authActionClient
  .inputSchema(schema)
  .action(async ({ ctx, parsedInput }) => {
    const { workspace } = ctx;
    const { key, value } = parsedInput;

    const valueSchema = workspacePreferencesValueSchemas[key];

    if (!valueSchema)
      throw new Error(`Invalid workspace preference key: ${key}`);

    const parsedValue = valueSchema.parse(value);

    const workspacePreferences =
      (workspace.users[0].workspacePreferences as
        | Record<string, any>
        | undefined
        | null) ?? {};

    await prisma.projectUsers.update({
      where: {
        userId_projectId: {
          userId: ctx.user.id,
          projectId: workspace.id,
        },
      },
      data: {
        workspacePreferences: {
          ...workspacePreferences,
          [key]: parsedValue,
        },
      },
    });

    return { ok: true };
  });