-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathConnectorsBusinessLogic.cs
502 lines (431 loc) · 25.7 KB
/
ConnectorsBusinessLogic.cs
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/********************************************************************************
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
using Microsoft.Extensions.Options;
using Org.Eclipse.TractusX.Portal.Backend.Administration.Service.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Administration.Service.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Async;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.IO;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Linq;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Identities;
using Org.Eclipse.TractusX.Portal.Backend.Processes.Library;
using Org.Eclipse.TractusX.Portal.Backend.SdFactory.Library.BusinessLogic;
using Org.Eclipse.TractusX.Portal.Backend.SdFactory.Library.Models;
using System.Text.RegularExpressions;
namespace Org.Eclipse.TractusX.Portal.Backend.Administration.Service.BusinessLogic;
/// <summary>
/// Implementation of <see cref="IConnectorsBusinessLogic"/> making use of <see cref="IConnectorsRepository"/> to retrieve data.
/// </summary>
public class ConnectorsBusinessLogic(
IPortalRepositories portalRepositories,
IOptions<ConnectorsSettings> options,
ISdFactoryBusinessLogic sdFactoryBusinessLogic,
IIdentityService identityService,
IServiceAccountManagement serviceAccountManagement,
ILogger<ConnectorsBusinessLogic> logger)
: IConnectorsBusinessLogic
{
private static readonly Regex BpnRegex = new(@"(\w|\d){16}", RegexOptions.None, TimeSpan.FromSeconds(1));
private readonly IIdentityData _identityData = identityService.IdentityData;
private readonly ConnectorsSettings _settings = options.Value;
/// <inheritdoc/>
public Task<Pagination.Response<ConnectorData>> GetAllCompanyConnectorDatas(int page, int size) =>
Pagination.CreateResponseAsync(
page,
size,
_settings.MaxPageSize,
portalRepositories.GetInstance<IConnectorsRepository>().GetAllCompanyConnectorsForCompanyId(_identityData.CompanyId));
/// <inheritdoc/>
public Task<Pagination.Response<ManagedConnectorData>> GetManagedConnectorForCompany(int page, int size) =>
Pagination.CreateResponseAsync(
page,
size,
_settings.MaxPageSize,
portalRepositories.GetInstance<IConnectorsRepository>().GetManagedConnectorsForCompany(_identityData.CompanyId));
public async Task<ConnectorData> GetCompanyConnectorData(Guid connectorId)
{
var companyId = _identityData.CompanyId;
var result = await portalRepositories.GetInstance<IConnectorsRepository>().GetConnectorByIdForCompany(connectorId, companyId).ConfigureAwait(ConfigureAwaitOptions.None);
if (result == default)
{
throw NotFoundException.Create(AdministrationConnectorErrors.CONNECTOR_NOT_FOUND, new ErrorParameter[] { new("connectorId", connectorId.ToString()) });
}
if (!result.IsProvidingOrHostCompany)
{
throw ForbiddenException.Create(AdministrationConnectorErrors.CONNECTOR_NOT_PROVIDER_COMPANY_NOR_HOST, new ErrorParameter[] { new("companyId", companyId.ToString()), new("connectorId", connectorId.ToString()) });
}
return result.ConnectorData;
}
/// <inheritdoc/>
public Task<Guid> CreateConnectorAsync(ConnectorInputModel connectorInputModel, CancellationToken cancellationToken) =>
CreateConnectorInternalAsync(connectorInputModel, cancellationToken);
public Task<Guid> CreateManagedConnectorAsync(ManagedConnectorInputModel connectorInputModel, CancellationToken cancellationToken) =>
CreateManagedConnectorInternalAsync(connectorInputModel, cancellationToken);
private async Task<Guid> CreateConnectorInternalAsync(ConnectorInputModel connectorInputModel, CancellationToken cancellationToken)
{
var companyId = _identityData.CompanyId;
var (name, connectorUrl, location, technicalUserId) = connectorInputModel;
await CheckDuplicateConnector(name, connectorUrl).ConfigureAwait(ConfigureAwaitOptions.None);
await CheckLocationExists(location);
var result = await portalRepositories
.GetInstance<ICompanyRepository>()
.GetCompanyBpnAndSelfDescriptionDocumentByIdAsync(companyId)
.ConfigureAwait(ConfigureAwaitOptions.None);
if (string.IsNullOrEmpty(result.Bpn))
{
throw UnexpectedConditionException.Create(AdministrationConnectorErrors.CONNECTOR_UNEXPECTED_NO_BPN_ASSIGNED, new ErrorParameter[] { new("companyId", companyId.ToString()) });
}
await ValidateTechnicalUser(technicalUserId, companyId).ConfigureAwait(ConfigureAwaitOptions.None);
var connectorRequestModel = new ConnectorRequestModel(name, connectorUrl, ConnectorTypeId.COMPANY_CONNECTOR, location, companyId, companyId, technicalUserId);
return await CreateAndRegisterConnectorAsync(
connectorRequestModel,
result.Bpn,
result.SelfDescriptionDocumentId,
null,
companyId,
cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
}
private async Task<Guid> CreateManagedConnectorInternalAsync(ManagedConnectorInputModel connectorInputModel, CancellationToken cancellationToken)
{
var companyId = _identityData.CompanyId;
var (name, connectorUrl, location, subscriptionId, technicalUserId) = connectorInputModel;
await CheckDuplicateConnector(name, connectorUrl).ConfigureAwait(ConfigureAwaitOptions.None);
await CheckLocationExists(location).ConfigureAwait(ConfigureAwaitOptions.None);
var result = await portalRepositories.GetInstance<IOfferSubscriptionsRepository>()
.CheckOfferSubscriptionWithOfferProvider(subscriptionId, companyId)
.ConfigureAwait(ConfigureAwaitOptions.None);
if (!result.Exists)
{
throw NotFoundException.Create(AdministrationConnectorErrors.CONNECTOR_NOT_OFFERSUBSCRIPTION_EXIST, new ErrorParameter[] { new("subscriptionId", subscriptionId.ToString()) });
}
if (!result.IsOfferProvider)
{
throw ForbiddenException.Create(AdministrationConnectorErrors.CONNECTOR_NOT_PROVIDER_COMPANY_OFFER);
}
if (result.OfferSubscriptionAlreadyLinked)
{
throw ConflictException.Create(AdministrationConnectorErrors.CONNECTOR_CONFLICT_OFFERSUBSCRIPTION_LINKED);
}
if (result.OfferSubscriptionStatus != OfferSubscriptionStatusId.ACTIVE &&
result.OfferSubscriptionStatus != OfferSubscriptionStatusId.PENDING)
{
throw ConflictException.Create(AdministrationConnectorErrors.CONNECTOR_CONFLICT_STATUS_ACTIVE_OR_PENDING, new ErrorParameter[] { new("offerSubscriptionStatusIdActive", OfferSubscriptionStatusId.ACTIVE.ToString()), new("offerSubscriptionStatusIdPending", OfferSubscriptionStatusId.PENDING.ToString()) });
}
if (string.IsNullOrWhiteSpace(result.ProviderBpn))
{
throw ConflictException.Create(AdministrationConnectorErrors.CONNECTOR_CONFLICT_SET_BPN, new ErrorParameter[] { new("companyId", result.CompanyId.ToString()) });
}
await ValidateTechnicalUser(technicalUserId, result.CompanyId).ConfigureAwait(ConfigureAwaitOptions.None);
var connectorRequestModel = new ConnectorRequestModel(name, connectorUrl, ConnectorTypeId.CONNECTOR_AS_A_SERVICE, location, companyId, result.CompanyId, technicalUserId);
return await CreateAndRegisterConnectorAsync(
connectorRequestModel,
result.ProviderBpn,
result.SelfDescriptionDocumentId,
subscriptionId,
result.CompanyId,
cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
}
private async Task CheckLocationExists(string location)
{
if (!await portalRepositories.GetInstance<ICountryRepository>()
.CheckCountryExistsByAlpha2CodeAsync(location.ToUpper()).ConfigureAwait(ConfigureAwaitOptions.None))
{
throw ControllerArgumentException.Create(AdministrationConnectorErrors.CONNECTOR_ARGUMENT_LOCATION_NOT_EXIST, new ErrorParameter[] { new("location", location) });
}
}
private async Task CheckDuplicateConnector(string name, string connectorUrl)
{
if (await portalRepositories.GetInstance<IConnectorsRepository>()
.CheckConnectorExists(name, connectorUrl).ConfigureAwait(ConfigureAwaitOptions.None))
{
throw ConflictException.Create(AdministrationConnectorErrors.CONNECTOR_DUPLICATE, [new("name", name), new("connectorUrl", connectorUrl)]);
}
}
private async Task ValidateTechnicalUser(Guid? technicalUserId, Guid companyId)
{
if (technicalUserId == null)
{
return;
}
if (!await portalRepositories.GetInstance<ITechnicalUserRepository>()
.CheckActiveServiceAccountExistsForCompanyAsync(technicalUserId.Value, companyId).ConfigureAwait(ConfigureAwaitOptions.None))
{
throw ControllerArgumentException.Create(AdministrationConnectorErrors.CONNECTOR_ARGUMENT_TECH_USER_NOT_ACTIVE, [new ErrorParameter("technicalUserId", technicalUserId.Value.ToString()), new ErrorParameter("companyId", companyId.ToString())]);
}
}
private async Task<Guid> CreateAndRegisterConnectorAsync(
ConnectorRequestModel connectorInputModel,
string businessPartnerNumber,
Guid? selfDescriptionDocumentId,
Guid? subscriptionId,
Guid companyId,
CancellationToken cancellationToken)
{
if (selfDescriptionDocumentId is null && !_settings.ClearinghouseConnectDisabled)
{
throw ConflictException.Create(AdministrationConnectorErrors.CONNECTOR_CONFLICT_NO_DESCRIPTION, [new("companyId", companyId.ToString())]);
}
var (name, connectorUrl, type, location, provider, host, technicalUserId) = connectorInputModel;
var connectorsRepository = portalRepositories.GetInstance<IConnectorsRepository>();
var createdConnector = connectorsRepository.CreateConnector(
name,
location.ToUpper(),
connectorUrl,
connector =>
{
connector.ProviderId = provider;
connector.HostId = host;
connector.TypeId = type;
connector.DateLastChanged = DateTimeOffset.UtcNow;
connector.StatusId = _settings.ClearinghouseConnectDisabled ? ConnectorStatusId.ACTIVE : ConnectorStatusId.PENDING;
if (technicalUserId != null)
{
connector.TechnicalUserId = technicalUserId;
}
});
if (subscriptionId != null)
{
connectorsRepository.CreateConnectorAssignedSubscriptions(createdConnector.Id, subscriptionId.Value);
}
if (!_settings.ClearinghouseConnectDisabled)
{
var selfDescriptionDocumentUrl = $"{_settings.SelfDescriptionDocumentUrl}/{selfDescriptionDocumentId}";
await sdFactoryBusinessLogic
.RegisterConnectorAsync(createdConnector.Id, selfDescriptionDocumentUrl, businessPartnerNumber, cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
}
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
return createdConnector.Id;
}
/// <inheritdoc/>
public async Task DeleteConnectorAsync(Guid connectorId, bool deleteServiceAccount)
{
var companyId = _identityData.CompanyId;
var connectorsRepository = portalRepositories.GetInstance<IConnectorsRepository>();
var processStepsToFilter = new[]
{
ProcessStepTypeId.CREATE_DIM_TECHNICAL_USER, ProcessStepTypeId.RETRIGGER_CREATE_DIM_TECHNICAL_USER,
ProcessStepTypeId.AWAIT_CREATE_DIM_TECHNICAL_USER_RESPONSE
};
var result = await connectorsRepository.GetConnectorDeleteDataAsync(connectorId, companyId, processStepsToFilter).ConfigureAwait(ConfigureAwaitOptions.None) ?? throw NotFoundException.Create(AdministrationConnectorErrors.CONNECTOR_NOT_FOUND, new ErrorParameter[] { new("connectorId", connectorId.ToString()) });
if (!result.IsProvidingOrHostCompany)
{
throw ForbiddenException.Create(AdministrationConnectorErrors.CONNECTOR_NOT_PROVIDER_COMPANY_NOR_HOST, [new("companyId", companyId.ToString()), new("connectorId", connectorId.ToString())]);
}
if (result is { ServiceAccountId: not null, UserStatusId: UserStatusId.ACTIVE or UserStatusId.PENDING } && deleteServiceAccount)
{
await serviceAccountManagement.DeleteServiceAccount(result.ServiceAccountId!.Value, result.DeleteServiceAccountData).ConfigureAwait(false);
}
switch (result.ConnectorStatus)
{
case ConnectorStatusId.PENDING when result.SelfDescriptionDocumentId == null:
await DeleteConnectorWithoutDocuments(connectorId, result.ConnectorOfferSubscriptions, connectorsRepository);
break;
case ConnectorStatusId.PENDING:
await DeleteConnectorWithDocuments(connectorId, result.SelfDescriptionDocumentId.Value, result.ConnectorOfferSubscriptions, connectorsRepository);
break;
case ConnectorStatusId.ACTIVE when result.SelfDescriptionDocumentId != null && result.DocumentStatusId != null:
await DeleteConnector(connectorId, result.ConnectorOfferSubscriptions, result.SelfDescriptionDocumentId.Value, result.DocumentStatusId.Value, connectorsRepository);
break;
default:
throw ConflictException.Create(AdministrationConnectorErrors.CONNECTOR_CONFLICT_DELETION_DECLINED);
}
}
private async Task DeleteConnector(Guid connectorId, IEnumerable<ConnectorOfferSubscription> connectorOfferSubscriptions, Guid selfDescriptionDocumentId, DocumentStatusId documentStatus, IConnectorsRepository connectorsRepository)
{
portalRepositories.GetInstance<IDocumentRepository>().AttachAndModifyDocument(
selfDescriptionDocumentId,
a => { a.DocumentStatusId = documentStatus; },
a => { a.DocumentStatusId = DocumentStatusId.INACTIVE; });
RemoveConnectorAssignedOfferSubscriptions(connectorId, connectorOfferSubscriptions, connectorsRepository);
await DeleteUpdateConnectorDetail(connectorId, connectorsRepository);
}
private async Task DeleteUpdateConnectorDetail(Guid connectorId, IConnectorsRepository connectorsRepository)
{
connectorsRepository.AttachAndModifyConnector(connectorId, null, con =>
{
con.TechnicalUserId = null;
con.StatusId = ConnectorStatusId.INACTIVE;
con.DateLastChanged = DateTimeOffset.UtcNow;
});
await portalRepositories.SaveAsync();
}
private async Task DeleteConnectorWithDocuments(Guid connectorId, Guid selfDescriptionDocumentId, IEnumerable<ConnectorOfferSubscription> connectorOfferSubscriptions, IConnectorsRepository connectorsRepository)
{
portalRepositories.GetInstance<IDocumentRepository>().RemoveDocument(selfDescriptionDocumentId);
RemoveConnectorAssignedOfferSubscriptions(connectorId, connectorOfferSubscriptions, connectorsRepository);
connectorsRepository.DeleteConnector(connectorId);
await portalRepositories.SaveAsync();
}
private async Task DeleteConnectorWithoutDocuments(Guid connectorId, IEnumerable<ConnectorOfferSubscription> connectorOfferSubscriptions, IConnectorsRepository connectorsRepository)
{
RemoveConnectorAssignedOfferSubscriptions(connectorId, connectorOfferSubscriptions, connectorsRepository);
connectorsRepository.DeleteConnector(connectorId);
await portalRepositories.SaveAsync();
}
private static void RemoveConnectorAssignedOfferSubscriptions(Guid connectorId, IEnumerable<ConnectorOfferSubscription> connectorOfferSubscriptions, IConnectorsRepository connectorsRepository)
{
var activeConnectorOfferSubscription = connectorOfferSubscriptions.Where(cos => cos.OfferSubscriptionStatus == OfferSubscriptionStatusId.ACTIVE)
.Select(cos => cos.AssignedOfferSubscriptionIds);
if (activeConnectorOfferSubscription.Any())
{
throw ForbiddenException.Create(AdministrationConnectorErrors.CONNECTOR_DELETION_FAILED_OFFER_SUBSCRIPTION, new ErrorParameter[] { new("connectorId", connectorId.ToString()), new("activeConnectorOfferSubscription", string.Join(",", activeConnectorOfferSubscription)) });
}
var assignedOfferSubscriptions = connectorOfferSubscriptions.Select(cos => cos.AssignedOfferSubscriptionIds);
if (assignedOfferSubscriptions.Any())
{
connectorsRepository.DeleteConnectorAssignedSubscriptions(connectorId, assignedOfferSubscriptions);
}
}
/// <inheritdoc/>
public IAsyncEnumerable<ConnectorEndPointData> GetCompanyConnectorEndPointAsync(IEnumerable<string>? bpns)
{
bpns ??= Enumerable.Empty<string>();
bpns.Where(bpn => !BpnRegex.IsMatch(bpn)).IfAny(invalid =>
throw ControllerArgumentException.Create(AdministrationConnectorErrors.CONNECTOR_ARGUMENT_INCORRECT_BPN, new ErrorParameter[] { new("bpns", string.Join(", ", invalid)) }));
return portalRepositories.GetInstance<IConnectorsRepository>()
.GetConnectorEndPointDataAsync(bpns.Select(x => x.ToUpper()))
.PreSortedGroupBy(data => data.BusinessPartnerNumber)
.Select(group =>
new ConnectorEndPointData(
group.Key,
group.Select(x => x.ConnectorEndpoint)));
}
/// <inheritdoc />
public async Task ProcessClearinghouseSelfDescription(SelfDescriptionResponseData data, CancellationToken cancellationToken)
{
logger.LogInformation("Process SelfDescription called with the following data {@Data}", data.ToString().Replace(Environment.NewLine, string.Empty));
var result = await portalRepositories.GetInstance<IConnectorsRepository>()
.GetConnectorDataById(data.ExternalId)
.ConfigureAwait(ConfigureAwaitOptions.None);
if (result == default)
{
throw NotFoundException.Create(AdministrationConnectorErrors.CONNECTOR_NOT_EXIST, new ErrorParameter[] { new("externalId", data.ExternalId.ToString()) });
}
if (result.SelfDescriptionDocumentId != null)
{
throw ConflictException.Create(AdministrationConnectorErrors.CONNECTOR_CONFLICT_ALREADY_ASSIGNED, new ErrorParameter[] { new("externalId", data.ExternalId.ToString()) });
}
await sdFactoryBusinessLogic.ProcessFinishSelfDescriptionLpForConnector(data, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}
/// <inheritdoc />
public Task UpdateConnectorUrl(Guid connectorId, ConnectorUpdateRequest data, CancellationToken cancellationToken)
{
data.ConnectorUrl.EnsureValidHttpUrl(() => nameof(data.ConnectorUrl));
return UpdateConnectorUrlInternal(connectorId, data, cancellationToken);
}
private async Task UpdateConnectorUrlInternal(Guid connectorId, ConnectorUpdateRequest data, CancellationToken cancellationToken)
{
var connectorsRepository = portalRepositories
.GetInstance<IConnectorsRepository>();
var documentRepository = portalRepositories
.GetInstance<IDocumentRepository>();
var connector = await connectorsRepository
.GetConnectorUpdateInformation(connectorId, _identityData.CompanyId)
.ConfigureAwait(ConfigureAwaitOptions.None);
if (connector == null)
{
throw NotFoundException.Create(AdministrationConnectorErrors.CONNECTOR_NOT_FOUND, new ErrorParameter[] { new("connectorId", connectorId.ToString()) });
}
if (connector.ConnectorUrl == data.ConnectorUrl)
{
return;
}
if (!connector.IsHostCompany)
{
throw ForbiddenException.Create(AdministrationConnectorErrors.CONNECTOR_NOT_HOST_COMPANY, new ErrorParameter[] { new("companyId", _identityData.CompanyId.ToString()) });
}
if (connector.Status == ConnectorStatusId.INACTIVE)
{
throw ConflictException.Create(AdministrationConnectorErrors.CONNECTOR_CONFLICT_INACTIVE_STATE, new ErrorParameter[] { new("connectorId", connectorId.ToString()), new("connectorStatusId", ConnectorStatusId.INACTIVE.ToString()) });
}
var bpn = connector.Type == ConnectorTypeId.CONNECTOR_AS_A_SERVICE
? connector.Bpn
: await portalRepositories.GetInstance<IUserRepository>()
.GetCompanyBpnForIamUserAsync(_identityData.IdentityId)
.ConfigureAwait(ConfigureAwaitOptions.None);
if (string.IsNullOrWhiteSpace(bpn))
{
throw ConflictException.Create(AdministrationConnectorErrors.CONNECTOR_CONFLICT_SET_BPN, new ErrorParameter[] { new("companyId", _identityData.CompanyId.ToString()) });
}
connectorsRepository.AttachAndModifyConnector(connectorId, null, con =>
{
con.ConnectorUrl = data.ConnectorUrl;
});
if (connector.SelfDescriptionDocumentId != null)
{
documentRepository.AttachAndModifyDocument(connector.SelfDescriptionDocumentId.Value, null, doc =>
{
doc.DocumentStatusId = DocumentStatusId.INACTIVE;
});
}
if (connector.SelfDescriptionCompanyDocumentId is null)
{
throw ConflictException.Create(AdministrationConnectorErrors.CONNECTOR_CONFLICT_NO_DESCRIPTION, [new("connectorId", connectorId.ToString())]);
}
var selfDescriptionDocumentUrl = $"{_settings.SelfDescriptionDocumentUrl}/{connector.SelfDescriptionCompanyDocumentId}";
await sdFactoryBusinessLogic
.RegisterConnectorAsync(connectorId, selfDescriptionDocumentUrl, bpn, cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}
/// <inheritdoc />
public IAsyncEnumerable<OfferSubscriptionConnectorData> GetConnectorOfferSubscriptionData(bool? connectorIdSet) =>
portalRepositories.GetInstance<IOfferSubscriptionsRepository>()
.GetConnectorOfferSubscriptionData(connectorIdSet, _identityData.CompanyId);
public Task<Pagination.Response<ConnectorMissingSdDocumentData>> GetConnectorsWithMissingSdDocument(int page, int size) =>
Pagination.CreateResponseAsync(
page,
size,
_settings.MaxPageSize,
portalRepositories.GetInstance<IConnectorsRepository>().GetConnectorsWithMissingSdDocument());
public async Task TriggerSelfDescriptionCreation()
{
var connectorRepository = portalRepositories.GetInstance<IConnectorsRepository>();
var processStepRepository = portalRepositories.GetInstance<IProcessStepRepository>();
var connectorIds = connectorRepository.GetConnectorIdsWithMissingSelfDescription();
await foreach (var connectorId in connectorIds)
{
var processId = processStepRepository.CreateProcess(ProcessTypeId.SELF_DESCRIPTION_CREATION).Id;
processStepRepository.CreateProcessStep(ProcessStepTypeId.SELF_DESCRIPTION_CONNECTOR_CREATION, ProcessStepStatusId.TODO, processId);
connectorRepository.AttachAndModifyConnector(connectorId, c => c.SdCreationProcessId = null, c => c.SdCreationProcessId = processId);
}
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}
public async Task RetriggerSelfDescriptionCreation(Guid processId)
{
const ProcessStepTypeId NextStep = ProcessStepTypeId.SELF_DESCRIPTION_CONNECTOR_CREATION;
const ProcessStepTypeId StepToTrigger = ProcessStepTypeId.RETRIGGER_SELF_DESCRIPTION_CONNECTOR_CREATION;
var (validProcessId, processData) = await portalRepositories.GetInstance<IProcessStepRepository>().IsValidProcess(processId, ProcessTypeId.SELF_DESCRIPTION_CREATION, Enumerable.Repeat(StepToTrigger, 1)).ConfigureAwait(ConfigureAwaitOptions.None);
if (!validProcessId)
{
throw new NotFoundException($"process {processId} does not exist");
}
var context = processData.CreateManualProcessData(StepToTrigger, portalRepositories, () => $"processId {processId}");
context.ScheduleProcessSteps(Enumerable.Repeat(NextStep, 1));
context.FinalizeProcessStep();
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}
}