@@ -7,8 +7,6 @@ const { getUserById, getMemberDetailsByHandle } = require('../common/helper')
77const { getCollectorName, createCollector, createMessage, upsertContactInSurveyMonkey, addContactsToSurvey, sendSurveyAPI } = require ( '../common/surveyMonkey' )
88
99const resourceBookingCache = { }
10- const contactIdToWorkPeriodIdMap = { }
11- const emailToWorkPeriodIdMap = { }
1210
1311function buildSentSurveyError ( e ) {
1412 return {
@@ -62,15 +60,16 @@ async function sendSurveys () {
6260 const collectorId = await createCollector ( collectorName )
6361 const messageId = await createMessage ( collectorId )
6462 // create map
65- contactIdToWorkPeriodIdMap [ collectorName ] = { }
66- emailToWorkPeriodIdMap [ collectorName ] = { }
6763 collectors [ collectorName ] = {
64+ workPeriodIds : [ ] ,
6865 collectorId,
6966 messageId,
7067 contacts : [ ]
7168 }
7269 }
7370
71+ collectors [ collectorName ] . workPeriodIds . push ( workPeriod . id )
72+
7473 const resourceBooking = _ . find ( resourceBookings , ( r ) => r . id === workPeriod . resourceBookingId )
7574 const userInfo = { }
7675 if ( ! resourceBookingCache [ resourceBooking . userId ] ) {
@@ -89,7 +88,6 @@ async function sendSurveys () {
8988 resourceBookingCache [ resourceBooking . userId ] = userInfo
9089 }
9190 }
92- emailToWorkPeriodIdMap [ collectorName ] [ resourceBookingCache [ resourceBooking . userId ] . email ] = workPeriod . id
9391 collectors [ collectorName ] . contacts . push ( resourceBookingCache [ resourceBooking . userId ] )
9492 } catch ( e ) {
9593 try {
@@ -105,17 +103,13 @@ async function sendSurveys () {
105103 }
106104
107105 // add contacts
108- for ( const collectorName in collectors ) {
106+ for ( const collectorName of _ . keys ( collectors ) ) {
109107 const collector = collectors [ collectorName ]
110108 collectors [ collectorName ] . contacts = await upsertContactInSurveyMonkey ( collector . contacts )
111-
112- for ( const contact of collectors [ collectorName ] . contacts ) {
113- contactIdToWorkPeriodIdMap [ collectorName ] [ contact . id ] = emailToWorkPeriodIdMap [ collectorName ] [ contact . email ]
114- }
115109 }
116110
117111 // send surveys
118- for ( const collectorName in collectors ) {
112+ for ( const collectorName of _ . keys ( collectors ) ) {
119113 const collector = collectors [ collectorName ]
120114 if ( collector . contacts . length ) {
121115 try {
@@ -125,23 +119,23 @@ async function sendSurveys () {
125119 collector . contacts
126120 )
127121 await sendSurveyAPI ( collector . collectorId , collector . messageId )
128- for ( const contactId in contactIdToWorkPeriodIdMap [ collectorName ] ) {
122+ for ( const workPeriodId of collectors [ collectorName ] . workPeriodIds ) {
129123 try {
130- await partiallyUpdateWorkPeriod ( currentUser , contactIdToWorkPeriodIdMap [ collectorName ] [ contactId ] , { sentSurvey : true } )
124+ await partiallyUpdateWorkPeriod ( currentUser , workPeriodId , { sentSurvey : true } )
131125 } catch ( e ) {
132- logger . error ( { component : 'SurveyService' , context : 'sendSurvey' , message : `Error updating survey as sent for Work Period "${ contactIdToWorkPeriodIdMap [ collectorName ] [ contactId ] } ": ` + e . message } )
126+ logger . error ( { component : 'SurveyService' , context : 'sendSurvey' , message : `Error updating survey as sent for Work Period "${ workPeriodId } ": ` + e . message } )
133127 }
134128 }
135129 } catch ( e ) {
136- for ( const contactId in contactIdToWorkPeriodIdMap [ collectorName ] ) {
130+ for ( const workPeriodId of collectors [ collectorName ] . workPeriodIds ) {
137131 try {
138132 await partiallyUpdateWorkPeriod (
139133 currentUser ,
140- contactIdToWorkPeriodIdMap [ collectorName ] [ contactId ] ,
134+ workPeriodId ,
141135 { sentSurveyError : buildSentSurveyError ( e ) }
142136 )
143137 } catch ( e ) {
144- logger . error ( { component : 'SurveyService' , context : 'sendSurvey' , message : `Error updating survey as failed for Work Period "${ contactIdToWorkPeriodIdMap [ collectorName ] [ contactId ] } ": ` + e . message } )
138+ logger . error ( { component : 'SurveyService' , context : 'sendSurvey' , message : `Error updating survey as failed for Work Period "${ workPeriodId } ": ` + e . message } )
145139 }
146140 }
147141 }
0 commit comments