-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathEnrolleeAgreementService.cs
More file actions
259 lines (221 loc) · 9.49 KB
/
Copy pathEnrolleeAgreementService.cs
File metadata and controls
259 lines (221 loc) · 9.49 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Prime.Extensions;
using Prime.Models;
using Prime.Models.Api;
using Prime.Services.Razor;
using Prime.DTOs.AgreementEngine;
using Prime.Engines;
namespace Prime.Services
{
public class EnrolleeAgreementService : BaseService, IEnrolleeAgreementService
{
private static readonly TimeSpan AgreementRenewalPeriod = TimeSpan.FromDays(365);
private readonly IAgreementService _agreementService;
private readonly IRazorConverterService _razorConverterService;
private readonly IMapper _mapper;
public EnrolleeAgreementService(
ApiDbContext context,
ILogger<EnrolleeAgreementService> logger,
IAgreementService agreementService,
IMapper mapper,
IRazorConverterService razorConverterService)
: base(context, logger)
{
_agreementService = agreementService;
_mapper = mapper;
_razorConverterService = razorConverterService;
}
/// <summary>
/// Gets the agreement for an enrollee by ID, if it exists (No Tracking).
/// </summary>
public async Task<Agreement> GetEnrolleeAgreementAsync(int enrolleeId, int agreementId, bool includeText = false)
{
var agreement = await _context.Agreements
.AsNoTracking()
.Where(at => at.Id == agreementId)
.Where(at => at.EnrolleeId == enrolleeId)
.If(includeText, q => q.Include(at => at.AgreementVersion).Include(at => at.LimitsConditionsClause))
.SingleOrDefaultAsync();
if (includeText)
{
await RenderHtml(agreement);
}
return agreement;
}
/// <summary>
/// Get the list of agreements for an enrollee, using filters (No Tracking).
/// </summary>
public async Task<IEnumerable<Agreement>> GetEnrolleeAgreementsAsync(int enrolleeId, AgreementFilters filters)
{
filters ??= new AgreementFilters();
var agreements = await _context.Agreements
.AsNoTracking()
.Include(at => at.SignedAgreement)
.Include(at => at.AgreementVersion)
.Where(at => at.EnrolleeId == enrolleeId)
.OrderByDescending(at => at.CreatedDate)
.If(filters.OnlyLatest, q => q.Take(1))
.If(filters.Accepted == true || filters.YearAccepted.HasValue, q => q.Where(at => at.AcceptedDate.HasValue))
.If(filters.Accepted == false, q => q.Where(at => !at.AcceptedDate.HasValue))
.If(filters.IncludeText, q => q.Include(at => at.AgreementVersion).Include(at => at.LimitsConditionsClause))
.ToArrayAsync();
var maxAgreementId = agreements.Select(a => a.Id).DefaultIfEmpty(0).Max();
if (filters.YearAccepted.HasValue)
{
// NpgSQL does not support DateTimeOffset operations, this filtering must be done after fetching all the data :(
agreements = agreements
.Where(at => at.AcceptedDate.Value.Year == filters.YearAccepted)
.ToArray();
}
foreach (var agreement in agreements)
{
agreement.IsCurrent = agreement.Id == maxAgreementId;
}
if (filters.IncludeText)
{
await RenderHtml(agreements);
}
return agreements;
}
public async Task CreateEnrolleeAgreementAsync(int enrolleeId)
{
var dto = await _context.Enrollees
.AsNoTracking()
.Where(e => e.Id == enrolleeId)
.Select(e => new
{
AssignedAgreementType = e.Submissions
.OrderByDescending(s => s.CreatedDate)
.Select(s => s.AgreementType)
.FirstOrDefault(),
e.AccessAgreementNote
})
.SingleAsync();
if (dto.AssignedAgreementType == null)
{
throw new InvalidOperationException("Agreement type is required to approve an enrollee");
}
var agreement = new Agreement
{
EnrolleeId = enrolleeId,
AgreementVersionId = await _agreementService.GetLatestAgreementVersionIdOfTypeAsync(dto.AssignedAgreementType.Value),
LimitsConditionsClause = LimitsConditionsClause.FromAgreementNote(dto.AccessAgreementNote),
CreatedDate = DateTimeOffset.Now
};
_context.Add(agreement);
await _context.SaveChangesAsync();
}
/// <summary>
/// Gets the Enrollee's newest Agreement
/// </summary>
public async Task<Agreement> GetCurrentAgreementAsync(int enrolleeId)
{
return await _context.Agreements
.OrderByDescending(at => at.CreatedDate)
.FirstAsync(at => at.EnrolleeId == enrolleeId);
}
/// <summary>
/// Accepts the Enrollee's newest Agreement, if it hasn't already been accepted.
/// </summary>
public async Task AcceptCurrentEnrolleeAgreementAsync(int enrolleeId)
{
var agreement = await GetCurrentAgreementAsync(enrolleeId);
if (agreement.AcceptedDate == null)
{
agreement.AcceptedDate = DateTimeOffset.Now;
agreement.ExpiryDate = DateTimeOffset.Now.Add(AgreementRenewalPeriod);
await _context.SaveChangesAsync();
}
}
/// <summary>
/// Expires the Enrollee's most recently accepted Agreement.
/// </summary>
public async Task ExpireCurrentEnrolleeAgreementAsync(int enrolleeId)
{
var agreement = await _context.Agreements
.OrderByDescending(a => a.CreatedDate)
.Where(a => a.EnrolleeId == enrolleeId)
.FirstOrDefaultAsync(a => a.AcceptedDate.HasValue);
if (agreement != null)
{
agreement.ExpiryDate = DateTimeOffset.Now;
await _context.SaveChangesAsync();
}
}
/// <summary>
/// Renders the HTML text of the Agreement for viewing on the frontend.
/// </summary>
private async Task RenderHtml(params Agreement[] agreements)
{
foreach (var agreement in agreements)
{
if (agreement != null)
{
agreement.AgreementContent = await _razorConverterService.RenderTemplateToStringAsync(RazorTemplates.Agreements.Base, agreement);
}
}
}
public async Task<bool> IsOboToRuAgreementTypeChangeAsync(int enrolleeId)
{
var currentAgreementType = await GetCurrentAgreementTypeAsync(enrolleeId);
// Get only the information needed to determine expected agreement type
var enrollee = await _context.Enrollees
.AsNoTracking()
.Include(e => e.Certifications)
.ThenInclude(c => c.License)
.ThenInclude(l => l.LicenseDetails)
.Include(e => e.EnrolleeCareSettings)
.SingleOrDefaultAsync(e => e.Id == enrolleeId);
if (!enrollee.ProfileCompleted)
{
//return false if the profile is not even completed yet.
return false;
}
var agreementDto = _mapper.Map<AgreementEngineDto>(enrollee);
var expectedAgreementType = AgreementEngine.DetermineAgreementType(agreementDto);
return expectedAgreementType != null && currentAgreementType != null &&
currentAgreementType.Value.IsOnBehalfOfAgreement() && expectedAgreementType.Value.IsRegulatedUserAgreement();
}
public async Task<AgreementGroup?> GetCurrentAgreementGroupForAnEnrolleeAsync(int enrolleeId)
{
var currentAgreementType = await GetCurrentAgreementTypeAsync(enrolleeId);
if (currentAgreementType == null)
{
return null;
}
if (currentAgreementType.Value.IsOnBehalfOfAgreement())
{
return AgreementGroup.OnBehalfOf;
}
return AgreementGroup.RegulatedUser;
}
private async Task<AgreementType?> GetCurrentAgreementTypeAsync(int enrolleeId)
{
return await _context.Agreements
.OrderByDescending(a => a.CreatedDate)
.Where(a => a.EnrolleeId == enrolleeId)
.Where(a => a.AcceptedDate != null)
.Select(a => (AgreementType?)a.AgreementVersion.AgreementType)
.FirstOrDefaultAsync();
}
public async Task DeleteObsoleteEnrolleeAgreementAsync(int enrolleeId)
{
var agreement = await GetCurrentAgreementAsync(enrolleeId);
if (agreement.AcceptedDate == null)
{
_context.Remove(agreement);
await _context.SaveChangesAsync();
}
else
{
throw new InvalidOperationException("Current Agreement was expected to be unaccepted.");
}
}
}
}