refactor: update assignment update logic to include recipientId and handle movements
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { UpdateAssignmentFormType } from "@/lib/schemas/assignment.schemas"
|
||||
import type { Item } from "@/lib/types"
|
||||
import { AssetService } from "@/services/asset.service"
|
||||
import { AssignmentService } from "@/services/assignment.service"
|
||||
import { ItemService } from "@/services/item.service"
|
||||
@@ -20,6 +21,13 @@ export default async function EditAssignmentPage({
|
||||
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 (
|
||||
<div>
|
||||
<AssignmentForm
|
||||
|
||||
@@ -129,7 +129,7 @@ export async function updateAssignment(formData: UpdateAssignmentFormType) {
|
||||
}
|
||||
|
||||
try {
|
||||
const { id, itemId, assetId, quantity } = validatedFields.data
|
||||
const { id, recipientId, itemId, assetId, quantity } = validatedFields.data
|
||||
|
||||
const assignment = await prisma.assignment.findUnique({
|
||||
where: { id },
|
||||
@@ -154,12 +154,12 @@ export async function updateAssignment(formData: UpdateAssignmentFormType) {
|
||||
}
|
||||
}
|
||||
|
||||
if (item.stock < quantity) {
|
||||
return {
|
||||
success: false,
|
||||
errors: { quantity: ["Item does not have enough stock"] },
|
||||
}
|
||||
}
|
||||
// if (item.stock < quantity) {
|
||||
// return {
|
||||
// success: false,
|
||||
// errors: { quantity: ["Item does not have enough stock"] },
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
if (assetId) {
|
||||
@@ -187,16 +187,51 @@ export async function updateAssignment(formData: UpdateAssignmentFormType) {
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.assignment.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...validatedFields.data,
|
||||
itemId,
|
||||
assetId,
|
||||
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,
|
||||
returnDate: new Date(),
|
||||
},
|
||||
})
|
||||
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({
|
||||
where: { id },
|
||||
data: {
|
||||
...validatedFields.data,
|
||||
itemId,
|
||||
assetId,
|
||||
quantity,
|
||||
returnDate: new Date(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
revalidatePath("/assignments")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user