-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathApproveApplicationsModal.cshtml.cs
More file actions
207 lines (171 loc) · 7.18 KB
/
ApproveApplicationsModal.cshtml.cs
File metadata and controls
207 lines (171 loc) · 7.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
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
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using Unity.GrantManager.GrantApplications;
using Unity.Modules.Shared.Utils;
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages;
namespace Unity.GrantManager.Web.Pages.BulkApprovals;
public class ApproveApplicationsModalModel(IBulkApprovalsAppService bulkApprovalsAppService,
BrowserUtils browserUtils) : AbpPageModel
{
[BindProperty]
public List<BulkApplicationApproval>? BulkApplicationApprovals { get; set; }
[TempData]
public int ApplicationsCount { get; set; }
[TempData]
public bool Invalid { get; set; }
[TempData]
public int MaxBatchCount { get; set; }
[TempData]
public string? MaxBatchCountExceededError { get; set; }
[TempData]
public bool MaxBatchCountExceeded { get; set; }
public async Task OnGetAsync(string applicationIds)
{
MaxBatchCount = BatchApprovalConsts.MaxBatchCount;
BulkApplicationApprovals = [];
MaxBatchCountExceededError = L["ApplicationBatchApprovalRequest:MaxCountExceeded", BatchApprovalConsts.MaxBatchCount.ToString()].Value;
Guid[] applicationGuids = ParseApplicationIds(applicationIds);
if (!ValidCount(applicationGuids))
{
MaxBatchCountExceeded = true;
}
if (applicationGuids.Length == 0)
{
return;
}
// Load the applications by Id
var applications = await bulkApprovalsAppService.GetApplicationsForBulkApproval(applicationGuids);
var offsetMinutes = browserUtils.GetBrowserOffset();
foreach (var application in applications)
{
var bulkApproval = new BulkApplicationApproval
{
ApplicationId = application.ApplicationId,
ReferenceNo = application.ReferenceNo,
ApplicantName = application.ApplicantName,
DecisionDate = application.FinalDecisionDate ?? DateTime.UtcNow.AddMinutes(-offsetMinutes),
RequestedAmount = application.RequestedAmount,
ApprovedAmount = application.ApprovedAmount == 0m ? application.RequestedAmount : application.ApprovedAmount,
ApplicationStatus = application.ApplicationStatus,
FormName = application.FormName,
IsValid = application.IsValid,
Notes = SetNotesForApplication(application)
};
BulkApplicationApprovals.Add(bulkApproval);
}
Invalid = applications.Exists(s => !s.IsValid) || MaxBatchCountExceeded;
ApplicationsCount = applications.Count;
}
private List<ApprovalNote> SetNotesForApplication(BulkApprovalDto application)
{
var notes = new List<ApprovalNote>
{
new("DECISION_DATE_DEFAULTED", false, L.GetString("ApplicationBatchApprovalRequest:DecisionDateDefaulted"), false),
new("APPROVED_AMOUNT_DEFAULTED", false, L.GetString("ApplicationBatchApprovalRequest:ApprovedAmountDefaulted"), false),
new("INVALID_STATUS", false, L.GetString("ApplicationBatchApprovalRequest:InvalidStatus"), true),
new("INVALID_PERMISSIONS", false, L.GetString("ApplicationBatchApprovalRequest:InvalidPermissions"), true),
new("INVALID_APPROVED_AMOUNT", false, L.GetString("ApplicationBatchApprovalRequest:InvalidApprovedAmount"), true)
};
if (application.FinalDecisionDate == null)
{
notes[0] = new ApprovalNote(notes[0].Key, true, notes[0].Description, notes[0].IsError);
}
if (application.ApprovedAmount == 0m)
{
notes[0] = new ApprovalNote(notes[1].Key, true, notes[1].Description, notes[1].IsError);
}
foreach (var validation in application.ValidationMessages)
{
var index = notes.FindIndex(note => note.Key == validation);
if (index != -1)
{
notes[index] = new ApprovalNote(validation, true, notes[index].Description, notes[index].IsError);
}
}
return notes;
}
public async Task<IActionResult> OnPostAsync()
{
try
{
if (BulkApplicationApprovals == null) return NoContent();
var approvalRequests = MapBulkApprovalRequests();
// Fire off request to approve the applications
var result = await bulkApprovalsAppService.BulkApproveApplications(approvalRequests);
// Return the result out
return new OkObjectResult(result);
}
catch (Exception ex)
{
Logger.LogError(ex, "Error updating application statuses");
}
return NoContent();
}
private List<BulkApprovalDto> MapBulkApprovalRequests()
{
var bulkApprovals = new List<BulkApprovalDto>();
foreach (var application in BulkApplicationApprovals ?? [])
{
bulkApprovals.Add(new BulkApprovalDto()
{
ApplicantName = application.ApplicantName ?? string.Empty,
ApplicationId = application.ApplicationId,
ApprovedAmount = application.ApprovedAmount,
FinalDecisionDate = application.DecisionDate,
ReferenceNo = application.ReferenceNo,
RequestedAmount = application.RequestedAmount,
ValidationMessages = []
});
}
return bulkApprovals;
}
private static Guid[] ParseApplicationIds(string applicationIds)
{
return JsonConvert.DeserializeObject<Guid[]>(applicationIds ?? string.Empty) ?? [];
}
private bool ValidCount(Guid[] applicationGuids)
{
// Soft check in the UI for max approvals in one batch, this is subject to be tweaked later after performance testing
return applicationGuids.Length <= MaxBatchCount;
}
public class BulkApplicationApproval
{
public BulkApplicationApproval()
{
Notes = [];
}
public Guid ApplicationId { get; set; }
public string ReferenceNo { get; set; } = string.Empty;
public string? ApplicantName { get; set; } = string.Empty;
public string FormName { get; set; } = string.Empty;
public string ApplicationStatus { get; set; } = string.Empty;
[DisplayName("Requested Amount")]
public decimal RequestedAmount { get; set; } = 0m;
[DisplayName("Approved Amount")]
public decimal ApprovedAmount { get; set; } = 0m;
[DisplayName("Decision Date")]
public DateTime DecisionDate { get; set; }
public bool IsValid { get; set; }
public List<ApprovalNote> Notes { get; set; }
}
public class ApprovalNote
{
public ApprovalNote(string key, bool active, string description, bool isError)
{
Key = key;
Active = active;
Description = description;
IsError = isError;
}
public string Key { get; set; }
public bool Active { get; set; }
public string Description { get; set; }
public bool IsError { get; set; }
}
}