-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Individual validation #753
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1099,25 +1099,27 @@ async function getRelatedProjects(request: any) { | |
} | ||
} | ||
|
||
async function updateProjectDates(request: any) { | ||
const projects = await getRelatedProjects(request); | ||
const { startDate, endDate } = request?.body?.CampaignDetails; | ||
for (const project of projects) { | ||
project.startDate = startDate || project.startDate; | ||
project.endDate = endDate || project.endDate; | ||
delete project?.address; | ||
} | ||
logger.info("Projects related to current Campaign : " + JSON.stringify(projects)); | ||
const projectUpdateBody = { | ||
RequestInfo: request?.body?.RequestInfo, | ||
Projects: projects | ||
} | ||
const projectUpdateResponse = await httpRequest(config?.host?.projectHost + config?.paths?.projectUpdate, projectUpdateBody); | ||
if (projectUpdateResponse?.Project && Array.isArray(projectUpdateResponse?.Project) && projectUpdateResponse?.Project?.length == projects?.length) { | ||
logger.info("Project dates updated successfully") | ||
} | ||
else { | ||
throwError("PROJECT", 500, "PROJECT_UPDATE_ERROR") | ||
async function updateProjectDates(request: any, actionInUrl: any) { | ||
const { startDate, endDate, projectId } = request?.body?.CampaignDetails | ||
if ((startDate || endDate) && projectId && actionInUrl == "update") { | ||
const projects = await getRelatedProjects(request); | ||
for (const project of projects) { | ||
project.startDate = startDate || project.startDate; | ||
project.endDate = endDate || project.endDate; | ||
delete project?.address; | ||
} | ||
logger.info("Projects related to current Campaign : " + JSON.stringify(projects)); | ||
const projectUpdateBody = { | ||
RequestInfo: request?.body?.RequestInfo, | ||
Projects: projects | ||
} | ||
const projectUpdateResponse = await httpRequest(config?.host?.projectHost + config?.paths?.projectUpdate, projectUpdateBody); | ||
if (projectUpdateResponse?.Project && Array.isArray(projectUpdateResponse?.Project) && projectUpdateResponse?.Project?.length == projects?.length) { | ||
logger.info("Project dates updated successfully") | ||
} | ||
else { | ||
throwError("PROJECT", 500, "PROJECT_UPDATE_ERROR") | ||
} | ||
} | ||
} | ||
|
||
|
@@ -1148,7 +1150,7 @@ async function getCodesTarget(request: any, localizationMap?: any) { | |
|
||
async function createProject(request: any, actionUrl: any, localizationMap?: any) { | ||
logger.info("Create Projects started for the given Campaign") | ||
var { tenantId, boundaries, projectType, projectId, startDate, endDate } = request?.body?.CampaignDetails; | ||
var { tenantId, boundaries, projectType, projectId } = request?.body?.CampaignDetails; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure proper type definitions to avoid using // Before
var { tenantId, boundaries, projectType, projectId } = request?.body?.CampaignDetails;
// After
interface CampaignDetails {
tenantId: string;
boundaries: Boundary[];
projectType: string;
projectId: string | null;
}
const { tenantId, boundaries, projectType, projectId }: CampaignDetails = request?.body?.CampaignDetails; This change introduces a TypeScript interface to ensure type safety and avoid the use of |
||
if (boundaries && projectType && !projectId) { | ||
const projectTypeResponse = await getMDMSV1Data({}, 'HCM-PROJECT-TYPES', "projectTypes", tenantId); | ||
var Projects: any = enrichProjectDetailsFromCampaignDetails(request?.body?.CampaignDetails, projectTypeResponse); | ||
|
@@ -1179,9 +1181,6 @@ async function createProject(request: any, actionUrl: any, localizationMap?: any | |
await projectCreate(projectCreateBody, request) | ||
} | ||
} | ||
else if ((startDate || endDate) && projectId && actionUrl == "update") { | ||
await updateProjectDates(request); | ||
} | ||
} | ||
|
||
|
||
|
@@ -1194,6 +1193,7 @@ async function processAfterPersist(request: any, actionInUrl: any) { | |
await enrichAndPersistProjectCampaignRequest(request, actionInUrl, false, localizationMap) | ||
} | ||
else { | ||
await updateProjectDates(request, actionInUrl); | ||
await enrichAndPersistProjectCampaignRequest(request, actionInUrl, false, localizationMap) | ||
} | ||
} catch (error: any) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -763,13 +763,15 @@ function validateProjectCampaignMissingFields(CampaignDetails: any) { | |
|
||
function validateDraftProjectCampaignMissingFields(CampaignDetails: any) { | ||
validateCampaignBodyViaSchema(campaignDetailsDraftSchema, CampaignDetails) | ||
const { startDate, endDate } = CampaignDetails; | ||
if (startDate && endDate && (new Date(endDate).getTime() - new Date(startDate).getTime()) < (24 * 60 * 60 * 1000)) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "endDate must be at least one day after startDate"); | ||
} | ||
const today: any = Date.now(); | ||
if (startDate <= today) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "startDate cannot be today or past date"); | ||
const { startDate, endDate, action } = CampaignDetails; | ||
if (action != "changeDates") { | ||
if (startDate && endDate && (new Date(endDate).getTime() - new Date(startDate).getTime()) < (24 * 60 * 60 * 1000)) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "endDate must be at least one day after startDate"); | ||
} | ||
const today: any = Date.now(); | ||
if (startDate <= today) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "startDate cannot be today or past date"); | ||
} | ||
Comment on lines
+766
to
+774
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure consistent date validation logic across different actions. The validation logic for - if (startDate && endDate && (new Date(endDate).getTime() - new Date(startDate).getTime()) < (24 * 60 * 60 * 1000)) {
- throwError("COMMON", 400, "VALIDATION_ERROR", "endDate must be at least one day after startDate");
- }
- const today: any = Date.now();
- if (startDate <= today) {
- throwError("COMMON", 400, "VALIDATION_ERROR", "startDate cannot be today or past date");
- }
+ validateCampaignDates(startDate, endDate);
|
||
} | ||
} | ||
|
||
|
@@ -812,7 +814,7 @@ async function validateCampaignName(request: any, actionInUrl: any) { | |
} | ||
|
||
async function validateById(request: any) { | ||
const { id, tenantId } = request?.body?.CampaignDetails | ||
const { id, tenantId, action } = request?.body?.CampaignDetails | ||
if (!id) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "id is required"); | ||
} | ||
|
@@ -829,8 +831,10 @@ async function validateById(request: any) { | |
if (searchResponse?.CampaignDetails?.length > 0) { | ||
logger.info("CampaignDetails : " + JSON.stringify(searchResponse?.CampaignDetails)); | ||
request.body.ExistingCampaignDetails = searchResponse?.CampaignDetails[0]; | ||
if (request.body.ExistingCampaignDetails?.campaignName != request?.body?.CampaignDetails?.campaignName && request.body.ExistingCampaignDetails?.status != campaignStatuses?.drafted) { | ||
throwError("CAMPAIGN", 400, "CAMPAIGNNAME_MISMATCH", `CampaignName can only be updated in ${campaignStatuses?.drafted} state. CampaignName mismatch, Provided CampaignName = ${request?.body?.CampaignDetails?.campaignName} but Existing CampaignName = ${request.body.ExistingCampaignDetails?.campaignName}`); | ||
if (action != "changeDates") { | ||
if (request.body.ExistingCampaignDetails?.status != campaignStatuses?.drafted) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", `Campaign can only be updated in drafted state. Change action to changeDates if you want to just update date.`); | ||
} | ||
} | ||
} | ||
else { | ||
|
@@ -878,27 +882,50 @@ async function validateProjectType(request: any, projectType: any, tenantId: any | |
} | ||
} | ||
|
||
function isObjectOrArray(value: any) { | ||
return typeof value === 'object' && value !== null && !Array.isArray(value); | ||
} | ||
|
||
|
||
|
||
async function validateProjectCampaignRequest(request: any, actionInUrl: any) { | ||
const CampaignDetails = request.body.CampaignDetails; | ||
const { id, hierarchyType, action, tenantId, boundaries, resources, projectType } = CampaignDetails; | ||
if (actionInUrl == "update") { | ||
if (!id) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "id is required for update"); | ||
async function validateChangeDatesRequest(request: any) { | ||
var ExistingCampaignDetails = request?.body?.ExistingCampaignDetails; | ||
const { startDate: exsistingStartDate, endDate: exsistingEndDate } = ExistingCampaignDetails; | ||
var newCampaignDetails = request?.body?.CampaignDetails; | ||
const { startDate: newStartDate, endDate: newEndDate } = newCampaignDetails; | ||
|
||
for (const key in newCampaignDetails) { | ||
if (!isObjectOrArray(newCampaignDetails[key])) { | ||
// If the value is not an object or array, compare it with the corresponding value in ExistingCampaignDetails | ||
if (!(key == "startDate" || key == "endDate") && newCampaignDetails[key] !== ExistingCampaignDetails[key]) { | ||
// Handle the validation failure (for example, throw an error or log a message) | ||
throwError("COMMON", 400, "VALIDATION_ERROR", `${key} value in request campaign is not matching with existing campaign`); | ||
} | ||
} | ||
} | ||
if (!CampaignDetails) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "CampaignDetails is required"); | ||
const today: any = Date.now(); | ||
if (exsistingStartDate >= today) { | ||
logger.info("Existing start date is greater than or equal to current date"); | ||
logger.info("Now only endDate can be updated") | ||
if (exsistingStartDate != newStartDate) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "StartDate cannot be updated as campaign is started or completed."); | ||
} | ||
} | ||
if (!action) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "CampaignDetails.action is required and must be either 'create' or 'draft'") | ||
if (exsistingEndDate > today) { | ||
logger.info("Existing end date is greater than or equal to current date"); | ||
if (exsistingEndDate != newEndDate) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "EndDate cannot be updated as campaign is completed."); | ||
} | ||
} | ||
if (!(action == "create" || action == "draft")) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "action can only be create or draft"); | ||
request.body.CampaignDetails = ExistingCampaignDetails; | ||
request.body.CampaignDetails.startDate = newStartDate; | ||
request.body.CampaignDetails.endDate = newEndDate; | ||
} | ||
Comment on lines
+885
to
+921
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify logic and improve readability in This function contains complex conditional logic that could be simplified. Consider breaking down the function into smaller, more focused functions and using early returns to reduce nesting and improve readability. - for (const key in newCampaignDetails) {
- if (!isObjectOrArray(newCampaignDetails[key])) {
- if (!(key == "startDate" || key == "endDate") && newCampaignDetails[key] !== ExistingCampaignDetails[key]) {
- throwError("COMMON", 400, "VALIDATION_ERROR", `${key} value in request campaign is not matching with existing campaign`);
- }
- }
- }
+ validateCampaignDetails(newCampaignDetails, ExistingCampaignDetails);
|
||
|
||
async function validateCampaignBody(request: any, CampaignDetails: any, actionInUrl: any) { | ||
const { hierarchyType, action, tenantId, boundaries, resources, projectType } = CampaignDetails; | ||
if (action == "changeDates") { | ||
await validateChangeDatesRequest(request); | ||
} | ||
if (action == "create") { | ||
else if (action == "create") { | ||
validateProjectCampaignMissingFields(CampaignDetails); | ||
await validateCampaignName(request, actionInUrl); | ||
if (tenantId != request?.body?.RequestInfo?.userInfo?.tenantId) { | ||
|
@@ -915,9 +942,32 @@ async function validateProjectCampaignRequest(request: any, actionInUrl: any) { | |
await validateHierarchyType(request, hierarchyType, tenantId); | ||
await validateProjectType(request, projectType, tenantId); | ||
} | ||
} | ||
|
||
async function validateProjectCampaignRequest(request: any, actionInUrl: any) { | ||
const CampaignDetails = request.body.CampaignDetails; | ||
const { id, action } = CampaignDetails; | ||
if (actionInUrl == "update") { | ||
if (!id) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "id is required for update"); | ||
} | ||
} | ||
if (!CampaignDetails) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "CampaignDetails is required"); | ||
} | ||
if (!action) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "CampaignDetails.action is required and must be either 'create' or 'draft'") | ||
} | ||
if (!(action == "create" || action == "draft" || action == "changeDates")) { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "action can only be create, draft or changeDates"); | ||
} | ||
if (actionInUrl == "update") { | ||
await validateById(request); | ||
} | ||
if (action == "changeDates" && actionInUrl == "create") { | ||
throwError("COMMON", 400, "VALIDATION_ERROR", "changeDates is not allowed during create"); | ||
} | ||
await validateCampaignBody(request, CampaignDetails, actionInUrl); | ||
} | ||
|
||
async function validateSearchProjectCampaignRequest(request: any) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor
updateProjectDates
to handle errors more gracefully.This change ensures that errors are logged before throwing, providing better traceability and error handling.
Committable suggestion