@@ -6,7 +6,6 @@ const _ = require("lodash");
66const config = require ( "config" ) ;
77const constants = require ( "../app-constants" ) ;
88const logger = require ( "./logger" ) ;
9- const { weekDiff } = require ( "./utils" ) ;
109const httpStatus = require ( "http-status" ) ;
1110const Interceptor = require ( "express-interceptor" ) ;
1211const 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
0 commit comments