809 lines
34 KiB
C#
809 lines
34 KiB
C#
using System.Linq.Expressions;
|
||
using System.Text.Json;
|
||
using AyCode.Core.Loggers;
|
||
using AyCode.Services.SignalRs;
|
||
using AyCode.Utils.Extensions;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using Newtonsoft.Json.Serialization;
|
||
using TIAM.Core.Enums;
|
||
using TIAM.Database.DataLayers.Admins;
|
||
using TIAM.Entities.Emails;
|
||
using TIAM.Entities.Transfers;
|
||
using TIAM.Services;
|
||
using TIAM.Services.Server;
|
||
using TIAMWebApp.Shared.Application.Models;
|
||
using TIAMWebApp.Shared.Application.Models.ClientSide.Messages;
|
||
using TIAMWebApp.Server.Services;
|
||
using TIAMWebApp.Shared.Application.Models.ClientSide;
|
||
using AyCode.Core.Extensions;
|
||
using TIAM.Entities.Users;
|
||
using TIAMSharedUI.Shared.Components.Grids;
|
||
using AyCode.Core.Helpers;
|
||
using System.Linq;
|
||
using DevExpress.Data.Filtering;
|
||
using DevExpress.Data.Linq;
|
||
using DevExpress.Data.Linq.Helpers;
|
||
using System;
|
||
using TIAM.Database.DbSets.Transfers;
|
||
using TIAM.Services.Interfaces;
|
||
using TIAM.Entities.Products;
|
||
using AyCode.Core.Enums;
|
||
using TIAM.Core.Consts;
|
||
using System.Security.Cryptography.Xml;
|
||
using TIAM.Models;
|
||
using TIAM.Models.Dtos.Users;
|
||
using TIAM.Models.PageViewModels;
|
||
|
||
namespace TIAMWebApp.Server.Controllers
|
||
{
|
||
[ApiController]
|
||
[Route("api/v1/[controller]")]
|
||
public class TransferDataAPIController : ControllerBase, ITransferApiControllerCommon
|
||
{
|
||
private readonly AdminDal _adminDal;
|
||
private readonly TIAM.Core.Loggers.ILogger _logger;
|
||
private readonly TransferBackendService _transferBackendService;
|
||
private readonly IMessageSenderService _messageSenderService;
|
||
private readonly AuthService _authService;
|
||
private readonly UserAPIController _userApiController;
|
||
|
||
public TransferDataAPIController(AdminDal adminDal, TransferBackendService transferBackendService, IMessageSenderService messageSenderService, IEnumerable<IAcLogWriterBase> logWriters, AuthService authService, UserAPIController userApiController)
|
||
{
|
||
_adminDal = adminDal;
|
||
_transferBackendService = transferBackendService;
|
||
|
||
_logger = new TIAM.Core.Loggers.Logger<TransferDataAPIController>(logWriters.ToArray());
|
||
_messageSenderService = messageSenderService;
|
||
_authService = authService;
|
||
_userApiController = userApiController;
|
||
}
|
||
|
||
//TODO: ezt meg a Messages-eket <20>tbuher<65>lni a UserDal-ra... - J.
|
||
[NonAction]
|
||
[SignalR(SignalRTags.GetSiteViewModelByUserId)]
|
||
public async Task<SiteViewModel> GetSiteViewModelByUserId(Guid userId)
|
||
{
|
||
_logger.Debug("$GetSiteViewModelByUserId called; userId: {userId}");
|
||
|
||
return await _adminDal.GetSiteViewModelByUserId(userId);
|
||
}
|
||
|
||
|
||
[AllowAnonymous]
|
||
[HttpGet]
|
||
[Route(APIUrls.GetTransferDestinationsRouteName)]
|
||
[SignalR(SignalRTags.GetAllTransferDestinations)]
|
||
public List<TransferDestination> GetTransferDestinations()
|
||
{
|
||
return _adminDal.GetTransferDestinations();
|
||
}
|
||
|
||
//[Authorize]
|
||
//[HttpGet]
|
||
//[Route(APIUrls.GetTransferDriversByTransferIdRouteName)]
|
||
[NonAction]
|
||
[SignalR(SignalRTags.GetTransferDestinationById)]
|
||
public async Task<TransferDestination?> GetTransferDestinationById(Guid transferDestinationId)
|
||
{
|
||
var transferDestination = await _adminDal.GetTransferDestinationByIdAsync(transferDestinationId);
|
||
return transferDestination;
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpGet]
|
||
[Route("GetTransferDestinationByCoordinates")]
|
||
public async Task<TransferDestination?> GetTransferDestinationByCoordinates(double latitude, double longitude)
|
||
{
|
||
return null;// await _transferDestinationDal.Context.TransferDestinations.FirstOrDefaultAsync(x => x.Latitude == latitude && x.Longitude == longitude);
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpGet]
|
||
[Route("GetTransferDestinationByAddress")]
|
||
public async Task<TransferDestination?> GetTransferDestinationByAddress(string address)
|
||
{
|
||
return null;//await _transferDestinationDal.Context.TransferDestinations.FirstOrDefaultAsync(x => x.Address == address);
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
[Route(APIUrls.CreateTransferDestinationRouteName)]
|
||
[SignalR(SignalRTags.CreateTransferDestination)]
|
||
public async Task<TransferDestination?> CreateTransferDestination([FromBody] TransferDestination transferDestination)
|
||
{
|
||
_logger.Info(@"CreateTransferDestination called!");
|
||
|
||
var isSuccess = false;
|
||
if (string.IsNullOrEmpty(transferDestination.Name) || string.IsNullOrEmpty(transferDestination.AddressString))
|
||
{
|
||
var logText = $"string.IsNullOrEmpty(transferDestination.Name) || string.IsNullOrEmpty(transferDestination.AddressString); Name: {transferDestination.Name}; AddressString: {transferDestination.AddressString}";
|
||
|
||
_logger.Error(logText);
|
||
}
|
||
else
|
||
{
|
||
if (transferDestination.Id.IsNullOrEmpty()) transferDestination.Id = Guid.NewGuid();
|
||
isSuccess = await _adminDal.AddTransferDestinationAsync(transferDestination);
|
||
}
|
||
|
||
return isSuccess ? transferDestination : null;
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
[Route(APIUrls.UpdateTransferDestinationRouteName)]
|
||
[SignalR(SignalRTags.UpdateTransferDestination)]
|
||
public async Task<TransferDestination?> UpdateTransferDestination([FromBody] TransferDestination transferDestination)
|
||
{
|
||
_logger.Info(@"UpdateTransferDestination called!");
|
||
|
||
var isSuccess = false;
|
||
if (transferDestination.Id.IsNullOrEmpty() || string.IsNullOrEmpty(transferDestination.Name) || string.IsNullOrEmpty(transferDestination.AddressString))
|
||
{
|
||
var logText = $"transferDestination.Id.IsNullOrEmpty() || string.IsNullOrEmpty(transferDestination.Name) || string.IsNullOrEmpty(transferDestination.AddressString); Id: {transferDestination.Id}; Name: {transferDestination.Name}; AddressString: {transferDestination.AddressString}";
|
||
|
||
_logger.Error(logText);
|
||
}
|
||
else isSuccess = await _adminDal.UpdateTransferDestinationAsync(transferDestination);
|
||
|
||
return isSuccess ? transferDestination : null;
|
||
|
||
}
|
||
|
||
//[Authorize]
|
||
//[HttpGet]
|
||
//[Route(APIUrls.GetTransferDriversByTransferIdRouteName)]
|
||
[NonAction]
|
||
[SignalR(SignalRTags.RemoveTransferDestination)]
|
||
public async Task<TransferDestination?> RemoveTransferDestination([FromBody] TransferDestination transferDestination)
|
||
{
|
||
var result = await _adminDal.RemoveTransferDestinationAsync(transferDestination, true);
|
||
return result ? transferDestination : null;
|
||
}
|
||
|
||
|
||
|
||
//[AllowAnonymous]
|
||
//[HttpPost]
|
||
//[Route(APIUrls.CreateTransferDestinationRouteName)]
|
||
//public async Task<IActionResult> CreateTransferDestination([FromBody] JsonElement serializedTransferDestinationModel)
|
||
//{
|
||
// _logger.Info(@"CreateTransferDestination called!");
|
||
// if (string.IsNullOrEmpty(serializedTransferDestinationModel.GetRawText()))
|
||
// {
|
||
// return BadRequest("SerializedTramsferDestinationWizardModel is required");
|
||
// }
|
||
// else
|
||
// {
|
||
// TransferDestination? transferDestination = JObject.Parse(serializedTransferDestinationModel.GetRawText()).ToObject<TransferDestination>();
|
||
|
||
// if (transferDestination != null)
|
||
// {
|
||
|
||
// var id = Guid.NewGuid();
|
||
// //TransferDestination transferDestination = new TransferDestination(id, transferDestinationModel.Name, transferDestinationModel.Description, transferDestinationModel.AddressString);
|
||
|
||
|
||
// if (string.IsNullOrEmpty(transferDestination.Name) || string.IsNullOrEmpty(transferDestination.AddressString))
|
||
// {
|
||
// return BadRequest("Invalid request");
|
||
// }
|
||
// else
|
||
// {
|
||
// _logger.Debug(transferDestination.ToString());
|
||
|
||
// //await _transferDestinationDal.Context.TransferDestinations.AddAsync(transferDestination);
|
||
// await _adminDal.AddTransferDestinationAsync(transferDestination);
|
||
// return Ok(transferDestination);
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// return BadRequest("Invalid request");
|
||
// }
|
||
|
||
|
||
// }
|
||
|
||
|
||
//}
|
||
|
||
//[AllowAnonymous]
|
||
//[HttpPost]
|
||
//[Route(APIUrls.UpdateTransferDestinationRouteName)]
|
||
//public async Task<IActionResult> UpdateTransferDestination([FromBody] JsonElement serializedTransferDestination)
|
||
//{
|
||
// _logger.Info(@"UpdateTransferDestination called!");
|
||
// if (string.IsNullOrEmpty(serializedTransferDestination.GetRawText()))
|
||
// {
|
||
// _logger.Error(@"Bad request!");
|
||
// return BadRequest("SerializedTramsferDestinationWizardModel is required");
|
||
// }
|
||
// else
|
||
// {
|
||
// _logger.Info(@"Serialized model not empty!");
|
||
// TransferDestination? transferDestination = JObject.Parse(serializedTransferDestination.GetRawText()).ToObject<TransferDestination>();
|
||
// _logger.Info($@"TransferDestination to be updated: {serializedTransferDestination.GetRawText()}");
|
||
// _logger.Info($@"TransferDestination to be updated: {transferDestination.AddressString}");
|
||
|
||
|
||
// if (transferDestination != null)
|
||
// {
|
||
|
||
|
||
// //TransferDestination transferDestination = new TransferDestination(id, transferDestinationModel.Name, transferDestinationModel.Description, transferDestinationModel.AddressString);
|
||
|
||
// if (transferDestination.Id == Guid.Empty || string.IsNullOrEmpty(transferDestination.Name) || string.IsNullOrEmpty(transferDestination.AddressString))
|
||
// {
|
||
// _logger.Error(@"Serialized model not empty, but bad request!");
|
||
// return BadRequest("Invalid request");
|
||
// }
|
||
// else
|
||
// {
|
||
|
||
// _logger.Info($@"TransferDestination to be updated: {transferDestination.Id}");
|
||
// _logger.Info($@"TransferDestination to be updated new name: {transferDestination.Name}");
|
||
// _logger.Info($@"TransferDestination to be updated new price: {transferDestination.Price}");
|
||
// //Console.WriteLine($"TransferDestination to be updated new price: {transferDestination.Price2}");
|
||
// //Console.WriteLine($"TransferDestination to be updated new price: {transferDestination.Price3}");
|
||
// //Console.WriteLine($"TransferDestination to be updated new priceType: {transferDestination.PriceType}");
|
||
// _logger.Info($@"TransferDestination to be updated new address: {transferDestination.AddressString}");
|
||
// _logger.Info($@"TransferDestination to be updated new description: {transferDestination.Description}");
|
||
|
||
// //var dbTransferDestinationJson = _adminDal.GetTransferDestinationJsonById(transferDestination.Id);
|
||
// //_logger.Info($"TransferDestination JSON to be updated: {dbTransferDestinationJson}");
|
||
|
||
// //var dbTransferDestination = JObject.Parse(dbTransferDestinationJson).ToObject<TransferDestination>();
|
||
|
||
// //var dbTransferDestination = _adminDal.GetTransferDestinationById(transferDestination.Id, true);
|
||
// //if (dbTransferDestination.Id != Guid.Empty)
|
||
// //{
|
||
// // dbTransferDestination.AddressId = transferDestination.AddressId;
|
||
// // dbTransferDestination.Price = transferDestination.Price;
|
||
// // dbTransferDestination.PriceType = transferDestination.PriceType;
|
||
// // dbTransferDestination.Name = transferDestination.Name;
|
||
// // dbTransferDestination.Description = transferDestination.Description;
|
||
// // dbTransferDestination.AddressString = transferDestination.AddressString;
|
||
// // dbTransferDestination.Address = transferDestination.Address;
|
||
|
||
// //}
|
||
|
||
// //await _transferDestinationDal.Context.TransferDestinations.AddAsync(transferDestination);
|
||
// await _adminDal.UpdateTransferDestinationAsync(transferDestination);
|
||
// return Ok(transferDestination);
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// return BadRequest("Invalid request");
|
||
// }
|
||
|
||
|
||
// }
|
||
|
||
|
||
//}
|
||
|
||
//TransferDestinationToProduct
|
||
|
||
[AllowAnonymous]
|
||
[HttpGet]
|
||
[Route(APIUrls.GetAllTransferDestinationToProductsRouteName)]
|
||
[SignalR(SignalRTags.GetAllTransferDestinationToProducts)]
|
||
public async Task<List<TransferDestinationToProduct>> GetAllTransferDestinationToProducts()
|
||
{
|
||
return await _adminDal.GetTransferDestinationToProducts();
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpGet]
|
||
[Route(APIUrls.GetTransferDestinationToProductsByProductIdRouteName)]
|
||
[SignalR(SignalRTags.GetTransferDestinationToProductsByProductId)]
|
||
public async Task<List<TransferDestinationToProduct>> GetTransferDestinationToProductsByProductId(Guid productId)
|
||
{
|
||
return await _adminDal.GetTransferDestinationToProductsByProductId(productId);
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpGet]
|
||
[Route(APIUrls.GetTransferDestinationToProductsByTransferDestinationIdRouteName)]
|
||
[SignalR(SignalRTags.GetTransferDestinationToProductsByTransferDestinationId)]
|
||
public async Task<List<TransferDestinationToProduct>> GetTransferDestinationToProductsByTransferDestinationId(Guid transferDestinationId)
|
||
{
|
||
return await _adminDal.GetTransferDestinationToProductsByTransferDestinationId(transferDestinationId);
|
||
}
|
||
|
||
//[Authorize]
|
||
//[HttpGet]
|
||
//[Route(APIUrls.GetTransferDestinationToProductById)]
|
||
[NonAction]
|
||
[SignalR(SignalRTags.GetTransferDestinationToProductById)]
|
||
public async Task<TransferDestinationToProduct?> GetTransferDestinationToProductById(Guid transferDestinationToProductId)
|
||
{
|
||
var transferDestination = await _adminDal.GetTransferDestinationToProductByIdAsync(transferDestinationToProductId);
|
||
return transferDestination;
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
[Route(APIUrls.CreateTransferDestinationToProductRouteName)]
|
||
[SignalR(SignalRTags.CreateTransferDestinationToProduct)]
|
||
public async Task<TransferDestinationToProduct?> CreateTransferDestinationToProduct([FromBody] TransferDestinationToProduct transferDestinationToProduct)
|
||
{
|
||
_logger.Info(@"CreateTransferDestination called!");
|
||
|
||
var isSuccess = false;
|
||
|
||
if (transferDestinationToProduct.ProductId.IsNullOrEmpty() || transferDestinationToProduct.TransferDestinationId.IsNullOrEmpty())
|
||
{
|
||
var logText = $"transferDestinationToProduct.ProductId.IsNullOrEmpty() || transferDestinationToProduct.TransferDestinationId.IsNullOrEmpty(); ProductId: {transferDestinationToProduct.ProductId}; TransferDestinationId: {transferDestinationToProduct.TransferDestinationId}";
|
||
|
||
_logger.Error(logText);
|
||
}
|
||
else
|
||
{
|
||
if (transferDestinationToProduct.Id.IsNullOrEmpty()) transferDestinationToProduct.Id = Guid.NewGuid();
|
||
isSuccess = await _adminDal.AddTransferDestinationToProductAsync(transferDestinationToProduct);
|
||
}
|
||
|
||
return isSuccess ? transferDestinationToProduct : null;
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
[Route(APIUrls.UpdateTransferDestinationToProductRouteName)]
|
||
[SignalR(SignalRTags.UpdateTransferDestinationToProduct)]
|
||
public async Task<TransferDestinationToProduct?> UpdateTransferDestinationToProduct([FromBody] TransferDestinationToProduct transferDestinationToProduct)
|
||
{
|
||
_logger.Info(@"UpdateTransferDestination called!");
|
||
|
||
var isSuccess = false;
|
||
if (transferDestinationToProduct.Id.IsNullOrEmpty() || transferDestinationToProduct.ProductId.IsNullOrEmpty() || transferDestinationToProduct.TransferDestinationId.IsNullOrEmpty())
|
||
{
|
||
var logText = $"transferDestinationToProduct.Id.IsNullOrEmpty() || transferDestinationToProduct.ProductId.IsNullOrEmpty() || transferDestinationToProduct.TransferDestinationId.IsNullOrEmpty(); Id: {transferDestinationToProduct.Id}; ProductId: {transferDestinationToProduct.ProductId}; TransferDestinationId: {transferDestinationToProduct.TransferDestinationId}";
|
||
|
||
_logger.Error(logText);
|
||
}
|
||
else isSuccess = await _adminDal.UpdateTransferDestinationToProductAsync(transferDestinationToProduct);
|
||
|
||
return isSuccess ? transferDestinationToProduct : null;
|
||
|
||
}
|
||
|
||
[Authorize]
|
||
[HttpGet]
|
||
[Route(APIUrls.RemoveTransferDestinationToProductRouteName)]
|
||
[SignalR(SignalRTags.RemoveTransferDestinationToProduct)]
|
||
public async Task<TransferDestinationToProduct?> RemoveTransferDestinationToProduct([FromBody] TransferDestinationToProduct transferDestinationToProduct)
|
||
{
|
||
var result = await _adminDal.RemoveTransferDestinationToProductAsync(transferDestinationToProduct);
|
||
return result ? transferDestinationToProduct : null;
|
||
}
|
||
|
||
//transfer
|
||
|
||
[NonAction]
|
||
[SignalR(SignalRTags.AddTransfer)]
|
||
public async Task<Transfer?> AddTransfer(Transfer transfer)
|
||
{
|
||
_logger.Info(@"CreateTransfer via SignalR called!");
|
||
|
||
if (transfer == null || string.IsNullOrEmpty(transfer.FromAddress) || string.IsNullOrEmpty(transfer.ToAddress))
|
||
{
|
||
return null;
|
||
}
|
||
|
||
await ProcessTransferAsync(transfer);
|
||
await SendTransferEmailAsync(transfer);
|
||
|
||
return transfer;
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
[Route(APIUrls.CreateTransferRouteName)]
|
||
public async Task<IActionResult> CreateTransfer([FromBody] JsonElement serializedTransferModel)
|
||
{
|
||
_logger.Info(@"CreateTransfer called!");
|
||
|
||
if (string.IsNullOrEmpty(serializedTransferModel.GetRawText()))
|
||
{
|
||
return BadRequest("SerializedTransferModel is required");
|
||
}
|
||
|
||
var transfer = DeserializeTransfer(serializedTransferModel);
|
||
if (transfer == null || string.IsNullOrEmpty(transfer.FromAddress) || string.IsNullOrEmpty(transfer.ToAddress))
|
||
{
|
||
return BadRequest("Invalid request");
|
||
}
|
||
|
||
await ProcessTransferAsync(transfer);
|
||
await SendTransferEmailAsync(transfer);
|
||
|
||
return Ok(transfer);
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
[Route(APIUrls.CreateTransfersRouteName)]
|
||
public async Task<IActionResult> CreateTransfers([FromBody] JsonElement serializedTransferList)
|
||
{
|
||
_logger.Info(@"CreateTransfers called!");
|
||
|
||
if (string.IsNullOrEmpty(serializedTransferList.GetRawText()))
|
||
{
|
||
return BadRequest("SerializedTransferList is required");
|
||
}
|
||
|
||
var transfers = DeserializeTransfers(serializedTransferList);
|
||
if (transfers == null || !transfers.Any())
|
||
{
|
||
return BadRequest("Invalid request");
|
||
}
|
||
|
||
var createdTransfers = new List<Transfer>();
|
||
foreach (var transfer in transfers)
|
||
{
|
||
await ProcessTransferAsync(transfer);
|
||
await SendTransferEmailAsync(transfer);
|
||
createdTransfers.Add(transfer);
|
||
}
|
||
|
||
return Ok(createdTransfers);
|
||
}
|
||
|
||
// Helper method to deserialize a single transfer
|
||
[NonAction]
|
||
private Transfer? DeserializeTransfer(JsonElement serializedTransferModel)
|
||
{
|
||
return JObject.Parse(serializedTransferModel.GetRawText()).ToObject<Transfer>();
|
||
}
|
||
|
||
// Helper method to deserialize a list of transfers
|
||
[NonAction]
|
||
private List<Transfer>? DeserializeTransfers(JsonElement serializedTransferList)
|
||
{
|
||
var settings = new JsonSerializerSettings
|
||
{
|
||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||
};
|
||
|
||
return JsonConvert.DeserializeObject<List<Transfer>>(serializedTransferList.GetRawText(), settings);
|
||
}
|
||
|
||
// Helper method to process a transfer
|
||
[NonAction]
|
||
private async Task ProcessTransferAsync(Transfer transfer)
|
||
{
|
||
var from = await _adminDal.Context.TransferDestinations.FirstOrDefaultAsync(x => x.AddressString == transfer.FromAddress);
|
||
var to = await _adminDal.Context.TransferDestinations.FirstOrDefaultAsync(x => x.AddressString == transfer.ToAddress);
|
||
|
||
//what if one is not transferdestination
|
||
if (to != null && from != null)
|
||
{
|
||
if (!transfer.ProductId.IsNullOrEmpty())
|
||
{
|
||
var product = await _adminDal.GetProductByIdAsync((Guid)transfer.ProductId);
|
||
transfer.Price = _transferBackendService.GetTransferPrice(transfer.ProductId.Value, from, to, transfer.PassengerCount);
|
||
|
||
if (transfer.Price.HasValue && transfer.Price > 0)
|
||
{
|
||
transfer.Revenue = _transferBackendService.GetCommission((Guid)transfer.ProductId, (double)transfer.Price, to);
|
||
}
|
||
else
|
||
{
|
||
transfer.Revenue = 0;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
transfer.Price = 0;
|
||
transfer.Revenue = 0;
|
||
}
|
||
|
||
|
||
transfer.TransferStatusType = TransferStatusType.OrderSubmitted;
|
||
await _adminDal.AddTransferAsync(transfer);
|
||
}
|
||
|
||
// Helper method to find or create user
|
||
[NonAction]
|
||
private async Task FindOrCreateUser(string Email)
|
||
{
|
||
|
||
}
|
||
|
||
// Helper method to send an email for the transfer
|
||
[NonAction]
|
||
private async Task SendTransferEmailAsync(Transfer transfer)
|
||
{
|
||
_logger.Info($"Created transfer, sending email...");
|
||
|
||
var message = new MessageSenderModel<EmailMessage>
|
||
{
|
||
Message = new EmailMessage
|
||
{
|
||
Id = Guid.NewGuid(),
|
||
EmailAddress = TiamConstClient.SystemEmailAddress,
|
||
Subject = $"[#{transfer.OrderId}][Tour I Am] New transfer in Budapest",
|
||
ContextId = transfer.Id,
|
||
ContextType = MessageContextType.Transfer,
|
||
SenderId = TiamConstClient.SysAccounts["SystemEmailSender"],
|
||
Text = EmailTemplateHelper.GenerateNewTransferEmail(
|
||
transfer.FullName,
|
||
transfer.FromAddress,
|
||
transfer.ToAddress,
|
||
transfer.Appointment.ToString(),
|
||
transfer.FullName,
|
||
transfer.PassengerCount.ToString(),
|
||
transfer.LuggageCount.ToString(),
|
||
Setting.BaseUrl,
|
||
transfer.Id.ToString())
|
||
},
|
||
MessageType = AyCode.Models.Enums.MessageTypesEnum.email
|
||
};
|
||
|
||
message.Message.Recipients.Add(new EmailRecipient(Guid.NewGuid(), transfer.UserId, message.Message.Id, transfer.ContactEmail));
|
||
|
||
foreach (var admin in TiamConstClient.SysAdmins)
|
||
{
|
||
var adminUser = _adminDal.GetUserById(admin, true);
|
||
message.Message.Recipients.Add(new EmailRecipient(Guid.NewGuid(), admin, message.Message.Id, adminUser.EmailAddress));
|
||
}
|
||
|
||
var result = await _messageSenderService.SendMessageAsync(message.Message, (int)message.MessageType);
|
||
await _adminDal.AddEmailMessageAsync(message.Message);
|
||
|
||
_logger.Info("SendEmail result: " + result);
|
||
}
|
||
|
||
[Authorize]
|
||
[HttpGet]
|
||
[Route(APIUrls.GetTransfersRouteName)]
|
||
[SignalR(SignalRTags.GetTransfers)]
|
||
public async Task<List<Transfer>> GetTransfers()
|
||
{
|
||
_logger.Debug($"GetTransfers called;");
|
||
//var token = _authService.GetAuthTokenFromRequest(Request);
|
||
//_logger.Detail(token);
|
||
|
||
var result = await _adminDal.GetTransfersAsync();
|
||
return result;
|
||
}
|
||
|
||
[NonAction]
|
||
[SignalR(SignalRTags.GetTransfersByFilterText)]
|
||
public async Task<List<Transfer>> GetTransfersByFilterText(string criteriaOperatorText)
|
||
{
|
||
_logger.Debug($"GetTransfersByFilterText called; criteriaOperatorText: {criteriaOperatorText}");
|
||
|
||
if (criteriaOperatorText.IsNullOrWhiteSpace()) return await GetTransfers();
|
||
|
||
var results = await _adminDal.GetTransfersByFilterAsync(CriteriaOperator.Parse(criteriaOperatorText));
|
||
return results;
|
||
}
|
||
|
||
[Authorize]
|
||
[HttpGet]
|
||
[Route(APIUrls.GetTransfersByUserIdRouteName)]
|
||
[SignalR(SignalRTags.GetTransfersByUserId)]
|
||
public async Task<List<Transfer>> GetTransfersByUserId(Guid userId)
|
||
{
|
||
_logger.Debug($"GetTransfersByUserId called; userId: {userId}");
|
||
|
||
var result = await _adminDal.GetTransfersByUserIdAsync(userId);
|
||
return result;
|
||
}
|
||
|
||
[NonAction]
|
||
[SignalR(SignalRTags.GetTransfersByOrderingProductId)]
|
||
public async Task<List<Transfer>> GetTransfersByProductId(Guid productId)
|
||
{
|
||
_logger.Debug($"GetTransfersByProductId called; productId: {productId}");
|
||
|
||
var result = await _adminDal.GetTransfersByProductIdAsync(productId);
|
||
return result;
|
||
}
|
||
|
||
[Authorize]
|
||
[HttpGet]
|
||
[Route(APIUrls.GetTransfersByUserProductMappingIdRouteName)]
|
||
[SignalR(SignalRTags.GetTransfersByUserProductMappingId)]
|
||
public async Task<string> GetTransfersByDriverId(Guid userProductMappingId)
|
||
{
|
||
_logger.Debug($"GetTransferByDriverId called; userProductMappingId: {userProductMappingId}");
|
||
|
||
var result = await _adminDal.GetTransfersByUserProductMappingIdAsync(userProductMappingId);
|
||
return result.ToJson();
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="driverId">UserProductMappingId</param>
|
||
/// <returns></returns>
|
||
[NonAction]
|
||
[SignalR(SignalRTags.GetDriverManageTransfersPageModelByDriverId)]
|
||
public async Task<DriverManageTransfersPageModel> GetDriverManageTransfersPageModelByDriverId(Guid driverId)
|
||
{
|
||
_logger.Debug($"GetTransferByDriverId called; userProductMappingId: {driverId}");
|
||
|
||
return await _adminDal.GetDriverManageTransfersPageModelByDriverId(driverId);
|
||
}
|
||
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
[Route(APIUrls.GetTransferByIdRouteName)]
|
||
[SignalR(SignalRTags.GetTransfer)]
|
||
public async Task<Transfer?> GetTransferById([FromBody] Guid transferId)
|
||
{
|
||
_logger.Debug($"GetTransferById called; transferId: {transferId}");
|
||
|
||
return await _adminDal.GetTransferByIdAsync(transferId);
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
[Route(APIUrls.UpdateTransferRouteName)]
|
||
[SignalR(SignalRTags.UpdateTransfer)]
|
||
public async Task<Transfer?> UpdateTransfer(Transfer transfer)
|
||
{
|
||
_logger.Info($"UpdateTransfer called! transferId: {transfer.Id}");
|
||
|
||
var to = await _adminDal.Context.TransferDestinations.FirstOrDefaultAsync(x => x.AddressString == transfer.ToAddress);
|
||
|
||
//check if revenue should be calculated
|
||
if (transfer.ProductId.HasValue && transfer.Price > 0)
|
||
{
|
||
if (transfer.Revenue == 0)
|
||
{
|
||
//price seems to have changed, update commission
|
||
if (to == null)
|
||
{
|
||
//get general product commission
|
||
var product = await _adminDal.Context.Products.FirstOrDefaultAsync(x => x.Id == transfer.ProductId);
|
||
if(product != null)
|
||
{
|
||
transfer.Revenue = transfer.Price * product.ServiceProvider.CommissionPercent;
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
transfer.Revenue = _transferBackendService.GetCommission((Guid)transfer.ProductId, (double)transfer.Price, to);
|
||
}
|
||
}
|
||
}
|
||
return await _adminDal.UpdateTransferAsync(transfer);
|
||
}
|
||
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
[Route(APIUrls.GuestUpdateTransferRouteName)]
|
||
[SignalR(SignalRTags.GuestUpdateTransfer)]
|
||
public async Task<Transfer?> GuestUpdateTransfer(Transfer transfer)
|
||
{
|
||
_logger.Info($"UpdateTransfer called! transferId: {transfer.Id}");
|
||
|
||
return await _adminDal.UpdateTransferAsync(transfer);
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
[Route(APIUrls.DriverUpdateTransferRouteName)]
|
||
[SignalR(SignalRTags.DriverUpdateTransfer)]
|
||
public async Task<Transfer?> DriverUpdateTransfer(Transfer transfer)
|
||
{
|
||
_logger.Info($"UpdateTransfer called! transferId: {transfer.Id}");
|
||
|
||
return await _adminDal.UpdateTransferAsync(transfer);
|
||
}
|
||
|
||
[NonAction]
|
||
[SignalR(SignalRTags.RemoveTransfer)]
|
||
public async Task<Transfer?> RemoveTransfer(Transfer transfer)
|
||
{
|
||
_logger.Info($"RemoveTransfer called! transferId: {transfer.Id}");
|
||
|
||
var result = await _adminDal.RemoveTransferAsync(transfer);
|
||
return result ? transfer : null;
|
||
}
|
||
|
||
//[Authorize]
|
||
//[HttpGet]
|
||
//[Route(APIUrls.GetTransferDriverRouteName)]
|
||
[NonAction]
|
||
[SignalR(SignalRTags.GetAllDrivers)]
|
||
public async Task<List<UserProductMapping>> GetAllDrivers()
|
||
{
|
||
_logger.Debug($"GetAllDrivers called!");
|
||
|
||
var result = await _adminDal.GetAllDriversAsync();
|
||
return result;
|
||
}
|
||
|
||
[NonAction]
|
||
//[Authorize]
|
||
//[HttpGet]
|
||
//[Route(APIUrls.GetTransferDriverRouteName)]
|
||
[SignalR(SignalRTags.GetAllDriversByProductId)]
|
||
public async Task<List<UserProductMapping>> GetAllDriversByProductId(Guid productId)
|
||
{
|
||
var result = await _adminDal.GetAllDriversByProductIdAsync(productId);
|
||
return result;
|
||
}
|
||
|
||
[Authorize]
|
||
[HttpGet]
|
||
[Route(APIUrls.GetTransferDriverRouteName)]
|
||
[SignalR(SignalRTags.GetTransferDriver)]
|
||
public async Task<TransferToDriver?> GetTransferDriver(Guid transferDriverId)
|
||
{
|
||
_logger.Debug($"GetTransferDriver called; transferDriverId: {transferDriverId}");
|
||
|
||
var result = await _adminDal.GetTransferToDriverByIdAsync(transferDriverId);
|
||
return result;
|
||
}
|
||
|
||
[Authorize]
|
||
[HttpGet]
|
||
[Route(APIUrls.GetTransferDriversByTransferIdRouteName)]
|
||
[SignalR(SignalRTags.GetTransferDriversByTransferId)]
|
||
public async Task<List<TransferToDriver>> GetTransferDrivers(Guid transferId)
|
||
{
|
||
_logger.Debug($"GetTransferDrivers called; transferId: {transferId}");
|
||
|
||
var result = await _adminDal.GetTransferToDriversByTransferIdAsync(transferId);
|
||
return result;
|
||
}
|
||
|
||
//[Authorize]
|
||
//[HttpGet]
|
||
//[Route(APIUrls.GetTransferDriversByTransferIdRouteName)]
|
||
[NonAction]
|
||
[SignalR(SignalRTags.AddTransferToDriver)]
|
||
public async Task<TransferToDriver?> AddTransferDriver([FromBody] TransferToDriver transferToDriver)
|
||
{
|
||
var result = await _adminDal.AddTransferToDriverAsync(transferToDriver);
|
||
//TODO: Send email to driver... - J.
|
||
|
||
//TODO: valami<6D>rt az AddTransferToDriverAsync nem t<>r vissza a Car-al <20>s a UserProductMapping-el! - J.
|
||
//return result ? transferToDriver : null;
|
||
return result ? await _adminDal.GetTransferToDriverByIdAsync(transferToDriver.Id) : null;
|
||
}
|
||
|
||
//[Authorize]
|
||
//[HttpGet]
|
||
//[Route(APIUrls.GetTransferDriversByTransferIdRouteName)]
|
||
[NonAction]
|
||
[SignalR(SignalRTags.UpdateTransferToDriver)]
|
||
public async Task<TransferToDriver?> UpdateTransferDriver([FromBody] TransferToDriver transferToDriver)
|
||
{
|
||
var result = await _adminDal.UpdateTransferToDriverAsync(transferToDriver);
|
||
//TODO: Send email to driver/user, ha van v<>ltoz<6F>s... - J.
|
||
|
||
return result; // ? transferToDriver : null;
|
||
}
|
||
|
||
//[Authorize]
|
||
//[HttpGet]
|
||
//[Route(APIUrls.GetTransferDriversByTransferIdRouteName)]
|
||
[NonAction]
|
||
[SignalR(SignalRTags.RemoveTransferToDriver)]
|
||
public async Task<TransferToDriver?> RemoveTransferDriver([FromBody] TransferToDriver transferToDriver)
|
||
{
|
||
var result = await _adminDal.RemoveTransferToDriverAsync(transferToDriver);
|
||
//TODO: Send email to driver/user... - J.
|
||
|
||
return result ? transferToDriver : null;
|
||
}
|
||
|
||
}
|
||
} |