167 lines
7.7 KiB
C#
167 lines
7.7 KiB
C#
using AlquilaFacil.Builder;
|
|
using Entidades;
|
|
using Entidades.Dto;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Modelo;
|
|
|
|
namespace AlquilaFacil.Controllers;
|
|
|
|
[ApiController]
|
|
public class NotificacionesController : ControllerBase
|
|
{
|
|
[HttpGet("api/notificaciones")]
|
|
public IActionResult GetNotificaciones([FromHeader(Name = "Auth")] string Auth, bool leido = false)
|
|
{
|
|
if (string.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
|
|
var cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return BadRequest(new { message = "Fallo al intentar encontrar tu usuario (puede que te hayas logeado desde otro dispositivo?)" });
|
|
|
|
IQueryable<Notificacione> notificaciones = RepositorioNotificaciones.Singleton.ObtenerNotificacionesDeUsuario(cli.Dni)
|
|
.Where(x => x.Leido == leido);
|
|
|
|
|
|
List<NotificacionDto> noti = new();
|
|
foreach (Notificacione i in notificaciones)
|
|
{
|
|
if (i.DniclienteNavigation == null || i.DniremitenteNavigation == null) return BadRequest(new { message = "Esta mal cargado el precontrato" });
|
|
var dto = new NotificacionDtoBuilder()
|
|
.SetRemitente(i.DniremitenteNavigation.Email ?? "")
|
|
.SetAccion(i.Accion ?? "")
|
|
.SetMensaje(i.Mensaje ?? "")
|
|
.SetFecha(i.Fecha)
|
|
.SetPropiedad(i.Idpropiedad.ToString() ?? "")
|
|
.SetReceptor(i.DniclienteNavigation.Email ?? "")
|
|
.Build();
|
|
|
|
noti.Add(dto);
|
|
}
|
|
|
|
return Ok(noti);
|
|
}
|
|
|
|
[HttpGet("api/notificaciones/haySinLeer")]
|
|
public IActionResult GetHayNotis([FromHeader(Name = "Auth")] string Auth)
|
|
{
|
|
if (String.IsNullOrWhiteSpace(Auth)) return Unauthorized();
|
|
|
|
var cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return BadRequest(new { message = "Fallo al intentar encontrar tu usuario (puede que te hayas logeado desde otro dispositivo?)" });
|
|
|
|
bool ret = RepositorioNotificaciones.Singleton.HayNotis(cli.Dni);
|
|
return Ok(new { message = ret });
|
|
}
|
|
|
|
[HttpPut("api/notificaciones")]
|
|
public IActionResult MarcarComoLeido([FromHeader(Name = "Auth")] string Auth, NotificacionMarcarLeidoDto nota)
|
|
{
|
|
if (String.IsNullOrWhiteSpace(Auth)) return Unauthorized();
|
|
|
|
if (nota.Fecha == null || String.IsNullOrWhiteSpace(nota.Email)) return BadRequest(new { message = "Faltan datos" });
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
|
|
if (cli == null) return BadRequest(new { message = "El token de autorizacion no pertenese a ningun Usuario" });
|
|
|
|
if (cli.Email != nota.Email) return BadRequest(new { message = "El token de autorizacion no corresponde a tu usuario" });
|
|
|
|
bool ret = RepositorioNotificaciones.Singleton.MarcarComoLeido(cli.Dni, nota.Fecha);
|
|
return ret ?
|
|
Ok(new { message = "Se Marco como leido" }) : BadRequest(new { message = "Fallo al marcarse como leido" });
|
|
}
|
|
|
|
[HttpPost("api/notificaciones/consultaAlquiler")]
|
|
public IActionResult ConsultaAlquiler([FromHeader(Name = "Auth")] string Auth, [FromBody] AltaNotificacionDto data)
|
|
{
|
|
if (String.IsNullOrWhiteSpace(Auth)) return Unauthorized();
|
|
bool validacion1 = RepositorioUsuarios.Singleton.CheckToken(data.Remitente, Auth);
|
|
if (validacion1 == false) return BadRequest(new { message = "el token no corresponde a su usuario" });
|
|
|
|
if (data.Accion == "") return BadRequest(new { message = "El campo Accion esta vacio" });
|
|
if (data.Mensaje == "") return BadRequest(new { message = "El campo Mensaje esta vacio" });
|
|
|
|
Cliente? inq = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (inq == null) return BadRequest(new { message = "no hay un usuario para el cual el token de autorizacion corresponda (vuelvase a logear)" });
|
|
|
|
Propiedade? prop = RepositorioPropiedades.Singleton.ObtenerPropiedadPorId(data.Propiedad);
|
|
if (prop == null || prop.Idestado != 1) return BadRequest(new { message = "No hay una propiedad dada de alta para ese id" });
|
|
if (prop.DnipropietarioNavigation == null) return BadRequest(new { message = "la propiedad no tiene propietario dado de alto ????" });
|
|
|
|
var noti = new NotificacioneBuilder()
|
|
.SetAccion(data.Accion)
|
|
.SetMensaje(data.Mensaje)
|
|
.SetLeido(false)
|
|
.SetDnicliente(prop.DnipropietarioNavigation.Dni)
|
|
.SetDniremitente(inq.Dni)
|
|
.SetIdpropiedad(prop.Id)
|
|
.SetFecha(DateTime.Now)
|
|
.Build();
|
|
|
|
var ret = RepositorioNotificaciones.Singleton.AltaNotificacion(noti);
|
|
return ret ?
|
|
Ok(new { message = "Se envio la notificacion" }) : BadRequest(new { message = "Fallo al intentar guardar la notificacion" });
|
|
}
|
|
|
|
[HttpPost("api/notificarInquilino")]
|
|
public IActionResult NotificarInq([FromHeader(Name = "Auth")] string Auth, [FromBody] AvisoInquilinoDto data)
|
|
{
|
|
if (String.IsNullOrWhiteSpace(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 12);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
if (data.Mensaje == "") return BadRequest(new { message = "El campo Mensaje esta vacio" });
|
|
if (data.Idpropiedad <= 0) return BadRequest(new { message = "La id de propiedad no puede ser 0 o menor" });
|
|
|
|
Contrato? cont = RepositorioContratos.Singleton.ObtenerContratoPorId(data.Idpropiedad);
|
|
if (cont == null || cont.DniinquilinoNavigation == null || cont.DnipropietarioNavigation == null || cont.IdpropiedadNavigation == null) return BadRequest(new { message = "no hay un contrato por esa id" });
|
|
|
|
var n = new NotificacioneBuilder()
|
|
.SetAccion("Notificacion Inquilino")
|
|
.SetMensaje(data.Mensaje)
|
|
.SetLeido(false)
|
|
.SetDnicliente(cont.DniinquilinoNavigation.Dni)
|
|
.SetDniremitente(cont.DnipropietarioNavigation.Dni)
|
|
.SetIdpropiedad(cont.IdpropiedadNavigation.Id)
|
|
.SetFecha(DateTime.Now)
|
|
.Build();
|
|
var ret = RepositorioNotificaciones.Singleton.AltaNotificacion(n);
|
|
return ret ?
|
|
Ok(new { message = "se envio el aviso" }) : BadRequest(new { message = "Fallo al intentar enviar el aviso" });
|
|
}
|
|
|
|
[HttpPost("api/notificar/ConsultaCompra")]
|
|
public IActionResult EnviarConsultaCompra([FromHeader(Name = "Auth")] string Auth, AltaNotificacionDto dto)
|
|
{
|
|
if (String.IsNullOrWhiteSpace(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 16);
|
|
if (validacion1 == false)
|
|
{
|
|
|
|
return Unauthorized();
|
|
|
|
}
|
|
|
|
if (dto.Accion == "") return BadRequest(new { message = "El campo Accion esta vacio" });
|
|
if (dto.Mensaje == "") return BadRequest(new { message = "El campo Mensaje esta vacio" });
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
|
|
Propiedade? prop = RepositorioPropiedades.Singleton.ObtenerPropiedadPorId(dto.Propiedad);
|
|
if (prop == null) return BadRequest(new { message = "No hay una propiedad con id 0 o menor" });
|
|
|
|
var n = new NotificacioneBuilder()
|
|
.SetAccion("Consulta Compra")
|
|
.SetMensaje(dto.Mensaje)
|
|
.SetLeido(false)
|
|
.SetDnicliente(prop.Dnipropietario ?? 0)
|
|
.SetDniremitente(cli.Dni)
|
|
.SetIdpropiedad(prop.Id)
|
|
.SetFecha(DateTime.Now)
|
|
.Build();
|
|
var ret2 = RepositorioNotificaciones.Singleton.AltaNotificacion(n, cli.Dni);
|
|
return ret2 ?
|
|
Ok(new { message = "se envio el aviso" }) : BadRequest(new { message = "Fallo al intentar enviar el aviso" });
|
|
|
|
}
|
|
}
|