refactor(items): move workflows into use cases
This commit is contained in:
@@ -1,182 +0,0 @@
|
||||
"use server"
|
||||
|
||||
import { revalidatePath } from "next/cache"
|
||||
|
||||
import prisma from "@/lib/prisma"
|
||||
import {
|
||||
type CreateItemFormType,
|
||||
createItemSchema,
|
||||
type UpdateItemFormType,
|
||||
updateItemSchema,
|
||||
} from "@/lib/schemas/item.schemas"
|
||||
import { getAuthenticatedUserId } from "@/services/auth.service"
|
||||
import { ItemService } from "@/services/item.service"
|
||||
|
||||
export async function createItemAction(formData: CreateItemFormType) {
|
||||
const validatedFields = createItemSchema.safeParse(formData)
|
||||
|
||||
if (!validatedFields.success) {
|
||||
return {
|
||||
errors: validatedFields.error.flatten().fieldErrors,
|
||||
}
|
||||
}
|
||||
|
||||
const { name, categoryId, stock } = validatedFields.data
|
||||
|
||||
if (stock < 0) {
|
||||
return {
|
||||
success: false,
|
||||
errors: {
|
||||
stock: ["Stock cannot be negative"],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const existingItem = await ItemService.findByName(name)
|
||||
|
||||
if (existingItem) {
|
||||
return {
|
||||
success: false,
|
||||
errors: {
|
||||
name: ["An item with this name already exists"],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const item = await ItemService.create({
|
||||
name,
|
||||
category: { connect: { id: categoryId } },
|
||||
stock: stock || 0,
|
||||
})
|
||||
|
||||
if (stock > 0) {
|
||||
await prisma.movement.create({
|
||||
data: {
|
||||
type: "IN",
|
||||
itemId: item.id,
|
||||
userId: await getAuthenticatedUserId(),
|
||||
quantity: stock,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
revalidatePath("/inventory/items")
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Item created successfully!",
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Database error:", error)
|
||||
return {
|
||||
error: "Error creating item",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateItemAction(formData: UpdateItemFormType) {
|
||||
const validatedFields = updateItemSchema.safeParse(formData)
|
||||
|
||||
if (!validatedFields.success) {
|
||||
return {
|
||||
errors: validatedFields.error.flatten().fieldErrors,
|
||||
}
|
||||
}
|
||||
|
||||
const { id, stock, name, categoryId } = validatedFields.data
|
||||
|
||||
try {
|
||||
const existingItem = await ItemService.findByIdWithAssetCount(id)
|
||||
|
||||
if (!existingItem) {
|
||||
return {
|
||||
success: false,
|
||||
errors: {
|
||||
id: ["Item not found"],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const existingItemByName = await ItemService.findByName(name)
|
||||
|
||||
if (existingItemByName && existingItemByName.id !== id) {
|
||||
return {
|
||||
success: false,
|
||||
errors: { name: ["An item with this name already exists"] },
|
||||
}
|
||||
}
|
||||
|
||||
await ItemService.update(id, {
|
||||
stock: stock || existingItem.stock,
|
||||
name: name || existingItem.name,
|
||||
category: { connect: { id: categoryId } },
|
||||
})
|
||||
|
||||
const quantity = stock - existingItem.stock
|
||||
|
||||
if (stock && stock > existingItem.stock) {
|
||||
await prisma.movement.create({
|
||||
data: {
|
||||
type: "IN",
|
||||
itemId: id,
|
||||
quantity,
|
||||
userId: await getAuthenticatedUserId(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Item updated successfully!",
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Database error:", error)
|
||||
return {
|
||||
error: "Failed to update item",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteItemAction(formData: FormData) {
|
||||
const { id } = Object.fromEntries(formData) as { id: string }
|
||||
|
||||
try {
|
||||
const existingItem = await ItemService.findByIdWithAssetCount(id)
|
||||
|
||||
if (!existingItem) {
|
||||
return {
|
||||
success: false,
|
||||
errors: { id: ["Item not found"] },
|
||||
}
|
||||
}
|
||||
|
||||
if (existingItem._count.assets > 0) {
|
||||
return {
|
||||
success: false,
|
||||
errors: { id: ["Item has assets, you cannot delete it"] },
|
||||
}
|
||||
}
|
||||
|
||||
if (existingItem.stock > 0) {
|
||||
return {
|
||||
success: false,
|
||||
errors: { id: ["Item has stock, you cannot delete it"] },
|
||||
}
|
||||
}
|
||||
|
||||
await ItemService.delete(id)
|
||||
|
||||
revalidatePath("/inventory/items")
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Item deleted successfully!",
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Database error:", error)
|
||||
return {
|
||||
error: "Failed to delete item",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const createItemSchema = z.object({
|
||||
name: z.string().min(1, {
|
||||
error: "Name is required"
|
||||
}),
|
||||
categoryId: z.string().min(1, {
|
||||
error: "Category is required"
|
||||
}),
|
||||
stock: z.coerce
|
||||
.number()
|
||||
.int()
|
||||
.nonnegative()
|
||||
.min(0, {
|
||||
error: "Stock is required"
|
||||
}),
|
||||
})
|
||||
|
||||
export type CreateItemFormType = z.input<typeof createItemSchema>
|
||||
|
||||
export const updateItemSchema = createItemSchema.extend({
|
||||
id: z.string().min(1, {
|
||||
error: "Item is required"
|
||||
}),
|
||||
})
|
||||
|
||||
export type UpdateItemFormType = z.input<typeof updateItemSchema>
|
||||
|
||||
export const getItemByIdSchema = z.object({
|
||||
id: z.string().min(1, {
|
||||
error: "Item is required"
|
||||
}),
|
||||
})
|
||||
|
||||
export type GetItemByIdFormType = z.infer<typeof getItemByIdSchema>
|
||||
Reference in New Issue
Block a user