Skip to content

Commit

Permalink
Merge pull request #1072 from egovernments/boundary-bulk-search
Browse files Browse the repository at this point in the history
Feat : added search controller
  • Loading branch information
ashish-egov authored Jul 15, 2024
2 parents 9c579c0 + 67bbd9b commit 39d0a41
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 7 deletions.
5 changes: 1 addition & 4 deletions utilities/boundary-bulk-bff/src/server/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ const config = {
DB_NAME: process.env.DB_NAME || "postgres",
DB_PASSWORD: process.env.DB_PASSWORD || "postgres",
DB_PORT: process.env.DB_PORT || "5432",
DB_CAMPAIGN_DETAILS_TABLE_NAME: `${getDBSchemaName(process.env.DB_SCHEMA)}.eg_cm_campaign_details`,
DB_CAMPAIGN_PROCESS_TABLE_NAME: `${getDBSchemaName(process.env.DB_SCHEMA)}.eg_cm_campaign_process`,
DB_GENERATED_RESOURCE_DETAILS_TABLE_NAME: `${getDBSchemaName(process.env.DB_SCHEMA)}.eg_cm_generated_resource_details`,
DB_RESOURCE_DETAILS_TABLE_NAME: `${getDBSchemaName(process.env.DB_SCHEMA)}.eg_cm_resource_details`
DB_BOUNDARY_DETAILS_TABLE_NAME: `${getDBSchemaName(process.env.DB_SCHEMA)}.eg_boundary_bulk_details`,
},
// Application configuration
app: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const boundarySearchBodySchema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"hierarchyType": {
"type": "string",
"maxLength": 128,
"minLength": 1
},
"tenantId": {
"type": "string",
"maxLength": 64,
"minLength": 1
},
"id": {
"type": "string",
"minLength": 1,
"maxLength": 128
}
},
"required": ["tenantId", "hierarchyType"]
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as express from "express";
import { } from "../../service/boundaryManageService";
import { searchBoundaryDetailService } from "../../service/boundaryManageService";
import { logger } from "../../utils/logger";
import { errorResponder, sendResponse } from "../../utils/genericUtils";
import { createBoundariesService } from "../../service/boundaryManageService";
Expand All @@ -21,6 +21,7 @@ class boundaryManageController {
// Initialize routes for MeasurementController
public intializeRoutes() {
this.router.post(`${this.path}/create`, this.createBoundaries);
this.router.post(`${this.path}/search`, this.searchBoundaryDetails);
}
/**
* Handles the creation of a project type campaign.
Expand All @@ -43,6 +44,22 @@ class boundaryManageController {
}
};

searchBoundaryDetails = async (
request: express.Request,
response: express.Response
) => {
try {
logger.info("RECEIVED A BOUNDARY DETAILS SEARCH REQUEST");
await searchBoundaryDetailService(request);
return sendResponse(response, { boundaryDetails: request.body.boundaryDetails }, request);
} catch (e: any) {
console.log(e)
logger.error(String(e))
// Handle errors and send error response
return errorResponder({ message: String(e), code: e?.code, description: e?.description }, request, response, e?.status || 500);
}
};

};
export default boundaryManageController;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import express from "express";
import { logger } from "../utils/logger";
import { validateCreateBoundariesRequest } from "../validators/boundaryValidator";
import { validateCreateBoundariesRequest, validateSearchBoundaryDetailRequest } from "../validators/boundaryValidator";
import { getLocalizedMessagesHandler } from "../utils/localisationUtils";
import { boundaryBulkUpload } from "../utils/boundaryUtils";
import { boundaryBulkUpload, getBoundaryDetails } from "../utils/boundaryUtils";
import { enrichAndPersistBoundaryDetails } from "../utils/persistUtils";

export async function createBoundariesService(request: express.Request, response: express.Response) {
Expand All @@ -15,3 +15,8 @@ export async function createBoundariesService(request: express.Request, response

boundaryBulkUpload(request, localizationMap);
}

export async function searchBoundaryDetailService(request: express.Request) {
validateSearchBoundaryDetailRequest(request);
await getBoundaryDetails(request);
}
35 changes: 35 additions & 0 deletions utilities/boundary-bulk-bff/src/server/utils/boundaryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import config from "../config";
import { executeQuery } from "./db";
import { throwError } from "./errorUtils";
import { addDataToSheet, createAndUploadFile, createExcelSheet, getExcelWorkbookFromFileURL, getNewExcelWorkbook, getSheetData, prepareDataForExcel } from "./excelUtils";
import { getLocalizedHeaders, getLocalizedName, transformAndCreateLocalisation } from "./localisationUtils";
Expand Down Expand Up @@ -599,3 +600,37 @@ export async function boundaryBulkUpload(request: any, localizationMap?: any) {
persistError(error, request);
}
}

export async function getBoundaryDetails(request: any) {
const boundaryDetails = await getBoundaryDetailsViaDb(request?.query);
request.body.boundaryDetails = boundaryDetails;
}

async function getBoundaryDetailsViaDb(searchBody: any) {
const { hierarchyType, tenantId, id } = searchBody || {};

let query = `SELECT * FROM ${config.DB_CONFIG.DB_BOUNDARY_DETAILS_TABLE_NAME} WHERE 1=1`;
const values: any[] = [];

if (tenantId) {
query += ' AND tenantId = $' + (values.length + 1);
values.push(tenantId);
}
if (hierarchyType) {
query += ' AND hierarchyType = $' + (values.length + 1);
values.push(hierarchyType);
}
if (id) {
query += ' AND id = $' + (values.length + 1);
values.push(id);
}

try {
const result = await executeQuery(query, values);
return result.rows;
} catch (error: any) {
console.log(error)
logger.error(`Error fetching boundary details: ${error.message}`);
throw error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import config from "../config";
import { getLocalizedHeaders, getLocalizedName } from "../utils/localisationUtils";
import { getHeadersOfBoundarySheet, getHierarchy } from "../utils/boundaryUtils";
import { getSheetData } from "../utils/excelUtils";
import { boundarySearchBodySchema } from "../config/schemas/boundarySearchBody";
const _ = require('lodash');


Expand Down Expand Up @@ -78,4 +79,8 @@ export async function validateCreateBoundariesRequest(request: any, localization
}
const fileUrl = await validateFile(request?.query?.tenantId, request?.query?.fileStoreId);
await validateBoundarySheetData(request, fileUrl, localizationMap);
}

export function validateSearchBoundaryDetailRequest(request: any) {
validateBodyViaSchema(boundarySearchBodySchema, request.query);
}

0 comments on commit 39d0a41

Please sign in to comment.