Repository navigation
Expand file tree
/
Copy pathLunchNotifier.cs
More file actions
119 lines (103 loc) · 4.18 KB
/
Copy pathLunchNotifier.cs
File metadata and controls
119 lines (103 loc) · 4.18 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using CodeBeingTested.Interfaces;
namespace CodeBeingTested
{
public class LunchNotifier
{
public enum NotificationType
{
Email,
Slack
}
private readonly INotificationService _notificationService;
private readonly IEmployeeService _employeeService;
private readonly ILogger _logger;
public LunchNotifier(INotificationService notifySrv, IEmployeeService employeeSrv, ILogger logger)
{
_notificationService = notifySrv;
_employeeService = employeeSrv;
_logger = logger;
}
/// <summary>
/// Determines which employees are currently working in the New York office and
/// sends notifications to their preferred notification platform. If lunch is
/// late (after 1pm), a "Late Lunch" notification template is used instead of
/// the typical template.
/// </summary>
public void SendLunchtimeNotifications()
{
var now = DateTime.Now;
var templateToUse = now.Hour > 12 ? LateLunchTemplate : RegularLunchTemplate;
_logger.Write($"Using template: {templateToUse}");
var nycEmployees = _employeeService.GetEmployeesInNewYorkOffice();
foreach (var employee in nycEmployees)
{
if (!employee.IsWorkingOnDate(now.Date))
{
// no need to notify employees that are out sick
// or on vacation today.
_logger.Debug("Skipping employe {employee}");
continue;
}
try
{
var notificationType = employee.GetNotificationPreference();
switch (notificationType)
{
case NotificationType.Email:
_notificationService.SendEmail(employee, templateToUse);
break;
case NotificationType.Slack:
_notificationService.SendSlackMessage(employee, templateToUse);
break;
}
}
catch (Exception ex)
{
_logger.Error(ex);
}
}
}
public virtual DateTime GetDateTime() => DateTime.Now;
/// <summary>
/// This method is identical to SendLunchtimeNotification, except that
/// it extracts the use of System.DateTime.Now into a seperate, public and virtual
/// method, so that constrained frameworks (Moq, RhinoMocks, NSubstitute) can mock
/// the "current" time.
/// </summary>
public void SendLunchtimeNotifications_DateTimeSeam()
{
var now = GetDateTime();
var templateToUse = now.Hour > 12 ? LateLunchTemplate : RegularLunchTemplate;
_logger.Write($"Using template: {templateToUse}");
var nycEmployees = _employeeService.GetEmployeesInNewYorkOffice();
foreach (var employee in nycEmployees)
{
if (!employee.IsWorkingOnDate(now.Date))
{
_logger.Debug("Skipping employe {employee}");
continue;
}
try
{
var notificationType = employee.GetNotificationPreference();
switch (notificationType)
{
case NotificationType.Email:
_notificationService.SendEmail(employee, templateToUse);
break;
case NotificationType.Slack:
_notificationService.SendSlackMessage(employee, templateToUse);
break;
}
}
catch (Exception ex)
{
_logger.Error(ex);
}
}
}
public const string RegularLunchTemplate = "It's Lunchtime, come eat!";
public const string LateLunchTemplate = "It's Lunchtime -- Sorry it's late!";
}
}