Skip to content

Commit 1b65791

Browse files
authored
Merge branch 'develop' into fetch-logs
2 parents 5fe2c65 + daed096 commit 1b65791

File tree

10 files changed

+114
-9
lines changed

10 files changed

+114
-9
lines changed

controllers/extensionRequests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const createTaskExtensionRequest = async (req, res) => {
7474
type: "extensionRequests",
7575
meta: {
7676
taskId: extensionBody.taskId,
77-
createdBy: req.userData.id,
77+
userId: req.userData.id,
7878
},
7979
body: {
8080
extensionRequestId: extensionRequest.id,

controllers/extensionRequestsv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export const createTaskExtensionRequest = async (req: ExtensionRequestRequest, r
8484
taskId,
8585
requestId: extensionRequest.id,
8686
action: LOG_ACTION.CREATE,
87-
createdBy: requestedBy,
87+
userId: requestedBy,
8888
createdAt: Date.now(),
8989
},
9090
body: extensionBody,

controllers/invites.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const createInviteLink = async (req: InviteBodyRequest, res: CustomRespon
3232
type: logType.DISCORD_INVITES,
3333
meta: {
3434
action: "create",
35-
createdBy: logType.EXTERNAL_SERVICE,
35+
userId: logType.EXTERNAL_SERVICE,
3636
createdAt: Date.now(),
3737
},
3838
body: {

controllers/logs.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,19 @@ const fetchAllLogs = async (req, res) => {
7575
}
7676
};
7777

78+
const updateLogs = async (req, res) => {
79+
try {
80+
const response = await logsQuery.updateLogs();
81+
return res.json({
82+
response,
83+
});
84+
} catch (error) {
85+
return res.boom.serverUnavailable(SOMETHING_WENT_WRONG);
86+
}
87+
};
88+
7889
module.exports = {
7990
fetchLogs,
8091
fetchAllLogs,
92+
updateLogs,
8193
};

controllers/oooRequests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const createOooRequestController = async (req: OooRequestCreateRequest, r
4141
meta: {
4242
requestId: requestResult.id,
4343
action: LOG_ACTION.CREATE,
44-
createdBy: userId,
44+
userId: userId,
4545
createdAt: Date.now(),
4646
},
4747
body: requestResult,
@@ -84,7 +84,7 @@ export const updateOooRequestController = async (req: UpdateRequest, res: Custom
8484
meta: {
8585
requestId: requestId,
8686
action: LOG_ACTION.UPDATE,
87-
createdBy: userId,
87+
userId: userId,
8888
createdAt: Date.now(),
8989
},
9090
body: requestResult,

controllers/taskRequestsv2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export const createTaskRequestController = async (req: TaskRequestRequest, res:
9292
meta: {
9393
taskRequestId: updatedRequest.id,
9494
action: "update",
95-
createdBy: req.userData.id,
95+
userId: req.userData.id,
9696
createdAt: Date.now(),
9797
lastModifiedBy: req.userData.id,
9898
lastModifiedAt: Date.now(),
@@ -150,7 +150,7 @@ export const createTaskRequestController = async (req: TaskRequestRequest, res:
150150
meta: {
151151
taskRequestId: newTaskRequest.id,
152152
action: "create",
153-
createdBy: req.userData.id,
153+
userId: req.userData.id,
154154
createdAt: Date.now(),
155155
lastModifiedBy: req.userData.id,
156156
lastModifiedAt: Date.now(),

controllers/tasksRequests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const addTaskRequests = async (req, res) => {
104104
meta: {
105105
taskRequestId: newTaskRequest.id,
106106
action: "create",
107-
createdBy: req.userData.id,
107+
userId: req.userData.id,
108108
createdAt: Date.now(),
109109
lastModifiedBy: req.userData.id,
110110
lastModifiedAt: Date.now(),
@@ -206,7 +206,7 @@ const updateTaskRequests = async (req, res) => {
206206
taskRequestId: taskRequestId,
207207
action: "update",
208208
subAction: action,
209-
createdBy: req.userData.id,
209+
userId: req.userData.id,
210210
createdAt: Date.now(),
211211
lastModifiedBy: req.userData.id,
212212
lastModifiedAt: Date.now(),

models/logs.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,72 @@ const fetchAllLogs = async (query) => {
288288
}
289289
};
290290

291+
const updateLogs = async () => {
292+
const batchSize = 500;
293+
let lastDoc = null;
294+
let isCompleted = false;
295+
296+
const summary = {
297+
totalLogsProcessed: 0,
298+
totalLogsUpdated: 0,
299+
totalOperationsFailed: 0,
300+
failedLogDetails: [],
301+
};
302+
303+
try {
304+
while (!isCompleted) {
305+
let query = logsModel.orderBy("timestamp").limit(batchSize);
306+
if (lastDoc) {
307+
query = query.startAfter(lastDoc);
308+
}
309+
const snapshot = await query.get();
310+
311+
if (snapshot.empty) {
312+
isCompleted = true;
313+
continue;
314+
}
315+
316+
const batch = firestore.batch();
317+
snapshot.forEach((doc) => {
318+
const data = doc.data();
319+
if (data.meta && data.meta.createdBy) {
320+
const updatedMeta = {
321+
...data.meta,
322+
userId: data.meta.createdBy,
323+
};
324+
delete updatedMeta.createdBy;
325+
326+
batch.update(doc.ref, { meta: updatedMeta });
327+
summary.totalLogsUpdated++;
328+
}
329+
summary.totalLogsProcessed++;
330+
});
331+
332+
try {
333+
await batch.commit();
334+
} catch (err) {
335+
logger.error("Batch update failed for logs collection:", err);
336+
summary.totalOperationsFailed += snapshot.docs.length;
337+
summary.failedLogDetails.push(...snapshot.docs.map((doc) => doc.id));
338+
}
339+
340+
lastDoc = snapshot.docs[snapshot.docs.length - 1];
341+
isCompleted = snapshot.docs.length < batchSize;
342+
}
343+
344+
logger.info("Migration completed:", summary);
345+
return summary;
346+
} catch (error) {
347+
logger.error("Error during logs migration:", error);
348+
throw error;
349+
}
350+
};
351+
291352
module.exports = {
292353
addLog,
293354
fetchLogs,
294355
fetchCacheLogs,
295356
fetchLastAddedCacheLog,
296357
fetchAllLogs,
358+
updateLogs,
297359
};

routes/logs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ const { SUPERUSER } = require("../constants/roles");
77

88
router.get("/:type", authenticate, authorizeRoles([SUPERUSER]), logs.fetchLogs);
99
router.get("/", authenticate, authorizeRoles([SUPERUSER]), logs.fetchAllLogs);
10+
router.post("/migrate", authenticate, authorizeRoles([SUPERUSER]), logs.updateLogs);
1011

1112
module.exports = router;

test/integration/logs.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,36 @@ describe("/logs", function () {
264264
});
265265
});
266266

267+
describe("Update logs", function () {
268+
it("should run the migration and update logs successfully", async function () {
269+
const res = await chai.request(app).post("/logs/migrate").set("cookie", `${cookieName}=${superUserToken}`).send();
270+
271+
expect(res).to.have.status(200);
272+
expect(res.body).to.be.an("object");
273+
expect(res.body.response).to.have.property("totalLogsProcessed").that.is.a("number");
274+
expect(res.body.response).to.have.property("totalLogsUpdated").that.is.a("number");
275+
expect(res.body.response).to.have.property("totalOperationsFailed").that.is.a("number");
276+
expect(res.body.response).to.have.property("failedLogDetails").that.is.an("array");
277+
278+
expect(res.body.response.totalLogsProcessed).to.be.at.least(0);
279+
expect(res.body.response.totalLogsUpdated).to.be.lessThanOrEqual(res.body.response.totalLogsProcessed);
280+
281+
expect(res.body.response).to.have.all.keys(
282+
"totalLogsProcessed",
283+
"totalLogsUpdated",
284+
"totalOperationsFailed",
285+
"failedLogDetails"
286+
);
287+
});
288+
289+
it("should return error if unauthorized user tries to run migration", async function () {
290+
const res = await chai.request(app).post("/logs/migrate").set("cookie", `${cookieName}=invalidToken`).send();
291+
292+
expect(res).to.have.status(401);
293+
expect(res.body).to.have.property("error").that.is.a("string");
294+
});
295+
});
296+
267297
describe("Add logs when user doc is update", function () {
268298
let jwt;
269299
let userId;

0 commit comments

Comments
 (0)