From c8ecbc94a259b22fe6d325b78c1ce0338109ad44 Mon Sep 17 00:00:00 2001 From: Loretta Date: Wed, 10 Jun 2026 18:09:26 +0200 Subject: [PATCH] Add EkaerHistory SignalR endpoints and service injection Added IFruitBankEkaerService to FruitBankDataController and introduced two new SignalR methods: GenerateEkaerXmlDocument (generates/updates EkaerHistory for a shipping document) and CreateEkaerHistory (idempotently creates or returns EkaerHistory for a given key and direction). Both methods include detailed logging and entity validation. --- .../Controllers/FruitBankDataController.cs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs index e343c73..9a213bd 100644 --- a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs +++ b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs @@ -13,6 +13,7 @@ using FruitBank.Common.Loggers; using FruitBank.Common.Models; using FruitBank.Common.Server; using FruitBank.Common.Server.Interfaces; +using FruitBank.Common.Server.Services.Ekaer; using FruitBank.Common.SignalRs; using LinqToDB; using Mango.Nop.Core.Dtos; @@ -40,6 +41,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers ICustomerRegistrationService customerRegistrationService, ILocalizationService localizationService, PreOrderConversionService preorderConversionService, + IFruitBankEkaerService fruitBankEkaerService, IEnumerable logWriters) : BasePluginController, IFruitBankDataControllerServer { @@ -307,6 +309,55 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers return await ctx.EkaerHistories.GetByIdAsync(ekaerHistory.Id); } + [SignalR(SignalRTags.GenerateEkaerXmlDocument)] + public async Task GenerateEkaerXmlDocument(int shippingDocumentId) + { + _logger.Detail($"GenerateEkaerXmlDocument invoked; shippingDocumentId: {shippingDocumentId}"); + + // A GetAll(true) a mapperhez kellő teljes gráfot tölti: Partner, Items+ProductDto, Shipping→járművek/fuvarozó. + var shippingDocument = await ctx.ShippingDocuments.GetByIdAsync(shippingDocumentId, true) + ?? throw new ArgumentException($"ShippingDocument not found; id: {shippingDocumentId}", nameof(shippingDocumentId)); + + // Upsert: dokumentumonként EGY bejövő rekord (ForeignKey + IsOutgoing) — az újragenerálás nem duplikál. + var ekaerHistory = await ctx.EkaerHistories.GetByForeignKey(shippingDocumentId).FirstOrDefaultAsync(eh => !eh.IsOutgoing); + ekaerHistory = fruitBankEkaerService.GenerateEkaerXmlDocument(shippingDocument, ekaerHistory); + + if (ekaerHistory.Id > 0) await ctx.EkaerHistories.UpdateAsync(ekaerHistory); + else await ctx.EkaerHistories.InsertAsync(ekaerHistory); + + return await ctx.EkaerHistories.GetByIdAsync(ekaerHistory.Id) ?? ekaerHistory; + } + + /// + /// Idempotens rekord-létrehozás: ha a (foreignKey, isOutgoing) párra már van EkaerHistory, azt adja vissza + /// érintetlenül; különben új Pending rekordot hoz létre. A generálás (XmlDoc) külön lépés: GenerateEkaerXmlDocument. + /// + [SignalR(SignalRTags.CreateEkaerHistory)] + public async Task CreateEkaerHistory(int foreignKey, bool isOutgoing) + { + _logger.Detail($"CreateEkaerHistory invoked; foreignKey: {foreignKey}; isOutgoing: {isOutgoing}"); + + var existing = await ctx.EkaerHistories.GetByForeignKey(foreignKey).FirstOrDefaultAsync(eh => eh.IsOutgoing == isOutgoing); + if (existing != null) return existing; + + // A forrás-entitás léte irányfüggő: bejövő → ShippingDocument, kimenő → Order. + if (!isOutgoing) + { + _ = await ctx.ShippingDocuments.GetByIdAsync(foreignKey, false) + ?? throw new ArgumentException($"ShippingDocument not found; id: {foreignKey}", nameof(foreignKey)); + } + else + { + _ = await ctx.OrderDtos.GetByIdAsync(foreignKey, false) + ?? throw new ArgumentException($"Order not found; id: {foreignKey}", nameof(foreignKey)); + } + + var ekaerHistory = new EkaerHistory { ForeignKey = foreignKey, IsOutgoing = isOutgoing, Status = EkaerStatus.Pending }; + await ctx.EkaerHistories.InsertAsync(ekaerHistory); + + return await ctx.EkaerHistories.GetByIdAsync(ekaerHistory.Id) ?? ekaerHistory; + } + [SignalR(SignalRTags.GetShippings)] public async Task> GetShippings() {