Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

♻️ Refactor safe action middleware (#1809)

Luke Vellac2701a4d4f6a
diff --git a/apps/web/src/app/[locale]/(space)/settings/profile/actions.ts b/apps/web/src/app/[locale]/(space)/settings/profile/actions.ts
index 0ae91251a..dd3aca198 100644
--- a/apps/web/src/app/[locale]/(space)/settings/profile/actions.ts
+++ b/apps/web/src/app/[locale]/(space)/settings/profile/actions.ts
@@ -5,8 +5,9 @@ import { signOut } from "@/next-auth";
import { subject } from "@casl/ability";
import { prisma } from "@rallly/database";
-export const deleteCurrentUserAction = authActionClient.action(
- async ({ ctx }) => {
+export const deleteCurrentUserAction = authActionClient
+ .metadata({ actionName: "delete_current_user" })
+ .action(async ({ ctx }) => {
const userId = ctx.user.id;
const user = await prisma.user.findUnique({
@@ -34,18 +35,9 @@ export const deleteCurrentUserAction = authActionClient.action(
},
});
- ctx.posthog?.capture({
- event: "delete_account",
- distinctId: ctx.user.id,
- properties: {
- email: ctx.user.email,
- },
- });
-
await signOut();
return {
success: true,
};
- },
-);
+ });
diff --git a/apps/web/src/app/[locale]/admin-setup/actions.ts b/apps/web/src/app/[locale]/admin-setup/actions.ts
index d9a3ef17a..ca69e0ace 100644
--- a/apps/web/src/app/[locale]/admin-setup/actions.ts
+++ b/apps/web/src/app/[locale]/admin-setup/actions.ts
@@ -5,22 +5,24 @@ import { subject } from "@casl/ability";
import { prisma } from "@rallly/database";
import { redirect } from "next/navigation";
-export const makeMeAdminAction = authActionClient.action(async ({ ctx }) => {
- if (ctx.ability.cannot("update", subject("User", ctx.user), "role")) {
- throw new ActionError({
- code: "UNAUTHORIZED",
- message: "You are not authorized to update your role",
+export const makeMeAdminAction = authActionClient
+ .metadata({ actionName: "make_admin" })
+ .action(async ({ ctx }) => {
+ if (ctx.ability.cannot("update", subject("User", ctx.user), "role")) {
+ throw new ActionError({
+ code: "UNAUTHORIZED",
+ message: "You are not authorized to update your role",
+ });
+ }
+
+ await prisma.user.update({
+ where: {
+ id: ctx.user.id,
+ },
+ data: {
+ role: "admin",
+ },
});
- }
- await prisma.user.update({
- where: {
- id: ctx.user.id,
- },
- data: {
- role: "admin",
- },
+ redirect("/control-panel");
});
-
- redirect("/control-panel");
-});
diff --git a/apps/web/src/features/safe-action/server.ts b/apps/web/src/features/safe-action/server.ts
index a958c4a55..fe012966a 100644
--- a/apps/web/src/features/safe-action/server.ts
+++ b/apps/web/src/features/safe-action/server.ts
@@ -2,7 +2,9 @@
import { requireUserAbility } from "@/auth/queries";
import { posthog } from "@rallly/posthog/server";
import { waitUntil } from "@vercel/functions";
-import { createSafeActionClient } from "next-safe-action";
+import { createMiddleware, createSafeActionClient } from "next-safe-action";
+import { revalidatePath } from "next/cache";
+import z from "zod";
type ActionErrorCode =
| "UNAUTHORIZED"
@@ -28,30 +30,60 @@ export class ActionError extends Error {
}
}
-export const actionClient = createSafeActionClient({
- handleServerError: async (error) => {
- if (error instanceof ActionError) {
- return error.code;
- }
+const autoRevalidateMiddleware = createMiddleware().define(async ({ next }) => {
+ const result = await next();
+ revalidatePath("/", "layout");
+ return result;
+});
- return "INTERNAL_SERVER_ERROR";
- },
-}).use(({ next }) => {
- const result = next({
+const posthogMiddleware = createMiddleware<{
+ ctx: { user: { id: string } };
+ metadata: { actionName: string };
+}>().define(async ({ ctx, next, metadata }) => {
+ let properties: Record<string, unknown> | undefined;
+
+ const result = await next({
ctx: {
posthog,
+ captureProperties: (props?: Record<string, unknown>) => {
+ properties = props;
+ },
},
});
- waitUntil(Promise.all([posthog?.shutdown()]));
+ posthog?.capture({
+ distinctId: ctx.user.id,
+ event: metadata.actionName,
+ properties,
+ });
+
+ if (posthog) {
+ waitUntil(posthog.shutdown());
+ }
return result;
});
-export const authActionClient = actionClient.use(async ({ next }) => {
- const { user, ability } = await requireUserAbility();
+export const actionClient = createSafeActionClient({
+ defineMetadataSchema: () =>
+ z.object({
+ actionName: z.string(),
+ }),
+ handleServerError: async (error) => {
+ if (error instanceof ActionError) {
+ return error.code;
+ }
+
+ return "INTERNAL_SERVER_ERROR";
+ },
+}).use(autoRevalidateMiddleware);
+
+export const authActionClient = actionClient
+ .use(async ({ next }) => {
+ const { user, ability } = await requireUserAbility();
- return next({
- ctx: { user, ability },
- });
-});
+ return next({
+ ctx: { user, ability },
+ });
+ })
+ .use(posthogMiddleware);
diff --git a/apps/web/src/features/scheduled-event/actions.ts b/apps/web/src/features/scheduled-event/actions.ts
index 4d4b453c2..13d70a8c2 100644
--- a/apps/web/src/features/scheduled-event/actions.ts
+++ b/apps/web/src/features/scheduled-event/actions.ts
@@ -3,11 +3,11 @@ import { ActionError, authActionClient } from "@/features/safe-action/server";
import { getEmailClient } from "@/utils/emails";
import { subject } from "@casl/ability";
import { prisma } from "@rallly/database";
-import { revalidatePath } from "next/cache";
import { z } from "zod";
import { formatEventDateTime } from "./utils";
export const cancelEventAction = authActionClient
+ .metadata({ actionName: "cancel_event" })
.inputSchema(
z.object({
eventId: z.string(),
@@ -43,14 +43,8 @@ export const cancelEventAction = authActionClient
},
});
- revalidatePath("/", "layout");
-
- ctx.posthog?.capture({
- event: "cancel_event",
- distinctId: ctx.user.id,
- properties: {
- event_id: parsedInput.eventId,
- },
+ ctx.captureProperties({
+ eventId: parsedInput.eventId,
});
// notify attendees
diff --git a/apps/web/src/features/setup/actions.ts b/apps/web/src/features/setup/actions.ts
index 7030adfb7..428e15aa9 100644
--- a/apps/web/src/features/setup/actions.ts
+++ b/apps/web/src/features/setup/actions.ts
@@ -1,12 +1,12 @@
"use server";
import { prisma } from "@rallly/database";
-import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { authActionClient } from "@/features/safe-action/server";
import { setupSchema } from "./schema";
-export const updateUserAction = authActionClient
+export const completeSetupAction = authActionClient
+ .metadata({ actionName: "complete_setup" })
.inputSchema(setupSchema)
.action(async ({ parsedInput, ctx }) => {
const { name, timeZone, locale } = parsedInput;
@@ -20,19 +20,13 @@ export const updateUserAction = authActionClient
},
});
- ctx.posthog?.capture({
- event: "user_setup_completed",
- distinctId: ctx.user.id,
- properties: {
- $set: {
- name,
- timeZone,
- locale,
- },
+ ctx.captureProperties({
+ $set: {
+ name,
+ timeZone,
+ locale,
},
});
- revalidatePath("/", "layout");
-
redirect("/");
});
diff --git a/apps/web/src/features/setup/components/setup-form.tsx b/apps/web/src/features/setup/components/setup-form.tsx
index 5d3c8c15b..bd0f48a2c 100644
--- a/apps/web/src/features/setup/components/setup-form.tsx
+++ b/apps/web/src/features/setup/components/setup-form.tsx
@@ -20,7 +20,7 @@ import { useSafeAction } from "@/features/safe-action/client";
import { useTimezone } from "@/features/timezone";
import { useTranslation } from "@/i18n/client";
-import { updateUserAction } from "../actions";
+import { completeSetupAction } from "../actions";
import { type SetupFormValues, setupSchema } from "../schema";
interface SetupFormProps {
@@ -30,7 +30,7 @@ interface SetupFormProps {
export function SetupForm({ defaultValues }: SetupFormProps) {
const { timezone } = useTimezone();
const { i18n } = useTranslation();
- const userSetupAction = useSafeAction(updateUserAction);
+ const completeSetup = useSafeAction(completeSetupAction);
const form = useForm<SetupFormValues>({
resolver: zodResolver(setupSchema),
@@ -45,7 +45,7 @@ export function SetupForm({ defaultValues }: SetupFormProps) {
<Form {...form}>
<form
onSubmit={form.handleSubmit(async (data) => {
- await userSetupAction.executeAsync(data);
+ await completeSetup.executeAsync(data);
})}
>
<div className="space-y-4">
@@ -108,8 +108,8 @@ export function SetupForm({ defaultValues }: SetupFormProps) {
</FormItem>
)}
/>
- {userSetupAction.result.serverError && (
- <FormMessage>{userSetupAction.result.serverError}</FormMessage>
+ {completeSetup.result.serverError && (
+ <FormMessage>{completeSetup.result.serverError}</FormMessage>
)}
</div>
<div className="mt-6">
diff --git a/apps/web/src/features/user/actions.ts b/apps/web/src/features/user/actions.ts
index 8b6588d3d..bdf0828fd 100644
--- a/apps/web/src/features/user/actions.ts
+++ b/apps/web/src/features/user/actions.ts
@@ -2,11 +2,11 @@
import { ActionError, authActionClient } from "@/features/safe-action/server";
import { subject } from "@casl/ability";
import { prisma } from "@rallly/database";
-import { revalidatePath } from "next/cache";
import { z } from "zod";
import { getUser } from "./queries";
export const changeRoleAction = authActionClient
+ .metadata({ actionName: "change_role" })
.inputSchema(
z.object({
userId: z.string(),
@@ -47,11 +47,10 @@ export const changeRoleAction = authActionClient
role,
},
});
-
- revalidatePath("/control-panel");
});
export const deleteUserAction = authActionClient
+ .metadata({ actionName: "delete_user" })
.inputSchema(
z.object({
userId: z.string(),
@@ -86,8 +85,6 @@ export const deleteUserAction = authActionClient
},
});
- revalidatePath("/control-panel");
-
return {
success: true,
};