Skip to content

Commit 3fbf6c2

Browse files
Migration script to add status pending in applicants collection (#2087)
* feat: add migration script to add status pending in applicants collection * chore: get createdAt time from firestore metadata * chore: update api route * chore: add deleted route back --------- Co-authored-by: Prakash <om.pcprakash@gmail.com>
1 parent 828793b commit 3fbf6c2

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

controllers/applications.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,22 @@ const getApplicationById = async (req: CustomRequest, res: CustomResponse) => {
149149
}
150150
};
151151

152+
153+
const batchUpdateApplicantsStatus = async (req: CustomRequest, res: CustomResponse): Promise<any> => {
154+
try {
155+
const updateStats = await ApplicationModel.updateApplicantsStatus();
156+
return res.json(updateStats);
157+
} catch (err) {
158+
logger.error(`Error in batch updating applicants: ${err}`);
159+
return res.boom.badImplementation("Internal Server Error");
160+
}
161+
};
162+
152163
module.exports = {
153164
getAllOrUserApplication,
154165
addApplication,
155166
updateApplication,
156167
getApplicationById,
157168
batchUpdateApplications,
169+
batchUpdateApplicantsStatus
158170
};

models/applications.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,72 @@ const updateApplication = async (dataToUpdate: object, applicationId: string) =>
180180
}
181181
};
182182

183+
const updateApplicantsStatus = async () => {
184+
try {
185+
const operationStats = {
186+
failedApplicantUpdateIds: [],
187+
applicantUpdatesFailed: 0,
188+
applicantUpdated: 0,
189+
totalApplicant: 0,
190+
};
191+
192+
const updatedApplicants = [];
193+
const applicantsSnapshot = await ApplicationsModel.get();
194+
195+
if (applicantsSnapshot.empty) {
196+
return operationStats;
197+
}
198+
199+
operationStats.totalApplicant = applicantsSnapshot.size;
200+
201+
applicantsSnapshot.forEach((applicant) => {
202+
const applicantData = applicant.data();
203+
204+
const createdAt = applicant.createTime.seconds * 1000 + applicant.createTime.nanoseconds / 1000000;
205+
206+
let propertyUpdated = false;
207+
208+
if ("createdAt" in applicantData === false) {
209+
const createdAtISO = new Date(createdAt).toISOString();
210+
applicantData.createdAt = createdAtISO;
211+
propertyUpdated = true;
212+
}
213+
if ("status" in applicantData === false) {
214+
applicantData.status = "pending";
215+
propertyUpdated = true;
216+
}
217+
if (propertyUpdated === true) {
218+
operationStats.applicantUpdated += 1;
219+
updatedApplicants.push({ id: applicant.id, data: applicantData });
220+
}
221+
});
222+
223+
const multipleApplicantUpdateBatch = [];
224+
const chunkedApplicants = chunks(updatedApplicants, FIRESTORE_BATCH_OPERATIONS_LIMIT);
225+
226+
for (const applicants of chunkedApplicants) {
227+
const batch = firestore.batch();
228+
applicants.forEach(({ id, data }) => {
229+
batch.update(firestore.collection("applicants").doc(id), data);
230+
});
231+
232+
try {
233+
await batch.commit();
234+
multipleApplicantUpdateBatch.push(batch);
235+
} catch (error) {
236+
operationStats.applicantUpdatesFailed += applicants.length;
237+
applicants.forEach(({ id }) => operationStats.failedApplicantUpdateIds.push(id));
238+
}
239+
}
240+
241+
await Promise.allSettled(multipleApplicantUpdateBatch);
242+
return operationStats;
243+
} catch (err) {
244+
logger.error("Error in batch update", err);
245+
throw err;
246+
}
247+
};
248+
183249
module.exports = {
184250
getAllApplications,
185251
getUserApplications,
@@ -188,4 +254,5 @@ module.exports = {
188254
getApplicationsBasedOnStatus,
189255
getApplicationById,
190256
batchUpdateApplications,
257+
updateApplicantsStatus,
191258
};

routes/applications.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ router.patch(
2525
applications.updateApplication
2626
);
2727
router.patch("/batch/update", authenticate, authorizeRoles([SUPERUSER]), applications.batchUpdateApplications);
28+
router.post("/batch", authenticate, authorizeRoles([SUPERUSER]), applications.batchUpdateApplicantsStatus);
2829

2930
module.exports = router;

0 commit comments

Comments
 (0)