implementado hasta el ultimo paso antes de hacer la tercera notificacion
This commit is contained in:
@@ -1,15 +1,141 @@
|
||||
using AlquilaFacil.Builder;
|
||||
using Entidades;
|
||||
using Entidades.Dto;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Modelo;
|
||||
namespace AlquilaFacil.Controllers;
|
||||
|
||||
[ApiController]
|
||||
public class ContratoController: ControllerBase {
|
||||
|
||||
[HttpGet("api/contratos")]
|
||||
[HttpGet("api/contratos")] //WIP
|
||||
public IActionResult ObtenerContratosPorUsuario([FromHeader(Name="Auth")]string Auth) {
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("api/contratos/precontrato")]
|
||||
public IActionResult IniciarPrecontrato([FromHeader(Name = "Auth")]string Auth, [FromBody] PrecontratoDto dto) {
|
||||
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
||||
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
||||
if (validacion1 == false) return Unauthorized();
|
||||
|
||||
string validacion2 = ValidarDtoPrecontrato(dto);
|
||||
if (validacion2 != "") return BadRequest(new {message = validacion2});
|
||||
|
||||
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
||||
if (cli == null) return BadRequest(new {message = "Tu token no corresponde a ningun cliente (volvete a logear)"});
|
||||
if (cli.Email != dto.EmailPropietario) return BadRequest(new {message = "No Corresponde el email de propietario con el del token"});
|
||||
|
||||
Cliente? propi = RepositorioPropietario.Singleton.ObtenerPropietarioPorEmail(dto.EmailPropietario);
|
||||
if (propi == null || propi.Dni == 0) return BadRequest(new {message = "No hay propietario por ese email"});
|
||||
|
||||
Cliente? inq = RepositorioInquilinos.Singleton.ObtenerInquilinoPorEmail(dto.EmailInquilino);
|
||||
if (inq == null || inq.Dni == 0) return BadRequest(new {message = "No hay inquilinos por ese email"});
|
||||
|
||||
Propiedade? p = RepositorioPropiedades.Singleton.ObtenerPropiedadPorId(dto.IdPropiedad);
|
||||
if (p == null || p.Id == 0) return BadRequest(new {message = "La id de propiedad no corresponde a una propiedad"});
|
||||
|
||||
var precontrato = new PrecontratoBuilder()
|
||||
.SetHabilitado()
|
||||
.SetPropietario(propi.Dni)
|
||||
.SetInquilino(inq.Dni)
|
||||
.SetCantidadGarantes(dto.CantidadGarantes)
|
||||
.SetIndiceActializacionInicial()
|
||||
.SetMesesHastaAumento(dto.MesesHastaAumento)
|
||||
.SetPropiedad(p.Id)
|
||||
.SetFecha(dto.fechaprimernotificacion)
|
||||
.Build();
|
||||
|
||||
var notificacion = new NotificacioneBuilder()
|
||||
.SetAccion("Carge Garantes")
|
||||
.SetDniremitente(propi.Dni)
|
||||
.SetDnicliente(inq.Dni)
|
||||
.SetLeido(false)
|
||||
.SetFecha(DateTime.Now)
|
||||
.SetIdpropiedad(p.Id)
|
||||
.SetMensaje($"El propietario {propi.Nombre} {propi.Apellido} te requiere que carges informacion de {dto.CantidadGarantes} Garantes")
|
||||
.Build();
|
||||
|
||||
var ret = RepositorioContratos.Singleton.CargaPrecontrato(precontrato, notificacion);
|
||||
|
||||
return (ret)?
|
||||
Ok(new {message = "Se Cargo el precontrato y envio una notificacion al inquilino"}):
|
||||
BadRequest(new {message = "No se pudo cargar el precontrato"});
|
||||
}
|
||||
|
||||
|
||||
[HttpPut("api/contratos/addGarantes")]
|
||||
public IActionResult AddGarantes([FromHeader(Name = "Auth")]string Auth, AltaGarantesDto dto) {
|
||||
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest("");
|
||||
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino");
|
||||
if (validacion1 == false) return Unauthorized();
|
||||
|
||||
var validacion2 = ValidarDtoAltaGarantes(dto);
|
||||
if (validacion2 != "") return BadRequest(new {message = validacion2});
|
||||
|
||||
var validacion4 = RepositorioContratos.Singleton.CantidadGarantesEncontrato(dto.EmailInquilino, dto.Idpropiedad);
|
||||
if (validacion4 <= 0 || dto.garantes.Count()!=validacion4) return BadRequest(new{message="Cantidad de garantes incorrecta"});
|
||||
|
||||
foreach (var i in dto.garantes) {
|
||||
string validacion3 = ValidarDtoGarante(i);
|
||||
if (validacion3 != "") return BadRequest( new { message = validacion3 });
|
||||
}
|
||||
|
||||
List<Garante> gar = new();
|
||||
|
||||
foreach (var i in dto.garantes) {
|
||||
Garante g = new GaranteBuilder()
|
||||
.SetNombre(i.Nombre)
|
||||
.SetApellido(i.Apellido)
|
||||
.SetCelular(i.Celular)
|
||||
.SetDomicilio(i.Domicilio)
|
||||
.SetDni(i.Dni)
|
||||
.SetDomicilioLaboral(i.Domiciliolaboral)
|
||||
.Build();
|
||||
gar.Add(g);
|
||||
}
|
||||
|
||||
var ret = RepositorioContratos.Singleton.CargaGarantes(gar, dto.EmailInquilino, dto.Idpropiedad);
|
||||
|
||||
return ret ?
|
||||
Ok(new {message = "Se Añadieron los Garantes"}):BadRequest(new { message = "Fallo la carga"});
|
||||
}
|
||||
|
||||
|
||||
private string ValidarDtoGarante(GaranteDto g){
|
||||
string ret = "";
|
||||
if (g == null) return "dto nulo";
|
||||
|
||||
if (g.Celular == "") ret += "No puede tener un numero de telefono vacio\n";
|
||||
if (g.Nombre == "") ret += "No puede tener un nombre vacio\n";
|
||||
if (g.Apellido == "") ret += "No puede tener un apellido vacio\n";
|
||||
if (g.Domiciliolaboral == "") ret += "Tiene que especificar su domicilio laboral\n";
|
||||
if (g.Domicilio == "") ret += "Tiene que especificar su domilio\n";
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private string ValidarDtoAltaGarantes(AltaGarantesDto dto){
|
||||
string ret = "";
|
||||
if (dto == null) return "dto nulo";
|
||||
|
||||
if (dto.garantes.Count()<=0) ret += "No se puede tener 0 o menos garantes\n";
|
||||
if (dto.Idpropiedad<=0) ret += "la id de propiedad no puede ser igual o menor a 0\n";
|
||||
if (dto.EmailInquilino == "") ret += "El email de inquilino no puede estar vacio\n";
|
||||
|
||||
return ret;
|
||||
}
|
||||
private string ValidarDtoPrecontrato( PrecontratoDto dto) {
|
||||
string ret = "";
|
||||
if (dto == null) return "dto nulo";
|
||||
|
||||
if (dto.CantidadGarantes<0) ret += "la cantidad de garantes necesarios no pueden ser menor a 0\n";
|
||||
if (dto.EmailInquilino == "") ret += "el email del inquilino no puede ser nulo\n";
|
||||
if (dto.EmailPropietario == "") ret += "el email del propietario no puede estar vacio\n";
|
||||
if (dto.IdPropiedad <= 0) ret += "la id de propiedad no puede ser igual o menor a 0\n";
|
||||
if (dto.MesesHastaAumento <= 0) ret += "No puede tener 0 o menos meses hasta el aumento\n";
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user