Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Agrega solicitud para crear direccion de cliente #72

Merged
merged 6 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions samples/Api.Sdk.ConsoleApp/JsonFactories/DireccionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Text.Json;
using Api.Core.Domain.Requests.Direcciones;
using ARSoftware.Contpaqi.Api.Common.Domain;
using ARSoftware.Contpaqi.Comercial.Sdk.Abstractions.Enums;
using ARSoftware.Contpaqi.Comercial.Sdk.Abstractions.Models;

namespace Api.Sdk.ConsoleApp.JsonFactories;

public sealed class DireccionFactory
{
public static CrearDireccionClienteRequest GetCrearDireccionClienteRequest()
{
return new CrearDireccionClienteRequest(
new CrearDireccionClienteRequestModel { CodigoCliente = "PRUEBA", Direccion = GetModeloPrueba() },
new CrearDireccionClienteRequestOptions());
}

public static CrearDireccionClienteResponse GetCrearDireccionClienteResponse()
{
return CrearDireccionClienteResponse.CreateInstance(GetModeloPrueba());
}

private static Direccion GetModeloPrueba()
{
return new Direccion
{
TipoCatalogo = TipoCatalogoDireccion.Clientes,
Tipo = TipoDireccion.Fiscal,
Calle = "Calle",
NumeroExterior = "1",
NumeroInterior = "1",
Colonia = "Colonia",
Ciudad = "Ciudad",
Estado = "Estado",
CodigoPostal = "123456",
Pais = "Mexico"
};
}

public static void CearJson(string directory)
{
Directory.CreateDirectory(directory);

JsonSerializerOptions options = FactoryExtensions.GetJsonSerializerOptions();

File.WriteAllText(Path.Combine(directory, $"{nameof(Direccion)}.json"), JsonSerializer.Serialize(GetModeloPrueba(), options));

File.WriteAllText(Path.Combine(directory, $"{nameof(CrearDireccionClienteRequest)}.json"),
JsonSerializer.Serialize<ContpaqiRequest>(GetCrearDireccionClienteRequest(), options));
File.WriteAllText(Path.Combine(directory, $"{nameof(CrearDireccionClienteResponse)}.json"),
JsonSerializer.Serialize<ContpaqiResponse>(GetCrearDireccionClienteResponse(), options));
}
}
7 changes: 5 additions & 2 deletions samples/Api.Sdk.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

const string baseDirectory = @"C:\AR Software\Contpaqi Comercial API\Requests";

Directory.Delete(baseDirectory, true);
if (Directory.Exists(baseDirectory)) Directory.Delete(baseDirectory, true);

Directory.CreateDirectory(baseDirectory);

AgenteFactory.CearJson(Path.Combine(baseDirectory, "Agentes"));
AlmacenFactory.CearJson(Path.Combine(baseDirectory, "Almacenes"));
Expand All @@ -16,5 +18,6 @@
CartaPorteFactory.CearJson(Path.Combine(baseDirectory, "CartaPorte"));
MovimientoFactory.CearJson(Path.Combine(baseDirectory, "Movimientos"));
ExistenciasFactory.CearJson(Path.Combine(baseDirectory, "Existencias"));
DireccionFactory.CearJson(Path.Combine(baseDirectory, "Direcciones"));

Console.WriteLine("Programa Fin");
Console.WriteLine("Programa Fin");
5 changes: 4 additions & 1 deletion src/Api.Core.Domain/Common/PolymorphicTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Api.Core.Domain.Requests;
using Api.Core.Domain.Requests.Direcciones;
using ARSoftware.Contpaqi.Api.Common.Domain;
using ARSoftware.Contpaqi.Api.Common.Responses;

Expand Down Expand Up @@ -43,6 +44,7 @@ public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions option
new JsonDerivedType(typeof(CrearAgenteRequest), nameof(CrearAgenteRequest)),
new JsonDerivedType(typeof(CrearAlmacenRequest), nameof(CrearAlmacenRequest)),
new JsonDerivedType(typeof(CrearClienteRequest), nameof(CrearClienteRequest)),
new JsonDerivedType(typeof(CrearDireccionClienteRequest), nameof(CrearDireccionClienteRequest)),
new JsonDerivedType(typeof(CrearDocumentoRequest), nameof(CrearDocumentoRequest)),
new JsonDerivedType(typeof(CrearFacturaRequest), nameof(CrearFacturaRequest)),
new JsonDerivedType(typeof(CrearProductoRequest), nameof(CrearProductoRequest)),
Expand Down Expand Up @@ -85,6 +87,7 @@ public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions option
new JsonDerivedType(typeof(CrearAgenteResponse), nameof(CrearAgenteResponse)),
new JsonDerivedType(typeof(CrearAlmacenResponse), nameof(CrearAlmacenResponse)),
new JsonDerivedType(typeof(CrearClienteResponse), nameof(CrearClienteResponse)),
new JsonDerivedType(typeof(CrearDireccionClienteResponse), nameof(CrearDireccionClienteResponse)),
new JsonDerivedType(typeof(CrearDocumentoResponse), nameof(CrearDocumentoResponse)),
new JsonDerivedType(typeof(CrearFacturaResponse), nameof(CrearFacturaResponse)),
new JsonDerivedType(typeof(CrearProductoResponse), nameof(CrearProductoResponse)),
Expand All @@ -101,4 +104,4 @@ public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions option

return jsonTypeInfo;
}
}
}
67 changes: 67 additions & 0 deletions src/Api.Core.Domain/Requests/Direcciones/CreaDireccionCliente.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using ARSoftware.Contpaqi.Api.Common.Domain;

