Skip to content
Merged
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
11 changes: 0 additions & 11 deletions controllers/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ const { API_RESPONSE_MESSAGES } = require("../constants/application");
const { getUserApplicationObject } = require("../utils/application");
const admin = require("firebase-admin");

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

const getAllOrUserApplication = async (req: CustomRequest, res: CustomResponse): Promise<any> => {
try {
const { userId, status, next, size } = req.query;
Expand Down Expand Up @@ -154,5 +144,4 @@ module.exports = {
addApplication,
updateApplication,
getApplicationById,
batchUpdateApplications,
};
51 changes: 0 additions & 51 deletions models/applications.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,6 @@
import { application } from "../types/application";
const firestore = require("../utils/firestore");
const ApplicationsModel = firestore.collection("applicants");
const { DOCUMENT_WRITE_SIZE: FIRESTORE_BATCH_OPERATIONS_LIMIT } = require("../constants/constants");
import { chunks } from "../utils/array";

const batchUpdateApplications = async () => {
try {
const operationStats = {
failedApplicationUpdateIds: [],
totalFailedApplicationUpdates: 0,
totalApplicationUpdates: 0,
};

const updatedApplications = [];
const applications = await ApplicationsModel.get();

if (applications.empty) {
return operationStats;
}

operationStats.totalApplicationUpdates = applications.size;

applications.forEach((application) => {
const taskData = application.data();
taskData.createdAt = null;
updatedApplications.push({ id: application.id, data: taskData });
});

const multipleApplicationUpdateBatch = [];
const chunkedApplication = chunks(updatedApplications, FIRESTORE_BATCH_OPERATIONS_LIMIT);

chunkedApplication.forEach(async (applications) => {
const batch = firestore.batch();
applications.forEach(({ id, data }) => {
batch.update(ApplicationsModel.doc(id), data);
});
try {
await batch.commit();
multipleApplicationUpdateBatch.push(batch);
} catch (error) {
operationStats.totalFailedApplicationUpdates += applications.length;
applications.forEach(({ id }) => operationStats.failedApplicationUpdateIds.push(id));
}
});

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

const getAllApplications = async (limit: number, lastDocId?: string) => {
try {
Expand Down Expand Up @@ -182,5 +132,4 @@ module.exports = {
updateApplication,
getApplicationsBasedOnStatus,
getApplicationById,
batchUpdateApplications,
};
1 change: 0 additions & 1 deletion routes/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ router.patch(
applicationValidator.validateApplicationUpdateData,
applications.updateApplication
);
router.patch("/batch/update", authenticate, authorizeRoles([SUPERUSER]), applications.batchUpdateApplications);

module.exports = router;
39 changes: 0 additions & 39 deletions test/integration/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,43 +400,4 @@ describe("Application", function () {
});
});
});

describe("PATCH /application/batch/update", function () {
it("should return 401 if the user is not super user", function (done) {
chai
.request(app)
.patch(`/applications/batch/update`)
.set("cookie", `${cookieName}=${jwt}`)
.end((err, res) => {
if (err) {
return done(err);
}

expect(res).to.have.status(401);
expect(res.body.message).to.be.equal("You are not authorized for this action.");
return done();
});
});

it("should return updated stats after updating all the application", function (done) {
chai
.request(app)
.patch(`/applications/batch/update`)
.set("cookie", `${cookieName}=${superUserJwt}`)
.end((err, res) => {
if (err) {
return done(err);
}

expect(res).to.have.status(200);
expect(res.body).to.be.a("object");
expect(res.body).to.be.deep.equal({
failedApplicationUpdateIds: [],
totalFailedApplicationUpdates: 0,
totalApplicationUpdates: 6,
});
return done();
});
});
});
});
7 changes: 0 additions & 7 deletions test/unit/models/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,4 @@ describe("applications", function () {
expect(application.status).to.be.equal("accepted");
});
});

describe("batchUpdateApplications", function () {
it("should add createdAt null to all existing application docs", async function () {
const operationStats = await ApplicationModel.batchUpdateApplications();
expect(operationStats.totalApplicationUpdates).to.be.equal(6);
});
});
});