Skip to content

Commit 864ec16

Browse files
committed
ECER-5270: International Certification repository layer (not completed)
1 parent 4dd3d00 commit 864ec16

File tree

4 files changed

+196
-1
lines changed

4 files changed

+196
-1
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
using AutoMapper;
2+
using ECER.Utilities.DataverseSdk.Model;
3+
using ECER.Utilities.ObjectStorage.Providers;
4+
using ECER.Utilities.ObjectStorage.Providers.S3;
5+
using Microsoft.Extensions.Configuration;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Configuration;
9+
using System.Linq;
10+
using System.Text;
11+
using System.Threading.Tasks;
12+
13+
namespace ECER.Resources.Documents.ICRA;
14+
15+
internal sealed partial class ICRARepository
16+
{
17+
private async Task UpdateInternationalCertifications(ecer_Application application, Contact contact, string ApplicantId, List<InternationalCertification> updatedEntities, CancellationToken ct)
18+
{
19+
await Task.CompletedTask;
20+
21+
var existingInternationalCertifications = context.ecer_InternationalCertificationSet.Where(t => t.ecer_Applicationid.Id == application.Id).ToList();
22+
foreach (var InternationalCertification in existingInternationalCertifications)
23+
{
24+
if (!updatedEntities.Any(t => t.Id == InternationalCertification.Id.ToString()))
25+
{
26+
// This deletes all files and document urls before deleting the professional development object
27+
await DeleteFilesForInternationalCertification(InternationalCertification.ecer_InternationalCertificationId, Guid.Parse(ApplicantId), ct);
28+
context.DeleteObject(InternationalCertification);
29+
}
30+
}
31+
32+
// Update Existing Professional Developments
33+
foreach (var InternationalCertification in updatedEntities.Where(d => !string.IsNullOrEmpty(d.Id)))
34+
{
35+
var oldInternationalCertification = existingInternationalCertifications.SingleOrDefault(t => t.Id.ToString() == InternationalCertification.Id);
36+
var ecerInternationalCertification = mapper.Map<ecer_InternationalCertification>(InternationalCertification)!;
37+
38+
if (oldInternationalCertification != null)
39+
{
40+
context.Detach(oldInternationalCertification);
41+
ecerInternationalCertification.StatusCode = oldInternationalCertification.StatusCode;
42+
}
43+
44+
context.Attach(ecerInternationalCertification);
45+
context.UpdateObject(ecerInternationalCertification);
46+
await HandleInternationalCertificationFiles(ecerInternationalCertification, Guid.Parse(ApplicantId), InternationalCertification.NewFiles, InternationalCertification.DeletedFiles, ct);
47+
}
48+
49+
foreach (var InternationalCertification in updatedEntities.Where(d => string.IsNullOrEmpty(d.Id)))
50+
{
51+
var ecerInternationalCertification = mapper.Map<ecer_InternationalCertification>(InternationalCertification)!;
52+
if (application.StatusCode == ecer_Application_StatusCode.Draft)
53+
{
54+
ecerInternationalCertification.StatusCode = ecer_InternationalCertification_StatusCode.Draft;
55+
}
56+
var newId = Guid.NewGuid();
57+
ecerInternationalCertification.ecer_InternationalCertificationId = newId;
58+
context.AddObject(ecerInternationalCertification);
59+
context.AddLink(application, ecer_Application.Fields.ecer_ecer_InternationalCertification_Applicationi, ecerInternationalCertification);
60+
context.AddLink(contact, Contact.Fields.ecer_ecer_InternationalCertification_Applicantid_, ecerInternationalCertification);
61+
await HandleInternationalCertificationFiles(ecerInternationalCertification, Guid.Parse(ApplicantId), InternationalCertification.NewFiles, InternationalCertification.DeletedFiles, ct);
62+
}
63+
}
64+
65+
private async Task HandleInternationalCertificationFiles(ecer_InternationalCertification InternationalCertification, Guid applicantId, IEnumerable<string> tobeAddedFileIds, IEnumerable<string> tobeDeletedFileIds, CancellationToken ct)
66+
{
67+
await Task.CompletedTask;
68+
await HandleDeletedFiles(InternationalCertification, tobeDeletedFileIds.ToList(), ct);
69+
await HandleAddedFiles(InternationalCertification, applicantId, tobeAddedFileIds.ToList(), ct);
70+
}
71+
72+
private async Task HandleDeletedFiles(ecer_InternationalCertification InternationalCertification, List<string> tobeDeletedFileIds, CancellationToken ct)
73+
{
74+
await Task.CompletedTask;
75+
foreach (var fileId in tobeDeletedFileIds)
76+
{
77+
if (InternationalCertification == null)
78+
{
79+
throw new InvalidOperationException($"InternationalCertification '{InternationalCertification}' not found");
80+
}
81+
// delete file and related document url
82+
await DeleteFileById(InternationalCertification.Id, fileId, ct);
83+
}
84+
}
85+
86+
private async Task DeleteFileById(Guid InternationalCertificationId, string fileId, CancellationToken ct)
87+
{
88+
await Task.CompletedTask;
89+
90+
var file = context.bcgov_DocumentUrlSet.SingleOrDefault(d => d.bcgov_DocumentUrlId == Guid.Parse(fileId));
91+
if (fileId == null)
92+
{
93+
throw new InvalidOperationException($"File with ID '{fileId}' not found");
94+
}
95+
var folder = "ecer_InternationalCertification/" + InternationalCertificationId;
96+
await objectStorageProvider.DeleteAsync(new S3Descriptor(GetBucketName(configuration), fileId, folder), ct);
97+
context.DeleteObject(file);
98+
}
99+
100+
private async Task HandleAddedFiles(ecer_InternationalCertification InternationalCertification, Guid applicantId, List<string> tobeAddedFileIds, CancellationToken ct)
101+
{
102+
await Task.CompletedTask;
103+
104+
foreach (var fileId in tobeAddedFileIds)
105+
{
106+
if (InternationalCertification == null)
107+
{
108+
throw new InvalidOperationException($"InternationalCertification '{InternationalCertification}' not found");
109+
}
110+
// add file and create document url
111+
// link it to professional development
112+
await AddFilesForInternationalCertification(InternationalCertification, applicantId, new List<string>() { fileId }, ct);
113+
}
114+
}
115+
116+
private async Task DeleteFilesForInternationalCertification(Guid? InternationalCertificationId, Guid? applicantId, CancellationToken ct)
117+
{
118+
await Task.CompletedTask;
119+
120+
var files = context.bcgov_DocumentUrlSet.Where(d => d.ecer_bcgov_documenturl_InternationalCertificationId.ecer_InternationalCertificationId == InternationalCertificationId && d.bcgov_contact_bcgov_documenturl.ContactId == applicantId).ToList();
121+
for (int i = 0; i < files.Count; i++)
122+
{
123+
var items = files[i].bcgov_Url.Split('/');
124+
if (items.Length != 2)
125+
{
126+
throw new FormatException($"Invalid file URL format: {files[i].bcgov_Url}");
127+
}
128+
var fileId = items[1];
129+
var folder = items[0];
130+
await objectStorageProvider.DeleteAsync(new S3Descriptor(GetBucketName(configuration), fileId, folder), ct);
131+
context.DeleteObject(files[i]);
132+
}
133+
}
134+
135+
private async Task AddFilesForInternationalCertification(ecer_InternationalCertification InternationalCertification, Guid? applicantId, List<string> fileIds, CancellationToken ct)
136+
{
137+
await Task.CompletedTask;
138+
139+
foreach (var fileId in fileIds)
140+
{
141+
var applicant = context.ContactSet.SingleOrDefault(c => c.ContactId == applicantId);
142+
if (applicant == null) throw new InvalidOperationException($"Applicant '{applicantId}' not found");
143+
144+
if (InternationalCertification == null) throw new InvalidOperationException($"InternationalCertification '{InternationalCertification}' not found");
145+
146+
var sourceFolder = "tempfolder";
147+
var destinationFolder = "ecer_InternationalCertification/" + InternationalCertification.Id;
148+
var file = await objectStorageProvider.GetAsync(new S3Descriptor(GetBucketName(configuration), fileId, sourceFolder), ct);
149+
await objectStorageProvider.MoveAsync(new S3Descriptor(GetBucketName(configuration), fileId, sourceFolder), new S3Descriptor(GetBucketName(configuration), fileId, destinationFolder), ct);
150+
151+
var documenturl = new bcgov_DocumentUrl()
152+
{
153+
bcgov_FileName = file!.FileName,
154+
bcgov_FileSize = Infrastructure.Common.UtilityFunctions.HumanFileSize(file!.Content.Length),
155+
bcgov_DocumentUrlId = Guid.Parse(fileId),
156+
bcgov_Url = destinationFolder,
157+
StatusCode = bcgov_DocumentUrl_StatusCode.Active,
158+
StateCode = bcgov_documenturl_statecode.Active
159+
};
160+
161+
context.AddObject(documenturl);
162+
context.AddLink(documenturl, bcgov_DocumentUrl.Fields.bcgov_contact_bcgov_documenturl, applicant);
163+
context.AddLink(documenturl, bcgov_DocumentUrl.Fields.ecer_bcgov_documenturl_InternationalCertificationId, InternationalCertification);
164+
}
165+
}
166+
167+
private static string GetBucketName(IConfiguration configuration) =>
168+
configuration.GetValue<string>("objectStorage:bucketName") ?? throw new InvalidOperationException("objectStorage:bucketName is not set");
169+
}
170+