namespace Api.Core.Domain.Requests.Direcciones;

/// <summary>
/// Solicitud para crear una dirección de un cliente.
/// </summary>
public sealed class CrearDireccionClienteRequest : ContpaqiRequest<CrearDireccionClienteRequestModel, CrearDireccionClienteRequestOptions,
CrearDireccionClienteResponse>
{
public CrearDireccionClienteRequest(CrearDireccionClienteRequestModel model, CrearDireccionClienteRequestOptions options) : base(model,
options)
{
}
}

/// <summary>
/// Modelo de la solicitud CreaDireccionClienteRequest.
/// </summary>
public sealed class CrearDireccionClienteRequestModel
{
/// <summary>
/// Código del cliente al que se le creara la dirección.
/// </summary>
public string CodigoCliente { get; set; }

/// <summary>
/// Direccion a crear.
/// </summary>
public Direccion Direccion { get; set; } = new();
}

/// <summary>
/// Opciones de la solicitud CreaDireccionClienteRequest.
/// </summary>
public sealed class CrearDireccionClienteRequestOptions : ILoadRelatedDataOptions
{
public bool CargarDatosExtra { get; set; }
}

/// <summary>
/// Respuesta de la solicitud CreaDireccionClienteRequest.
/// </summary>
public sealed class CrearDireccionClienteResponse : ContpaqiResponse<CrearDireccionClienteResponseModel>
{
public CrearDireccionClienteResponse(CrearDireccionClienteResponseModel model) : base(model)
{
}

public static CrearDireccionClienteResponse CreateInstance(Direccion direccion)
{
return new CrearDireccionClienteResponse(new CrearDireccionClienteResponseModel(direccion));
}
}

/// <summary>
/// Modelo de la respuesta CreaDireccionClienteResponse.
/// </summary>
public sealed class CrearDireccionClienteResponseModel
{
public CrearDireccionClienteResponseModel(Direccion direccion)
{
Direccion = direccion;
}

public Direccion Direccion { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Api.Sync.Core.Application.ContpaqiComercial.Interfaces;

public interface IDireccionRepository
{
Direccion? BuscarDireccionPorId(int id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Api.Core.Domain.Requests.Direcciones;
using Api.Sync.Core.Application.ContpaqiComercial.Interfaces;
using ARSoftware.Contpaqi.Comercial.Sdk.Extras.Extensions;
using ARSoftware.Contpaqi.Comercial.Sdk.Extras.Interfaces;
using MediatR;

namespace Api.Sync.Core.Application.Requests.Direcciones.CrearDireccionCliente;

public sealed class CrearDireccionClienteRequestHandler : IRequestHandler<CrearDireccionClienteRequest, CrearDireccionClienteResponse>
{
private readonly IDireccionRepository _direccionRepository;
private readonly IDireccionService _direccionService;
private readonly IContpaqiSdk _sdk;

public CrearDireccionClienteRequestHandler(IDireccionService direccionService, IDireccionRepository direccionRepository,
IContpaqiSdk sdk)
{
_direccionService = direccionService;
_direccionRepository = direccionRepository;
_sdk = sdk;
}

public Task<CrearDireccionClienteResponse> Handle(CrearDireccionClienteRequest request, CancellationToken cancellationToken)
{
// Todo: el buscar el cliente antes de crear la dirección es un parche para que no falle el sdk
_sdk.fBuscaCteProv(request.Model.CodigoCliente).ToResultadoSdk(_sdk).ThrowIfError();

// Todo: voler a validar en la siguiente version del SDK que se puedan crear direcciones de envio

int direccionId = _direccionService.Crear(request.Model.CodigoCliente, request.Model.Direccion);

Direccion direccion = _direccionRepository.BuscarDireccionPorId(direccionId) ?? throw new InvalidOperationException();

return Task.FromResult(CrearDireccionClienteResponse.CreateInstance(direccion));
}
}
3 changes: 2 additions & 1 deletion src/Api.Sync.Infrastructure/ConfigureServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ private static IServiceCollection AddContpaqiComercialServices(this IServiceColl
serviceCollection.AddTransient<IFolioDigitalRepository, FolioDigitalRepository>();
serviceCollection.AddTransient<IMovimientoRepository, MovimientoRepository>();
serviceCollection.AddTransient<IProductoRepository, ProductoRepository>();
serviceCollection.AddTransient<IDireccionRepository, DireccionRepository>();

return serviceCollection;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Api.Sync.Core.Application.ContpaqiComercial.Interfaces;
using ARSoftware.Contpaqi.Comercial.Sdk.Abstractions.Dtos;
using ARSoftware.Contpaqi.Comercial.Sdk.Abstractions.Repositories;
using AutoMapper;

namespace Api.Sync.Infrastructure.ContpaqiComercial;

public sealed class DireccionRepository : IDireccionRepository
{
private readonly IDireccionRepository<DireccionDto> _direccionRepository;
private readonly IMapper _mapper;

public DireccionRepository(IDireccionRepository<DireccionDto> direccionRepository, IMapper mapper)
{
_direccionRepository = direccionRepository;
_mapper = mapper;
}

public Direccion? BuscarDireccionPorId(int id)
{
DireccionDto? direccionDto = _direccionRepository.BuscarPorId(id);

if (direccionDto is null) return null;

return _mapper.Map<Direccion>(direccionDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"ApiSyncConfig": {
"SubscriptionKey": "00000000000000000000000000000000",
"BaseAddress": "https://contpaqiapim.azure-api.net/comercial/",
"BaseAddress": "https://localhost:7082/",
"ShutdownTime": "20:00:00",
"Empresas": [ "URE180429TM6" ]
},
Expand Down