refactor(recipients): move mutations into use cases
This commit is contained in:
@@ -1,138 +0,0 @@
|
||||
"use server"
|
||||
|
||||
import { revalidatePath } from "next/cache"
|
||||
|
||||
import prisma from "@/lib/prisma"
|
||||
import {
|
||||
type CreateRecipientFormType,
|
||||
createRecipientSchema,
|
||||
type UpdateRecipientFormType,
|
||||
updateRecipientSchema,
|
||||
} from "@/lib/schemas/recipients.schemas"
|
||||
import { RecipientService } from "@/services/recipient.service"
|
||||
|
||||
export async function createNewRecipient(formData: CreateRecipientFormType) {
|
||||
const validatedFields = createRecipientSchema.safeParse(formData)
|
||||
|
||||
if (!validatedFields.success) {
|
||||
return {
|
||||
success: false,
|
||||
errors: validatedFields.error.flatten().fieldErrors,
|
||||
}
|
||||
}
|
||||
|
||||
const { username, firstName, lastName, department, email, phone } =
|
||||
validatedFields.data
|
||||
|
||||
try {
|
||||
const existingRecipientUsername =
|
||||
await RecipientService.findByUsername(username)
|
||||
|
||||
if (existingRecipientUsername) {
|
||||
return {
|
||||
success: false,
|
||||
errors: {
|
||||
username: ["Username already exists"],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const existingRecipientEmail = await RecipientService.findByEmail(
|
||||
email || "",
|
||||
)
|
||||
|
||||
if (existingRecipientEmail) {
|
||||
return {
|
||||
success: false,
|
||||
errors: {
|
||||
email: ["Email already exists"],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
await RecipientService.create({
|
||||
username: username || (firstName[0] + lastName).toLowerCase(),
|
||||
firstName,
|
||||
lastName,
|
||||
department: department,
|
||||
email: email || null,
|
||||
phone: phone || null,
|
||||
})
|
||||
|
||||
revalidatePath("/recipients")
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Recipient created successfully",
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Database error:", error)
|
||||
return {
|
||||
message: "Failed to create recipient",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateRecipient(formData: UpdateRecipientFormType) {
|
||||
const validatedFields = updateRecipientSchema.safeParse(formData)
|
||||
|
||||
if (!validatedFields.success) {
|
||||
return {
|
||||
success: false,
|
||||
errors: validatedFields.error.flatten().fieldErrors,
|
||||
}
|
||||
}
|
||||
|
||||
const { id, username, firstName, lastName, department, email, phone } =
|
||||
validatedFields.data
|
||||
|
||||
try {
|
||||
const existingRecipient = await RecipientService.findByUsername(username)
|
||||
|
||||
if (existingRecipient && existingRecipient.id !== id) {
|
||||
return {
|
||||
success: false,
|
||||
errors: {
|
||||
username: ["Username already exists"],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const existingRecipientEmail = await RecipientService.findByEmail(
|
||||
email || "",
|
||||
)
|
||||
|
||||
if (existingRecipientEmail && existingRecipientEmail.id !== id) {
|
||||
return {
|
||||
success: false,
|
||||
errors: {
|
||||
email: ["Email already exists"],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.recipient.update({
|
||||
where: { id },
|
||||
data: {
|
||||
username,
|
||||
firstName,
|
||||
lastName,
|
||||
department: department,
|
||||
email: email || null,
|
||||
phone: phone || null,
|
||||
},
|
||||
})
|
||||
|
||||
revalidatePath("/recipients")
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Recipient updated successfully",
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Database error:", error)
|
||||
return {
|
||||
message: "Failed to update recipient",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,17 @@ export const SIGN_IN_URL = "/login"
|
||||
|
||||
export const TOKEN_EXPIRATION_SECONDS = 60 * 60 * 2 // 2 hour
|
||||
|
||||
export const RECIPIENT_DEPARTMENTS = {
|
||||
IT: "IT",
|
||||
ENGINEERING: "ENGINEERING",
|
||||
LOGISTICS: "LOGISTICS",
|
||||
TRAFFIC: "TRAFFIC",
|
||||
DRIVER: "DRIVER",
|
||||
ADMINISTRATION: "ADMINISTRATION",
|
||||
SALES: "SALES",
|
||||
OTHER: "OTHER",
|
||||
} as const
|
||||
|
||||
export const ITEM_STATUS = {
|
||||
AVAILABLE: "AVAILABLE",
|
||||
ASSIGNED: "ASSIGNED",
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const recipientSchema = z.object({
|
||||
id: z.string().optional(),
|
||||
username: z
|
||||
.string()
|
||||
.min(1, {
|
||||
error: "Username is required"
|
||||
})
|
||||
.nonempty("Username is required"),
|
||||
firstName: z.string().min(1, {
|
||||
error: "First name is required"
|
||||
}),
|
||||
lastName: z.string().min(1, {
|
||||
error: "Last name is required"
|
||||
}),
|
||||
department: z.enum(
|
||||
[
|
||||
"IT",
|
||||
"ENGINEERING",
|
||||
"TRAFFIC",
|
||||
"DRIVER",
|
||||
"LOGISTICS",
|
||||
"ADMINISTRATION",
|
||||
"SALES",
|
||||
"OTHER",
|
||||
],
|
||||
{
|
||||
error: "Department is required"
|
||||
},
|
||||
),
|
||||
email: z.string().optional().nullable(),
|
||||
phone: z.string().optional().nullable(),
|
||||
})
|
||||
|
||||
export const createRecipientSchema = recipientSchema.superRefine(
|
||||
(data, ctx) => {
|
||||
if (data.email && !z.string().email().safeParse(data.email).success) {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: "Email format is invalid",
|
||||
path: ["email"],
|
||||
})
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
export type CreateRecipientFormType = z.infer<typeof createRecipientSchema>
|
||||
|
||||
export const updateRecipientSchema = recipientSchema.extend({
|
||||
id: z.string().nonempty("ID is required"),
|
||||
})
|
||||
|
||||
export type UpdateRecipientFormType = z.infer<typeof updateRecipientSchema>
|
||||
Reference in New Issue
Block a user