using AyCode.Core.Loggers; using AyCode.Services.SignalRs; using DocumentFormat.OpenXml.Office2010.Excel; using FruitBank.Common.Dtos; using FruitBank.Common.Entities; using FruitBank.Common.Interfaces; using FruitBank.Common.Loggers; using FruitBank.Common.Models; using FruitBank.Common.Server; using FruitBank.Common.SignalRs; using LinqToDB; using Mango.Nop.Core.Dtos; using Mango.Nop.Core.Loggers; using Mango.Nop.Core.Models; using Nop.Core; using Nop.Core.Domain.Customers; using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; using Nop.Services.Customers; using Nop.Services.Localization; using Nop.Web.Framework.Controllers; using NUglify.Helpers; namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers { //https://linq2db.github.io/articles/sql/Join-Operators.html public class FruitBankDataController( FruitBankDbContext ctx, IWorkContext workContext, ICustomerService customerService, ICustomerRegistrationService customerRegistrationService, ILocalizationService localizationService, IEnumerable logWriters) : BasePluginController, IFruitBankDataControllerServer { private readonly ILogger _logger = new Logger(logWriters.ToArray()); [SignalR(SignalRTags.GetMeasuringModels)] public async Task> GetMeasuringModels() { throw new NotImplementedException("GetMeasuringModels"); } [SignalR(SignalRTags.GetPartners)] public async Task> GetPartners() { _logger.Detail($"GetPartners invoked"); return await ctx.Partners.GetAll().ToListAsync(); } [SignalR(SignalRTags.GetPartnerById)] public async Task GetPartnerById(int id) { _logger.Detail($"GetPartnerById invoked; id: {id}"); //var customers = await ctx.GetCustormersBySystemRoleName("Measuring").ToListAsync(); //_logger.Error($"COUNT: {customers.Count}"); return await ctx.Partners.GetByIdAsync(id); } [SignalR(SignalRTags.UpdatePartner)] public async Task UpdatePartner(Partner partner) { ArgumentNullException.ThrowIfNull(partner); _logger.Detail($"UpdatePartner invoked; id: {partner.Id}"); await ctx.Partners.UpdateAsync(partner); return await ctx.Partners.GetByIdAsync(partner.Id, partner.ShippingDocuments != null); } [SignalR(SignalRTags.GetShippings)] public async Task> GetShippings() { _logger.Detail($"GetShippings invoked"); return await ctx.Shippings.GetAll(true).ToListAsync(); //return await ctx.Shippings.Table.LoadWith(sd => sd.ShippingDocuments).ThenLoad(si => si.ShippingItems).ToListAsync(); } [SignalR(SignalRTags.GetNotMeasuredShippings)] public async Task> GetNotMeasuredShippings() { _logger.Detail($"GetNotMeasuredShippings invoked"); return await ctx.Shippings.GetAllNotMeasured(true).ToListAsync(); } [SignalR(SignalRTags.GetShippingById)] public async Task GetShippingById(int id) { _logger.Detail($"GetShippingById invoked; id: {id}"); return await ctx.Shippings.GetByIdAsync(id, true); } [SignalR(SignalRTags.UpdateShipping)] public async Task UpdateShipping(Shipping shipping) { ArgumentNullException.ThrowIfNull(shipping); _logger.Detail($"UpdateShipping invoked; id: {shipping.Id}"); await ctx.Shippings.UpdateAsync(shipping); return await ctx.Shippings.GetByIdAsync(shipping.Id, shipping.ShippingDocuments != null); } [SignalR(SignalRTags.GetShippingItems)] public async Task> GetShippingItems() { _logger.Detail($"GetShippingItems invoked"); return await ctx.ShippingItems.GetAll(true).ToListAsync(); } [SignalR(SignalRTags.GetShippingItemById)] public async Task GetShippingItemById(int id) { _logger.Detail($"GetShippingItemById invoked; id: {id}"); var shippingItem = await ctx.ShippingItems.GetByIdAsync(id, true); return shippingItem; } [SignalR(SignalRTags.UpdateShippingItem)] public async Task UpdateShippingItem(ShippingItem shippingItem) { ArgumentNullException.ThrowIfNull(shippingItem); _logger.Detail($"UpdateShippingItem invoked; id: {shippingItem.Id}"); if (!await ctx.UpdateShippingItemSafeAsync(shippingItem)) return null; return await ctx.ShippingItems.GetByIdAsync(shippingItem.Id, shippingItem.ShippingDocument != null); } [SignalR(SignalRTags.UpdateMeasuredShippingItem)] public async Task UpdateMeasuredShippingItem(ShippingItem shippingItem) { ArgumentNullException.ThrowIfNull(shippingItem); _logger.Detail($"UpdateMeasuredShippingItem invoked; id: {shippingItem.Id}"); if (!await ctx.UpdateMeasuredShippingItemSafeAsync(shippingItem)) return null; return await ctx.ShippingItems.GetByIdAsync(shippingItem.Id, shippingItem.ShippingDocument != null); } [SignalR(SignalRTags.GetShippingDocuments)] public async Task> GetShippingDocuments() { _logger.Detail($"GetShippingDocuments invoked"); return await ctx.ShippingDocuments.GetAll(true).ToListAsync(); } [SignalR(SignalRTags.GetShippingDocumentById)] public async Task GetShippingDocumentById(int id) { _logger.Detail($"GetShippingDocumentById invoked; id: {id}"); return await ctx.ShippingDocuments.GetByIdAsync(id, true); } [SignalR(SignalRTags.UpdateShippingDocument)] public async Task UpdateShippingDocument(ShippingDocument shippingDocument) { ArgumentNullException.ThrowIfNull(shippingDocument); _logger.Detail($"UpdateShippingDocument invoked; id: {shippingDocument.Id}"); await ctx.ShippingDocuments.UpdateAsync(shippingDocument); return await ctx.ShippingDocuments.GetByIdAsync(shippingDocument.Id, shippingDocument.Shipping != null || shippingDocument.Partner != null); } [SignalR(SignalRTags.GetMeasuringUsers)] public async Task> GetMeasuringUsers() { _logger.Detail($"GetMeasuringUsers invoked"); var customers = await ctx.GetCustomersBySystemRoleName(FruitBankConst.MeasuringRoleSystemName).Select(c => new CustomerDto(c)).ToListAsync(); return customers; //.ToModelDto(); } [SignalR(SignalRTags.GetCustomerRolesByCustomerId)] public async Task> GetCustomerRolesByCustomerId(int customerId) { _logger.Detail($"GetCustomerRolesByCustomerId invoked; customerId: {customerId}"); return await ctx.GetCustomerRolesByCustomerId(customerId).ToListAsync(); } [SignalR(SignalRTags.GetProductDtos)] public async Task> GetProductDtos() { _logger.Detail($"GetProductDtos invoked"); return await ctx.GetAllProductDtos(false).ToListAsync(); } [SignalR(SignalRTags.GetAllMeasuringProductDtos)] public async Task> GetAllMeasuringProductDtos() { _logger.Detail($"GetAllMeasuringProductDtos invoked"); return await ctx.GetAllMeasuringProductDtos(false).ToListAsync(); } [SignalR(SignalRTags.GetMeasuringProductDtoById)] public async Task GetMeasuringProductDtoById(int productId) { _logger.Detail($"GetMeasuringProductDtoById invoked; productId: {productId}"); return await ctx.GetMeasuringProductDtoByIdAsync(productId); } [SignalR(SignalRTags.AuthenticateUser)] public async Task LoginMeasuringUser(MgLoginModelRequest loginModelRequest) { var customerEmail = loginModelRequest?.Email; var customerPassword = loginModelRequest?.Password; _logger.Detail($"LoginMeasuringUser invoked; customerEmail; {customerEmail}"); var resultLoginModel = new MgLoginModelResponse(); if (!customerEmail.IsNullOrWhiteSpace() && !customerPassword.IsNullOrWhiteSpace()) { var loginResult = await customerRegistrationService.ValidateCustomerAsync(customerEmail, customerPassword); switch (loginResult) { case CustomerLoginResults.Successful: { var customer = await customerService.GetCustomerByEmailAsync(customerEmail); var isInMeasuringRole = await customerService.IsInCustomerRoleAsync(customer, FruitBankConst.MeasuringRoleSystemName); if (!isInMeasuringRole) { resultLoginModel.ErrorMessage = "Is not in MeauringRole!"; break; } //var actionResult = await customerRegistrationService.SignInCustomerAsync(customer, returnUrl, loginModel.RememberMe); //await _workContext.SetCurrentCustomerAsync(customer); //await _authenticationService.SignInAsync(customer, isPersist); ////raise event //await _eventPublisher.PublishAsync(new CustomerLoggedinEvent(customer)); resultLoginModel.CustomerDto = new CustomerDto(customer); //customer.ToModel(); break; } case CustomerLoginResults.CustomerNotExist: resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials.CustomerNotExist"); break; case CustomerLoginResults.Deleted: resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials.Deleted"); break; case CustomerLoginResults.NotActive: resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials.NotActive"); break; case CustomerLoginResults.NotRegistered: resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials.NotRegistered"); break; case CustomerLoginResults.LockedOut: resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials.LockedOut"); break; case CustomerLoginResults.WrongPassword: default: resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials"); break; } } else resultLoginModel.ErrorMessage = await localizationService.GetResourceAsync("Account.Login.WrongCredentials"); if (!resultLoginModel.ErrorMessage.IsNullOrWhiteSpace()) _logger.Error($"{resultLoginModel.ErrorMessage}; email: {customerEmail}"); return resultLoginModel; } } }