-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFormsReportSyncServiceAppService.cs
More file actions
201 lines (172 loc) · 7.23 KB
/
FormsReportSyncServiceAppService.cs
File metadata and controls
201 lines (172 loc) · 7.23 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
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Threading.Tasks;
using Unity.GrantManager.Applications;
using Unity.GrantManager.Reporting.DataGenerators;
using Unity.GrantManager.Reporting.FieldGenerators;
using Unity.Modules.Shared.Permissions;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Features;
using Volo.Abp.TenantManagement;
using Volo.Abp.Uow;
namespace Unity.GrantManager.Reporting
{
[Authorize(IdentityConsts.ITAdminPolicyName)]
public class FormsReportSyncServiceAppService(IReportingFieldsGeneratorService reportingFieldsGeneratorService,
IApplicationFormVersionRepository applicationFormVersionRepository,
IReportingDataGenerator reportingDataGenerator,
IUnitOfWorkManager unitOfWorkManager,
ITenantRepository tenantRepository,
IApplicationFormSubmissionRepository applicationFormSubmissionRepository,
IFeatureChecker featureChecker)
: ApplicationService, IFormsReportSyncServiceAppService
{
const string ReportingFeatureName = "Unity.Reporting";
/// <summary>
/// Generate the reporting fields for a form version
/// </summary>
/// <param name="formVersionId"></param>
/// <returns></returns>
private async Task GenerateFormVersionFields(Guid formVersionId)
{
var applicationFormVersion = await applicationFormVersionRepository.GetAsync(formVersionId);
await reportingFieldsGeneratorService.GenerateAndSetAsync(applicationFormVersion);
}
/// <summary>
/// Generate the form submission data for a form version
/// </summary>
/// <param name="submissionId"></param>
/// <returns></returns>
/// <exception cref="EntityNotFoundException"></exception>
private async Task GenerateFormSubmissionData(Guid submissionId)
{
var submission = await applicationFormSubmissionRepository.GetAsync(submissionId);
Guid applicationFormVersionId;
ApplicationFormVersion? applicationFormVersion;
if (submission.ApplicationFormVersionId == null)
{
applicationFormVersion = await applicationFormVersionRepository.GetByChefsFormVersionAsync(submission.FormVersionId ?? Guid.Empty);
if (applicationFormVersion == null)
{
throw new EntityNotFoundException();
}
applicationFormVersionId = applicationFormVersion.Id;
}
else
{
applicationFormVersionId = submission.ApplicationFormVersionId.Value;
}
applicationFormVersion = await applicationFormVersionRepository.GetAsync(applicationFormVersionId) ?? throw new EntityNotFoundException();
JObject submissionData = JObject.Parse(submission.Submission);
var reportData = reportingDataGenerator.Generate(submissionData, applicationFormVersion.ReportKeys, submissionId);
submission.ReportData = reportData ?? "{}";
}
/// <summary>
/// Sync / Generate all the form version reporting fields that are missing for all tenants
/// </summary>
/// <returns></returns>
public async Task SyncFormVersionFields(Guid? tenantId)
{
// Sync the fields for the current tenant if specified
if (tenantId.HasValue)
{
using (CurrentTenant.Change(tenantId))
{
if (await featureChecker.IsEnabledAsync(ReportingFeatureName))
{
await SyncFormVersionFieldsForCurrentTenant();
}
}
return;
}
// Sync the fields for all tenants
var tenants = await tenantRepository.GetListAsync();
foreach (var tenant in tenants)
{
using (CurrentTenant.Change(tenant.Id))
{
if (await featureChecker.IsEnabledAsync(ReportingFeatureName))
{
await SyncFormVersionFieldsForCurrentTenant();
}
}
}
}
/// <summary>
/// Sync form version fields for current tenant context
/// </summary>
/// <returns></returns>
private async Task SyncFormVersionFieldsForCurrentTenant()
{
using var uow = unitOfWorkManager.Begin(isTransactional: false);
// Get all the Form Version thats have no report data associated
var formVersionIds = (await applicationFormVersionRepository.GetListAsync())
.Where(w => string.IsNullOrEmpty(w.ReportViewName))
.Select(s => s.Id);
foreach (var formVersionId in formVersionIds)
{
// Generate the Reporting Fields for the form version
await GenerateFormVersionFields(formVersionId);
}
await uow.CompleteAsync();
}
/// <summary>
/// Sync / Generate all the form submission reporting data that are missing for all tenants
/// </summary>
/// <returns></returns>
public async Task SyncFormSubmissionData(Guid? tenantId)
{
// Sync submission data for specific tenant
if (tenantId.HasValue)
{
using (CurrentTenant.Change(tenantId))
{
if (await featureChecker.IsEnabledAsync(ReportingFeatureName))
{
await SyncFormSubmissionDataForCurrentTenant();
}
}
return;
}
// Sync submission data for all tenants
var tenants = await tenantRepository.GetListAsync();
foreach (var tenant in tenants)
{
using (CurrentTenant.Change(tenant.Id))
{
if (await featureChecker.IsEnabledAsync(ReportingFeatureName))
{
await SyncFormSubmissionDataForCurrentTenant();
}
}
}
}
/// <summary>
/// Sync form submission data for current tenant context
/// </summary>
/// <returns></returns>
private async Task SyncFormSubmissionDataForCurrentTenant()
{
const int BatchSize = 25;
var submissionIds = (await applicationFormSubmissionRepository
.GetListAsync())
.Where(s => string.IsNullOrEmpty(s.ReportData) || s.ReportData == "{}")
.Select(s => s.Id)
.ToList();
// Process in batches of 25
foreach (var batch in submissionIds.Chunk(BatchSize))
{
using var uow = unitOfWorkManager.Begin(isTransactional: false);
foreach (var submissionId in batch)
{
// Generate the Reporting Fields for the form version
await GenerateFormSubmissionData(submissionId);
}
await uow.CompleteAsync();
}
}
}
}