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 { 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
|
||||||
|
|||||||
@@ -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,16 +187,51 @@ export async function updateAssignment(formData: UpdateAssignmentFormType) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await prisma.assignment.update({
|
const createdBy = await getAuthenticatedUserId()
|
||||||
where: { id },
|
|
||||||
data: {
|
if (assignment.recipientId !== recipientId) {
|
||||||
...validatedFields.data,
|
await MovementService.create({
|
||||||
itemId,
|
type: "RETURN",
|
||||||
assetId,
|
quantity: assignment.quantity || 1,
|
||||||
|
itemId: assignment.itemId || undefined,
|
||||||
|
assetId: assignment.assetId || undefined,
|
||||||
|
recipientId: assignment.recipientId || undefined,
|
||||||
|
assignmentId: id,
|
||||||
|
})
|
||||||
|
|
||||||
|
await MovementService.create({
|
||||||
|
type: "ASSIGNMENT",
|
||||||
quantity,
|
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")
|
revalidatePath("/assignments")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user