tengo que arreglar el tema de los streams
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using AlquilaFacil.Builder;
|
||||
using AlquilaFacil.Config;
|
||||
using Entidades;
|
||||
using Entidades.Dto;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -210,12 +213,21 @@ public class ContratoController: ControllerBase {
|
||||
}
|
||||
|
||||
private readonly IMinioClient mc;
|
||||
public ContratoController(IMinioClient mcc){
|
||||
mc = mcc;
|
||||
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 IActionResult subirContrato([FromHeader(Name = "Auth")]string Auth, [FromForm]long idcontrato, IFormFile contrato) {
|
||||
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();
|
||||
@@ -223,49 +235,117 @@ public class ContratoController: ControllerBase {
|
||||
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";
|
||||
|
||||
var ret = subirContrato(contrato, mc, nuevoNombreArchivo).Wait();
|
||||
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/getdocumento")]
|
||||
public async Task<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) 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{
|
||||
using( var memstream = new MemoryStream()){
|
||||
|
||||
var goa = new GetObjectArgs()
|
||||
.WithBucket("alquilafacil")
|
||||
.WithObject(nuevoNombreArchivo)
|
||||
.WithCallbackStream(async stream => {
|
||||
memstream.Position=0;
|
||||
await stream.CopyToAsync(memstream);
|
||||
});
|
||||
|
||||
await mc.GetObjectAsync(goa);
|
||||
memstream.Position = 0;
|
||||
|
||||
if (memstream.Length == 0) return BadRequest(new { message = "El archivo está vacío" });
|
||||
|
||||
return File(memstream.ToArray(), "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"});
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("api/contratos/aceptarContrato")]
|
||||
public IActionResult AceptarContrato([FromHeader(Name = "Auth")]string Auth){
|
||||
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest("");
|
||||
if (String.IsNullOrWhiteSpace(Auth)) return BadRequest();
|
||||
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino");
|
||||
if (validacion1 == false) return Unauthorized();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
private async Task<bool> subirContrato(IFormFile f, IMinioClient m, string flname) {
|
||||
private async Task<bool> subirContrato(IFormFile f, string flname) {
|
||||
try {
|
||||
var buck = new BucketExistsArgs().WithBucket("alquilafacil");
|
||||
bool encontrado = await m.BucketExistsAsync(buck).ConfigureAwait(false);
|
||||
bool encontrado = await mc.BucketExistsAsync(buck).ConfigureAwait(false);
|
||||
|
||||
if(!encontrado){
|
||||
var mb = new MakeBucketArgs().WithBucket("alquilafacil");
|
||||
await m.MakeBucketAsync(mb).ConfigureAwait(false);
|
||||
await mc.MakeBucketAsync(mb).ConfigureAwait(false);
|
||||
}
|
||||
using (var stream = f.OpenReadStream()){
|
||||
|
||||
var putbj = new PutObjectArgs()
|
||||
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(f.Length);
|
||||
await m.PutObjectAsync(putbj).ConfigureAwait(false);
|
||||
.WithObjectSize(stream.Length);
|
||||
await mc.PutObjectAsync(putbj);
|
||||
}
|
||||
return true;
|
||||
} catch (MinioException e ) {
|
||||
} catch (Exception e ) {
|
||||
Console.Error.WriteLine(e.Message);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -13,9 +13,10 @@ if (mcon == null) return;
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddMinio(confi => confi
|
||||
builder.Services.AddMinio(options => options
|
||||
.WithCredentials(mcon.usr, mcon.scrt)
|
||||
.WithEndpoint("http://192.168.1.11:9000")
|
||||
.WithEndpoint("192.168.1.11:9000")
|
||||
.WithSSL(false)
|
||||
.Build());
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
let file:File | null = $state(null);
|
||||
let confirmaGarantes: boolean = $state(false);
|
||||
|
||||
let clickeado: boolean = $state(false);
|
||||
|
||||
function handleFile(e:Event):void {
|
||||
const input = e.target as HTMLInputElement;
|
||||
@@ -22,7 +22,9 @@
|
||||
|
||||
function submit() {
|
||||
if (file == null) return;
|
||||
clickeado = true;
|
||||
onConfirm(file, men.mensaje.split(" ").reverse()[0]);
|
||||
onClose();
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -89,8 +91,14 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer d-flex justify-content-between">
|
||||
<button class="btn btn-primary" disabled={!(confirmaGarantes && file != null)} onclick={submit} >
|
||||
<button class="btn btn-primary" disabled={!(confirmaGarantes && file != null && clickeado == false)} onclick={submit} >
|
||||
{#if clickeado == false}
|
||||
Subir
|
||||
{:else}
|
||||
<div class="spinner-border" role="status">
|
||||
<span class="visually-hidden">Loading...</span>
|
||||
</div>
|
||||
{/if}
|
||||
</button>
|
||||
<button type="button" class="btn btn-danger" onclick={onCancel}>Cancelar Contrato</button>
|
||||
</div>
|
||||
|
||||
91
Front/src/Componentes/ModalVeryAceptarContrato.svelte
Normal file
91
Front/src/Componentes/ModalVeryAceptarContrato.svelte
Normal file
@@ -0,0 +1,91 @@
|
||||
<script lang="ts">
|
||||
import type { MensajeDto } from "../types";
|
||||
|
||||
let {getContrato, onConfirm, onCancel, onClose, men
|
||||
} : {
|
||||
getContrato:(idcontrato:number)=>void,
|
||||
onConfirm:()=>void,
|
||||
onCancel:()=>void,
|
||||
onClose:()=>void,
|
||||
men: MensajeDto
|
||||
} = $props();
|
||||
|
||||
let contratoDescargado = $state(false);
|
||||
let leiContrato:boolean = $state(false);
|
||||
|
||||
function descargarContrato() {
|
||||
let idcontrato = Number(men.mensaje.split(" ").reverse()[0]);
|
||||
getContrato(idcontrato);
|
||||
contratoDescargado = true;
|
||||
}
|
||||
|
||||
function confirmar() {
|
||||
if (leiContrato) {
|
||||
onConfirm();
|
||||
}
|
||||
}
|
||||
|
||||
function rechazar() {
|
||||
if (leiContrato) {
|
||||
onCancel();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="modal show d-block" tabindex="-1">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">Contrato</h5>
|
||||
<button type="button" class="btn-close" onclick={onClose}></button>
|
||||
</div>
|
||||
|
||||
<div class="modal-body">
|
||||
<p>Por favor, descargue y lea el contrato antes de aceptar o rechazar.</p>
|
||||
<div class="d-flex flex-column align-items-center">
|
||||
<button class="btn btn-primary mb-3" onclick={descargarContrato}>
|
||||
Descargar contrato
|
||||
</button>
|
||||
<div class="form-check">
|
||||
<input
|
||||
class="form-check-input"
|
||||
type="checkbox"
|
||||
id="leiContratoCheckbox"
|
||||
bind:checked={leiContrato}
|
||||
disabled={!contratoDescargado}
|
||||
/>
|
||||
<label class="form-check-label" for="leiContratoCheckbox">
|
||||
Leí el contrato
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="modal-footer d-flex justify-content-between">
|
||||
<button
|
||||
class="btn btn-success"
|
||||
onclick={confirmar}
|
||||
disabled={!leiContrato}
|
||||
>
|
||||
Aceptar
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-danger"
|
||||
onclick={rechazar}
|
||||
disabled={!leiContrato}
|
||||
>
|
||||
Rechazar
|
||||
</button>
|
||||
<button class="btn btn-secondary" onclick={onClose}>
|
||||
Cerrar
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.modal {
|
||||
background-color: rgba(0, 0, 0, 0.5); /* Fondo semi-transparente */
|
||||
}
|
||||
</style>
|
||||
@@ -10,6 +10,8 @@
|
||||
import ModalAddGarantes from "../Componentes/ModalAddGarantes.svelte";
|
||||
import ModalCheckYContrato from "../Componentes/ModalCheckYContrato.svelte";
|
||||
import { self } from "svelte/legacy";
|
||||
import ModalVeryAceptarContrato from "../Componentes/ModalVeryAceptarContrato.svelte";
|
||||
import { getRequest } from "@sveltejs/kit/node";
|
||||
|
||||
const token = sessionStorage.getItem("token");
|
||||
let mensajes: MensajeDto[] = $state([]);
|
||||
@@ -122,6 +124,11 @@
|
||||
obtenerListaGarantes(idcontrato);
|
||||
return;
|
||||
}
|
||||
|
||||
if (mensaje.accion === "Aceptar Contrato") {
|
||||
Selmens = {...mensaje};
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async function obtenerListaGarantes(idcontrato: number) {
|
||||
@@ -253,6 +260,7 @@
|
||||
} else {
|
||||
SinLeer();
|
||||
}
|
||||
Selmens.accion = "";
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -268,22 +276,59 @@
|
||||
let formdata = new FormData();
|
||||
formdata.append("idcontrato", idcontrato);
|
||||
formdata.append("contrato", file);
|
||||
formdata.append("ubicarnotificacion", Selmens.fecha);
|
||||
try{
|
||||
const responce = await fetch("", {
|
||||
const responce = await fetch($urlG+"/api/contratos/subirContrato", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Auth": String(token),
|
||||
},
|
||||
body: formdata,
|
||||
});
|
||||
if () {
|
||||
|
||||
if (responce.ok) {
|
||||
let data = await responce.json();
|
||||
modaldata = data.message;
|
||||
if (mostrarleidos) {
|
||||
Leidos();
|
||||
} else {
|
||||
SinLeer();
|
||||
}
|
||||
return;
|
||||
}
|
||||
let data = await responce.json();
|
||||
modaldata = data.message;
|
||||
return;
|
||||
}catch{
|
||||
modaldata="Fallo al intentar hacer la request";
|
||||
}
|
||||
}
|
||||
|
||||
async function ObtenerContrato(idcontrato: number) {
|
||||
if (Selmens.accion != "Aceptar Contrato") {
|
||||
modaldata = "Hay algo mal comunicate con el administrador";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const responce = await fetch($urlG+"/api/contrato/getdocumento?idcontrato="+idcontrato, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Auth": String(token),
|
||||
}
|
||||
});
|
||||
if (!responce.ok) {
|
||||
modaldata="Error al obtener el archivo";
|
||||
return;
|
||||
}
|
||||
|
||||
let blob = await Blob(responce.body());
|
||||
const blobUrl = URL.createObjectURL(blob);
|
||||
window.open(blobUrl, '_blank');
|
||||
setTimeout(() => URL.revokeObjectURL(blobUrl), 100000);
|
||||
} catch {
|
||||
modaldata = "Fallo al intentar conectarse al servidor";
|
||||
}
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<NavBarAutocompletable/>
|
||||
@@ -298,6 +343,8 @@
|
||||
<ModalAddGarantes maxGarantes={setCantGarantes} onClose={() => (Selmens.accion = "")} onSubmit={handleEnviarmensaje3}/>
|
||||
{:else if Selmens.accion == "Check y Contrato"}
|
||||
<ModalCheckYContrato {garantes} men={Selmens} onCancel={handleCancelPrecontrato} onClose={() => (Selmens.accion = "")} onConfirm={handleEnviarmensaje4}/>
|
||||
{:else if Selmens.accion == "Aceptar Contrato"}
|
||||
<ModalVeryAceptarContrato onClose={() => (Selmens.accion = "")} onConfirm={()=> ""} onCancel={()=>""} getContrato={ObtenerContrato} men={Selmens}/>
|
||||
{/if}
|
||||
|
||||
<div class="container">
|
||||
|
||||
@@ -88,9 +88,21 @@ public class RepositorioContratos: RepositorioBase<RepositorioContratos> {
|
||||
|
||||
public Contrato? ObtenerPreContratoPorId(long idcontrato) {
|
||||
var con = Context;
|
||||
Contrato? contr = con.Contratos.Include(x=>x.Idgarantes).Where(x=>x.Cancelado == 0 && x.Habilitado == 0)
|
||||
Contrato? contr = con.Contratos
|
||||
.Include(x=>x.DnipropietarioNavigation)
|
||||
.Include(x=>x.Idgarantes)
|
||||
.Where(x=>x.Cancelado == 0 && x.Habilitado == 0)
|
||||
.FirstOrDefault(x=>x.Id == idcontrato);
|
||||
if (contr == null) return null;
|
||||
return contr;
|
||||
}
|
||||
|
||||
public bool AddUrl(long id, string nuevoNombreArchivo) {
|
||||
var con = Context;
|
||||
Contrato? contrato = con.Contratos
|
||||
.FirstOrDefault(x=>x.Id == id);
|
||||
if (contrato == null) return false;
|
||||
contrato.UrlContrato = nuevoNombreArchivo;
|
||||
return Guardar(con);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user