Skip to content
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

User flag hcm #920

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion utilities/project-factory/migration/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ COPY migrate.sh /usr/bin/migrate.sh

RUN chmod +x /usr/bin/migrate.sh

ENTRYPOINT ["/usr/bin/migrate.sh"]
ENTRYPOINT ["/usr/bin/migrate.sh"]
85 changes: 70 additions & 15 deletions utilities/project-factory/src/server/api/campaignApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,31 @@ function matchCreatedAndSearchedData(createdData: any[], searchedData: any[], re
request.body.Activities = activities
}

const createBatchRequest = async (request: any, batch: any[]) => {
async function getUuidsError(request: any, response: any, mobileNumberRowNumberMapping: any) {
var errors: any[] = []
var count = 0;
request.body.mobileNumberUuidsMapping = request.body.mobileNumberUuidsMapping ? request.body.mobileNumberUuidsMapping : {};
for (const user of response.Individual) {
if (!user?.userUuid) {
logger.info(`User with mobileNumber ${user?.mobileNumber} doesn't have userUuid`)
errors.push({ status: "INVALID", rowNumber: mobileNumberRowNumberMapping[user?.mobileNumber], errorDetails: `User with mobileNumber ${user?.mobileNumber} doesn't have userUuid` })
count++;
}
else if (!user?.userDetails?.username) {
logger.info(`User with mobileNumber ${user?.mobileNumber} doesn't have username`)
errors.push({ status: "INVALID", rowNumber: mobileNumberRowNumberMapping[user?.mobileNumber], errorDetails: `User with mobileNumber ${user?.mobileNumber} doesn't have username` })
count++;
}
else {
request.body.mobileNumberUuidsMapping[user?.mobileNumber] = { userUuid: user?.id, code: user?.userDetails?.username, rowNumber: mobileNumberRowNumberMapping[user?.mobileNumber] }
}
}
if (count > 0) {
request.body.sheetErrorDetails = request?.body?.sheetErrorDetails ? [...request?.body?.sheetErrorDetails, ...errors] : errors;
}
}

const createBatchRequest = async (request: any, batch: any[], mobileNumberRowNumberMapping: any) => {
const searchBody = {
RequestInfo: request?.body?.RequestInfo,
Individual: {
Expand All @@ -258,19 +282,25 @@ const createBatchRequest = async (request: any, batch: any[]) => {
includeDeleted: true
};
logger.info("Individual search to validate the mobile no initiated");
logger.info("Individual search url : " + config.host.healthIndividualHost + "health-individual/v1/_search");
logger.info("Individual search body : " + JSON.stringify(searchBody));
logger.info("Individual search params : " + JSON.stringify(params));
const response = await httpRequest(config.host.healthIndividualHost + "health-individual/v1/_search", searchBody, params);

if (!response) {
throwError("COMMON", 400, "INTERNAL_SERVER_ERROR", "Error occurred during user search while validating mobile number.");
}
if (config.values.notCreateUserIfAlreadyThere) {
await getUuidsError(request, response, mobileNumberRowNumberMapping);
}

if (response?.Individual?.length > 0) {
return response.Individual.map((item: any) => item?.mobileNumber);
}
return [];
};

async function getUserWithMobileNumbers(request: any, mobileNumbers: any[]) {
async function getUserWithMobileNumbers(request: any, mobileNumbers: any[], mobileNumberRowNumberMapping: any) {
logger.info("mobileNumbers to search: " + JSON.stringify(mobileNumbers));
const BATCH_SIZE = 50;
let allResults: any[] = [];
Expand All @@ -279,7 +309,7 @@ async function getUserWithMobileNumbers(request: any, mobileNumbers: any[]) {
const batchPromises = [];
for (let i = 0; i < mobileNumbers.length; i += BATCH_SIZE) {
const batch = mobileNumbers.slice(i, i + BATCH_SIZE);
batchPromises.push(createBatchRequest(request, batch));
batchPromises.push(createBatchRequest(request, batch, mobileNumberRowNumberMapping));
}

// Wait for all batch requests to complete
Expand All @@ -289,7 +319,6 @@ async function getUserWithMobileNumbers(request: any, mobileNumbers: any[]) {
for (const result of batchResults) {
allResults = allResults.concat(result);
}

// Convert the results array to a Set to eliminate duplicates
const resultSet = new Set(allResults);
logger.info(`Already Existing mobile numbers : ${JSON.stringify(resultSet)}`);
Expand All @@ -306,9 +335,9 @@ async function matchUserValidation(createdData: any[], request: any) {
return acc;
}, {});
logger.info("mobileNumberRowNumberMapping : " + JSON.stringify(mobileNumberRowNumberMapping));
const mobileNumberResponse = await getUserWithMobileNumbers(request, mobileNumbers);
const mobileNumberResponse = await getUserWithMobileNumbers(request, mobileNumbers, mobileNumberRowNumberMapping);
for (const key in mobileNumberRowNumberMapping) {
if (mobileNumberResponse.has(key)) {
if (mobileNumberResponse.has(key) && !config.values.notCreateUserIfAlreadyThere) {
errors.push({ status: "INVALID", rowNumber: mobileNumberRowNumberMapping[key], errorDetails: `User with mobileNumber ${key} already exists` })
count++;
}
Expand Down Expand Up @@ -605,7 +634,7 @@ async function enrichEmployees(employees: any[], request: any) {
}
}

function enrichDataToCreateForUser(dataToCreate: any[], responsePayload: any) {
function enrichDataToCreateForUser(dataToCreate: any[], responsePayload: any, request: any) {
const createdEmployees = responsePayload?.Employees;
// create an object which have keys as employee.code and values as employee.uuid
const employeeMap = createdEmployees.reduce((map: any, employee: any) => {
Expand Down Expand Up @@ -643,17 +672,43 @@ async function performAndSaveResourceActivity(request: any, createAndSearchConfi
var responsePayload = await httpRequest(createAndSearchConfig?.createBulkDetails?.url, newRequestBody, params, "post", undefined, undefined, true);
}
else if (type == "user") {
var responsePayload = await httpRequest(createAndSearchConfig?.createBulkDetails?.url, newRequestBody, params, "post", undefined, undefined, true);
if (responsePayload?.Employees && responsePayload?.Employees?.length > 0) {
enrichDataToCreateForUser(dataToCreate, responsePayload);
if (config.values.notCreateUserIfAlreadyThere) {
var Employees: any[] = []
if (request.body?.mobileNumberUuidsMapping) {
for (const employee of newRequestBody.Employees) {
if (request.body.mobileNumberUuidsMapping[employee?.user?.mobileNumber]) {
logger.info(`User with mobile number ${employee?.user?.mobileNumber} already exist`);
}
else {
Employees.push(employee)
}
}
}
newRequestBody.Employees = Employees
}
if (newRequestBody.Employees.length > 0) {
var responsePayload = await httpRequest(createAndSearchConfig?.createBulkDetails?.url, newRequestBody, params, "post", undefined, undefined, true);
if (responsePayload?.Employees && responsePayload?.Employees?.length > 0) {
enrichDataToCreateForUser(dataToCreate, responsePayload, request);
}
else {
throwError("COMMON", 500, "INTERNAL_SERVER_ERROR", "Some internal server error occured during user creation.");
}
var activity = await generateActivityMessage(request?.body?.ResourceDetails?.tenantId, request.body, newRequestBody, responsePayload, type, createAndSearchConfig?.createBulkDetails?.url, responsePayload?.statusCode)
logger.info(`Activity : ${createAndSearchConfig?.createBulkDetails?.url} status: ${responsePayload?.statusCode}`);
activities.push(activity);
}
else {
throwError("COMMON", 500, "INTERNAL_SERVER_ERROR", "Some internal server error occured during user creation.");
}
}
if (request.body.ResourceDetails.type == "user" && request?.body?.mobileNumberUuidsMapping) {
for (const employee of request.body.dataToCreate) {
if (request?.body?.mobileNumberUuidsMapping[employee?.user?.mobileNumber]) {
employee.uuid = request?.body?.mobileNumberUuidsMapping[employee?.user?.mobileNumber].userUuid;
employee.code = request?.body?.mobileNumberUuidsMapping[employee?.user?.mobileNumber].code;
employee.user.userName = request?.body?.mobileNumberUuidsMapping[employee?.user?.mobileNumber].code;
employee.user.password = config.user.userDefaultPassword;
}
}
var activity = await generateActivityMessage(request?.body?.ResourceDetails?.tenantId, request.body, newRequestBody, responsePayload, type, createAndSearchConfig?.createBulkDetails?.url, responsePayload?.statusCode)
logger.info(`Activity : ${createAndSearchConfig?.createBulkDetails?.url} status: ${responsePayload?.statusCode}`);
activities.push(activity);
}
logger.info(`Waiting for 10 seconds`);
await new Promise(resolve => setTimeout(resolve, 10000));
Expand Down
7 changes: 4 additions & 3 deletions utilities/project-factory/src/server/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ if (!HOST) {
}


const getDBSchemaName = (dbSchema="")=>{
return dbSchema ? (dbSchema=="egov"?"public":dbSchema) :"public";
const getDBSchemaName = (dbSchema = "") => {
return dbSchema ? (dbSchema == "egov" ? "public" : dbSchema) : "public";
}
// Configuration object containing various environment variables
const config = {
Expand Down Expand Up @@ -143,7 +143,8 @@ const config = {
idName: process.env.CMP_IDGEN_IDNAME || "campaign.number"
},
matchFacilityData: false,
retryCount: process.env.CREATE_RESOURCE_RETRY_COUNT || "3"
retryCount: process.env.CREATE_RESOURCE_RETRY_COUNT || "3",
notCreateUserIfAlreadyThere: process.env.NOT_CREATE_USER_IF_ALREADY_THERE || false,
}
};
// Exporting getErrorCodes function and config object
Expand Down
Loading