@@ -196,40 +196,83 @@ proc(request: Request) =
196196 if not userUUID.isValidUUID ():
197197 resp Http200 , nimfOptinUnubscribe (" " , " " , " " )
198198
199+ let unsubscribeFromAll = @ " unsubscribeFromAll" == " true"
200+
201+
202+ # For streamlining the code
203+ var listID: string
204+ var listIdentifier: string
205+ var listUUID: string
206+
207+ # Used in callback event
208+ var listUUIDs: seq [string ]
209+ var listIdentifiers: seq [string ]
210+
211+ var onlyOneList: bool = false
199212 var userData: seq [string ]
213+
200214 pg.withConnection conn:
201- exec (conn, sqlUpdate (
202- table = " contacts" ,
203- data = [
204- " double_opt_in" ,
205- " double_opt_in_data" ,
206- ],
207- where = [" uuid = ?" ]
208- ), " false" , " " , userUUID)
209215
216+ # First get user info
210217 userData = getRow (conn, sqlSelect (table = " contacts" , select = [" id" , " name" , " email" ], where = [" uuid = ?" ]), userUUID)
211218
219+ if not unsubscribeFromAll:
220+ # Now get listID from latest email from pending_emails
221+ let lastEmailListID = getValue (conn, sqlSelect (table = " pending_emails" , select = [" list_id" ], where = [" user_id = ?" ], customSQL = " ORDER BY created_at DESC LIMIT 1" ), userData[0 ])
222+ if lastEmailListID != " " :
223+ listID = lastEmailListID
224+ let listData = getRow (conn, sqlSelect (table = " lists" , select = [" id" , " identifier" , " uuid" ], where = [" id = ?" ]), listID)
225+ listUUID = listData[2 ]
226+ listIdentifier = listData[1 ]
227+
228+ else :
229+ exec (conn, sqlUpdate (
230+ table = " contacts" ,
231+ data = [
232+ " double_opt_in" ,
233+ " double_opt_in_data" ,
234+ ],
235+ where = [" uuid = ?" ]
236+ ), " false" , " " , userUUID)
237+
238+
212239 #
213240 # Remove from lists
214241 #
215- let listIds = getAllRows (conn, sqlSelect (table = " subscriptions" , select = [" list_id" ], where = [" user_id = ?" ]), userData[0 ])
216- for listId in listIds:
217- discard contactRemoveFromList (userData[0 ], listId[0 ], " list" )
242+ if not unsubscribeFromAll:
243+ listUUIDs.add (listUUID)
244+ listIdentifiers.add (listIdentifier)
245+ discard contactRemoveFromList (userData[0 ], listID, " list" )
246+ else :
247+ let listIds = getAllRows (conn, sqlSelect (table = " subscriptions" , select = [" list_id" ], where = [" user_id = ?" ]), userData[0 ])
248+ var tmpListIDs: seq [string ]
249+ for listId in listIds:
250+ tmpListIDs.add (listId[0 ])
251+ discard contactRemoveFromList (userData[0 ], listId[0 ], " list" )
252+
253+ # Get data to event hook
254+ if tmpListIDs.len () > 0 :
255+ let listData = getAllRows (conn, sqlSelect (table = " lists" , select = [" uuid" , " identifier" ], where = [" id = ANY(?::int[])" ]), " {" & tmpListIDs.join (" ," ) & " }" )
256+ for listId in listData:
257+ listUUIDs.add (listId[0 ])
258+ listIdentifiers.add (listId[1 ])
218259
219260 if userData[0 ] == " " :
220- resp Http200 , nimfOptinUnubscribe (" " , " " , " " )
261+ resp Http200 , nimfOptinUnubscribe (" " , " " , " " , unsubscribeFromAll )
221262
222263 let data = %* {
223264 " success" : true ,
224265 " id" : userData[0 ],
225266 " name" : userData[1 ],
226267 " email" : userData[2 ],
268+ " listUUIDs" : listUUIDs,
269+ " listIdentifiers" : listIdentifiers,
227270 " event" : " contact_optedin"
228271 }
229272
230273 parseWebhookEvent (contact_optedin, data)
231274
232- resp Http200 , nimfOptinUnubscribe (userUUID, userData[1 ], userData[2 ])
275+ resp Http200 , nimfOptinUnubscribe (userUUID, userData[1 ], userData[2 ], unsubscribeFromAll )
233276)
234277
235278
0 commit comments