-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVendorManager.cs
More file actions
256 lines (222 loc) · 11.4 KB
/
Copy pathVendorManager.cs
File metadata and controls
256 lines (222 loc) · 11.4 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using LantanaGroup.Link.Shared.Application.Interfaces.Services.Security.Token;
using LantanaGroup.Link.Shared.Application.Models;
using LantanaGroup.Link.Shared.Application.Models.Configs;
using LantanaGroup.Link.Shared.Application.Models.Kafka;
using LantanaGroup.Link.Shared.Application.Models.Tenant;
using LantanaGroup.Link.Shared.Application.Services.Security;
using LantanaGroup.Link.Shared.Domain.Repositories.Interfaces;
using LantanaGroup.Link.Sdk.Clients;
using LantanaGroup.Link.Tenant.Business.Queries;
using LantanaGroup.Link.Tenant.Commands;
using LantanaGroup.Link.Tenant.Config;
using LantanaGroup.Link.Tenant.Entities;
using LantanaGroup.Link.Tenant.Models;
using LantanaGroup.Link.Tenant.Repository.Context;
using LantanaGroup.Link.Tenant.Utils;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using OpenTelemetry.Trace;
using System.Diagnostics;
using System.Net.Http.Headers;
using System.Text;
using static LantanaGroup.Link.Shared.Application.Extensions.Security.BackendAuthenticationServiceExtension;
namespace LantanaGroup.Link.Tenant.Business.Managers
{
public interface IVendorManager
{
Task<VendorModel> CreateVendorAsync(VendorModel newVendor, CancellationToken cancellationToken = default);
Task<VendorVersionModel> CreateVendorVersionAsync(VendorVersionModel newVendorVersion, CancellationToken cancellationToken = default);
Task<VendorModel> UpdateVendorAsync(Guid id, VendorModel vendor, CancellationToken cancellationToken = default);
Task<VendorVersionModel> UpdateVendorVersionAsync(Guid id, VendorVersionModel vendorVersion, CancellationToken cancellationToken = default);
Task DeleteVendorAsync(Guid vendorId, CancellationToken cancellationToken = default);
Task DeleteVendorVersionAsync(Guid vendorVersionId, CancellationToken cancellationToken = default);
}
public sealed class VendorVersionInUseException : InvalidOperationException
{
public VendorVersionInUseException(Guid vendorVersionId)
: base($"Vendor version '{vendorVersionId}' is referenced by Normalization and cannot be deleted.")
{
}
}
public class VendorManager : IVendorManager
{
private readonly ILogger<VendorManager> _logger;
private readonly TenantDbContext _dbContext;
private readonly INormalizationServiceClient _normalizationServiceClient;
private readonly CreateAuditEventCommand _createAuditEventCommand;
public VendorManager(
ILogger<VendorManager> logger,
TenantDbContext dbContext,
INormalizationServiceClient normalizationServiceClient,
CreateAuditEventCommand createAuditEventCommand)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
_normalizationServiceClient = normalizationServiceClient ?? throw new ArgumentNullException(nameof(normalizationServiceClient));
_createAuditEventCommand = createAuditEventCommand ?? throw new ArgumentNullException(nameof(createAuditEventCommand));
}
public async Task<VendorModel> CreateVendorAsync(VendorModel newVendor, CancellationToken cancellationToken = default)
{
if(newVendor == null)
{
throw new ArgumentNullException(nameof(newVendor));
}
if (string.IsNullOrEmpty(newVendor.Name))
{
throw new ArgumentNullException(nameof(newVendor.Name));
}
var existingVendor = await _dbContext.Vendors.FirstOrDefaultAsync(v => v.Name == newVendor.Name, cancellationToken);
if (existingVendor != null)
{
throw new InvalidOperationException($"Vendor with name '{newVendor.Name}' already exists.");
}
var vendorEntity = new Vendor
{
Id = Guid.NewGuid(),
Name = newVendor.Name,
Authentication = newVendor.Authentication
};
var vendorVersionEntity = new VendorVersion
{
Id = Guid.NewGuid(),
VendorId = vendorEntity.Id,
Version = "default"
};
await _dbContext.Vendors.AddAsync(vendorEntity, cancellationToken);
await _dbContext.VendorVersions.AddAsync(vendorVersionEntity, cancellationToken);
await _dbContext.SaveChangesAsync(cancellationToken);
_createAuditEventCommand.Execute(
vendorEntity.Id.ToString(), Helper.CreateVendorAuditEvent(vendorEntity), cancellationToken);
return new VendorModel
{
Id = vendorEntity.Id,
Name = vendorEntity.Name,
Authentication = vendorEntity.Authentication
};
}
public async Task<VendorVersionModel> CreateVendorVersionAsync(VendorVersionModel newVendorVersion, CancellationToken cancellationToken = default)
{
if(newVendorVersion == null)
{
throw new ArgumentNullException(nameof(newVendorVersion));
}
if (string.IsNullOrEmpty(newVendorVersion.Version))
{
throw new ArgumentNullException(nameof(newVendorVersion.Version));
}
var existingVendor = await _dbContext.Vendors.FirstOrDefaultAsync(v => v.Id == newVendorVersion.VendorId, cancellationToken);
if (existingVendor == null)
{
throw new InvalidOperationException($"Vendor with ID '{newVendorVersion.VendorId}' does not exist.");
}
var existingVendorVersion = await _dbContext.VendorVersions.FirstOrDefaultAsync(vv => vv.VendorId == newVendorVersion.VendorId && vv.Version == newVendorVersion.Version, cancellationToken);
if (existingVendorVersion != null)
{
throw new InvalidOperationException($"Vendor version '{newVendorVersion.Version}' for vendor ID '{newVendorVersion.VendorId}' already exists.");
}
var vendorVersionEntity = new VendorVersion
{
Id = Guid.NewGuid(),
VendorId = newVendorVersion.VendorId!.Value,
Version = newVendorVersion.Version
};
await _dbContext.VendorVersions.AddAsync(vendorVersionEntity, cancellationToken);
await _dbContext.SaveChangesAsync(cancellationToken);
return new VendorVersionModel
{
Id = vendorVersionEntity.Id,
VendorId = vendorVersionEntity.VendorId,
Version = vendorVersionEntity.Version
};
}
public async Task DeleteVendorAsync(Guid vendorId, CancellationToken cancellationToken = default)
{
var vendorVersionIds = await _dbContext.VendorVersions
.Where(vendorVersion => vendorVersion.VendorId == vendorId)
.Select(vendorVersion => vendorVersion.Id)
.ToListAsync(cancellationToken);
foreach (var vendorVersionId in vendorVersionIds)
{
await EnsureVendorVersionIsNotReferencedByNormalizationAsync(vendorVersionId, cancellationToken);
}
var vendor = await _dbContext.Vendors.AsNoTracking()
.FirstOrDefaultAsync(v => v.Id == vendorId, cancellationToken);
await _dbContext.Vendors.Where(q => q.Id == vendorId).ExecuteDeleteAsync(cancellationToken);
if (vendor != null)
{
_createAuditEventCommand.Execute(
vendor.Id.ToString(), Helper.DeleteVendorAuditEvent(vendor), cancellationToken);
}
}
public async Task DeleteVendorVersionAsync(Guid vendorVersionId, CancellationToken cancellationToken = default)
{
await EnsureVendorVersionIsNotReferencedByNormalizationAsync(vendorVersionId, cancellationToken);
await _dbContext.VendorVersions.Where(q => q.Id == vendorVersionId).ExecuteDeleteAsync(cancellationToken);
}
private async Task EnsureVendorVersionIsNotReferencedByNormalizationAsync(Guid vendorVersionId, CancellationToken cancellationToken)
{
var response = await _normalizationServiceClient.GetVendorVersionOperationPresetsAsync(
vendorVersionId: vendorVersionId,
cancellationToken: cancellationToken);
if (!response.IsSuccessStatusCode)
{
throw new InvalidOperationException("Unable to verify Normalization references before deleting the vendor version.");
}
if (response.Body?.Count > 0)
{
throw new VendorVersionInUseException(vendorVersionId);
}
}
public async Task<VendorModel> UpdateVendorAsync(Guid id, VendorModel vendor, CancellationToken cancellationToken = default)
{
if(string.IsNullOrWhiteSpace(vendor.Name))
{
throw new ArgumentNullException(nameof(vendor.Name));
}
var existingVendor = await _dbContext.Vendors.FirstOrDefaultAsync(v => v.Id == id, cancellationToken);
if (existingVendor == null)
{
throw new InvalidOperationException($"Vendor with ID '{id}' does not exist.");
}
string? previousName = existingVendor.Name;
string? previousSigningKeySecretId = existingVendor.Authentication?.SigningKeySecretId;
existingVendor.Name = vendor.Name ?? existingVendor.Name;
existingVendor.Authentication = vendor.Authentication ?? existingVendor.Authentication;
_dbContext.Vendors.Update(existingVendor);
await _dbContext.SaveChangesAsync(cancellationToken);
AuditEventMessage? auditEvent =
Helper.UpdateVendorAuditEvent(existingVendor, previousName, previousSigningKeySecretId);
if (auditEvent != null)
{
_createAuditEventCommand.Execute(existingVendor.Id.ToString(), auditEvent, cancellationToken);
}
return new VendorModel
{
Id = existingVendor.Id,
Name = existingVendor.Name,
Authentication = existingVendor.Authentication
};
}
public async Task<VendorVersionModel> UpdateVendorVersionAsync(Guid id, VendorVersionModel vendorVersion, CancellationToken cancellationToken = default)
{
if(string.IsNullOrWhiteSpace(vendorVersion.Version))
{
throw new ArgumentNullException(nameof(vendorVersion.Version));
}
var existingVendorVersion = await _dbContext.VendorVersions.FirstOrDefaultAsync(vv => vv.Id == id, cancellationToken);
if (existingVendorVersion == null)
{
throw new InvalidOperationException($"Vendor version with ID '{id}' does not exist.");
}
existingVendorVersion.Version = vendorVersion.Version;
_dbContext.VendorVersions.Update(existingVendorVersion);
await _dbContext.SaveChangesAsync(cancellationToken);
return new VendorVersionModel
{
Id = existingVendorVersion.Id,
VendorId = existingVendorVersion.VendorId,
Version = existingVendorVersion.Version
};
}
}
}