--- /dev/null
+DO $FUNC$
+ DECLARE
+ match_data ums.duplicates_matched_with_lead%ROWTYPE;
+ BEGIN
+ FOR match_data IN SELECT udmwl.*
+ FROM ums.duplicates_excluding_outreach udmwl
+ LOOP
+ IF (match_data.usr = match_data.lead_usr) THEN
+ RAISE NOTICE 'Adding alert to lead user %', match_data.usr;
+ UPDATE actor.usr
+ SET alert_message = array_to_string(
+ array[
+ 'CANDIDATE FOR MERGE: This account is identified as the lead account for the following library cards: ',
+ (array_to_string(
+ array[
+ (select string_agg(barcode, ', ')
+ from actor.card cd
+ where cd.id in (
+ select card
+ from actor.usr u
+ where u.id in (
+ select usr
+ from ums.duplicates_excluding_outreach
+ where usr <> lead_usr
+ and grouping_id = match_data.grouping_id
+ )
+ )
+ )
+ ], ', ')
+ ), alert_message
+ ], ' '
+ )
+ WHERE id = match_data.lead_usr;
+ ELSE
+ RAISE NOTICE 'Adding alert to duplicate user %', match_data.usr;
+ UPDATE actor.usr
+ SET alert_message = array_to_string(
+ array[
+ 'CANDIDATE FOR MERGE: This account is identified as a duplicate to account ', (
+ select barcode
+ from actor.card cd
+ where cd.id in (
+ select card
+ from actor.usr u
+ where u.id = match_data.lead_usr
+ )
+ ),
+ alert_message
+ ], ' '
+ )
+ WHERE id = match_data.usr;
+ END IF;
+ END LOOP;
+ END;
+$FUNC$;