@@ -299,7 +299,7 @@ async function getJobCandidates(criteria) {
299299 * @param {* } userId
300300 * @returns
301301 */
302- async function handlePlacedJobCandidates ( jobCandidates , userId ) {
302+ async function handlePlacedJobCandidates ( jobCandidates , userId , userHandle ) {
303303 if ( ! jobCandidates || jobCandidates . length == 0 || ! userId ) {
304304 return ;
305305 }
@@ -345,13 +345,71 @@ async function handlePlacedJobCandidates(jobCandidates, userId) {
345345 new Date ( rb . endDate ) . toDateString ( ) != new Date ( ) . toDateString ( )
346346 ) {
347347 jc . status = "completed" ;
348+ jc . rbStartDate = rb . startDate ;
349+ jc . rbEndDate = rb . endDate ;
350+ jc . rbId = rb . id ;
351+ jc . userHandle = userHandle ;
348352 }
349353 }
350354 }
351355 } ) ;
356+ await getWorkingPeriods ( jobCandidates , userHandle , rbRes ) ;
352357 return ;
353358}
354359
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+
355413/**
356414 * Return jobs by given criteria
357415 * @param {string } criteria the search criteria
0 commit comments