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