Skip to content

Commit c976157

Browse files
Update: replace query of username to userId
1 parent c55479f commit c976157

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

controllers/members.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const members = require("../models/members");
33
const tasks = require("../models/tasks");
44
const { SOMETHING_WENT_WRONG } = require("../constants/errorMessages");
55
const dataAccess = require("../services/dataAccessLayer");
6+
const { addLog } = require("../models/logs");
67
/**
78
* Fetches the data about our members
89
*
@@ -82,18 +83,23 @@ const moveToMembers = async (req, res) => {
8283
const archiveMembers = async (req, res) => {
8384
try {
8485
const { username } = req.params;
85-
const user = await fetchUser({ username });
86-
const userId = user.user.id;
86+
const user = await dataAccess.retrieveUsers({ username });
8787
const superUserId = req.userData.id;
8888
const body = req.body;
89+
const meta = {
90+
userId: user?.user?.id,
91+
superUserId: superUserId,
92+
username: username,
93+
};
8994
if (!body?.reason || /^\s*$/.test(body?.reason)) {
9095
return res.boom.badRequest("Reason is required");
9196
}
9297
if (user?.userExists) {
93-
const successObject = await members.addArchiveRoleToMembers(userId, username, superUserId, body?.reason);
98+
const successObject = await members.addArchiveRoleToMembers(user.user.id);
9499
if (successObject.isArchived) {
95100
return res.boom.badRequest("User is already archived");
96101
}
102+
addLog("archiveDetails", meta, { body: body?.reason });
97103
return res.status(204).send();
98104
}
99105
return res.boom.notFound("User doesn't exist");

models/logs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ const fetchLogs = async (query, param) => {
4242
}
4343
});
4444

45-
const { limit, lastDocId, username } = query;
45+
const { limit, lastDocId, userId } = query;
4646
let lastDoc;
4747
const limitDocuments = Number(limit);
4848

4949
if (lastDocId) {
5050
lastDoc = await logsModel.doc(lastDocId).get();
5151
}
52-
if (username) {
52+
if (userId) {
5353
const logsSnapshot = await logsModel
5454
.where("type", "==", param)
55-
.where("meta.username", "==", username)
55+
.where("meta.userId", "==", userId)
5656
.orderBy("timestamp", "desc")
5757
.get();
5858
const logs = [];

test/unit/models/logs.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ describe("Logs", function () {
4646
const query = {};
4747

4848
const data = await logsQuery.fetchLogs(query, type);
49+
4950
expect(data).to.be.an("array").with.lengthOf(0);
5051
});
5152
it("Should fetch all archived logs", async function () {
@@ -55,7 +56,7 @@ describe("Logs", function () {
5556
await logsQuery.addLog(type, meta, body);
5657
const data = await logsQuery.fetchLogs(query, type);
5758

58-
expect(data).to.be.an("array").with.lengthOf(1);
59+
expect(data).to.be.an("array").with.lengthOf.greaterThan(0);
5960
expect(data[0]).to.have.property("timestamp").that.is.an("object");
6061
expect(data[0].timestamp).to.have.property("_seconds").that.is.a("number");
6162
expect(data[0].timestamp).to.have.property("_nanoseconds").that.is.a("number");
@@ -65,12 +66,12 @@ describe("Logs", function () {
6566
it("Should fetch all archived logs for given username", async function () {
6667
const { type, meta, body } = logsData.archivedUserDetailsModal[0];
6768
const query = {
68-
username: meta.username,
69+
userId: meta.userId,
6970
};
7071
await logsQuery.addLog(type, meta, body);
7172
const data = await logsQuery.fetchLogs(query, type);
7273

73-
expect(data).to.be.an("array").to.have.lengthOf.greaterThan(0);
74+
expect(data).to.be.an("array").with.lengthOf.greaterThan(0);
7475
expect(data[0]).to.have.property("timestamp").that.is.an("object");
7576
expect(data[0].timestamp).to.have.property("_seconds").that.is.a("number");
7677
expect(data[0].timestamp).to.have.property("_nanoseconds").that.is.a("number");
@@ -80,7 +81,7 @@ describe("Logs", function () {
8081
it("Should throw response status 404, if username is incorrect in the query", async function () {
8182
const { type, meta, body } = logsData.archivedUserDetailsModal[0];
8283
const query = {
83-
username: "TEST_USERNAME", // incorrect username
84+
userId: "1234_test", // incorrect username
8485
};
8586
await logsQuery.addLog(type, meta, body);
8687
const data = await logsQuery.fetchLogs(query, type);

0 commit comments

Comments
 (0)