888 lines
41 KiB
C#
888 lines
41 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.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;
|
||
}
|
||
|
||
|
||
|
||
[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)
|
||
{
|
||
|
||
var id = Guid.NewGuid();
|
||
//TransferDestination transferDestination = new TransferDestination(id, transferDestinationModel.Name, transferDestinationModel.Description, transferDestinationModel.AddressString);
|
||
|
||
|
||
if (string.IsNullOrEmpty(transfer.FromAddress) || string.IsNullOrEmpty(transfer.ToAddress))
|
||
{
|
||
return null;
|
||
}
|
||
else
|
||
{
|
||
_logger.Info($@"TransferDestination to be created: {id}");
|
||
_logger.Info($@"TransferDestination to be created: {transfer.FromAddress}");
|
||
_logger.Info($@"TransferDestination to be created: {transfer.ToAddress}");
|
||
_logger.Info($@"TransferDestination to be created: {transfer.ProductId}");
|
||
_logger.Info($@"TransferDestination to be created: {transfer.Price}");
|
||
|
||
var destList = _adminDal.GetTransferDestinations();
|
||
var from = destList.FirstOrDefault(x => x.AddressString == transfer.FromAddress);
|
||
var to = destList.FirstOrDefault(x => x.AddressString == transfer.ToAddress);
|
||
|
||
////TODO
|
||
//if (!transfer.ProductId.IsNullOrEmpty())
|
||
// transfer.Price = _transferBackendService.GetTransferPrice(transfer.ProductId.Value, from, to, transfer.PassengerCount);
|
||
|
||
Product? product = null;
|
||
//TODO
|
||
if (!transfer.ProductId.IsNullOrEmpty())
|
||
{
|
||
product = await _adminDal.GetProductByIdAsync((Guid)transfer.ProductId);
|
||
transfer.Price = _transferBackendService.GetTransferPrice(transfer.ProductId.Value, from, to, transfer.PassengerCount);
|
||
}
|
||
|
||
transfer.TransferStatusType = TransferStatusType.OrderSubmitted;
|
||
if (transfer.Price != null && transfer.Price > 0 && product != null)
|
||
{
|
||
if (product.ServiceProvider.CommissionPercent != null)
|
||
{
|
||
transfer.Revenue = transfer.Price * product.ServiceProvider.CommissionPercent / 100;
|
||
}
|
||
else
|
||
{
|
||
transfer.Revenue = 0;
|
||
}
|
||
}
|
||
await _adminDal.AddTransferAsync(transfer);
|
||
|
||
|
||
_logger.Info($"Created transfer, send emailMessage!!!");
|
||
var message = new MessageSenderModel<EmailMessage>();
|
||
message.Message = new EmailMessage();
|
||
message.Message.Id = Guid.NewGuid();
|
||
message.Message.Subject = "[Tour I Am] New transfer in Budapest";
|
||
message.Message.ContextId = transfer.Id;
|
||
message.Message.SenderId = Guid.Empty;
|
||
message.Message.Recipients.Add(new EmailRecipient(Guid.NewGuid(), transfer.UserId, Guid.NewGuid(), transfer.ContactEmail));
|
||
|
||
string FormatEmailContent()
|
||
{
|
||
return $@"
|
||
<html>
|
||
<body>
|
||
<p>Dear {transfer.FullName},</p>
|
||
<p>We are pleased to inform you that a transfer order has been placed. Below are the details of the transfer:</p>
|
||
<p>{transfer.FromAddress} - {transfer.ToAddress}</p>
|
||
<p>{transfer.Appointment}</p>
|
||
<p>{transfer.FullName}</p>
|
||
<p>{transfer.PassengerCount}</p>
|
||
<p>Please confirm the transfer by clicking on the following link:</p>
|
||
<p><a href=""{Setting.BaseUrl}/mytransfers/{transfer.Id}"">Confirm Transfer</a></p>
|
||
<p>If you did not request this transfer, please disregard this email.</p>
|
||
<p>Thank you,<br/>Tour I Am team</p>
|
||
</body>
|
||
</html>";
|
||
}
|
||
message.Message.Text = FormatEmailContent();
|
||
_logger.Info(message.Message.Text);
|
||
//message.Message.Text = $"Dear {transfer.FullName}! /n We have received an order from you, please confirm the details here: https://www.touriam.com/mytransfer?{transfer.Id}";
|
||
var messageElement = message.Message;
|
||
var result = await _messageSenderService.SendMessageAsync(messageElement, (int)message.MessageType);
|
||
await _adminDal.AddEmailMessageAsync(messageElement);
|
||
_logger.Info("SendEmail result: " + result);
|
||
|
||
|
||
return transfer;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return null;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
[Route(APIUrls.CreateTransferRouteName)]
|
||
public async Task<IActionResult> CreateTransfer([FromBody] JsonElement serializedTransferModel)
|
||
{
|
||
_logger.Info(@"CreateTransfer called!");
|
||
if (string.IsNullOrEmpty(serializedTransferModel.GetRawText()))
|
||
{
|
||
return BadRequest("SerializedTramsferDestinationWizardModel is required");
|
||
}
|
||
else
|
||
{
|
||
Transfer? transfer = JObject.Parse(serializedTransferModel.GetRawText()).ToObject<Transfer>();
|
||
|
||
if (transfer != null)
|
||
{
|
||
|
||
var id = Guid.NewGuid();
|
||
//TransferDestination transferDestination = new TransferDestination(id, transferDestinationModel.Name, transferDestinationModel.Description, transferDestinationModel.AddressString);
|
||
|
||
|
||
if (string.IsNullOrEmpty(transfer.FromAddress) || string.IsNullOrEmpty(transfer.ToAddress))
|
||
{
|
||
return BadRequest("Invalid request");
|
||
}
|
||
else
|
||
{
|
||
_logger.Info($@"TransferDestination to be created: {id}");
|
||
_logger.Info($@"TransferDestination to be created: {transfer.FromAddress}");
|
||
_logger.Info($@"TransferDestination to be created: {transfer.ToAddress}");
|
||
_logger.Info($@"TransferDestination to be created: {transfer.ProductId}");
|
||
_logger.Info($@"TransferDestination to be created: {transfer.Price}");
|
||
|
||
var from = await _adminDal.Context.TransferDestinations.FirstOrDefaultAsync(x => x.AddressString == transfer.FromAddress);
|
||
var to = await _adminDal.Context.TransferDestinations.FirstOrDefaultAsync(x => x.AddressString == transfer.ToAddress);
|
||
Product? product = null;
|
||
//TODO
|
||
if (!transfer.ProductId.IsNullOrEmpty())
|
||
{
|
||
product = await _adminDal.GetProductByIdAsync((Guid)transfer.ProductId);
|
||
transfer.Price = _transferBackendService.GetTransferPrice(transfer.ProductId.Value, from, to, transfer.PassengerCount);
|
||
|
||
if (transfer.Price != null && transfer.Price > 0)
|
||
{
|
||
transfer.Revenue = _transferBackendService.GetCommission((Guid)transfer.ProductId, (double)transfer.Price, to);
|
||
}
|
||
else
|
||
{
|
||
transfer.Revenue = 0;
|
||
}
|
||
}
|
||
|
||
transfer.TransferStatusType = TransferStatusType.OrderSubmitted;
|
||
|
||
await _adminDal.AddTransferAsync(transfer);
|
||
|
||
|
||
_logger.Info($"Created transfer, send emailMessage!!!");
|
||
var message = new MessageSenderModel<EmailMessage>();
|
||
message.Message = new EmailMessage();
|
||
message.Message.Id = Guid.NewGuid();
|
||
message.Message.EmailAddress = TiamConstClient.SystemEmailAddress;
|
||
message.MessageType = AyCode.Models.Enums.MessageTypesEnum.email;
|
||
message.Message.Subject = $"[#{transfer.OrderId}][Tour I Am] New transfer in Budapest";
|
||
message.Message.ContextId = transfer.Id;
|
||
message.Message.ContextType = MessageContextType.Transfer;
|
||
message.Message.SenderId = TiamConstClient.SysAccounts["SystemEmailSender"];
|
||
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));
|
||
}
|
||
message.Message.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());
|
||
_logger.Info(message.Message.Text);
|
||
//message.Message.Text = $"Dear {transfer.FullName}! /n We have received an order from you, please confirm the details here: https://www.touriam.com/mytransfer?{transfer.Id}";
|
||
var messageElement = message.Message;
|
||
var result = await _messageSenderService.SendMessageAsync(messageElement, (int)message.MessageType);
|
||
await _adminDal.AddEmailMessageAsync(messageElement);
|
||
_logger.Info("SendEmail result: " + result);
|
||
|
||
|
||
return Ok(transfer);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return BadRequest("Invalid request");
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
[Route(APIUrls.CreateTransfersRouteName)]
|
||
public async Task<IActionResult> CreateTransfers([FromBody] JsonElement serializedTransferList)
|
||
{
|
||
_logger.Info(@"CreateTransfers called!");
|
||
if (string.IsNullOrEmpty(serializedTransferList.GetRawText()))
|
||
{
|
||
return BadRequest("SerializedTramsferDestinationWizardModel is required");
|
||
}
|
||
else
|
||
{
|
||
_logger.Info($@"Serialized model: {serializedTransferList.GetRawText()}");
|
||
|
||
var settings = new JsonSerializerSettings
|
||
{
|
||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||
};
|
||
|
||
List<Transfer>? transfers = JsonConvert.DeserializeObject<List<Transfer>>(serializedTransferList.GetRawText(), settings);
|
||
|
||
//List<Transfer>? transfers = JObject.Parse(serializedTransferModel.GetRawText()).ToObject<List<Transfer>>();
|
||
List<Transfer> createdTransfers = new List<Transfer>();
|
||
if (transfers != null)
|
||
{
|
||
foreach (var transfer in transfers)
|
||
{
|
||
var id = Guid.NewGuid();
|
||
var result = await _adminDal.AddTransferAsync(transfer);
|
||
if (result)
|
||
{
|
||
createdTransfers.Add(transfer);
|
||
}
|
||
}
|
||
|
||
foreach (var createdTransfer in createdTransfers)
|
||
{
|
||
_logger.Info($"Created transfer, send emailMessage!!!");
|
||
var message = new MessageSenderModel<EmailMessage>();
|
||
message.Message = new EmailMessage();
|
||
message.Message.Id = Guid.NewGuid();
|
||
message.Message.EmailAddress = TiamConstClient.SystemEmailAddress;
|
||
message.MessageType = AyCode.Models.Enums.MessageTypesEnum.email;
|
||
message.Message.Subject = $"[#{createdTransfer.OrderId}][Tour I Am] New transfer in Budapest";
|
||
message.Message.ContextId = createdTransfer.Id;
|
||
message.Message.ContextType = MessageContextType.Transfer;
|
||
message.Message.SenderId = TiamConstClient.SysAccounts["SystemEmailSender"];
|
||
message.Message.Recipients.Add(new EmailRecipient(Guid.NewGuid(), createdTransfer.UserId, Guid.NewGuid(), createdTransfer.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));
|
||
}
|
||
message.Message.Text = EmailTemplateHelper.GenerateNewTransferEmail(
|
||
createdTransfer.FullName,
|
||
createdTransfer.FromAddress,
|
||
createdTransfer.ToAddress,
|
||
createdTransfer.Appointment.ToString(),
|
||
createdTransfer.FullName,
|
||
createdTransfer.PassengerCount.ToString(),
|
||
createdTransfer.LuggageCount.ToString(),
|
||
Setting.BaseUrl,
|
||
createdTransfer.Id.ToString());
|
||
|
||
_logger.Info(message.Message.Text);
|
||
var messageElement = message.Message;
|
||
Console.WriteLine(message.Message);
|
||
var result = await _messageSenderService.SendMessageAsync(messageElement, (int)message.MessageType);
|
||
await _adminDal.AddEmailMessageAsync(messageElement);
|
||
_logger.Info("SendEmail result: " + result);
|
||
}
|
||
|
||
return Ok(createdTransfers);
|
||
}
|
||
else
|
||
{
|
||
return BadRequest("Invalid request");
|
||
}
|
||
}
|
||
}
|
||
|
||
[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}");
|
||
|
||
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;
|
||
}
|
||
|
||
}
|
||
} |