-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathEnrolleeAgreementsController.cs
More file actions
346 lines (307 loc) · 15.3 KB
/
Copy pathEnrolleeAgreementsController.cs
File metadata and controls
346 lines (307 loc) · 15.3 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Prime.Configuration.Auth;
using Prime.Models;
using Prime.Services;
using Prime.Models.Api;
using Prime.ViewModels;
using Prime.Services.Razor;
using System.Linq;
using System;
namespace Prime.Controllers
{
[Produces("application/json")]
[Route("api/enrollees")]
[ApiController]
[Authorize(Roles = Roles.PrimeEnrollee + "," + Roles.ViewEnrollee)]
public class EnrolleeAgreementsController : PrimeControllerBase
{
private readonly IEnrolleeService _enrolleeService;
private readonly IEnrolleeAgreementService _enrolleeAgreementService;
private readonly IEnrolleeSubmissionService _enrolleeSubmissionService;
private readonly IRazorConverterService _razorConverterService;
private readonly IBusinessEventService _businessEventService;
private readonly IDocumentService _documentService;
private readonly IPdfService _pdfService;
public EnrolleeAgreementsController(
IEnrolleeService enrolleeService,
IEnrolleeAgreementService enrolleeAgreementService,
IEnrolleeSubmissionService enrolleeSubmissionService,
IRazorConverterService razorConverterService,
IBusinessEventService businessEventService,
IDocumentService documentService,
IPdfService pdfService)
{
_enrolleeService = enrolleeService;
_enrolleeAgreementService = enrolleeAgreementService;
_enrolleeSubmissionService = enrolleeSubmissionService;
_razorConverterService = razorConverterService;
_businessEventService = businessEventService;
_documentService = documentService;
_pdfService = pdfService;
}
// GET: api/enrollees/5/agreements
/// <summary>
/// Get a list of the enrollee's agreements.
/// </summary>
/// <param name="enrolleeId"></param>
/// <param name="filters"></param>
[HttpGet("{enrolleeId}/agreements", Name = nameof(GetEnrolleeAgreements))]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(ApiMessageResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResultResponse<IEnumerable<Agreement>>), StatusCodes.Status200OK)]
public async Task<ActionResult> GetEnrolleeAgreements(int enrolleeId, [FromQuery] AgreementFilters filters)
{
var record = await _enrolleeService.GetPermissionsRecordAsync(enrolleeId);
if (record == null)
{
return NotFound($"Enrollee not found with id {enrolleeId}");
}
if (!record.AccessableBy(User))
{
return Forbid();
}
var agreements = await _enrolleeAgreementService.GetEnrolleeAgreementsAsync(enrolleeId, filters);
if (User.IsAdministrant())
{
await _businessEventService.CreateAdminViewEventAsync(enrolleeId, "Admin viewing PRIME History");
}
return Ok(agreements);
}
// GET: api/enrollees/5/agreement
/// <summary>
/// Get the enrollee's current accepted agreement in PDF.
/// </summary>
/// <param name="enrolleeId"></param>
[HttpGet("{enrolleeId}/agreement", Name = nameof(GetEnrolleeAcceptedAgreement))]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(ApiMessageResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResultResponse<IEnumerable<Agreement>>), StatusCodes.Status200OK)]
public async Task<ActionResult> GetEnrolleeAcceptedAgreement(int enrolleeId)
{
var record = await _enrolleeService.GetPermissionsRecordAsync(enrolleeId);
if (record == null)
{
return NotFound($"Enrollee not found with id {enrolleeId}");
}
if (!record.AccessableBy(User))
{
return Forbid();
}
Agreement agreement = await _enrolleeAgreementService.GetCurrentAgreementAsync(enrolleeId);
if (agreement == null)
{
return NotFound($"Agreement not found on enrollee with id {enrolleeId}");
}
var token = await _documentService.GetDownloadTokenForSignedAgreementDocument(agreement.Id);
return Ok(token);
}
// GET: api/enrollees/5/cards
/// <summary>
/// Get a list of the enrollee's enrolment card view models.
/// </summary>
/// <param name="enrolleeId"></param>
/// <param name="filters"></param>
[HttpGet("{enrolleeId}/cards", Name = nameof(GetEnrolleeEnrolmentCards))]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(ApiMessageResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResultResponse<IEnumerable<EnrolmentCardViewModel>>), StatusCodes.Status200OK)]
public async Task<ActionResult> GetEnrolleeEnrolmentCards(int enrolleeId, [FromQuery] AgreementFilters filters)
{
var record = await _enrolleeService.GetPermissionsRecordAsync(enrolleeId);
if (record == null)
{
return NotFound($"Enrollee not found with id {enrolleeId}");
}
if (!record.AccessableBy(User))
{
return Forbid();
}
var enrolmentCards = new List<EnrolmentCardViewModel>();
var submissions = await _enrolleeSubmissionService.GetEnrolleeSubmissionsAsync(enrolleeId);
var agreements = await _enrolleeAgreementService.GetEnrolleeAgreementsAsync(enrolleeId, filters);
var lastSubmissionDate = null as DateTimeOffset?;
var currentSubmission = submissions.First();
foreach (var submission in submissions)
{
//filter the submission that within the year selected
if (submission.CreatedDate.Year == filters.YearAccepted || filters.YearAccepted == null)
{
//find the agreement that created after the submission and before the next submission (if exists)
var agreement = agreements.Where(a => submission.CreatedDate <= a.CreatedDate &&
(lastSubmissionDate == null || a.CreatedDate < lastSubmissionDate)).FirstOrDefault();
var card = new EnrolmentCardViewModel
{
AgreementId = agreement != null ? agreement.Id : 0,
AgreementType = agreement != null ? agreement.AgreementVersion.AccessType : null,
AgreementAcceptedDate = agreement != null ? agreement.AcceptedDate : null,
EnrolmentApprovedDate = agreement != null ? agreement.CreatedDate : null,
Submission = submission,
SubmissionId = submission.Id,
IsCurrent = currentSubmission.Id == submission.Id
};
enrolmentCards.Add(card);
lastSubmissionDate = submission.CreatedDate;
}
}
if (User.IsAdministrant())
{
await _businessEventService.CreateAdminViewEventAsync(enrolleeId, "Admin viewing PRIME History");
}
return Ok(enrolmentCards);
}
// GET: api/enrollees/5/agreements/2
/// <summary>
/// Get a specific agreement for an enrollee.
/// </summary>
/// <param name="enrolleeId"></param>
/// <param name="agreementId"></param>
[HttpGet("{enrolleeId}/agreements/{agreementId}", Name = nameof(GetAgreement))]
[ProducesResponseType(typeof(ApiMessageResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(ApiMessageResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResultResponse<Agreement>), StatusCodes.Status200OK)]
public async Task<ActionResult> GetAgreement(int enrolleeId, int agreementId)
{
var record = await _enrolleeService.GetPermissionsRecordAsync(enrolleeId);
if (record == null)
{
return NotFound($"Enrollee not found with id {enrolleeId}");
}
if (!record.AccessableBy(User))
{
return Forbid();
}
var agreement = await _enrolleeAgreementService.GetEnrolleeAgreementAsync(enrolleeId, agreementId, true);
if (agreement == null)
{
return NotFound($"Agreement not found with id {agreementId} on enrollee with id {enrolleeId}");
}
if (User.IsAdministrant())
{
await _businessEventService.CreateAdminViewEventAsync(enrolleeId, "Admin viewing Agreement");
}
return Ok(agreement);
}
// GET: api/enrollees/5/agreements/3/submission
/// <summary>
/// Get the submission for a given agreement.
/// </summary>
/// <param name="enrolleeId"></param>
/// <param name="agreementId"></param>
[HttpGet("{enrolleeId}/agreements/{submissionId}/submission", Name = nameof(GetSubmissionForAgreement))]
[ProducesResponseType(typeof(ApiMessageResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(ApiMessageResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResultResponse<Submission>), StatusCodes.Status200OK)]
public async Task<ActionResult> GetSubmissionForAgreement(int enrolleeId, int submissionId)
{
var record = await _enrolleeService.GetPermissionsRecordAsync(enrolleeId);
if (record == null)
{
return NotFound($"Enrollee not found with id {enrolleeId}");
}
if (!record.AccessableBy(User))
{
return Forbid();
}
var enrolleeSubmission = await _enrolleeSubmissionService.GetEnrolleeSubmissionAsync(submissionId);
if (enrolleeSubmission == null)
{
return NotFound($"No enrolment submissions were found for Submission with id {submissionId} for enrollee with id {enrolleeId}.");
}
if (User.IsAdministrant())
{
await _businessEventService.CreateAdminViewEventAsync(enrolleeId, "Admin viewing Enrolment in PRIME History");
}
return Ok(enrolleeSubmission);
}
// GET: api/enrollees/5/agreements/2/signable
/// <summary>
/// Downloads a specific unsigned access term for an enrollee.
/// </summary>
/// <param name="enrolleeId"></param>
/// <param name="agreementId"></param>
[HttpGet("{enrolleeId}/agreements/{agreementId}/signable", Name = nameof(GetAccessTermSignable))]
[ProducesResponseType(typeof(ApiMessageResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(ApiMessageResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResultResponse<byte[]>), StatusCodes.Status200OK)]
public async Task<ActionResult> GetAccessTermSignable(int enrolleeId, int agreementId)
{
var record = await _enrolleeService.GetPermissionsRecordAsync(enrolleeId);
if (record == null)
{
return NotFound($"Enrollee not found with id {enrolleeId}");
}
if (!record.AccessableBy(User))
{
return Forbid();
}
Agreement agreement = await _enrolleeAgreementService.GetEnrolleeAgreementAsync(enrolleeId, agreementId, true);
if (agreement == null)
{
return NotFound($"Agreement not found with id {agreementId} on enrollee with id {enrolleeId}");
}
var html = await _razorConverterService.RenderTemplateToStringAsync(RazorTemplates.Agreements.Pdf, agreement);
var download = _pdfService.Generate(html);
return Ok(download);
}
// GET: api/enrollees/5/agreements/current/obo-to-ru
/// <summary>
/// Gets boolean re: whether enrollee's current agreement type is OBO and the agreement type
/// they would be assigned is RU, if automatic assignment occurred today
/// </summary>
/// <param name="enrolleeId"></param>
[HttpGet("{enrolleeId}/agreements/current/obo-to-ru", Name = nameof(IsOboToRuAgreementTypeChange))]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(ApiMessageResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResultResponse<bool>), StatusCodes.Status200OK)]
public async Task<ActionResult> IsOboToRuAgreementTypeChange(int enrolleeId)
{
var record = await _enrolleeService.GetPermissionsRecordAsync(enrolleeId);
if (record == null)
{
return NotFound($"Enrollee not found with id {enrolleeId}");
}
if (!record.AccessableBy(User))
{
return Forbid();
}
var isOboToRuChange = await _enrolleeAgreementService.IsOboToRuAgreementTypeChangeAsync(enrolleeId);
return Ok(isOboToRuChange);
}
// GET: api/enrollees/5/agreements/current/agreement-group
/// <summary>
/// Gets the agreement group for enrollees current agreement, null if no current agreement
/// </summary>
/// <param name="enrolleeId"></param>
[HttpGet("{enrolleeId}/agreements/current/agreement-group", Name = nameof(GetCurrentAgreementGroupForAnEnrollee))]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(ApiMessageResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResultResponse<AgreementGroup?>), StatusCodes.Status200OK)]
public async Task<ActionResult> GetCurrentAgreementGroupForAnEnrollee(int enrolleeId)
{
var record = await _enrolleeService.GetPermissionsRecordAsync(enrolleeId);
if (record == null)
{
return NotFound($"Enrollee not found with id {enrolleeId}");
}
if (!record.AccessableBy(User))
{
return Forbid();
}
var agreementGroup = await _enrolleeAgreementService.GetCurrentAgreementGroupForAnEnrolleeAsync(enrolleeId);
return Ok(agreementGroup);
}
}
}