@@ -53,9 +53,52 @@ ${config.URL.BASE}${config.GIGS_PAGES_PATH}/${props.id}`,
5353 * Send gig referral invite
5454 */
5555 async onSendClick ( email ) {
56- const { profile, growSurf } = this . props ;
56+ const {
57+ profile, growSurf, tokenV3,
58+ } = this . props ;
5759 const { formData } = this . state ;
58- // email the invite
60+ // should not be able to send emails to themselves
61+ if ( profile . email === email ) {
62+ this . setState ( {
63+ isReferrError : {
64+ message : 'You are not allowed to send to yourself.' ,
65+ userError : true ,
66+ } ,
67+ } ) ;
68+ // exit no email sending
69+ return ;
70+ }
71+ // process sent log
72+ let { emailInvitesLog, emailInvitesStatus } = growSurf . data . metadata ;
73+ if ( ! emailInvitesLog ) emailInvitesLog = '' ;
74+ // check if email is in sent log alredy?
75+ const foundInLog = emailInvitesLog . indexOf ( email ) ;
76+ if ( foundInLog !== - 1 ) {
77+ this . setState ( {
78+ isReferrError : {
79+ message : `${ email } was already invited.` ,
80+ userError : true ,
81+ } ,
82+ } ) ;
83+ // exit no email sending
84+ return ;
85+ }
86+ // check if email is already referred?
87+ const growCheck = await fetch ( `${ PROXY_ENDPOINT } /growsurf/participant/${ email } ` ) ;
88+ if ( growCheck . status === 200 ) {
89+ const growCheckData = await growCheck . json ( ) ;
90+ if ( growCheckData . referrer ) {
91+ this . setState ( {
92+ isReferrError : {
93+ message : `${ email } has already been referred.` ,
94+ userError : true ,
95+ } ,
96+ } ) ;
97+ // exit no email sending
98+ return ;
99+ }
100+ }
101+ // // email the invite
59102 const res = await fetch ( `${ PROXY_ENDPOINT } /mailchimp/email` , {
60103 method : 'POST' ,
61104 body : JSON . stringify ( {
@@ -79,21 +122,66 @@ ${config.URL.BASE}${config.GIGS_PAGES_PATH}/${props.id}`,
79122 this . setState ( {
80123 isReferrError : await res . json ( ) ,
81124 } ) ;
82- } else {
125+ // exit no email tracking due to the error
126+ return ;
127+ }
128+ // parse the log to array of emails
129+ if ( emailInvitesLog . length ) {
130+ emailInvitesLog = emailInvitesLog . split ( ',' ) ;
131+ } else emailInvitesLog = [ ] ;
132+ // prepare growSurf update payload
133+ // we keep only 10 emails in the log to justify program rules
134+ if ( emailInvitesLog . length < 10 ) {
135+ emailInvitesLog . push ( email ) ;
136+ }
137+ // Auto change status when 10 emails sent
138+ if ( emailInvitesLog . length === 10 && emailInvitesStatus !== 'Paid' && emailInvitesStatus !== 'Payment Pending' ) {
139+ emailInvitesStatus = 'Payment Pending' ;
140+ }
141+ // put the tracking update in growsurf
142+ const updateRed = await fetch ( `${ PROXY_ENDPOINT } /growsurf/participant/${ growSurf . data . id } ` , {
143+ method : 'PATCH' ,
144+ headers : {
145+ 'Content-Type' : 'application/json' ,
146+ Authorization : `Bearer ${ tokenV3 } ` ,
147+ } ,
148+ body : JSON . stringify ( {
149+ ...growSurf . data ,
150+ metadata : {
151+ ...growSurf . data . metadata ,
152+ emailInvitesSent : Number ( growSurf . data . metadata . emailInvitesSent || 0 ) + 1 ,
153+ emailInvitesLog : emailInvitesLog . join ( ) ,
154+ emailInvitesStatus,
155+ } ,
156+ } ) ,
157+ } ) ;
158+ if ( updateRed . status >= 300 ) {
83159 this . setState ( {
84- isReferrSucess : true ,
160+ isReferrError : await updateRed . json ( ) ,
85161 } ) ;
162+ // exit no email tracking due to the error
163+ // just notify the user about it
164+ return ;
86165 }
166+ // finally do:
167+ this . setState ( {
168+ isReferrSucess : true ,
169+ } ) ;
87170 }
88171
89172 /**
90173 * Reset the form when referral done
91174 */
92- onReferralDone ( ) {
175+ onReferralDone ( refresh = false ) {
176+ if ( refresh ) {
177+ window . location . reload ( false ) ;
178+ return ;
179+ }
93180 const { formData } = this . state ;
94181 delete formData . email ;
95182 this . setState ( {
96183 isReferrSucess : false ,
184+ isReferrError : false ,
97185 formData,
98186 } ) ;
99187 }
@@ -143,6 +231,7 @@ RecruitCRMJobDetailsContainer.defaultProps = {
143231 application : null ,
144232 profile : { } ,
145233 growSurf : { } ,
234+ tokenV3 : null ,
146235} ;
147236
148237RecruitCRMJobDetailsContainer . propTypes = {
@@ -154,6 +243,7 @@ RecruitCRMJobDetailsContainer.propTypes = {
154243 application : PT . shape ( ) ,
155244 profile : PT . shape ( ) ,
156245 growSurf : PT . shape ( ) ,
246+ tokenV3 : PT . string ,
157247} ;
158248
159249function mapStateToProps ( state , ownProps ) {
@@ -166,6 +256,7 @@ function mapStateToProps(state, ownProps) {
166256 application : data ? data . application : null ,
167257 profile,
168258 growSurf,
259+ tokenV3 : state . auth ? state . auth . tokenV3 : null ,
169260 } ;
170261}
171262
0 commit comments