765 lines
38 KiB
C#
765 lines
38 KiB
C#
using System.Net;
|
|
using System.Text.Json;
|
|
using AlquilaFacil.Builder;
|
|
using AlquilaFacil.Config;
|
|
using Entidades;
|
|
using Entidades.Dto;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Minio;
|
|
using Minio.DataModel;
|
|
using Minio.DataModel.Args;
|
|
using Minio.Exceptions;
|
|
using Modelo;
|
|
|
|
namespace AlquilaFacil.Controllers;
|
|
|
|
[ApiController]
|
|
public class ContratoController: ControllerBase {
|
|
|
|
[HttpPost("api/contratos/marcarPago")]
|
|
public IActionResult marcarPago([FromHeader(Name="Auth")]string Auth, MarcarPagoDto dto) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false)return Unauthorized();
|
|
if (dto.Idcontrato<=0) return BadRequest(new { message = "No puede existir un contrato con id 0 o menor"});
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null)return Unauthorized();
|
|
|
|
Contrato? cont = RepositorioContratos.Singleton.ObtenerContratoPorId(dto.Idcontrato);
|
|
if (cont == null) return BadRequest(new { message = "No hay un contrato por esa id"});
|
|
|
|
if (cli.Dni != cont.Dnipropietario) return BadRequest(new {message = "No sos propietario o intenta volviendote a logear"});
|
|
|
|
Canon? c = RepositorioCanons.Singleton.ObtenerCanonContrato(dto.fecha, dto.Idcontrato);
|
|
if (c == null) return BadRequest(new { message = "no hay un canon por esa id"});
|
|
|
|
Recibo re = new Recibo{
|
|
Monto = c.Monto,
|
|
Fecha = c.Fecha,
|
|
};
|
|
|
|
bool ret = RepositorioCanons.Singleton.SetRecibo(c, re);
|
|
return ret ?
|
|
Ok(new { message = "Se guardo correctamente"}):BadRequest(new { message = "No se pudo guardar"});
|
|
|
|
}
|
|
|
|
[HttpPost("api/contratos/realizarPago")]
|
|
public IActionResult realizarPago([FromHeader(Name="Auth")]string Auth, MarcarPagoDto dto) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino");
|
|
if (validacion1 == false) return Unauthorized();
|
|
if (dto.Idcontrato<=0) return BadRequest(new { message = "No puede existir un contrato con id 0 o menor"});
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null)return Unauthorized();
|
|
|
|
Contrato? cont = RepositorioContratos.Singleton.ObtenerContratoPorId(dto.Idcontrato);
|
|
if (cont == null) return BadRequest(new { message = "No hay un contrato por esa id"});
|
|
|
|
if (cli.Dni != cont.Dniinquilino) return BadRequest(new {message = "No sos inquilino o intenta volviendote a logear"});
|
|
|
|
Canon? c = RepositorioCanons.Singleton.ObtenerCanonContrato(dto.fecha, dto.Idcontrato);
|
|
if (c == null) return BadRequest(new { message = "no hay un canon por esa id"});
|
|
|
|
Recibo re = new Recibo{
|
|
Monto = c.Monto,
|
|
Fecha = c.Fecha,
|
|
};
|
|
|
|
bool ret = RepositorioCanons.Singleton.SetRecibo(c, re);
|
|
return ret ?
|
|
Ok(new { message = "Se guardo correctamente"}):BadRequest(new { message = "No se pudo guardar"});
|
|
}
|
|
|
|
[HttpPost("api/contratos/crearcanons")] //WIP
|
|
public IActionResult crearCanons([FromHeader(Name="Auth")]string Auth){
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false)return Unauthorized();
|
|
|
|
}
|
|
|
|
[HttpGet("api/contratos/canon")]
|
|
public ActionResult getCanons([FromHeader(Name="Auth")]string Auth, int id = 0) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false){
|
|
validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino");
|
|
if (validacion1 == false) {
|
|
return Unauthorized();
|
|
}
|
|
}
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if(cli == null) return Unauthorized();
|
|
|
|
var cont = RepositorioContratos.Singleton.ObtenerContratoPorId(id);
|
|
if (cont == null) return BadRequest(new { message = "No existe el contrato"});
|
|
if ( cont.Dnipropietario != cli.Dni && cont.Dniinquilino != cli.Dni) return Unauthorized();
|
|
|
|
var list = RepositorioCanons.Singleton.ObtenerCanonsPorContrato(id);
|
|
if (list == null) return BadRequest(new { message = "No hay contrato por esa id"});
|
|
|
|
string divisa ="";
|
|
if (cont.Iddivisa == 0) divisa = "AR$"; else if (cont.Iddivisa == 1) divisa = "US$";
|
|
|
|
List<CanonDto> d = new();
|
|
|
|
foreach (var i in list) {
|
|
var c = new CanonDtoBuilder()
|
|
.SetId(i.Id)
|
|
.SetPago(i.Idrecibo==null?false:true)
|
|
.SetDivisa(divisa==""?"Ugh esta mal cargado la divisa en el contrato":divisa)
|
|
.SetMes(i.Fecha)
|
|
.SetMesNum(int.Parse((i.Fecha.Month - cont.Fechainicio.Month).ToString()) + 1)
|
|
.SetMonto(i.Monto)
|
|
.Build();
|
|
d.Add(c);
|
|
}
|
|
|
|
return Ok(d);
|
|
}
|
|
|
|
[HttpGet("api/contratos/propietario")]
|
|
public IActionResult ObtenerContratosPorPropietario([FromHeader(Name="Auth")]string Auth) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli==null) return Unauthorized();
|
|
|
|
var list = RepositorioContratos.Singleton.ObtenerContratosDePropietario(cli.Dni);
|
|
|
|
List<ContratoDto> dtos = new();
|
|
foreach (var i in list) {
|
|
if (i.DniinquilinoNavigation == null || i.IdpropiedadNavigation == null
|
|
|| i.DnipropietarioNavigation == null) continue;
|
|
|
|
var cont = new ContratoDtoBuilder()
|
|
.SetInquilino($"{i.DniinquilinoNavigation.Nombre} {i.DniinquilinoNavigation.Apellido}")
|
|
.SetUbicacion(i.IdpropiedadNavigation.Ubicacion)
|
|
.SetId(i.Id)
|
|
.SetPropietario($"{i.DnipropietarioNavigation.Nombre} {i.DnipropietarioNavigation.Apellido}")
|
|
.SetTipo(i.IdpropiedadNavigation.IdtipropiedadNavigation.Descripcion)
|
|
.SetFechaInicio(i.Fechainicio)
|
|
.SetEstado(i.Habilitado, i.Cancelado)
|
|
.Build();
|
|
dtos.Add(cont);
|
|
}
|
|
return Ok(dtos);
|
|
}
|
|
|
|
[HttpGet("api/contrato/propietario")]
|
|
public IActionResult ObtenerContratoPorPropietarioPorId([FromHeader(Name="Auth")]string Auth, int id=0) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli==null) return Unauthorized();
|
|
|
|
if (id <= 0) return BadRequest(new { message = "No hay propiedades con id igual o menor a 0"});
|
|
var i = RepositorioContratos.Singleton.ObtenerContratoPorId(id);
|
|
if (i == null || i.DniinquilinoNavigation == null ||
|
|
i.IdpropiedadNavigation == null || i.DnipropietarioNavigation == null)return BadRequest(new { message = "Fallo la query"});
|
|
if (cli.Dni != i.Dnipropietario) return BadRequest(new { message = "No sos el propietario"});
|
|
|
|
var cont = new ContratoPropiedadDtoBuilder()
|
|
.SetInquilino($"{i.DniinquilinoNavigation.Nombre} {i.DniinquilinoNavigation.Apellido}")
|
|
.SetUbicacion(i.IdpropiedadNavigation.Ubicacion)
|
|
.SetId(i.Id)
|
|
.SetPropietario($"{i.DnipropietarioNavigation.Nombre} {i.DnipropietarioNavigation.Apellido}")
|
|
.SetTipo(i.IdpropiedadNavigation.IdtipropiedadNavigation.Descripcion)
|
|
.SetFechaInicio(i.Fechainicio)
|
|
.SetEstado(i.Habilitado, i.Cancelado)
|
|
.SetHabitaciones(i.IdpropiedadNavigation.Canthabitaciones)
|
|
.SetPiso(i.IdpropiedadNavigation.Piso??0)
|
|
.SetLetra(i.IdpropiedadNavigation.Letra??"")
|
|
.SetMesesAumento(i.MesesHastaAumento)
|
|
.SetMesesDuracion(i.MesesDurationContrato)
|
|
.Build();
|
|
|
|
return Ok(cont);
|
|
}
|
|
|
|
[HttpGet("api/contratos/inquilino")]
|
|
public IActionResult ObtenerContratosPorInquilino([FromHeader(Name="Auth")]string Auth) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino");
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli==null) return Unauthorized();
|
|
|
|
var list = RepositorioContratos.Singleton.ObtenerContratosDeInquilino(cli.Dni);
|
|
|
|
List<ContratoDto> dtos = new();
|
|
foreach (var i in list) {
|
|
if (i.DniinquilinoNavigation == null || i.IdpropiedadNavigation == null
|
|
|| i.DnipropietarioNavigation == null) continue;
|
|
|
|
var cont = new ContratoDtoBuilder()
|
|
.SetInquilino($"{i.DniinquilinoNavigation.Nombre} {i.DniinquilinoNavigation.Apellido}")
|
|
.SetUbicacion(i.IdpropiedadNavigation.Ubicacion)
|
|
.SetPropietario($"{i.DnipropietarioNavigation.Nombre} {i.DnipropietarioNavigation.Apellido}")
|
|
.SetId(i.Id)
|
|
.SetTipo(i.IdpropiedadNavigation.IdtipropiedadNavigation.Descripcion)
|
|
.SetFechaInicio(i.Fechainicio)
|
|
.SetEstado(i.Habilitado, i.Cancelado)
|
|
.Build();
|
|
dtos.Add(cont);
|
|
}
|
|
return Ok(dtos);
|
|
}
|
|
|
|
[HttpGet("api/contrato/inquilino")]
|
|
public IActionResult ObtenerContratoPorInquilinoPorId([FromHeader(Name="Auth")]string Auth, int id=0) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino");
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli==null) return Unauthorized();
|
|
|
|
if (id <= 0) return BadRequest(new { message = "No hay propiedades con id igual o menor a 0"});
|
|
var i = RepositorioContratos.Singleton.ObtenerContratoPorId(id);
|
|
if (i == null || i.DniinquilinoNavigation == null ||
|
|
i.IdpropiedadNavigation == null || i.DnipropietarioNavigation == null)return BadRequest(new { message = "Fallo la query"});
|
|
if (cli.Dni != i.Dniinquilino) return BadRequest(new { message = "No sos el inquilino"});
|
|
|
|
var cont = new ContratoPropiedadDtoBuilder()
|
|
.SetInquilino($"{i.DniinquilinoNavigation.Nombre} {i.DniinquilinoNavigation.Apellido}")
|
|
.SetUbicacion(i.IdpropiedadNavigation.Ubicacion)
|
|
.SetId(i.Id)
|
|
.SetPropietario($"{i.DnipropietarioNavigation.Nombre} {i.DnipropietarioNavigation.Apellido}")
|
|
.SetTipo(i.IdpropiedadNavigation.IdtipropiedadNavigation.Descripcion)
|
|
.SetFechaInicio(i.Fechainicio)
|
|
.SetEstado(i.Habilitado, i.Cancelado)
|
|
.SetHabitaciones(i.IdpropiedadNavigation.Canthabitaciones)
|
|
.SetPiso(i.IdpropiedadNavigation.Piso??0)
|
|
.SetLetra(i.IdpropiedadNavigation.Letra??"")
|
|
.SetMesesAumento(i.MesesHastaAumento)
|
|
.SetMesesDuracion(i.MesesDurationContrato)
|
|
.Build();
|
|
|
|
return Ok(cont);
|
|
}
|
|
|
|
[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))
|
|
.SetMesesDuracion(dto.MesesDuracionContrato)
|
|
.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"});
|
|
|
|
LinkedList<GaranteDto> list = new();
|
|
foreach (var i in contr.Idgarantes) {
|
|
list.AddFirst(new GaranteDtoBuilder()
|
|
.SetCelular(i.Celular)
|
|
.SetDni(i.Dni)
|
|
.SetDomicilio(i.Domicilio)
|
|
.SetDomicilioLaboral(i.Domiciliolaboral)
|
|
.SetNombre(i.Nombre)
|
|
.SetApellido(i.Apellido)
|
|
.Build());
|
|
}
|
|
return Ok(list);
|
|
}
|
|
|
|
private readonly IMinioClient mc;
|
|
public ContratoController(IMinioClient minioClient) {
|
|
mc = minioClient;
|
|
if (mc == null){
|
|
MinioConfigcus? mcon = JsonSerializer.Deserialize<MinioConfigcus>(System.IO.File.ReadAllText("./settings.json"))?? null;
|
|
if (mcon == null) throw new Exception();
|
|
|
|
mc = new MinioClient().WithCredentials(mcon.usr, mcon.scrt)
|
|
.WithEndpoint("192.168.1.11:9000")
|
|
.WithSSL(false)
|
|
.Build();
|
|
}
|
|
}
|
|
|
|
[HttpPost("api/contratos/subirContrato")]
|
|
public async Task<IActionResult> subirContrato([FromHeader(Name = "Auth")]string Auth, [FromForm]long idcontrato, [FromForm]DateTime ubicarnotificacion, IFormFile contrato) {
|
|
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest("");
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
if (idcontrato<=0) return BadRequest(new {message = "No puede tener un id contrato menor o igual a 0"});
|
|
Contrato? contr = RepositorioContratos.Singleton.ObtenerPreContratoPorId(idcontrato);
|
|
if (contr == null) return BadRequest(new { message = "No hay precontrato por esa id"});
|
|
if (contr.Dniinquilino == 0 || contr.Dnipropietario == 0 || contr.Idpropiedad == 0 ||
|
|
contr.Dniinquilino == null || contr.Dnipropietario == null || contr.Idpropiedad == null) {
|
|
return BadRequest(new { message = "Estan mal cargados los datos del precontrato comunicate con un administrador"});
|
|
}
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null || contr.DnipropietarioNavigation == null) return BadRequest(new { message ="No se pudo checkear que el token corresponda al propietario"});
|
|
if (cli.Dni != contr.DnipropietarioNavigation.Dni) return BadRequest(new { message = "el token de usuario no coinside con el usuario propietario"});
|
|
|
|
if (contrato == null) return BadRequest(new { message = "Debe subir un archivo." });
|
|
if (contrato.ContentType != "application/pdf") return BadRequest(new { message = "El archivo debe ser un documento PDF." });
|
|
if (!Path.GetExtension(contrato.FileName).Equals(".pdf", StringComparison.OrdinalIgnoreCase)) return BadRequest(new { message = "El archivo debe tener la extensión .pdf." });
|
|
|
|
string nuevoNombreArchivo = $"id:{contr.Id}-inq:{contr.Dniinquilino}-propi:{contr.Dnipropietario}-idprop:{contr.Idpropiedad}.pdf";
|
|
|
|
bool ret = await subirContrato(contrato, nuevoNombreArchivo);
|
|
if(ret == false) return BadRequest(new {message = "No se pudo subir el archivo"});
|
|
|
|
ret = RepositorioContratos.Singleton.AddUrl(contr.Id, nuevoNombreArchivo);
|
|
if (ret == false) return BadRequest(new { message = "No se pudo guardar la url del contrato" });
|
|
|
|
var noti = new NotificacioneBuilder()
|
|
.SetDniremitente(contr.Dnipropietario??0)
|
|
.SetIdpropiedad(contr.Idpropiedad??0)
|
|
.SetDnicliente(contr.Dniinquilino??0)
|
|
.SetAccion("Aceptar Contrato")
|
|
.SetMensaje($"El propietario: {contr.Dnipropietario}, hizo un documento de contrato: {contr.Id}")
|
|
.SetFecha(DateTime.Now)
|
|
.SetLeido(false)
|
|
.Build();
|
|
|
|
RepositorioNotificaciones.Singleton.MarcarComoLeido(cli.Dni, ubicarnotificacion);
|
|
ret = RepositorioNotificaciones.Singleton.AltaNotificacion(noti);
|
|
|
|
return (ret)?
|
|
Ok(new { message = "se notifico al futuro inquilino"}): BadRequest(new { message = "No se pudo enviar la notificacion"});
|
|
|
|
}
|
|
[HttpGet("api/contrato/DocumentoFinal")]
|
|
public IActionResult ObtenerContratoFinal ([FromHeader(Name = "Auth")]string Auth, [FromQuery]long idcontrato) {
|
|
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest("");
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino");
|
|
if (validacion1 == false){
|
|
validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false) return Unauthorized();
|
|
}
|
|
|
|
if (idcontrato <= 0) return BadRequest(new {message = "La id no puede ser igual o menor a 0"});
|
|
|
|
Contrato? contr = RepositorioContratos.Singleton.ObtenerContratoPorId(idcontrato);
|
|
if (contr == null || contr.Dniinquilino == 0) return BadRequest(new { message = "No hay un contrato por esa id"});
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return BadRequest(new { message = "No hay un cliente por ese token"});
|
|
if (cli.Dni != contr.Dniinquilino && cli.Dni != contr.Dnipropietario) return BadRequest(new { message = "El token no corresponde con el del inquilino"});
|
|
|
|
try{
|
|
var memstream = new MemoryStream();
|
|
|
|
var goa = new GetObjectArgs()
|
|
.WithBucket("alquilafacil")
|
|
.WithObject(contr.UrlContrato)
|
|
.WithCallbackStream(stream => {
|
|
memstream.Position=0;
|
|
stream.CopyTo(memstream);
|
|
});
|
|
|
|
mc.GetObjectAsync(goa).Wait();
|
|
memstream.Position = 0;
|
|
|
|
if (memstream.Length == 0) return BadRequest(new { message = "El archivo está vacío" });
|
|
|
|
return File(memstream, "application/pdf", contr.UrlContrato);
|
|
|
|
} catch (Exception e){
|
|
Console.Error.WriteLine(e);
|
|
return BadRequest(new { message = "Fallo al intentar obtener el archivo del almacenamiento o este no existe"});
|
|
}
|
|
}
|
|
|
|
[HttpGet("api/contrato/getdocumento")]
|
|
public IActionResult ObtenerContrato([FromHeader(Name = "Auth")]string Auth, [FromQuery]long idcontrato) {
|
|
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest("");
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino");
|
|
if (validacion1 == false){
|
|
validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false) return Unauthorized();
|
|
}
|
|
|
|
if (idcontrato <= 0) return BadRequest(new {message = "La id no puede ser igual o menor a 0"});
|
|
|
|
Contrato? contr = RepositorioContratos.Singleton.ObtenerPreContratoPorId(idcontrato);
|
|
if (contr == null || contr.Dniinquilino == 0) return BadRequest(new { message = "No hay un precontrato por esa id"});
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return BadRequest(new { message = "No hay un cliente por ese token"});
|
|
if (cli.Dni != contr.Dniinquilino) return BadRequest(new { message = "El token no corresponde con el del inquilino"});
|
|
|
|
string nuevoNombreArchivo = $"id:{contr.Id}-inq:{contr.Dniinquilino}-propi:{contr.Dnipropietario}-idprop:{contr.Idpropiedad}.pdf";
|
|
try{
|
|
var memstream = new MemoryStream();
|
|
|
|
var goa = new GetObjectArgs()
|
|
.WithBucket("alquilafacil")
|
|
.WithObject(nuevoNombreArchivo)
|
|
.WithCallbackStream(stream => {
|
|
memstream.Position=0;
|
|
stream.CopyTo(memstream);
|
|
});
|
|
|
|
mc.GetObjectAsync(goa).Wait();
|
|
memstream.Position = 0;
|
|
|
|
if (memstream.Length == 0) return BadRequest(new { message = "El archivo está vacío" });
|
|
|
|
return File(memstream, "application/pdf", nuevoNombreArchivo);
|
|
|
|
} catch (Exception e){
|
|
Console.Error.WriteLine(e);
|
|
return BadRequest(new { message = "Fallo al intentar obtener el archivo del almacenamiento o este no existe"});
|
|
}
|
|
|
|
}
|
|
|
|
[HttpPost("api/contratos/aceptarContrato")]
|
|
public IActionResult AceptarContrato([FromHeader(Name = "Auth")]string Auth, [FromBody] AceptarContratoDto dto){
|
|
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest();
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino");
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
if (dto.Idcontrato <= 0) return BadRequest(new {message = "La id no puede ser igual o menor a 0"});
|
|
|
|
Contrato? contr = RepositorioContratos.Singleton.ObtenerPreContratoPorId(dto.Idcontrato);
|
|
if (contr == null || contr.Dniinquilino == 0) return BadRequest(new { message = "No hay un precontrato por esa id"});
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return BadRequest(new { message = "No hay un cliente por ese token"});
|
|
if (cli.Dni != contr.Dniinquilino) return BadRequest(new { message = "El token no corresponde con el del inquilino"});
|
|
|
|
bool ret = RepositorioContratos.Singleton.AceptarContrato(dto.Idcontrato);
|
|
if (ret == false) return BadRequest(new { message ="fallo al aceptar el contrato"});
|
|
|
|
RepositorioNotificaciones.Singleton.MarcarComoLeido(cli.Dni, dto.Fecha);
|
|
|
|
var noti = new NotificacioneBuilder()
|
|
.SetDniremitente(cli.Dni)
|
|
.SetIdpropiedad(contr.Idpropiedad??0)
|
|
.SetDnicliente(contr.Dnipropietario??0)
|
|
.SetAccion("Aceptado Contrato")
|
|
.SetMensaje($"Se inicio el alquiler")
|
|
.SetFecha(DateTime.Now)
|
|
.SetLeido(false)
|
|
.Build();
|
|
|
|
ret = RepositorioNotificaciones.Singleton.AltaNotificacion(noti);
|
|
return ret ?
|
|
Ok(new { message = "Se acepto el contrato y se crearon los Canons a ser pagados"}) :
|
|
BadRequest(new { message = "No se pudo aceptar el contrato"});
|
|
|
|
}
|
|
|
|
[HttpPut("api/contratos/rechazarPreContrato")]
|
|
public IActionResult CancelarContrato([FromHeader(Name = "Auth")]string Auth, [FromBody] RechazarPreContrato dto ) {
|
|
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest();
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino");
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
if (dto.Idcontrato <= 0) return BadRequest(new {message = "La id no puede ser igual o menor a 0"});
|
|
|
|
Contrato? contr = RepositorioContratos.Singleton.ObtenerPreContratoPorId(dto.Idcontrato);
|
|
if (contr == null || contr.Dniinquilino == 0) return BadRequest(new { message = "No hay un precontrato por esa id"});
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return BadRequest(new { message = "No hay un cliente por ese token"});
|
|
if (cli.Dni != contr.Dniinquilino) return BadRequest(new { message = "El token no corresponde con el del inquilino"});
|
|
|
|
var ret = RepositorioContratos.Singleton.CancelarPrecontrato(dto.Idcontrato);
|
|
if (ret == false) return BadRequest(new {message = "Fallo al intentar cancelar el precontrato"});
|
|
|
|
RepositorioNotificaciones.Singleton.MarcarComoLeido(cli.Dni, dto.Fecha);
|
|
var noti = new NotificacioneBuilder()
|
|
.SetDniremitente(cli.Dni)
|
|
.SetIdpropiedad(contr.Idpropiedad??0)
|
|
.SetDnicliente(contr.Dnipropietario??0)
|
|
.SetAccion("Rechazo Contrato")
|
|
.SetMensaje($"Se cancelo el proceso para alquilar de: {cli.Nombre}")
|
|
.SetFecha(DateTime.Now)
|
|
.SetLeido(false)
|
|
.Build();
|
|
ret = RepositorioNotificaciones.Singleton.AltaNotificacion(noti);
|
|
|
|
return ret?
|
|
Ok(new { message = "Se cancelo el proceso para iniciar el alquiler"}):
|
|
BadRequest(new { message = "No se pudo cancelar"});
|
|
}
|
|
|
|
private async Task<bool> subirContrato(IFormFile f, string flname) {
|
|
try {
|
|
var buck = new BucketExistsArgs().WithBucket("alquilafacil");
|
|
bool encontrado = await mc.BucketExistsAsync(buck).ConfigureAwait(false);
|
|
|
|
if(!encontrado){
|
|
var mb = new MakeBucketArgs().WithBucket("alquilafacil");
|
|
await mc.MakeBucketAsync(mb).ConfigureAwait(false);
|
|
}
|
|
using (var stream = new MemoryStream()){
|
|
await f.CopyToAsync(stream);
|
|
stream.Position=0;
|
|
PutObjectArgs putbj = new PutObjectArgs()
|
|
.WithBucket("alquilafacil")
|
|
.WithObject(flname)
|
|
.WithStreamData(stream)
|
|
.WithContentType("application/pdf")
|
|
.WithObjectSize(stream.Length);
|
|
await mc.PutObjectAsync(putbj);
|
|
}
|
|
return true;
|
|
} catch (Exception e ) {
|
|
Console.Error.WriteLine(e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
[HttpGet("api/contratos/garantes")]
|
|
public IActionResult ObtenerGarantes([FromHeader(Name ="Auth")] string Auth, int idcontrato) {
|
|
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest();
|
|
if(idcontrato <= 0) return BadRequest();
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return BadRequest();
|
|
|
|
Contrato? cont = RepositorioContratos.Singleton.ObtenerContratoPorId(idcontrato);
|
|
if (cont == null) return BadRequest();
|
|
|
|
if (cont.Dniinquilino != cli.Dni && cont.Dnipropietario != cli.Dni) return BadRequest();
|
|
|
|
var list = cont.Idgarantes;
|
|
List<GaranteDto> l = new();
|
|
foreach (var i in list) {
|
|
l.Add(new GaranteDtoBuilder()
|
|
.SetCelular(i.Celular)
|
|
.SetDni(i.Dni)
|
|
.SetDomicilio(i.Domicilio)
|
|
.SetDomicilioLaboral(i.Domiciliolaboral)
|
|
.SetNombre(i.Nombre)
|
|
.SetApellido(i.Apellido)
|
|
.Build());
|
|
}
|
|
return Ok(l);
|
|
}
|
|
|
|
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";
|
|
if (dto.MesesDuracionContrato <= 0) ret += "No puede tener 0 o menos meses de duracion\n";
|
|
if (dto.MesesDuracionContrato < dto.MesesHastaAumento) ret += "el tiempo hasta aumento no puede ser mayor de \n";
|
|
return ret;
|
|
}
|
|
|
|
} |