Skip to content

Commit

Permalink
Merge pull request #349 from egovernments/campaign-synchronous
Browse files Browse the repository at this point in the history
Campaign synchronous
  • Loading branch information
ashish-egov authored Apr 19, 2024
2 parents 999d166 + 49facd1 commit 3cbdf66
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
38 changes: 23 additions & 15 deletions utilities/project-factory/src/server/utils/campaignUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,17 @@ function enrichRootProjectId(requestBody: any) {
}
}

async function enrichAndPersistCampaignForCreate(request: any) {
async function enrichAndPersistCampaignForCreate(request: any, firstPersist: boolean = false) {
const action = request?.body?.CampaignDetails?.action;
request.body.CampaignDetails.campaignNumber = await getCampaignNumber(request.body, "CMP-[cy:yyyy-MM-dd]-[SEQ_EG_CMP_ID]", "campaign.number", request?.body?.CampaignDetails?.tenantId);
if (firstPersist) {
request.body.CampaignDetails.campaignNumber = await getCampaignNumber(request.body, "CMP-[cy:yyyy-MM-dd]-[SEQ_EG_CMP_ID]", "campaign.number", request?.body?.CampaignDetails?.tenantId);
}
request.body.CampaignDetails.campaignDetails = { deliveryRules: request?.body?.CampaignDetails?.deliveryRules };
request.body.CampaignDetails.status = action == "create" ? "started" : "drafted";
request.body.CampaignDetails.boundaryCode = getRootBoundaryCode(request.body.CampaignDetails.boundaries)
request.body.CampaignDetails.projectType = request?.body?.CampaignDetails?.projectType ? request?.body?.CampaignDetails?.projectType : null;
request.body.CampaignDetails.hierarchyType = request?.body?.CampaignDetails?.hierarchyType ? request?.body?.CampaignDetails?.hierarchyType : null;
request.body.CampaignDetails.additionalDetails = request?.body?.CampaignDetails?.additionalDetails ? request?.body?.CampaignDetails?.additionalDetails : {};
request.body.CampaignDetails.projectType = request?.body?.CampaignDetails?.projectType || null;
request.body.CampaignDetails.hierarchyType = request?.body?.CampaignDetails?.hierarchyType || null;
request.body.CampaignDetails.additionalDetails = request?.body?.CampaignDetails?.additionalDetails || {};
request.body.CampaignDetails.startDate = request?.body?.CampaignDetails?.startDate || null
request.body.CampaignDetails.endDate = request?.body?.CampaignDetails?.endDate || null
request.body.CampaignDetails.auditDetails = {
Expand All @@ -284,18 +286,19 @@ async function enrichAndPersistCampaignForCreate(request: any) {
lastModifiedBy: request?.body?.RequestInfo?.userInfo?.uuid,
lastModifiedTime: Date.now(),
}
if (action == "create" && !request?.body?.CampaignDetails?.projectId) {
if (action == "create" && !request?.body?.CampaignDetails?.projectId && !firstPersist) {
enrichRootProjectId(request.body);
}
else {
request.body.CampaignDetails.projectId = null
}
logger.info("Persisting CampaignDetails : " + JSON.stringify(request?.body?.CampaignDetails));
produceModifiedMessages(request?.body, config.KAFKA_SAVE_PROJECT_CAMPAIGN_DETAILS_TOPIC);
const topic = firstPersist ? config.KAFKA_SAVE_PROJECT_CAMPAIGN_DETAILS_TOPIC : config.KAFKA_UPDATE_PROJECT_CAMPAIGN_DETAILS_TOPIC
produceModifiedMessages(request?.body, topic);
delete request.body.CampaignDetails.campaignDetails
}

async function enrichAndPersistCampaignForUpdate(request: any) {
async function enrichAndPersistCampaignForUpdate(request: any, firstPersist: boolean = false) {
const action = request?.body?.CampaignDetails?.action;
const ExistingCampaignDetails = request?.body?.ExistingCampaignDetails;
request.body.CampaignDetails.campaignNumber = ExistingCampaignDetails?.campaignNumber
Expand Down Expand Up @@ -327,12 +330,12 @@ async function enrichAndPersistCampaignForUpdate(request: any) {
}


async function enrichAndPersistProjectCampaignRequest(request: any, actionInUrl: any) {
async function enrichAndPersistProjectCampaignRequest(request: any, actionInUrl: any, firstPersist: boolean = false) {
if (actionInUrl == "create") {
await enrichAndPersistCampaignForCreate(request)
await enrichAndPersistCampaignForCreate(request, firstPersist)
}
else if (actionInUrl == "update") {
await enrichAndPersistCampaignForUpdate(request)
await enrichAndPersistCampaignForUpdate(request, firstPersist)
}
}

Expand Down Expand Up @@ -757,10 +760,7 @@ async function createProject(request: any, actionUrl: any) {
}
}

async function processBasedOnAction(request: any, actionInUrl: any) {
if (actionInUrl == "create") {
request.body.CampaignDetails.id = uuidv4()
}
async function processAfterPersist(request: any, actionInUrl: any) {
if (request?.body?.CampaignDetails?.action == "create") {
await createProjectCampaignResourcData(request);
await createProject(request, actionInUrl)
Expand All @@ -770,6 +770,14 @@ async function processBasedOnAction(request: any, actionInUrl: any) {
await enrichAndPersistProjectCampaignRequest(request, actionInUrl)
}
}

async function processBasedOnAction(request: any, actionInUrl: any) {
if (actionInUrl == "create") {
request.body.CampaignDetails.id = uuidv4()
}
await enrichAndPersistProjectCampaignRequest(request, actionInUrl, true)
processAfterPersist(request, actionInUrl)
}
async function appendSheetsToWorkbook(boundaryData: any[], differentTabsBasedOnLevel: any) {
try {
const uniqueDistrictsForMainSheet: string[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,29 @@ async function validateProjectCampaignBoundaries(boundaries: any[], hierarchyTyp
}
}

async function validateProjectCampaignResources(resources: any[]) {
async function validateResources(resources: any, request: any) {
for (const resource of resources) {
const resourceDetails = {
type: resource.type,
fileStoreId: resource.filestoreId,
tenantId: request?.body?.CampaignDetails?.tenantId,
action: "validate",
hierarchyType: request?.body?.CampaignDetails?.hierarchyType,
additionalDetails: {}
};
try {
await axios.post(`${config.host.projectFactoryBff}project-factory/v1/data/_create`, {
RequestInfo: request.body.RequestInfo,
ResourceDetails: resourceDetails
});
} catch (error: any) {
logger.error(`Error during resource validation of ${resourceDetails.fileStoreId} :` + error?.response?.data?.Errors?.[0]?.description || error?.response?.data?.Errors?.[0]?.message);
throwError("COMMON", error?.response?.status, error?.response?.data?.Errors?.[0]?.code, `Error during resource validation of ${resourceDetails.fileStoreId} :` + error?.response?.data?.Errors?.[0]?.description || error?.response?.data?.Errors?.[0]?.message);
}
}
}

async function validateProjectCampaignResources(resources: any, request: any) {
if (resources) {
if (!Array.isArray(resources)) {
throwError("COMMON", 400, "VALIDATION_ERROR", "resources should be an array");
Expand All @@ -315,10 +337,14 @@ async function validateProjectCampaignResources(resources: any[]) {
throwError("COMMON", 400, "VALIDATION_ERROR", "Invalid resource type");
}
}
if (request?.body?.CampaignDetails?.action == "create" && request?.body?.CampaignDetails?.resources) {
await validateResources(request.body.CampaignDetails.resources, request);
}
}
}



function validateProjectCampaignMissingFields(CampaignDetails: any) {
const ajv = new Ajv();
const validate = ajv.compile(campaignDetailsSchema);
Expand Down Expand Up @@ -417,7 +443,7 @@ async function validateProjectCampaignRequest(request: any, actionInUrl: any) {
}

await validateProjectCampaignBoundaries(boundaries, hierarchyType, tenantId, request);
await validateProjectCampaignResources(resources);
await validateProjectCampaignResources(resources, request);
}
if (actionInUrl == "update") {
await validateById(request);
Expand Down

0 comments on commit 3cbdf66

Please sign in to comment.