97 lines
3.6 KiB
JavaScript
97 lines
3.6 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, isFlexibleAllowedPt, 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 => 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: vaAssignedArray,
|
|
dedicatedHours: dedicatedHours ? parseFloat(dedicatedHours) : 0,
|
|
vaWorkingScheduleStart: vaWorkingScheduleStart || null,
|
|
vaWorkingScheduleEnd: vaWorkingScheduleEnd || null,
|
|
isFlexibleAllowedPt: isFlexibleAllowedPt || true,
|
|
vaWorkingDelayAllowedStart: vaWorkingDelayAllowedStart || null,
|
|
vaWorkingDelayAllowedEnd: vaWorkingDelayAllowedEnd || null
|
|
};
|
|
|
|
const created = await ProjectModel.create(toCreate);
|
|
|
|
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 }; |