installer assigned quation teamMember

master^2
Bhaskar 7 months ago
parent 6f93671e64
commit 55310b1407

@ -3,7 +3,7 @@ const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken'); const jwt = require('jsonwebtoken');
const customJwtAuth = require("../customAuthJwt"); const customJwtAuth = require("../customAuthJwt");
const { Deparments } = require("../models/Department"); const { Deparments } = require("../models/Department");
const { Install } = require("../models/store"); const { Install, SensorStock, SensorQuotation, Order } = require("../models/store");
const { Counter } = require("../models/User") const { Counter } = require("../models/User")
const fastify = require("fastify")({ const fastify = require("fastify")({
logger: true, logger: true,
@ -135,73 +135,154 @@ exports.createTeamMember = async (request, reply) => {
} }
}; };
exports.assignTeamMemberToQuotation = async (request, reply) => { // exports.assignTeamMemberToQuotation = async (request, reply) => {
try { // try {
const { installationId } = request.params; // Get installationId from URL params // const { installationId } = request.params; // Get installationId from URL params
const { teamMemberId } = request.body; // Get teamMemberId from request body // const { teamMemberId } = request.body; // Get teamMemberId from request body
if (!teamMemberId) { // if (!teamMemberId) {
return reply.status(400).send({ // return reply.status(400).send({
simplydata: { // simplydata: {
error: true, // error: true,
message: "teamMemberId is required", // message: "teamMemberId is required",
}, // },
}); // });
} // }
// Find installation by installationId // // Find installation by installationId
const installation = await Install.findOne({ installationId }); // const installation = await Install.findOne({ installationId });
if (!installation) { // if (!installation) {
return reply.status(404).send({ // return reply.status(404).send({
simplydata: { // simplydata: {
error: true, // error: true,
message: "Installation not found", // message: "Installation not found",
}, // },
}); // });
} // }
// Extract team members list // // Extract team members list
const teamMembers = installation.team_member.team_member; // const teamMembers = installation.team_member.team_member;
// Check if provided teamMemberId exists in the installation's team // // Check if provided teamMemberId exists in the installation's team
const assignedTeamMember = teamMembers.find(member => member.teamMemberId === teamMemberId); // const assignedTeamMember = teamMembers.find(member => member.teamMemberId === teamMemberId);
if (!assignedTeamMember) { // if (!assignedTeamMember) {
return reply.status(404).send({ // return reply.status(404).send({
simplydata: { // simplydata: {
error: true, // error: true,
message: "Team member not found in this installation", // message: "Team member not found in this installation",
}, // },
}); // });
} // }
// Here, you would save the assigned team member to the quotation (modify as needed) // // Here, you would save the assigned team member to the quotation (modify as needed)
const quotation = { // const quotation = {
installationId, // installationId,
assignedTeamMember // assignedTeamMember
}; // };
return reply.send({ // return reply.send({
simplydata: { // simplydata: {
error: false, // error: false,
message: "Team member assigned to quotation successfully", // message: "Team member assigned to quotation successfully",
quotation // 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.assignTeamMemberToQuotation = async (request, reply) => {
try {
const { installationId } = request.params;
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
const installation = await Install.findOne({ installationId });
if (!installation) {
return reply.status(404).send({
simplydata: {
error: true,
message: "Installation not found",
},
});
}
// Extract team members list
const teamMembers = installation.team_member?.team_member || [];
// Check if the provided teamMemberId exists in the installation's team
const assignedTeamMember = teamMembers.find(member => member.teamMemberId === teamMemberId);
if (!assignedTeamMember) {
return reply.status(404).send({
simplydata: {
error: true,
message: "Team member not found in this installation",
},
});
}
// Find or create the quotation for the given installationId
let quotation = await Order.findOne({ installationId, quatationId: quotationId });
if (!quotation) {
quotation = new Order({
installationId,
quatationId: quotationId,
// assignedTeamMembers: [],
});
}
// Assign the team member to the quotation
if (!quotation.assignedTeamMembers) {
quotation.assignedTeamMembers = [];
}
if (!quotation.assignedTeamMembers.includes(teamMemberId)) {
quotation.assignedTeamMembers.push(teamMemberId);
}
// Save the updated quotation
await quotation.save();
return reply.send({
simplydata: {
error: false,
message: "Team member assigned to quotation successfully",
quotation,
},
});
} catch (err) {
console.error("Error assigning team member to quotation:", err);
reply.status(500).send({
simplydata: {
error: true,
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

@ -67,9 +67,13 @@ module.exports = function (fastify, opts, next) {
teamMemberId: { teamMemberId: {
type: "string", type: "string",
description: "The team member ID to assign" description: "The team member ID to assign"
},
quotationId: {
type: "string",
description: "The team member ID to assign"
} }
}, },
required: ["teamMemberId"] // required: ["teamMemberId"]
}, },
}, },
handler: installationController.assignTeamMemberToQuotation handler: installationController.assignTeamMemberToQuotation

Loading…
Cancel
Save