-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUpsertSupplierHandler.cs
More file actions
121 lines (107 loc) · 5.15 KB
/
UpsertSupplierHandler.cs
File metadata and controls
121 lines (107 loc) · 5.15 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Unity.Payments.Domain.Suppliers;
using Unity.Payments.Events;
using Unity.Payments.Suppliers;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus;
using Volo.Abp.EventBus.Local;
namespace Unity.Payments.Handlers
{
public class UpsertSupplierHandler(ISupplierAppService supplierAppService,
SiteAppService siteAppService,
ILogger<UpsertSupplierHandler> logger,
ILocalEventBus localEventBus) : ILocalEventHandler<UpsertSupplierEto>, ITransientDependency
{
public async Task HandleEventAsync(UpsertSupplierEto eventData)
{
SupplierDto supplierDto = await GetSupplierFromEvent(eventData);
var existingSites = await siteAppService.GetSitesBySupplierIdAsync(supplierDto.Id);
var existingSitesDictionary = existingSites?.ToDictionary(s => s.Number) ?? new Dictionary<string, Site>();
await UpsertSitesFromEventDtoAsync(existingSitesDictionary, supplierDto.Id, eventData);
// Send event notification to application module
await localEventBus.PublishAsync(
new ApplicantSupplierEto
{
SupplierId = supplierDto.Id,
ApplicantId = eventData.CorrelationId,
ExistingSitesDictionary = existingSitesDictionary,
SiteEtos = eventData.SiteEtos
}
);
}
private async Task<Dictionary<string, Site>> UpsertSitesFromEventDtoAsync(
Dictionary<string, Site> existingSitesDictionary,
Guid supplierId,
UpsertSupplierEto upsertSupplierEto)
{
foreach (var siteEto in upsertSupplierEto.SiteEtos)
{
var siteDto = supplierAppService.GetSiteDtoFromSiteEto(siteEto, supplierId);
if (existingSitesDictionary.TryGetValue(siteDto.Number, out var existingSite))
{
siteDto.Id = existingSite.Id;
await siteAppService.UpdateAsync(siteDto);
}
else
{
await siteAppService.InsertAsync(siteDto);
}
}
return existingSitesDictionary;
}
private async Task<SupplierDto> GetSupplierFromEvent(UpsertSupplierEto eventData)
{
var existing = await supplierAppService.GetBySupplierNumberAsync(eventData.Number);
logger.LogInformation("Upserting supplier from event data: {Existing}", existing);
// This is subject to some business rules and a domain implementation
if (existing != null)
{
existing.Number = eventData.Number;
UpdateSupplierDto updateSupplierDto = GetUpdateSupplierDtoFromEvent(eventData);
SupplierDto updatedSupplierDto = await supplierAppService.UpdateAsync(existing.Id, updateSupplierDto);
return updatedSupplierDto;
}
CreateSupplierDto createSupplierDto = GetCreateSupplierDtoFromEvent(eventData);
SupplierDto supplierDto = await supplierAppService.CreateAsync(createSupplierDto);
return supplierDto;
}
private static UpdateSupplierDto GetUpdateSupplierDtoFromEvent(UpsertSupplierEto eventData)
{
return new UpdateSupplierDto()
{
Name = eventData.Name,
Number = eventData.Number,
Subcategory = eventData.Subcategory,
ProviderId = eventData.ProviderId,
BusinessNumber = eventData.BusinessNumber,
Status = eventData.Status,
SupplierProtected = eventData.SupplierProtected,
StandardIndustryClassification = eventData.StandardIndustryClassification,
LastUpdatedInCAS = eventData.LastUpdatedInCAS,
CorrelationId = eventData.CorrelationId,
CorrelationProvider = eventData.CorrelationProvider,
};
}
private static CreateSupplierDto GetCreateSupplierDtoFromEvent(UpsertSupplierEto eventData)
{
return new CreateSupplierDto()
{
Name = eventData.Name,
Number = eventData.Number,
Subcategory = eventData.Subcategory,
ProviderId = eventData.ProviderId,
BusinessNumber = eventData.BusinessNumber,
Status = eventData.Status,
SupplierProtected = eventData.SupplierProtected,
StandardIndustryClassification = eventData.StandardIndustryClassification,
LastUpdatedInCAS = eventData.LastUpdatedInCAS,
CorrelationId = eventData.CorrelationId,
CorrelationProvider = eventData.CorrelationProvider,
};
}
}
}