106 lines
4.1 KiB
JavaScript
106 lines
4.1 KiB
JavaScript
import { ProjectModel, VAModel, ClientModel } from "../../models/vaModel.js";
|
|
import { Types } from "mongoose";
|
|
import { updateEntityRelationships } from "../../helper/updateEntityRelationships.js";
|
|
// Register a new VA
|
|
async function project_add_sf(req, res) {
|
|
try {
|
|
const payload = req.body || {};
|
|
const { name, hourlyRate, clientId, vaAssigned, dedicatedHours, vaWorkingScheduleStart, vaWorkingScheduleEnd, isFlexibleAllowed, vaWorkingDelayAllowedStart, vaWorkingDelayAllowedEnd } = payload;
|
|
|
|
// Validate required fields
|
|
if (!name) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
message: "Project name is required"
|
|
});
|
|
}
|
|
|
|
if (!clientId) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
message: "Client ID is required"
|
|
});
|
|
}
|
|
|
|
// Validate and convert clientId to ObjectId
|
|
let clientObjectId;
|
|
try {
|
|
clientObjectId = new Types.ObjectId(clientId);
|
|
} catch (error) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
message: "Invalid Client ID format"
|
|
});
|
|
}
|
|
|
|
// Validate and convert vaAssigned to array of ObjectIds
|
|
// let vaAssignedArray = [];
|
|
// if (vaAssigned) {
|
|
// if (Array.isArray(vaAssigned)) {
|
|
// try {
|
|
// vaAssignedArray = vaAssigned.map(vaId => {
|
|
// if(typeof vaId === 'object' && "isFlexibleAllowed" in vaId){
|
|
// // Handle Error form SF Side for now - That vaId is coming as object
|
|
// return new Types.ObjectId(vaId.vaId)
|
|
// } else {
|
|
// return new Types.ObjectId(vaId)
|
|
// }
|
|
// });
|
|
// } catch (error) {
|
|
// return res.status(400).json({
|
|
// success: false,
|
|
// message: "Invalid VA ID format in vaAssigned array"
|
|
// });
|
|
// }
|
|
// } else if (typeof vaAssigned === 'string') {
|
|
// // Handle single VA ID as string
|
|
// try {
|
|
// vaAssignedArray = [new Types.ObjectId(vaAssigned)];
|
|
// } catch (error) {
|
|
// return res.status(400).json({
|
|
// success: false,
|
|
// message: "Invalid VA ID format"
|
|
// });
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// sanitize/construct allowed fields
|
|
const toCreate = {
|
|
name: name.trim(),
|
|
hourlyRate: hourlyRate || '0',
|
|
clientId: clientObjectId,
|
|
vaAssigned: vaAssigned,
|
|
dedicatedHours: dedicatedHours ? parseFloat(dedicatedHours) : 0,
|
|
vaWorkingScheduleStart: vaWorkingScheduleStart || null,
|
|
vaWorkingScheduleEnd: vaWorkingScheduleEnd || null,
|
|
isFlexibleAllowed: isFlexibleAllowed || true,
|
|
vaWorkingDelayAllowedStart: vaWorkingDelayAllowedStart || null,
|
|
vaWorkingDelayAllowedEnd: vaWorkingDelayAllowedEnd || null
|
|
};
|
|
|
|
const created = await ProjectModel.create(toCreate);
|
|
|
|
const vaAssignedArray = vaAssigned ? vaAssigned.map(vaDtl => vaDtl.vaId) : [];
|
|
|
|
const relationshipUpdate = await updateEntityRelationships({
|
|
project: {
|
|
projectId: created._id,
|
|
clientId: clientObjectId,
|
|
vaId: vaAssignedArray
|
|
}
|
|
});
|
|
|
|
if (!relationshipUpdate.success) {
|
|
console.warn('Project created but relationship update failed:', relationshipUpdate.message);
|
|
}
|
|
|
|
res.status(201).json({ success: true, data: created, relationships: relationshipUpdate });
|
|
|
|
} catch (error) {
|
|
console.error("project_add_sf error:", error);
|
|
return res.status(500).json({ success: false, message: "Internal Server Error" });
|
|
}
|
|
}
|
|
|
|
export { project_add_sf }; |