-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathApplicationFormAppService.cs
More file actions
158 lines (142 loc) · 7 KB
/
ApplicationFormAppService.cs
File metadata and controls
158 lines (142 loc) · 7 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
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unity.GrantManager.Applications;
using Unity.GrantManager.Forms;
using Unity.GrantManager.GrantApplications;
using Unity.GrantManager.Integration.Chefs;
using Unity.GrantManager.Permissions;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Security.Encryption;
namespace Unity.GrantManager.ApplicationForms;
[Authorize]
public class ApplicationFormAppService :
CrudAppService<
ApplicationForm,
ApplicationFormDto,
Guid,
PagedAndSortedResultRequestDto,
CreateUpdateApplicationFormDto>,
IApplicationFormAppService
{
private readonly IStringEncryptionService _stringEncryptionService;
private readonly IFormsApiService _formsApiService;
private readonly IApplicationFormVersionAppService _applicationFormVersionAppService;
private readonly IApplicationFormVersionRepository _applicationFormVersionRepository;
private readonly IRepository<ApplicationForm, Guid> _applicationFormRepository;
public ApplicationFormAppService(IRepository<ApplicationForm, Guid> repository,
IStringEncryptionService stringEncryptionService,
IApplicationFormVersionAppService applicationFormVersionAppService,
IApplicationFormVersionRepository applicationFormVersionRepository,
IFormsApiService formsApiService)
: base(repository)
{
_stringEncryptionService = stringEncryptionService;
_applicationFormVersionAppService = applicationFormVersionAppService;
_formsApiService = formsApiService;
_applicationFormVersionRepository = applicationFormVersionRepository;
_applicationFormRepository = repository;
}
[Authorize(GrantManagerPermissions.ApplicationForms.Default)]
public override async Task<ApplicationFormDto> CreateAsync(CreateUpdateApplicationFormDto input)
{
input.ApiKey = _stringEncryptionService.Encrypt(input.ApiKey);
ApplicationFormDto applicationFormDto = await base.CreateAsync(input);
return await InitializeFormVersion(applicationFormDto.Id, input);
}
[Authorize(GrantManagerPermissions.ApplicationForms.Default)]
public override async Task<ApplicationFormDto> UpdateAsync(Guid id, CreateUpdateApplicationFormDto input)
{
var existingForm = await Repository.GetAsync(id);
input.ApiKey = _stringEncryptionService.Encrypt(input.ApiKey);
bool hasFormGuidChanged = existingForm.ChefsApplicationFormGuid != input.ChefsApplicationFormGuid;
bool hasFormApiKeyChanged = existingForm.ApiKey != input.ApiKey;
// Only initialize form version if changes are made to form connection details
if (hasFormGuidChanged || hasFormApiKeyChanged)
{
return await InitializeFormVersion(id, input);
}
else
{
return await base.UpdateAsync(id, input);
}
}
[Authorize(GrantManagerPermissions.ApplicationForms.Default)]
private async Task<ApplicationFormDto> InitializeFormVersion(Guid id, CreateUpdateApplicationFormDto input)
{
var applicationFormDto = new ApplicationFormDto();
try
{
if (input.ChefsApplicationFormGuid != null && input.ApiKey != null)
{
dynamic form = await _formsApiService.GetForm(Guid.Parse(input.ChefsApplicationFormGuid), input.ChefsApplicationFormGuid.ToString(), input.ApiKey);
if (form != null)
{
JObject formObject = JObject.Parse(form.ToString());
var formName = formObject.SelectToken("name");
if (formName != null)
{
input.ApplicationFormName = formName.ToString();
applicationFormDto = await base.UpdateAsync(id, input);
}
bool initializePublishedOnly = false;
await _applicationFormVersionAppService.InitializePublishedFormVersion(form, id, initializePublishedOnly);
}
}
return applicationFormDto;
}
catch (Exception ex)
{
throw new UserFriendlyException("Exception: " + ex.Message + "\n\r Please check the CHEFS Form ID and CHEFS Form API Key");
}
}
[Authorize(GrantManagerPermissions.ApplicationForms.Default)]
public override async Task<ApplicationFormDto> GetAsync(Guid id)
{
var dto = await base.GetAsync(id);
dto.ApiKey = _stringEncryptionService.Decrypt(dto.ApiKey);
dto.ApiToken = _stringEncryptionService.Decrypt(dto.ApiToken);
return dto;
}
[Authorize]
public async Task<AddressType> GetElectoralDistrictAddressTypeAsync(Guid id)
{
var applicationFormDto = await base.GetAsync(id);
return applicationFormDto?.ElectoralDistrictAddressType ?? ApplicationForm.GetDefaultElectoralDistrictAddressType();
}
[Authorize(GrantManagerPermissions.ApplicationForms.Default)]
public async Task<IList<ApplicationFormVersionDto>> GetPublishedVersionsAsync(Guid id)
{
IQueryable<ApplicationFormVersion> queryableFormVersions = _applicationFormVersionRepository.GetQueryableAsync().Result;
var formVersions = queryableFormVersions.Where(c => c.ApplicationFormId.Equals(id) && c.Published.Equals(true)).ToList();
return await Task.FromResult<IList<ApplicationFormVersionDto>>(ObjectMapper.Map<List<ApplicationFormVersion>, List<ApplicationFormVersionDto>>(formVersions.OrderByDescending(s => s.Version).ToList()));
}
[Authorize(GrantManagerPermissions.ApplicationForms.Default)]
public async Task<IList<ApplicationFormVersionDto>> GetVersionsAsync(Guid id)
{
IQueryable<ApplicationFormVersion> queryableFormVersions = _applicationFormVersionRepository.GetQueryableAsync().Result;
var formVersions = queryableFormVersions.Where(c => c.ApplicationFormId.Equals(id)).ToList();
return await Task.FromResult<IList<ApplicationFormVersionDto>>(ObjectMapper.Map<List<ApplicationFormVersion>, List<ApplicationFormVersionDto>>(formVersions.OrderByDescending(s => s.Version).ToList()));
}
[Authorize(GrantManagerPermissions.ApplicationForms.Default)]
public async Task SaveApplicationFormScoresheet(FormScoresheetDto dto)
{
var appForm = await _applicationFormRepository.GetAsync(dto.ApplicationFormId);
appForm.ScoresheetId = dto.ScoresheetId;
await _applicationFormRepository.UpdateAsync(appForm);
}
[Authorize(GrantManagerPermissions.ApplicationForms.Default)]
public async Task PatchOtherConfig(Guid id, OtherConfigDto config)
{
var form = await _applicationFormRepository.GetAsync(id);
form.IsDirectApproval = config.IsDirectApproval;
form.ElectoralDistrictAddressType = config.ElectoralDistrictAddressType;
await _applicationFormRepository.UpdateAsync(form);
}
}