refactor: update assignment update logic to include recipientId and handle movements

This commit is contained in:
2025-11-12 19:13:05 +01:00
parent 90de682cec
commit 3db1ddce23
2 changed files with 59 additions and 16 deletions
@@ -1,4 +1,5 @@
import { UpdateAssignmentFormType } from "@/lib/schemas/assignment.schemas" import { UpdateAssignmentFormType } from "@/lib/schemas/assignment.schemas"
import type { Item } from "@/lib/types"
import { AssetService } from "@/services/asset.service" import { AssetService } from "@/services/asset.service"
import { AssignmentService } from "@/services/assignment.service" import { AssignmentService } from "@/services/assignment.service"
import { ItemService } from "@/services/item.service" import { ItemService } from "@/services/item.service"
@@ -20,6 +21,13 @@ export default async function EditAssignmentPage({
return <div>Assignment not found</div> return <div>Assignment not found</div>
} }
let assignmentItem: Item = {} as Item
if (assignment.itemId) {
assignmentItem = (await ItemService.findById(assignment.itemId)) as Item
items.push(assignmentItem)
}
return ( return (
<div> <div>
<AssignmentForm <AssignmentForm
+42 -7
View File
@@ -129,7 +129,7 @@ export async function updateAssignment(formData: UpdateAssignmentFormType) {
} }
try { try {
const { id, itemId, assetId, quantity } = validatedFields.data const { id, recipientId, itemId, assetId, quantity } = validatedFields.data
const assignment = await prisma.assignment.findUnique({ const assignment = await prisma.assignment.findUnique({
where: { id }, where: { id },
@@ -154,12 +154,12 @@ export async function updateAssignment(formData: UpdateAssignmentFormType) {
} }
} }
if (item.stock < quantity) { // if (item.stock < quantity) {
return { // return {
success: false, // success: false,
errors: { quantity: ["Item does not have enough stock"] }, // errors: { quantity: ["Item does not have enough stock"] },
} // }
} // }
} }
if (assetId) { if (assetId) {
@@ -187,6 +187,40 @@ export async function updateAssignment(formData: UpdateAssignmentFormType) {
} }
} }
const createdBy = await getAuthenticatedUserId()
if (assignment.recipientId !== recipientId) {
await MovementService.create({
type: "RETURN",
quantity: assignment.quantity || 1,
itemId: assignment.itemId || undefined,
assetId: assignment.assetId || undefined,
recipientId: assignment.recipientId || undefined,
assignmentId: id,
})
await MovementService.create({
type: "ASSIGNMENT",
quantity,
itemId,
assetId: assetId || undefined,
recipientId,
assignmentId: id,
})
await prisma.assignment.update({
data: {
...validatedFields.data,
createdBy,
recipientId,
itemId,
assetId,
quantity,
returnDate: null,
},
where: { id },
})
} else {
await prisma.assignment.update({ await prisma.assignment.update({
where: { id }, where: { id },
data: { data: {
@@ -197,6 +231,7 @@ export async function updateAssignment(formData: UpdateAssignmentFormType) {
returnDate: new Date(), returnDate: new Date(),
}, },
}) })
}
revalidatePath("/assignments") revalidatePath("/assignments")