forked from b00tc4mp/isdi-bootcamp-202405
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement sign contract, create document and tests b00tc4mp#85
- Loading branch information
1 parent
fa4338a
commit f4647a5
Showing
35 changed files
with
686 additions
and
368 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
staff/matias-sargo/project/api/handlers/createDocumentHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { logic } from '../../cor/index.js'; | ||
import fs from 'fs'; | ||
|
||
export default (req, res, next) => { | ||
const { propertyId, type, url } = req.body; | ||
const file = req.file; // Aquí se encuentra el archivo subido por `multer` | ||
|
||
if (!file) { | ||
return res.status(400).json({ error: 'BadRequest', message: 'File is required' }); | ||
} | ||
|
||
try { | ||
logic.createDocument(propertyId, file.path, type, url) | ||
.then(() => { | ||
fs.unlinkSync(file.path); // Elimina el archivo temporal después de guardarlo | ||
res.status(201).send(); | ||
}) | ||
.catch(error => { | ||
fs.unlinkSync(file.path); // Asegúrate de eliminar el archivo temporal si ocurre un error | ||
next(error); | ||
}); | ||
} catch (error) { | ||
if (fs.existsSync(file.path)) { | ||
fs.unlinkSync(file.path); // Elimina el archivo si ocurre un error en la escritura | ||
} | ||
next(error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
import registerUserHandler from '../handlers/registerUserHandler.js' | ||
import authenticateUserHandler from '../handlers/authenticateUserHandler.js' | ||
import createPropHandler from '../handlers/createPropHandler.js' | ||
import getAllPropsHandler from '../handlers/getAllPropsHandler.js' | ||
import createContractHandler from './createContractHandler.js' | ||
import registerUserHandler from "../handlers/registerUserHandler.js"; | ||
import authenticateUserHandler from "../handlers/authenticateUserHandler.js"; | ||
import createPropHandler from "../handlers/createPropHandler.js"; | ||
import getAllPropsHandler from "../handlers/getAllPropsHandler.js"; | ||
import createContractHandler from "./createContractHandler.js"; | ||
import signContractHandler from "./signContractHandler.js"; | ||
import createDocumentHandler from "./createDocumentHandler.js"; | ||
|
||
export { | ||
registerUserHandler, | ||
authenticateUserHandler, | ||
createPropHandler, | ||
getAllPropsHandler, | ||
createContractHandler | ||
|
||
} | ||
registerUserHandler, | ||
authenticateUserHandler, | ||
createPropHandler, | ||
getAllPropsHandler, | ||
createContractHandler, | ||
signContractHandler, | ||
createDocumentHandler | ||
}; |
14 changes: 14 additions & 0 deletions
14
staff/matias-sargo/project/api/handlers/signContractHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { logic } from '../../cor/index.js'; | ||
|
||
export default (req, res, next) => { | ||
const { userId } = req; // Obtenido del middleware | ||
const { contractId } = req.params; | ||
|
||
try { | ||
logic.signContract(userId, contractId) | ||
.then(() => res.status(200).send()) | ||
.catch(error => next(error)); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,51 @@ | ||
import 'dotenv/config'; | ||
import express from 'express'; | ||
import cors from 'cors'; | ||
import "dotenv/config"; | ||
import express from "express"; | ||
import cors from "cors"; | ||
import multer from "multer"; | ||
|
||
import { mongoose } from '../cor/index.js'; | ||
|
||
import { jsonBodyParser, jwtVerifier, errorHandler } from './middlewares/index.js'; | ||
import { mongoose } from "../cor/index.js"; | ||
|
||
import { | ||
registerUserHandler, | ||
authenticateUserHandler, | ||
createPropHandler, | ||
getAllPropsHandler, | ||
createContractHandler | ||
} from './handlers/index.js'; | ||
|
||
mongoose.connect(process.env.MONGODB_URI) | ||
.then(() => { | ||
console.info(`API connected to ${process.env.MONGODB_URI}`); | ||
|
||
const api = express(); | ||
|
||
api.use(cors()); | ||
|
||
api.post('/users', jsonBodyParser, registerUserHandler); | ||
api.post('/users/auth', jsonBodyParser, authenticateUserHandler); | ||
jsonBodyParser, | ||
jwtVerifier, | ||
errorHandler, | ||
} from "./middlewares/index.js"; | ||
|
||
api.post('/properties', jsonBodyParser, jwtVerifier, createPropHandler); | ||
api.get('/properties', jwtVerifier, getAllPropsHandler) | ||
|
||
api.post('/contracts', jsonBodyParser, jwtVerifier, createContractHandler); | ||
|
||
api.use(errorHandler); | ||
|
||
api.listen(process.env.PORT, () => console.info(`API listening on PORT ${process.env.PORT}`)); | ||
}) | ||
.catch(error => console.error(error)); | ||
import { | ||
registerUserHandler, | ||
authenticateUserHandler, | ||
createPropHandler, | ||
getAllPropsHandler, | ||
createContractHandler, | ||
signContractHandler, | ||
createDocumentHandler | ||
} from "./handlers/index.js"; | ||
|
||
mongoose | ||
.connect(process.env.MONGODB_URI) | ||
.then(() => { | ||
console.info(`API connected to ${process.env.MONGODB_URI}`); | ||
|
||
const api = express(); | ||
const upload = multer({ dest: 'uploads/' }); | ||
|
||
api.use(cors()); | ||
|
||
api.post("/users", jsonBodyParser, registerUserHandler); | ||
api.post("/users/auth", jsonBodyParser, authenticateUserHandler); | ||
|
||
api.post("/properties", jsonBodyParser, jwtVerifier, createPropHandler); | ||
api.get("/properties", jwtVerifier, getAllPropsHandler); | ||
|
||
api.post("/contracts", jsonBodyParser, jwtVerifier, createContractHandler); | ||
api.post("/contracts/:contractId/sign", jsonBodyParser, jwtVerifier, signContractHandler); | ||
|
||
api.post('/documents', upload.single('content'), createDocumentHandler); | ||
|
||
api.use(errorHandler); | ||
|
||
api.listen(process.env.PORT, () => | ||
console.info(`API listening on PORT ${process.env.PORT}`) | ||
); | ||
}) | ||
.catch((error) => console.error(error)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import multer from 'multer'; | ||
|
||
// Configura multer para almacenar archivos en una carpeta temporal | ||
const upload = multer({ dest: 'uploads/' }); |
Oops, something went wrong.