Skip to content

Commit 7c8752a

Browse files
janekhuongtektaxi
authored andcommitted
Created function to find hacker by their status
1 parent df47e20 commit 7c8752a

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

services/hacker.service.js

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ function updateOne(id, hackerDetails) {
3737
const TAG = `[Hacker Service # update ]:`;
3838

3939
const query = {
40-
_id: id
40+
_id: id,
4141
};
4242

43-
return logger.logUpdate(TAG, "hacker", Hacker.findOneAndUpdate(query, hackerDetails, { new: true }));
43+
return logger.logUpdate(
44+
TAG,
45+
"hacker",
46+
Hacker.findOneAndUpdate(query, hackerDetails, { new: true }),
47+
);
4448
}
4549

4650
/**
@@ -67,7 +71,12 @@ async function findIds(queries) {
6771
let ids = [];
6872

6973
for (const query of queries) {
70-
let currId = await logger.logQuery(TAG, "hacker", query, Hacker.findOne(query, "_id"));
74+
let currId = await logger.logQuery(
75+
TAG,
76+
"hacker",
77+
query,
78+
Hacker.findOne(query, "_id"),
79+
);
7180
ids.push(currId);
7281
}
7382
return ids;
@@ -81,21 +90,40 @@ async function findIds(queries) {
8190
function findByAccountId(accountId) {
8291
const TAG = `[ Hacker Service # findByAccountId ]:`;
8392
const query = {
84-
accountId: accountId
93+
accountId: accountId,
8594
};
8695

8796
return logger.logUpdate(TAG, "hacker", Hacker.findOne(query));
8897
}
8998

99+
/**
100+
* Find all hackers with a specific status
101+
* @param {string} status - The status to search for (e.g., "Accepted", "Declined")
102+
* @return {Promise<Array<Hacker>>} Array of hacker documents with the specified status
103+
*/
104+
function findByStatus(status) {
105+
const TAG = `[ Hacker Service # findByStatus ]:`;
106+
const query = { status: status };
107+
108+
return logger.logQuery(
109+
TAG,
110+
"hacker",
111+
query,
112+
Hacker.find(query).populate("accountId"),
113+
);
114+
}
115+
90116
async function getStatsAllHackersCached() {
91117
const TAG = `[ hacker Service # getStatsAll ]`;
92118
if (cache.get(Constants.CACHE_KEY_STATS) !== null) {
93119
logger.info(`${TAG} Getting cached stats`);
94120
return cache.get(Constants.CACHE_KEY_STATS);
95121
}
96-
const allHackers = await logger.logUpdate(TAG, "hacker", Hacker.find({})).populate({
97-
path: "accountId"
98-
});
122+
const allHackers = await logger
123+
.logUpdate(TAG, "hacker", Hacker.find({}))
124+
.populate({
125+
path: "accountId",
126+
});
99127
cache.put(Constants.CACHE_KEY_STATS, stats, Constants.CACHE_TIMEOUT_STATS); //set a time-out of 5 minutes
100128
return getStats(allHackers);
101129
}
@@ -106,7 +134,7 @@ async function getStatsAllHackersCached() {
106134
*/
107135
async function generateQRCode(str) {
108136
const response = await QRCode.toDataURL(str, {
109-
scale: 4
137+
scale: 4,
110138
});
111139
return response;
112140
}
@@ -139,7 +167,7 @@ function getStats(hackers) {
139167
dietaryRestrictions: {},
140168
shirtSize: {},
141169
age: {},
142-
applicationDate: {}
170+
applicationDate: {},
143171
};
144172

145173
hackers.forEach((hacker) => {
@@ -213,7 +241,9 @@ function getStats(hackers) {
213241
// const age = hacker.accountId.getAge();
214242
// stats.age[age] = stats.age[age] ? stats.age[age] + 1 : 1;
215243

216-
stats.age[hacker.accountId.age] = stats.age[hacker.accountId.age] ? stats.age[age] + 1 : 1;
244+
stats.age[hacker.accountId.age] = stats.age[hacker.accountId.age]
245+
? stats.age[age] + 1
246+
: 1;
217247

218248
const applicationDate = hacker._id
219249
.getTimestamp() //
@@ -235,8 +265,9 @@ module.exports = {
235265
updateOne: updateOne,
236266
findIds: findIds,
237267
findByAccountId: findByAccountId,
268+
findByStatus: findByStatus,
238269
getStats: getStats,
239270
getStatsAllHackersCached: getStatsAllHackersCached,
240271
generateQRCode: generateQRCode,
241-
generateHackerViewLink: generateHackerViewLink
272+
generateHackerViewLink: generateHackerViewLink,
242273
};

0 commit comments

Comments
 (0)