Skip to content

Commit

Permalink
replace action errors with returnValidationErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuasilva414 committed Sep 27, 2024
1 parent 95717d9 commit 7975007
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
5 changes: 4 additions & 1 deletion apps/web/src/actions/admin/user-actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use server";

import { adminAction } from "@/lib/safe-action";
import { returnValidationErrors } from "next-safe-action";
import { z } from "zod";
import { perms } from "config";
import { userCommonData } from "db/schema";
Expand All @@ -24,7 +25,9 @@ export const updateRole = adminAction
user.role !== "super_admin" &&
(roleToSet === "super_admin" || roleToSet === "admin")
) {
throw new Error("You are not allowed to do this");
returnValidationErrors(z.null(), {
_errors: ["You are not allowed to do this!"],
});
}
await db
.update(userCommonData)
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/actions/rsvp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { db } from "db";
import { eq } from "db/drizzle";
import { userCommonData } from "db/schema";
import { getUser } from "db/functions";
import { returnValidationErrors } from "next-safe-action";

export const rsvpMyself = authenticatedAction.action(
async ({ ctx: { userId } }) => {
const user = await getUser(userId);
if (!user) throw new Error("User not found");
if (!user)
returnValidationErrors(z.null(), { _errors: ["User not found"] });

await db
.update(userCommonData)
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/actions/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import { userHackerData, teams, invites } from "db/schema";
import { eq } from "db/drizzle";
import { revalidatePath } from "next/cache";
import { getHacker } from "db/functions";
import { returnValidationErrors } from "next-safe-action";

export const leaveTeam = authenticatedAction.action(
async ({ ctx: { userId } }) => {
const user = await getHacker(userId, false);
if (!user) throw new Error("User not found");
if (!user)
returnValidationErrors(z.null(), { _errors: ["User not found"] });

if (!user.hackerData.teamID) {
revalidatePath("/dash/team");
Expand Down
9 changes: 7 additions & 2 deletions apps/web/src/actions/user-profile-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { userCommonData } from "db/schema";
import { eq } from "db/drizzle";
import { put } from "@vercel/blob";
import { decodeBase64AsFile } from "@/lib/utils/shared/files";
import { returnValidationErrors } from "next-safe-action";
import { revalidatePath } from "next/cache";
import { getUser } from "db/functions";

Expand All @@ -20,7 +21,8 @@ export const modifyRegistrationData = authenticatedAction
)
.action(async ({ parsedInput: { bio, skills }, ctx: { userId } }) => {
const user = await getUser(userId);
if (!user) throw new Error("User not found");
if (!user)
returnValidationErrors(z.null(), { _errors: ["User not found"] });

await db
.update(userCommonData)
Expand Down Expand Up @@ -59,7 +61,10 @@ export const updateProfileImage = authenticatedAction
async ({ parsedInput: { fileBase64, fileName }, ctx: { userId } }) => {
const image = await decodeBase64AsFile(fileBase64, fileName);
const user = await getUser(userId);
if (!user) throw new Error("User not found");
if (!user)
returnValidationErrors(z.null(), {
_errors: ["User not found"],
});

const blobUpload = await put(image.name, image, {
access: "public",
Expand Down

0 comments on commit 7975007

Please sign in to comment.