-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSubmissionAppService.cs
More file actions
217 lines (180 loc) · 9.32 KB
/
SubmissionAppService.cs
File metadata and controls
217 lines (180 loc) · 9.32 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
using Microsoft.AspNetCore.Authorization;
using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Unity.GrantManager.Applications;
using Unity.GrantManager.Attachments;
using Unity.GrantManager.GrantApplications;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Security.Encryption;
using Volo.Abp.TenantManagement;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Unity.GrantManager.Intakes;
[Authorize]
public class SubmissionAppService(
IApplicationFormSubmissionRepository applicationFormSubmissionRepository,
IRepository<ApplicationForm, Guid> applicationFormRepository,
RestClient restClient,
IStringEncryptionService stringEncryptionService,
IApplicationRepository applicationRepository,
ITenantRepository tenantRepository
) : GrantManagerAppService, ISubmissionAppService
{
protected ILogger logger => LazyServiceProvider.LazyGetService<ILogger>(provider => LoggerFactory?.CreateLogger(GetType().FullName!) ?? NullLogger.Instance);
public async Task<object?> GetSubmission(Guid? formSubmissionId)
{
if (formSubmissionId == null)
{
throw new ApiException(400, "Missing required parameter 'formId' when calling GetSubmission");
}
ApplicationForm? applicationForm = await GetApplicationFormBySubmissionId(formSubmissionId);
if (applicationForm == null)
{
throw new ApiException(400, "Missing Form configuration");
}
if (applicationForm.ChefsApplicationFormGuid == null)
{
throw new ApiException(400, "Missing CHEFS form Id");
}
if (applicationForm.ApiKey == null)
{
throw new ApiException(400, "Missing CHEFS Api Key");
}
ApplicationFormSubmission? applicationFormSubmisssion = await GetApplicationFormSubmissionBySubmissionId(formSubmissionId);
if (applicationFormSubmisssion == null)
{
throw new ApiException(400, "Missing Form Submission");
}
return applicationFormSubmisssion.Submission;
}
public async Task<BlobDto> GetChefsFileAttachment(Guid? formSubmissionId, Guid? chefsFileAttachmentId, string name)
{
if (formSubmissionId == null)
{
throw new ApiException(400, "Missing required parameter 'formId' when calling GetSubmission");
}
if (chefsFileAttachmentId == null)
{
throw new ApiException(400, "Missing required parameter 'chefsFileAttachmentId' when calling GetFileAttachment");
}
ApplicationForm? applicationForm = await GetApplicationFormBySubmissionId(formSubmissionId);
if (applicationForm == null)
{
throw new ApiException(400, "Missing Form configuration");
}
if (applicationForm.ChefsApplicationFormGuid == null)
{
throw new ApiException(400, "Missing CHEFS form Id");
}
if (applicationForm.ApiKey == null)
{
throw new ApiException(400, "Missing CHEFS Api Key");
}
var request = new RestRequest($"/files/{chefsFileAttachmentId}", Method.Get)
{
Authenticator = new HttpBasicAuthenticator(applicationForm.ChefsApplicationFormGuid!, stringEncryptionService.Decrypt(applicationForm.ApiKey!) ?? string.Empty)
};
var response = await restClient.GetAsync(request);
if (((int)response.StatusCode) != 200)
{
throw new ApiException((int)response.StatusCode, "Error calling GetChefsFileAttachment: " + response.Content, response.ErrorMessage ?? $"{response.StatusCode}");
}
return new BlobDto { Name = name, Content = response.RawBytes ?? Array.Empty<byte>(), ContentType = response.ContentType ?? "application/octet-stream" };
}
public async Task<ApplicationForm?> GetApplicationFormBySubmissionId(Guid? formSubmissionId)
{
ApplicationForm? applicationFormData = new();
if (formSubmissionId != null)
{
var query = from applicationSubmission in await applicationFormSubmissionRepository.GetQueryableAsync()
join applicationForm in await applicationFormRepository.GetQueryableAsync() on applicationSubmission.ApplicationFormId equals applicationForm.Id
where applicationSubmission.ChefsSubmissionGuid == formSubmissionId.ToString()
select applicationForm;
applicationFormData = await AsyncExecuter.FirstOrDefaultAsync(query);
}
return applicationFormData;
}
public async Task<ApplicationFormSubmission?> GetApplicationFormSubmissionBySubmissionId(Guid? formSubmissionId)
{
ApplicationFormSubmission? applicationFormSubmissionData = new();
if (formSubmissionId != null)
{
var query = from applicationFormSubmission in await applicationFormSubmissionRepository.GetQueryableAsync()
where applicationFormSubmission.ChefsSubmissionGuid == formSubmissionId.ToString()
select applicationFormSubmission;
applicationFormSubmissionData = await AsyncExecuter.FirstOrDefaultAsync(query);
}
return applicationFormSubmissionData;
}
public async Task<PagedResultDto<FormSubmissionSummaryDto>> GetSubmissionsList(Guid? formId)
{
List<FormSubmissionSummaryDto> chefsSubmissions = new List<FormSubmissionSummaryDto>();
var tenants = await tenantRepository.GetListAsync();
foreach (var tenant in tenants)
{
using (CurrentTenant.Change(tenant.Id))
{
var groupedResult = await applicationRepository.WithFullDetailsGroupedAsync(0, int.MaxValue);
var appDtos = new List<GrantApplicationDto>();
var rowCounter = 0;
List<string> checkedForms = new List<string>();
foreach (var grouping in groupedResult)
{
var appDto = ObjectMapper.Map<Application, GrantApplicationDto>(grouping.First());
appDto.RowCount = rowCounter;
appDtos.Add(appDto);
rowCounter++;
// Chef's API call to get submissions
if (!checkedForms.Contains(appDto.ApplicationForm.ChefsApplicationFormGuid ?? string.Empty))
{
var id = appDto.ApplicationForm.ChefsApplicationFormGuid;
var apiKey = stringEncryptionService.Decrypt(appDto.ApplicationForm.ApiKey! ?? string.Empty);
var request = new RestRequest($"/forms/{id}/submissions", Method.Get)
.AddParameter("fields", "applicantAgent.name");
request.Authenticator = new HttpBasicAuthenticator(id ?? "ID", apiKey ?? "no api key given");
RestResponse? response = null;
try
{
response = await restClient.GetAsync(request);
var submissionOptions = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNameCaseInsensitive = true,
ReadCommentHandling = JsonCommentHandling.Skip,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
var submissions = JsonSerializer.Deserialize<List<FormSubmissionSummaryDto>>(response.Content ?? string.Empty, submissionOptions);
if (submissions != null)
{
foreach (var submission in submissions)
{
submission.tenant = tenant.Name;
submission.form = appDto.ApplicationForm.ApplicationFormName ?? "";
}
chefsSubmissions.AddRange(submissions);
}
}
catch (Exception ex)
{
var ExceptionMessage = ex.Message;
logger.LogError(ex, "GetSubmissionsList Exception: {ExceptionMessage}", ExceptionMessage);
}
checkedForms.Add(id ?? string.Empty);
}
}
// Remove chef's submissions if Unity has an application with the same reference number
chefsSubmissions.RemoveAll(r => appDtos.Any(appDto => r.ConfirmationId.ToString() == appDto.ReferenceNo));
}
}
// Remove all deleted submissions
chefsSubmissions.RemoveAll(r => r.Deleted);
return new PagedResultDto<FormSubmissionSummaryDto>(chefsSubmissions.Count, chefsSubmissions);
}
}