265 lines
13 KiB
C#
265 lines
13 KiB
C#
using AlquilaFacil.Builder;
|
|
using Entidades;
|
|
using Entidades.Dto;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
using Modelo;
|
|
using ZstdSharp.Unsafe;
|
|
namespace AlquilaFacil.Controllers;
|
|
|
|
[ApiController]
|
|
public class ContratoController: ControllerBase {
|
|
|
|
[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(DateTime.Parse(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);
|
|
if (ret) {
|
|
ret = RepositorioNotificaciones.Singleton.MarcarComoLeido(cli.Dni, DateTime.Parse(dto.fechaprimernotificacion));
|
|
}
|
|
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});
|
|
|
|
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.EmailInquilino) return BadRequest(new {message = "No Corresponde el email de inquilino con el del token"});
|
|
|
|
var validacion4 = RepositorioContratos.Singleton.CantidadGarantesEncontrato(dto.EmailInquilino, dto.Idpropiedad);
|
|
if (validacion4 <= 0 || dto.garantes.Count()!=validacion4) return BadRequest(new{message="Cantidad de garantes incorrecta"});
|
|
|
|
Cliente? propi = RepositorioPropietario.Singleton.ObtenerPropietarioPorIdPropiedad(dto.Idpropiedad);
|
|
if(propi == null) return BadRequest(new{message = "No se encuentra el propietario de la propiedad"});
|
|
|
|
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 contr = RepositorioContratos.Singleton.ObtenerContrato(dto.EmailInquilino, dto.Idpropiedad);
|
|
if (contr == null) return BadRequest(new { message = "No existe el contrato o ya fue activado"});
|
|
|
|
var ret = RepositorioContratos.Singleton.CargaGarantes(gar, dto.EmailInquilino, dto.Idpropiedad);
|
|
if (ret) {
|
|
Console.WriteLine(dto.fecha);
|
|
RepositorioNotificaciones.Singleton.MarcarComoLeido(cli.Dni, dto.fecha);
|
|
|
|
var noti = new NotificacioneBuilder()
|
|
.SetIdpropiedad(dto.Idpropiedad)
|
|
.SetAccion("Check y Contrato")
|
|
.SetMensaje($"El inquilino cargó los datos de garantes comprobá y carga el contrato: {contr.Id}")
|
|
.SetLeido(false)
|
|
.SetDniremitente(cli.Dni)
|
|
.SetDnicliente(propi.Dni)
|
|
.SetFecha(DateTime.Now)
|
|
.Build();
|
|
ret = RepositorioNotificaciones.Singleton.AltaNotificacion(noti);
|
|
}
|
|
return ret ?
|
|
Ok(new {message = "Se Añadieron los Garantes"}):BadRequest(new { message = "Fallo la carga"});
|
|
}
|
|
|
|
[HttpPut("api/contratos/cancelar")]
|
|
public IActionResult CancelarPrecontrato([FromHeader(Name = "Auth")]string Auth, CancelarPrecontratoDto dto) {
|
|
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest("");
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
var validacion2 = ValidarCancelarDto(dto);
|
|
if (validacion2 != "") return BadRequest(new {message = validacion2});
|
|
|
|
Cliente? pro = RepositorioPropietario.Singleton.ObtenerPropietarioPorEmail(dto.EmailPropietario);
|
|
if (pro == null) return BadRequest(new {message = "No Existe Usuario con ese email"});
|
|
if (pro.Token != Auth) return BadRequest(new {message = "El token de auth no corresponde al token el usuario propietario"});
|
|
|
|
Propiedade? prop = RepositorioPropiedades.Singleton.ObtenerPropiedadPorId(dto.idpropiedad);
|
|
if (prop == null) return BadRequest(new {message = "No existe la propiedad por esa id"});
|
|
if (prop.Dnipropietario != pro.Dni) return BadRequest(new {message = "Este propietario no es el dueño de la propiedad"});
|
|
|
|
var inq = RepositorioInquilinos.Singleton.ObtenerInquilinoPorEmail(dto.EmailInquilino);
|
|
if (inq == null) return BadRequest(new {message = "No hay un inquilino por ese email"});
|
|
|
|
var ret = RepositorioContratos.Singleton.CancelarPrecontrato(dto.EmailInquilino, dto.idpropiedad);
|
|
if (ret) {
|
|
prop.Idestado = 1;
|
|
ret = RepositorioPropiedades.Singleton.PatchPropiedad(prop);
|
|
if (ret){
|
|
RepositorioNotificaciones.Singleton.MarcarComoLeido(pro.Dni, dto.fecha);
|
|
var noti = new NotificacioneBuilder()
|
|
.SetAccion("ContratoCancelado")
|
|
.SetIdpropiedad(dto.idpropiedad)
|
|
.SetDniremitente(pro.Dni)
|
|
.SetDnicliente(inq.Dni)
|
|
.SetMensaje($"Se cancelo el intento de alquilar la propiedad: {dto.idpropiedad}")
|
|
.SetIdpropiedad(dto.idpropiedad)
|
|
.SetLeido(false)
|
|
.Build();
|
|
ret = RepositorioNotificaciones.Singleton.AltaNotificacion(noti);
|
|
if (ret){
|
|
return Ok(new {message = "Se cancelo el precontrato"});
|
|
}else{
|
|
return Ok(new {message = "Se cancelo el precontrato, pero no se pudo notificar al inquilino"});
|
|
}
|
|
}else{
|
|
return BadRequest(new {message = "No se pudo setear la propiedad como Disponible en busqueda"});
|
|
}
|
|
}else{
|
|
return BadRequest(new {message = "Se fallo al intentar cancelar el precontrato"});
|
|
}
|
|
}
|
|
|
|
[HttpGet("api/contratos/precontrato/listaGarantes")]
|
|
public IActionResult ObtenerListaGarantes([FromHeader(Name = "Auth")]string Auth, long idcontrato = 0, [FromQuery] string EmailPropietario = "" ) {
|
|
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest("");
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
if (idcontrato == 0 || EmailPropietario == "") return BadRequest(new { message = "Estan mal cargados los datos"});
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return BadRequest(new {message = "No hay un propietario por ese token"});
|
|
if (cli.Email != EmailPropietario) return BadRequest(new {message = "El Email del usuario no coinside con el del token"});
|
|
|
|
Contrato? contr = RepositorioContratos.Singleton.ObtenerPreContratoPorId(idcontrato);
|
|
if (contr == null) return BadRequest(new {message = "No hay un precontrato por esa id"});
|
|
|
|
return Ok(contr.Idgarantes);
|
|
}
|
|
|
|
[HttpPost("api/contratos/subirContrato")]
|
|
public IActionResult subirContrato([FromHeader(Name = "Auth")]string Auth, [FromForm] IFormFile contrato) {
|
|
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest("");
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
return Ok();
|
|
|
|
}
|
|
|
|
[HttpPost("api/contratos/aceptarContrato")]
|
|
public IActionResult AceptarContrato([FromHeader(Name = "Auth")]string Auth){
|
|
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest("");
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino");
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
return Ok();
|
|
}
|
|
|
|
private string ValidarCancelarDto(CancelarPrecontratoDto dto) {
|
|
if (dto == null) return "dto nulo";
|
|
string ret = "";
|
|
|
|
if (dto.EmailInquilino =="") ret += "No puede tener un EmailInquilino Vacio\n";
|
|
if (dto.EmailPropietario =="") ret += "No puede tener un EmailPropietario Vacio\n";
|
|
if (dto.idpropiedad <= 0 ) ret += "No puede tener id propiedad igual o menor a 0\n";
|
|
if (dto.fecha == DateTime.MinValue) ret += "Falta fecha\n";
|
|
return ret;
|
|
}
|
|
|
|
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.CantidadGarantes>10) ret += "Hay un maximo de 10 garantes\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;
|
|
}
|
|
|
|
} |