@@ -126,10 +126,57 @@ export const googleCallback = async (
126126
127127 let isNewUser = isNil ( mtUser . encUserId ) ;
128128
129+ if ( ! isNewUser ) {
130+ // catch if pending user data was not added to existing VMT user
131+ // Matches for VMT user with same email and 'pending' accountType
132+ // env toggle to enable feature
133+ if (
134+ process . env . VMT_YES_TO_CONVT_PENDING &&
135+ process . env . VMT_YES_TO_CONVT_PENDING . toLowerCase ( ) === 'yes'
136+ ) {
137+ // find VMT user with data
138+ const vmtPendingData = await VmtUser . findOne ( {
139+ email : mtUser . email ,
140+ accountType : 'pending' ,
141+ } ) ;
142+ // find duplicate VMT user to remove
143+ const vmtMTdata = await VmtUser . findOne ( {
144+ email : mtUser . email ,
145+ ssoId : mtUser . _id ,
146+ } ) ;
147+ if (
148+ vmtPendingData &&
149+ vmtPendingData . accountType === 'pending' &&
150+ vmtMTdata
151+ ) {
152+ // Swap linked Ids to link sso account with VMT account with data
153+ await VmtUser . findByIdAndUpdate ( vmtPendingData . _id , {
154+ ssoId : mtUser . _id ,
155+ accountType : 'facilitator' ,
156+ isEmailConfirmed : true ,
157+ } ) ;
158+ mtUser . vmtUserId = vmtPendingData . _id ;
159+ await mtUser . save ( ) ;
160+
161+ // invalidate or delete duplicate record
162+ await VmtUser . findByIdAndUpdate ( vmtMTdata . _id , {
163+ isTrashed : true ,
164+ accountType : 'temp' ,
165+ email : '' ,
166+ } ) ;
167+ // await VmtUser.findByIdAndUpdate(vmtMTdata._id, { ssoId: '' });
168+ // await VmtUser.findByIdAndDelete(vmtMTdata._id);
169+ }
170+ }
171+ }
172+
129173 if ( isNewUser ) {
174+ // check to see if the email is pre-loaded to VMT with user-data
175+ const vmtUserData = await VmtUser . findOne ( { email : mtUser . email } ) ;
176+
130177 [ encUser , vmtUser ] = await Promise . all ( [
131178 createEncUser ( mtUser , { } , 'google' ) ,
132- createVmtUser ( mtUser , { } , 'google' ) ,
179+ createVmtUser ( mtUser , vmtUserData || { } , 'google' ) ,
133180 ] ) ;
134181
135182 if ( encUser === null || vmtUser === null ) {
0 commit comments