src/ECER.Resources.Documents/ICRA/ICRARepository.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
using AutoMapper;
22
using ECER.Utilities.DataverseSdk.Model;
33
using ECER.Utilities.DataverseSdk.Queries;
4+
using ECER.Utilities.ObjectStorage.Providers;
45
using Microsoft.Extensions.Configuration;
56
using Microsoft.Xrm.Sdk.Client;
67

78
namespace ECER.Resources.Documents.ICRA;
89

910
internal sealed partial class ICRARepository : IICRARepository
1011
{
12+
1113
private readonly EcerContext context;
1214
private readonly IMapper mapper;
15+
private readonly IObjecStorageProvider objectStorageProvider;
16+
private readonly IConfiguration configuration;
1317

1418
public ICRARepository(
1519
EcerContext context,
20+
IObjecStorageProvider objectStorageProvider,
1621
IMapper mapper,
1722
IConfiguration configuration)
1823
{
1924
this.context = context;
2025
this.mapper = mapper;
26+
this.objectStorageProvider = objectStorageProvider;
27+
this.configuration = configuration;
2128
}
2229

2330
public async Task<IEnumerable<ICRAEligibility>> Query(ICRAQuery query, CancellationToken cancellationToken)

src/ECER.Resources.Documents/ICRA/ICRARepositoryMapper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public ICRARepositoryMapper()
1717
.ReverseMap()
1818
.ForMember(d => d.ApplicantId, opts => opts.MapFrom(s => s.ecer_icraeligibilityassessment_ApplicantId.Id));
1919

20-
2120
CreateMap<ICRAStatus, ecer_ICRAEligibilityAssessment_StatusCode>()
2221
.ConvertUsingEnumMapping(opts => opts.MapByName(true))
2322
.ReverseMap();

src/ECER.Resources.Documents/ICRA/IICRARepository.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,27 @@ public record ICRAEligibility()
2020
public string? PortalStage { get; set; }
2121
public string ApplicantId { get; set; } = string.Empty;
2222
public ICRAStatus Status { get; set; }
23+
public IEnumerable<InternationalCertification> InternationalCertifications { get; set; } = Array.Empty<InternationalCertification>();
24+
}
25+
public record InternationalCertification
26+
{
27+
public string? CountryId { get; set; }
28+
public string? NameOfRegulatoryAuthority { get; set; }
29+
public string? EmailOfRegulatoryAuthority { get; set; }
30+
public string? PhoneOfRegulatoryAuthority { get; set; }
31+
public string? WebsiteOfRegulatoryAuthority { get; set; }
32+
public string? OnlineCertificateValidationToolOfRegulatoryAuthority { get; set; }
33+
public CertificateStatus CertificateStatus { get; set; }
34+
public string? CertificateTitle { get; set; }
35+
public DateTime? IssueDate { get; set; }
36+
public DateTime? ExpiryDate { get; set; }
2337
}
2438

39+
public enum CertificateStatus
40+
{
41+
Valid,
42+
Expired
43+
}
2544
public enum ICRAStatus
2645
{
2746
Active,

0 commit comments

Comments
 (0)