-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspNotifyExpiringTrustees.sql
More file actions
55 lines (42 loc) · 3.06 KB
/
Copy pathspNotifyExpiringTrustees.sql
File metadata and controls
55 lines (42 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
CREATE PROCEDURE [dbo].[spNotifyExpiringTrustees]
AS
BEGIN
Declare @lateUsers table (UserID int, Fullname varchar(max), Callsign varchar(max), Email varchar(max), State varchar(50), Website varchar(255), RepeaterList varchar(max))
Insert into @lateUsers (UserID, Fullname, Callsign, Email, State, Website)
Select Users.ID, Users.FullName, Users.Callsign, Users.Email, States.State, States.website From Repeaters
join Users on Repeaters.TrusteeID = Users.ID
join States on Repeaters.State = States.StateAbbreviation
where Repeaters.DateUpdated < DateAdd(month, -33, GetDate()) and Repeaters.DateUpdated > DateAdd(month, -36, GetDate())
and Repeaters.status <> 6 and Users.Email is not null
group by Users.ID, Users.FullName, Users.Callsign, Users.Email, States.State, States.Website;
Declare @userid int, @fullname varchar(max), @callsign varchar(10), @email varchar(max);
While exists (select 1 from @lateUsers where RepeaterList is null)
Begin
Select top 1 @userid = userid, @fullname = Fullname, @callsign = Callsign, @email = Email from @lateUsers where RepeaterList is null;
Declare @Enumerator table (ID int, Callsign varchar(max), OutputFrequency varchar(max), MonthsLeft int)
Insert into @Enumerator
Select Repeaters.ID, Repeaters.Callsign 'Callsign', Repeaters.OutputFrequency 'OutputFrequency',
DateDiff(month, DateUpdated, DateAdd(month, -33, GetDate())) 'MonthsLeft'
from Repeaters
join RepeaterStatuses on RepeaterStatuses.ID = Repeaters.Status
where Repeaters.DateUpdated < DateAdd(month, -33, GetDate()) and Repeaters.DateUpdated > DateAdd(month, -36, GetDate())
and Repeaters.status <> 6 and Repeaters.TrusteeID = @userid
Declare @repeaters varchar(max), @repeatercallsign varchar(10), @output varchar(max), @months int, @repeaterID int
Set @repeaters = '';
While exists (select 1 from @Enumerator)
Begin
Select top 1 @repeatercallsign = Callsign, @output = OutputFrequency, @months = MonthsLeft, @repeaterID = ID from @Enumerator
Order by MonthsLeft Desc;
Select @repeaters = CONCAT(@repeaters, '<br>', CHAR(13), CHAR(10), @repeatercallsign, ' ', @output, ' has ', @months, ' month(s) left before its coordination expires.');
Delete from @Enumerator where Callsign = @repeatercallsign and OutputFrequency = @output;
-- Add a note that we emailed them.
Insert into RepeaterChangeLogs (RepeaterID, UserID, ChangeDateTime, ChangeDescription) values (@repeaterID, 264, GetDate(), 'Notice of upcoming coordination expiration sent to trustee.');
End
Update @lateUsers set RepeaterList = @repeaters where userid = @userid;
Declare @templateData varchar(max) = (Select FullName as 'name', Callsign as 'callsign', RepeaterList as 'repeaters', State as 'state', Website as 'website' from @lateUsers where userid = @userid for json auto, WITHOUT_ARRAY_WRAPPER);
-- Create the email record
Insert into EmailQueue (ToName, ToEmail, Subject, Body, TemplateID)
values (@fullname, @email, 'ACTION REQUIRED: Repeater coordination expiring', @templateData, 'd-c68aa4d9e64045a980020d7cf858c2e0');
End
Select * from @lateUsers
END;