status maintain in order schema

master^2
Bhaskar 8 months ago
parent 55310b1407
commit 3d33e2e465

@ -203,86 +203,93 @@ exports.createTeamMember = async (request, reply) => {
exports.assignTeamMemberToQuotation = async (request, reply) => { exports.assignTeamMemberToQuotation = async (request, reply) => {
try { try {
const { installationId } = request.params; const { installationId } = request.params;
const { teamMemberId, quotationId } = request.body; const { teamMemberId, quotationId } = request.body;
if (!teamMemberId || !quotationId) {
return reply.status(400).send({
simplydata: {
error: true,
message: "Both teamMemberId and quotationId are required",
},
});
}
// Find installation by installationId if (!teamMemberId || !quotationId) {
const installation = await Install.findOne({ installationId }); return reply.status(400).send({
simplydata: {
error: true,
message: "Both teamMemberId and quotationId are required",
},
});
}
if (!installation) { // 🔹 Find installation by installationId
return reply.status(404).send({ const installation = await Install.findOne({ installationId });
simplydata: {
error: true,
message: "Installation not found",
},
});
}
// Extract team members list if (!installation) {
const teamMembers = installation.team_member?.team_member || []; return reply.status(404).send({
simplydata: {
error: true,
message: "Installation not found",
},
});
}
// Check if the provided teamMemberId exists in the installation's team // 🔹 Extract team members list
const assignedTeamMember = teamMembers.find(member => member.teamMemberId === teamMemberId); const teamMembers = installation.team_member?.team_member || [];
if (!assignedTeamMember) { // 🔹 Check if the provided teamMemberId exists in the installation's team
return reply.status(404).send({ const assignedTeamMember = teamMembers.find(
simplydata: { (member) => member.teamMemberId === teamMemberId
error: true, );
message: "Team member not found in this installation",
},
});
}
// Find or create the quotation for the given installationId if (!assignedTeamMember) {
let quotation = await Order.findOne({ installationId, quatationId: quotationId }); return reply.status(404).send({
simplydata: {
error: true,
message: "Team member not found in this installation",
},
});
}
if (!quotation) { // 🔹 Find or create the quotation for the given installationId
quotation = new Order({ let quotation = await Order.findOne({ installationId, quatationId: quotationId });
installationId,
quatationId: quotationId,
// assignedTeamMembers: [],
});
}
// Assign the team member to the quotation if (!quotation) {
if (!quotation.assignedTeamMembers) { quotation = new Order({
quotation.assignedTeamMembers = []; installationId,
} quatationId: quotationId,
assignedTeamMembers: [],
status: "Pending", // Default status when created
});
}
if (!quotation.assignedTeamMembers.includes(teamMemberId)) { // 🔹 Assign the team member to the quotation
quotation.assignedTeamMembers.push(teamMemberId); if (!quotation.assignedTeamMembers) {
} quotation.assignedTeamMembers = [];
}
// Save the updated quotation if (!quotation.assignedTeamMembers.includes(teamMemberId)) {
await quotation.save(); quotation.assignedTeamMembers.push(teamMemberId);
}
return reply.send({ // 🔹 Update order status when a team member is assigned
simplydata: { quotation.status = "Assigned"; // Update status
error: false,
message: "Team member assigned to quotation successfully", // 🔹 Save the updated quotation
quotation, await quotation.save();
},
}); return reply.send({
simplydata: {
error: false,
message: "Team member assigned to quotation successfully",
quotation,
},
});
} catch (err) { } catch (err) {
console.error("Error assigning team member to quotation:", err); console.error("Error assigning team member to quotation:", err);
reply.status(500).send({ reply.status(500).send({
simplydata: { simplydata: {
error: true, error: true,
message: "Internal server error", message: "Internal server error",
}, },
}); });
} }
}; };
exports.getAllInstallers = async (request, reply) => { exports.getAllInstallers = async (request, reply) => {
try { try {
const { departmentName } = request.params; // Get installationId from request params const { departmentName } = request.params; // Get installationId from request params

Loading…
Cancel
Save