-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathEnrolleeSubmissionService.cs
More file actions
155 lines (139 loc) · 6.17 KB
/
Copy pathEnrolleeSubmissionService.cs
File metadata and controls
155 lines (139 loc) · 6.17 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
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Prime.DTOs.AgreementEngine;
using Prime.Engines;
using Prime.Models;
namespace Prime.Services
{
public class EnrolleeSubmissionService : BaseService, IEnrolleeSubmissionService
{
private readonly IEnrolleeService _enrolleeService;
private readonly IMapper _mapper;
public EnrolleeSubmissionService(
ApiDbContext context,
ILogger<EnrolleeSubmissionService> logger,
IEnrolleeService enrolleeService,
IMapper mapper)
: base(context, logger)
{
_enrolleeService = enrolleeService;
_mapper = mapper;
}
public async Task<IEnumerable<Submission>> GetEnrolleeSubmissionsAsync(int enrolleeId)
{
return await _context.Submissions
.Where(epv => epv.EnrolleeId == enrolleeId)
.OrderByDescending(epv => epv.CreatedDate)
.ToListAsync();
}
public async Task<Submission> GetEnrolleeSubmissionAsync(int enrolleeSubmissionId)
{
return await _context.Submissions
.SingleOrDefaultAsync(epv => epv.Id == enrolleeSubmissionId);
}
/**
* Get the most recent submission before a given date.
*/
public async Task<Submission> GetEnrolleeSubmissionBeforeDateAsync(int enrolleeId, DateTimeOffset dateTime)
{
return await _context.Submissions
.Where(epv => epv.EnrolleeId == enrolleeId)
.Where(epv => epv.CreatedDate < dateTime)
.OrderByDescending(epv => epv.CreatedDate)
.FirstOrDefaultAsync();
}
public async Task<Submission> CreateEnrolleeSubmissionAsync(int enrolleeId, bool assignAgreement = true)
{
var enrollee = await _context.Enrollees
.AsNoTracking()
.Include(e => e.Addresses)
.ThenInclude(ea => ea.Address).AsSplitQuery()
.Include(e => e.Certifications)
.ThenInclude(c => c.License)
.ThenInclude(l => l.LicenseDetails).AsSplitQuery()
.Include(e => e.UnlistedCertifications)
.Include(e => e.OboSites)
.ThenInclude(s => s.PhysicalAddress).AsSplitQuery()
.Include(e => e.EnrolleeCareSettings)
.Include(e => e.EnrolleeHealthAuthorities)
.Include(e => e.EnrolleeDeviceProviders)
.Include(e => e.EnrolleeRemoteUsers)
.Include(e => e.RemoteAccessSites)
.ThenInclude(ras => ras.Site)
.ThenInclude(ras => ras.PhysicalAddress).AsSplitQuery()
.Include(r => r.RemoteAccessLocations)
.ThenInclude(rul => rul.PhysicalAddress).AsSplitQuery()
.Include(e => e.EnrolmentStatuses)
.ThenInclude(es => es.Status).AsSplitQuery()
.Include(e => e.EnrolmentStatuses)
.ThenInclude(es => es.EnrolmentStatusReasons)
.ThenInclude(esr => esr.StatusReason).AsSplitQuery()
.Include(e => e.AccessAgreementNote)
.Include(e => e.SelfDeclarations)
.Include(e => e.SelfDeclarationDocuments)
.Include(e => e.IdentificationDocuments)
.Include(e => e.Agreements).AsSplitQuery()
.FirstOrDefaultAsync(e => e.Id == enrolleeId);
var selfDeclarationQuestions = await _context.Set<SelfDeclarationType>()
.AsNoTracking()
.OrderBy(sdt => sdt.SortingNumber)
.Select(t => _context.Set<SelfDeclarationVersion>()
.Where(av => av.EffectiveDate <= enrollee.SelfDeclarationCompletedDate)
.Where(av => av.SelfDeclarationTypeCode == t.Code)
.OrderByDescending(av => av.EffectiveDate)
.First())
.ToListAsync();
// set the self declaration version Id and add unanswered items
// *** answered Yes - it should have self declaration ID set
// *** answered No - it should NOT have self declaration ID
foreach (var sd in selfDeclarationQuestions)
{
if (enrollee.SelfDeclarations == null)
{
enrollee.SelfDeclarations = new List<SelfDeclaration>();
}
var answered = enrollee.SelfDeclarations.FirstOrDefault(s => s.SelfDeclarationTypeCode == sd.SelfDeclarationTypeCode);
if (answered != null)
{
answered.SelfDeclarationVersionId = sd.Id;
}
else
{
enrollee.SelfDeclarations.Add(new SelfDeclaration()
{
SelfDeclarationVersionId = sd.Id,
SelfDeclarationTypeCode = sd.SelfDeclarationTypeCode,
});
}
}
var enrolleeSubmission = new Submission
{
EnrolleeId = enrolleeId,
ProfileSnapshot = JObject.FromObject(enrollee, JsonSerializer.Create(
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
})
),
RequestedRemoteAccess = enrollee.EnrolleeRemoteUsers.Any(),
CreatedDate = DateTimeOffset.Now
};
if (assignAgreement)
{
var agreementDto = _mapper.Map<AgreementEngineDto>(enrollee);
enrolleeSubmission.AgreementType = AgreementEngine.DetermineAgreementType(agreementDto);
}
_context.Submissions.Add(enrolleeSubmission);
await _context.SaveChangesAsync();
return enrolleeSubmission;
}
}
}