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

Cleanup: Revert "Migration script to add status pending in applicants collection" #2092

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 0 additions & 12 deletions controllers/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,10 @@ const getApplicationById = async (req: CustomRequest, res: CustomResponse) => {
}
};


const batchUpdateApplicantsStatus = async (req: CustomRequest, res: CustomResponse): Promise<any> => {
try {
const updateStats = await ApplicationModel.updateApplicantsStatus();
return res.json(updateStats);
} catch (err) {
logger.error(`Error in batch updating applicants: ${err}`);
return res.boom.badImplementation("Internal Server Error");
}
};

module.exports = {
getAllOrUserApplication,
addApplication,
updateApplication,
getApplicationById,
batchUpdateApplications,
batchUpdateApplicantsStatus
};
67 changes: 0 additions & 67 deletions models/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,72 +175,6 @@ const updateApplication = async (dataToUpdate: object, applicationId: string) =>
}
};

const updateApplicantsStatus = async () => {
try {
const operationStats = {
failedApplicantUpdateIds: [],
applicantUpdatesFailed: 0,
applicantUpdated: 0,
totalApplicant: 0,
};

const updatedApplicants = [];
const applicantsSnapshot = await ApplicationsModel.get();

if (applicantsSnapshot.empty) {
return operationStats;
}

operationStats.totalApplicant = applicantsSnapshot.size;

applicantsSnapshot.forEach((applicant) => {
const applicantData = applicant.data();

const createdAt = applicant.createTime.seconds * 1000 + applicant.createTime.nanoseconds / 1000000;

let propertyUpdated = false;

if ("createdAt" in applicantData === false) {
const createdAtISO = new Date(createdAt).toISOString();
applicantData.createdAt = createdAtISO;
propertyUpdated = true;
}
if ("status" in applicantData === false) {
applicantData.status = "pending";
propertyUpdated = true;
}
if (propertyUpdated === true) {
operationStats.applicantUpdated += 1;
updatedApplicants.push({ id: applicant.id, data: applicantData });
}
});

const multipleApplicantUpdateBatch = [];
const chunkedApplicants = chunks(updatedApplicants, FIRESTORE_BATCH_OPERATIONS_LIMIT);

for (const applicants of chunkedApplicants) {
const batch = firestore.batch();
applicants.forEach(({ id, data }) => {
batch.update(firestore.collection("applicants").doc(id), data);
});

try {
await batch.commit();
multipleApplicantUpdateBatch.push(batch);
} catch (error) {
operationStats.applicantUpdatesFailed += applicants.length;
applicants.forEach(({ id }) => operationStats.failedApplicantUpdateIds.push(id));
}
}

await Promise.allSettled(multipleApplicantUpdateBatch);
return operationStats;
} catch (err) {
logger.error("Error in batch update", err);
throw err;
}
};

module.exports = {
getAllApplications,
getUserApplications,
Expand All @@ -249,5 +183,4 @@ module.exports = {
getApplicationsBasedOnStatus,
getApplicationById,
batchUpdateApplications,
updateApplicantsStatus,
};
1 change: 0 additions & 1 deletion routes/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ router.patch(
applications.updateApplication
);
router.patch("/batch/update", authenticate, authorizeRoles([SUPERUSER]), applications.batchUpdateApplications);
router.post("/batch", authenticate, authorizeRoles([SUPERUSER]), applications.batchUpdateApplicantsStatus);

module.exports = router;
Loading