Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit f2cd91a

Browse files
agg API update
1 parent f8fec69 commit f2cd91a

File tree

3 files changed

+66
-31
lines changed

3 files changed

+66
-31
lines changed

src/api/common/helper.js

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const _ = require("lodash");
66
const config = require("config");
77
const constants = require("../app-constants");
88
const logger = require("./logger");
9-
const { weekDiff } = require("./utils");
109
const httpStatus = require("http-status");
1110
const Interceptor = require("express-interceptor");
1211
const m2mAuth = require("tc-core-library-js").auth.m2m;
@@ -300,7 +299,7 @@ async function getJobCandidates(criteria) {
300299
* @param {*} userId
301300
* @returns
302301
*/
303-
async function handlePlacedJobCandidates(jobCandidates, userId) {
302+
async function handlePlacedJobCandidates(jobCandidates, userId, userHandle) {
304303
if (!jobCandidates || jobCandidates.length == 0 || !userId) {
305304
return;
306305
}
@@ -346,17 +345,71 @@ async function handlePlacedJobCandidates(jobCandidates, userId) {
346345
new Date(rb.endDate).toDateString() != new Date().toDateString()
347346
) {
348347
jc.status = "completed";
349-
jc.rbPay = rb.memberRate;
350348
jc.rbStartDate = rb.startDate;
351349
jc.rbEndDate = rb.endDate;
352-
jc.rbDuration = weekDiff(rb.startDate, rb.endDate);
350+
jc.rbId = rb.id;
351+
jc.userHandle = userHandle;
353352
}
354353
}
355354
}
356355
});
356+
await getWorkingPeriods(jobCandidates, userHandle, rbRes);
357357
return;
358358
}
359359

360+
/**
361+
* Get payment Total for working period
362+
*
363+
* @param {*} jobCandidates job candidates we will process
364+
* @param {*} userHandle the user's handle
365+
* @param {*} resourceBookings the resource booking belongs to this user
366+
* @returns
367+
*/
368+
async function getWorkingPeriods(jobCandidates, userHandle, resourceBookings) {
369+
if (
370+
!userHandle ||
371+
!resourceBookings ||
372+
resourceBookings.length == 0 ||
373+
!jobCandidates ||
374+
jobCandidates.length == 0
375+
) {
376+
return;
377+
}
378+
const rbIds = resourceBookings.map((item) => item.id);
379+
const token = await getM2MToken();
380+
const url = `${config.API.V5}/work-periods`;
381+
const criteria = {
382+
userHandle: userHandle,
383+
resourceBookingIds: rbIds.join(","),
384+
};
385+
const res = await request
386+
.get(url)
387+
.query(criteria)
388+
.set("Authorization", `Bearer ${token}`)
389+
.set("Accept", "application/json");
390+
localLogger.debug({
391+
context: "getWorkingPeriods",
392+
message: `response body: ${JSON.stringify(res.body)}`,
393+
});
394+
if (res.body && res.body.length == 0) {
395+
return;
396+
}
397+
// All the working periods for the rbs.
398+
const wpRes = res.body;
399+
_.each(rbIds, (rbId) => {
400+
const wps = wpRes.filter(
401+
(wp) => wp.userHandle == userHandle && wp.resourceBookingId == rbId
402+
);
403+
const paymentTotal = wps.reduce((total, wp) => total + wp.paymentTotal, 0);
404+
const jc = jobCandidates.find(
405+
(item) => item.rbId == rbId && item.userHandle == userHandle
406+
);
407+
if (jc) {
408+
jc.paymentTotal = paymentTotal;
409+
}
410+
});
411+
}
412+
360413
/**
361414
* Return jobs by given criteria
362415
* @param {string} criteria the search criteria

src/api/common/utils.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/api/services/JobApplicationService.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ async function getMyJobApplications(currentUser, criteria) {
3030
return emptyResult;
3131
}
3232
// get user id by calling taas-api with current user's token
33-
const { id: userId } = await helper.getCurrentUserDetails(
33+
const { id: userId, handle: userHandle } = await helper.getCurrentUserDetails(
3434
currentUser.jwtToken
3535
);
36-
if (!userId) {
36+
if (!userId || !userHandle) {
3737
throw new errors.NotFoundError(
38-
`Id for user: ${currentUser.userId} not found`
38+
`Id for user: ${currentUser.userId} or handle for user: ${currentUser.handle} not found`
3939
);
4040
}
4141
// get jobCandidates of current user by calling taas-api
@@ -54,7 +54,11 @@ async function getMyJobApplications(currentUser, criteria) {
5454
let jcResult = jobCandidates.result;
5555
// handle placed status for completed_jobs, archived_jobs query
5656
if (status && (status == "active_jobs" || status == "completed_jobs")) {
57-
await helper.handlePlacedJobCandidates(jobCandidates.result, userId);
57+
await helper.handlePlacedJobCandidates(
58+
jobCandidates.result,
59+
userId,
60+
userHandle
61+
);
5862
if (status == "completed_jobs") {
5963
jcResult = jobCandidates.result.filter(
6064
(item) => item.status == "completed"
@@ -76,11 +80,9 @@ async function getMyJobApplications(currentUser, criteria) {
7680
const job = _.find(jobs, ["id", jobCandidate.jobId]);
7781
return {
7882
title: job.title,
79-
description: job.description,
80-
rbPay: jobCandidate.rbPay,
83+
paymentTotal: jobCandidate.paymentTotal,
8184
rbStartDate: jobCandidate.rbStartDate,
8285
rbEndDate: jobCandidate.rbEndDate,
83-
rbDuration: jobCandidate.rbDuration,
8486
payment: {
8587
min: job.minSalary,
8688
max: job.maxSalary,

0 commit comments

Comments
 (0)