Skip to content

Commit

Permalink
changed the logic of header validation
Browse files Browse the repository at this point in the history
  • Loading branch information
nitish-egov committed Jun 13, 2024
1 parent a746a0d commit 4b08d74
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
4 changes: 1 addition & 3 deletions utilities/project-factory/src/server/api/campaignApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { httpRequest } from "../utils/request";
import { getFormattedStringForDebug, logger } from "../utils/logger";
import createAndSearch from '../config/createAndSearch';
import { getDataFromSheet, generateActivityMessage, throwError, translateSchema, replicateRequest } from "../utils/genericUtils";
import { immediateValidationForTargetSheet, validateSheetData, validateTargetSheetData } from '../validators/campaignValidators';
import { immediateValidationForTargetSheet, validateSheetData, validateTargetSheetData } from '../validators/campaignValidators';
import { callMdmsTypeSchema, getCampaignNumber } from "./genericApis";
import { boundaryBulkUpload, convertToTypeData, generateHierarchy, generateProcessedFileAndPersist, getLocalizedName, reorderBoundariesOfDataAndValidate } from "../utils/campaignUtils";
const _ = require('lodash');
Expand Down Expand Up @@ -497,8 +497,6 @@ async function processValidate(request: any, localizationMap?: { [key: string]:
immediateValidationForTargetSheet(dataFromSheet, localizationMap);
logger.info("target sheet format validation completed and starts with data validation");
validateTargetSheetData(dataFromSheet, request, createAndSearchConfig?.boundaryValidation, localizationMap);
logger.info("target sheet data validation completed");

}

else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ async function validateSheetData(data: any, request: any, schema: any, boundaryV
async function validateTargetSheetData(data: any, request: any, boundaryValidation: any, localizationMap?: any) {
try {
const errors: any[] = [];
await validateHeadersOfTargetSheet(request, localizationMap);
if (boundaryValidation) {
// const localizedBoundaryValidationColumn = getLocalizedName(boundaryValidation?.column, localizationMap)
// await validateTargetBoundaryData(data, request, localizedBoundaryValidationColumn, errors, localizationMap);
Expand All @@ -427,13 +428,27 @@ async function validateTargetSheetData(data: any, request: any, boundaryValidati
request.body.ResourceDetails.status = resourceDataStatuses.invalid;
}
await generateProcessedFileAndPersist(request, localizationMap);
logger.info("target sheet data validation completed");
}
catch (error) {
console.log(error)
await handleResouceDetailsError(request, error);
}
}


async function validateHeadersOfTargetSheet(request: any, localizationMap?: any) {
const fileUrl = await validateFile(request);
const targetWorkbook: any = await getTargetWorkbook(fileUrl);
const hierarchy = await getHierarchy(request, request?.body?.ResourceDetails?.tenantId, request?.body?.ResourceDetails?.hierarchyType);
const finalValidHeadersForTargetSheetAsPerCampaignType = await getFinalValidHeadersForTargetSheetAsPerCampaignType(request, hierarchy, localizationMap);
logger.info("finalValidHeadersForTargetSheetAsPerCampaignType :" + JSON.stringify(finalValidHeadersForTargetSheetAsPerCampaignType));
logger.info("validating headers of target sheet started")
validateHeadersOfTabsWithTargetInTargetSheet(targetWorkbook, finalValidHeadersForTargetSheetAsPerCampaignType);
logger.info("validation of target sheet headers completed")
}


function validateBooleanField(obj: any, fieldName: any, index: any) {
if (!obj.hasOwnProperty(fieldName)) {
throwError("COMMON", 400, "VALIDATION_ERROR", `Object at index ${index} is missing field "${fieldName}".`);
Expand Down Expand Up @@ -522,17 +537,17 @@ async function validateCreateRequest(request: any, localizationMap?: any) {
if (request.body.ResourceDetails.type == 'boundary') {
await validateBoundarySheetData(request, fileUrl, localizationMap);
}
if (request?.body?.ResourceDetails?.type == 'boundaryWithTarget') {
const targetWorkbook: any = await getTargetWorkbook(fileUrl);
const hierarchy = await getHierarchy(request, request?.body?.ResourceDetails?.tenantId, request?.body?.ResourceDetails?.hierarchyType);
const finalValidHeadersForTargetSheetAsPerCampaignType = await getFinalValidHeadersForTargetSheetAsPerCampaignType(request, hierarchy, localizationMap);
logger.info("finalValidHeadersForTargetSheetAsPerCampaignType :" + JSON.stringify(finalValidHeadersForTargetSheetAsPerCampaignType));
validateTabsWithTargetInTargetSheet(targetWorkbook, finalValidHeadersForTargetSheetAsPerCampaignType);
}
// if (request?.body?.ResourceDetails?.type == 'boundaryWithTarget') {
// const targetWorkbook: any = await getTargetWorkbook(fileUrl);
// const hierarchy = await getHierarchy(request, request?.body?.ResourceDetails?.tenantId, request?.body?.ResourceDetails?.hierarchyType);
// const finalValidHeadersForTargetSheetAsPerCampaignType = await getFinalValidHeadersForTargetSheetAsPerCampaignType(request, hierarchy, localizationMap);
// logger.info("finalValidHeadersForTargetSheetAsPerCampaignType :" + JSON.stringify(finalValidHeadersForTargetSheetAsPerCampaignType));
// validateTabsWithTargetInTargetSheet(targetWorkbook, finalValidHeadersForTargetSheetAsPerCampaignType);
// }
}
}

function validateTabsWithTargetInTargetSheet(targetWorkbook: any, expectedHeadersForTargetSheet: any) {
function validateHeadersOfTabsWithTargetInTargetSheet(targetWorkbook: any, expectedHeadersForTargetSheet: any) {
targetWorkbook.eachSheet((worksheet: any, sheetId: any) => {
if (sheetId > 2) { // Starting from the second sheet
// Convert the sheet to an array of headers
Expand Down Expand Up @@ -1140,8 +1155,10 @@ async function validateDownloadRequest(request: any) {
await validateHierarchyType(request, hierarchyType, tenantId);
}

function immediateValidationForTargetSheet(dataFromSheet: any, localizationMap: any) {
async function immediateValidationForTargetSheet(dataFromSheet: any, localizationMap: any) {
logger.info("validating all district tabs present started")
validateAllDistrictTabsPresentOrNot(dataFromSheet, localizationMap);
logger.info("validation of all district tabs present completed")
for (const key in dataFromSheet) {
if (key !== getLocalizedName(getBoundaryTabName(), localizationMap) && key !== getLocalizedName(config?.values?.readMeTab, localizationMap)) {
if (Object.prototype.hasOwnProperty.call(dataFromSheet, key)) {
Expand Down

0 comments on commit 4b08d74

Please sign in to comment.