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 create contract beetwen landlord and tenatn b00tc4mp#85
- Loading branch information
1 parent
eab25bd
commit fa4338a
Showing
40 changed files
with
948 additions
and
238 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
staff/matias-sargo/project/api/handlers/createContractHandler.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,15 @@ | ||
import { logic } from '../../cor/index.js'; | ||
|
||
export default (req, res, next) => { | ||
const { userId } = req; // Asumiendo que el `userId` está incluido en el token JWT y fue verificado por el middleware. | ||
|
||
const { propertyId, ownerId, tenantId, startDate, endDate, price } = req.body; | ||
|
||
try { | ||
logic.createContract(propertyId, ownerId || userId, tenantId, startDate, endDate, price) // Usa `ownerId` del cuerpo, pero si no está presente, usa `userId`. | ||
.then(() => res.status(201).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
12 changes: 6 additions & 6 deletions
12
staff/matias-sargo/project/api/handlers/getAllPropsHandler.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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { logic } from '../../cor/index.js' | ||
import { logic } from '../../cor/index.js'; | ||
|
||
export default (req, res, next) => { | ||
const { userId } = req | ||
const { userId } = req; | ||
|
||
try { | ||
logic.getAllProps(userId) | ||
.then(events => res.json(events)) | ||
.catch(error => next(error)) | ||
.then(properties => res.json(properties)) // Cambia 'events' por 'properties' para mayor claridad | ||
.catch(error => next(error)); | ||
} catch (error) { | ||
next(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
12 changes: 6 additions & 6 deletions
12
staff/matias-sargo/project/api/handlers/registerUserHandler.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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { logic } from '../../cor/index.js' | ||
import { logic } from '../../cor/index.js'; | ||
|
||
export default (req, res, next) => { | ||
const { name, surname, email, username, dni, password, passwordRepeat } = req.body | ||
const { name, surname, email, username, dni, password, passwordRepeat, role } = req.body; | ||
|
||
try { | ||
logic.registerUser(name, surname, email, username, dni, password, passwordRepeat) | ||
logic.registerUser(name, surname, email, username, dni, password, passwordRepeat, role) | ||
.then(() => res.status(201).send()) | ||
.catch(error => next(error)) | ||
.catch(error => next(error)); | ||
} catch (error) { | ||
next(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
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 @@ | ||
curl -v http://localhost:8080/users/auth -X POST -d '{"username":"matsarg","password":"123123123"}' -H "Content-Type: application/json" |
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,11 @@ | ||
curl -X POST http://localhost:8080/contracts \ | ||
-H "Content-Type: application/json" \ | ||
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmNjOTUyM2ZjOTY2NDc5NWFlNWMzOTIiLCJpYXQiOjE3MjQ2ODQyMzV9.2k87e-M8OeRb1t6lElneipBAslBty8c2aftFrBQl13k" \ | ||
-d '{ | ||
"propertyId": "66cc971efc9664795ae5c396", | ||
"ownerId": "66cc9523fc9664795ae5c392", | ||
"tenantId": "66cc36e584c8d8294429e65a", | ||
"startDate": "2024-09-01T00:00:00.000Z", | ||
"endDate": "2025-09-01T00:00:00.000Z", | ||
"price": 1200 | ||
}' |
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,12 +1,13 @@ | ||
curl -X POST http://localhost:8080/properties \ | ||
-H "Content-Type: application/json" \ | ||
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmM0NTY4OWVjZDY2MjNjYmM5ZTA2YzAiLCJpYXQiOjE3MjQyNDUwMDJ9.ZyH0N7naSi4LO8DL8xCOnY_s65DLBxrkuSCslIzXXww" \ | ||
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmNjOTUyM2ZjOTY2NDc5NWFlNWMzOTIiLCJpYXQiOjE3MjQ2ODM2MDN9.zjb08W1WRnPJ5yYN2I4s4tCfII-92eNBOoQevj6krB0" \ | ||
-d '{ | ||
"title": "Beautiful apartment in the city center", | ||
"images": ["https://example.com/image1.jpg", "https://example.com/image2.jpg"], | ||
"description": "This is a beautiful apartment located in the heart of the city.", | ||
"address": "123 Main St, New York, NY", | ||
"latitude": 40.7128, | ||
"longitude": -74.0060, | ||
"price": 2500, | ||
"type": "apartment" | ||
}' | ||
}' |
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 +1 @@ | ||
curl -v http://localhost:8080/properties -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmM0NTY4OWVjZDY2MjNjYmM5ZTA2YzAiLCJpYXQiOjE3MjQyNDUwMDJ9.ZyH0N7naSi4LO8DL8xCOnY_s65DLBxrkuSCslIzXXww" | ||
curl -v http://localhost:8080/properties -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmNjMzZlNTg0YzhkODI5NDQyOWU2NWEiLCJpYXQiOjE3MjQ2NjA2Mjl9.VTo9ZTzTAMb414MxOyYsJ2QaiZDawSGtmFDaM2faI94" |
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 @@ | ||
curl -v http://localhost:8080/users -X POST -d '{"name":"mat","surname":"sarg","email":"[email protected]","username":"matsarg","dni": "y12345612b", "password":"123123123","passwordRepeat":"123123123", "role": "landlord"}' -H "Content-Type: application/json" |
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 +1 @@ | ||
curl -v http://localhost:8080/users -X POST -d '{"name":"Peter","surname":"Pan","email":"[email protected]","username":"peterpan","dni": "y12345612v", "password":"123123123","passwordRepeat":"123123123"}' -H "Content-Type: application/json" | ||
curl -v http://localhost:8080/users -X POST -d '{"name":"Peter","surname":"Pan","email":"[email protected]","username":"peterpan","dni": "y12345612v", "password":"123123123","passwordRepeat":"123123123", "role": "tenant"}' -H "Content-Type: application/json" |
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,41 @@ | ||
export default (propertyId, ownerId, tenantId, startDate, endDate, price) => { | ||
// Validaciones | ||
validate.string(propertyId, 'propertyId'); | ||
validate.string(ownerId, 'ownerId'); | ||
validate.string(tenantId, 'tenantId'); | ||
validateDate(startDate, 'startDate'); | ||
validateDate(endDate, 'endDate'); | ||
validate.number(price, 'price'); | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/contracts`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${sessionStorage.token}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
propertyId, | ||
ownerId, | ||
tenantId, | ||
startDate, | ||
endDate, | ||
price, | ||
}), | ||
}) | ||
.catch(error => { | ||
throw new SystemError(error.message); | ||
}) | ||
.then(response => { | ||
const { status } = response; | ||
|
||
if (status === 201) return; | ||
|
||
return response.json().then(body => { | ||
const { error, message } = body; | ||
|
||
const constructor = errors[error]; | ||
|
||
throw new constructor(message); | ||
}); | ||
}); | ||
}; |
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,28 +1,31 @@ | ||
import { errors } from '../../com/index.js' | ||
import { errors } from '../../com/index.js'; | ||
|
||
const { SystemError } = errors | ||
const { SystemError } = errors; | ||
|
||
export default () => { | ||
return fetch(`${import.meta.env.VITE_API_URL}/properties`, { | ||
headers: { | ||
Authorization: `Bearer ${sessionStorage.token}` | ||
} | ||
Authorization: `Bearer ${sessionStorage.token}`, | ||
'Content-Type': 'application/json', // Especifica el tipo de contenido | ||
}, | ||
}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.catch(error => { | ||
throw new SystemError(error.message); // Manejo de errores del sistema | ||
}) | ||
.then(response => { | ||
const { status } = response | ||
const { status } = response; | ||
|
||
if (status === 200) | ||
if (status === 200) { | ||
return response.json() | ||
.then(events => events) | ||
.then(properties => properties); // Devuelve las propiedades si la solicitud es exitosa | ||
} | ||
|
||
return response.json() | ||
.then(body => { | ||
const { error, message } = body | ||
return response.json().then(body => { | ||
const { error, message } = body; | ||
|
||
const constructor = errors[error] | ||
const constructor = errors[error]; // Obtiene el constructor del error correspondiente | ||
|
||
throw new constructor(message) | ||
}) | ||
}) | ||
} | ||
throw new constructor(message); // Lanza un error personalizado basado en la respuesta del servidor | ||
}); | ||
}); | ||
}; |
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,36 +1,38 @@ | ||
import { validate, errors } from 'com' | ||
import { validate, errors } from 'com'; | ||
|
||
const { SystemError } = errors | ||
const { SystemError } = errors; | ||
|
||
export default (name, surname, email, username, dni, password, passwordRepeat) => { | ||
validate.name(name) | ||
validate.name(surname, 'surname') | ||
validate.email(email) | ||
validate.username(username) | ||
validate.string(dni) | ||
validate.password(password) | ||
validate.password(passwordRepeat, 'passwordRepeat') | ||
export default (name, surname, email, username, dni, password, passwordRepeat, role = 'tenant') => { | ||
validate.name(name); | ||
validate.name(surname, 'surname'); | ||
validate.email(email); | ||
validate.username(username); | ||
validate.string(dni); | ||
validate.password(password); | ||
validate.password(passwordRepeat, 'passwordRepeat'); | ||
|
||
if (password !== passwordRepeat) throw new ValidationError('Passwords do not match'); | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/users`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ name, surname, email, username, dni, password, passwordRepeat }) | ||
body: JSON.stringify({ name, surname, email, username, dni, password, role }), // Asegúrate de incluir el rol en el cuerpo de la solicitud | ||
}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(response => { | ||
const { status } = response | ||
|
||
if (status === 201) return | ||
.catch((error) => { | ||
throw new SystemError(error.message); | ||
}) | ||
.then((response) => { | ||
const { status } = response; | ||
|
||
return response.json() | ||
.then(body => { | ||
const { error, message } = body | ||
if (status === 201) return; // Usuario registrado con éxito | ||
|
||
const constructor = errors[error] | ||
return response.json().then((body) => { | ||
const { error, message } = body; | ||
const constructor = errors[error]; | ||
|
||
throw new constructor(message) | ||
}) | ||
}) | ||
} | ||
throw new constructor(message); | ||
}); | ||
}); | ||
}; |
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,6 @@ | ||
export default function formatDate(date) { | ||
const year = date.getFullYear(); | ||
const month = String(date.getMonth() + 1).padStart(2, '0'); | ||
const day = String(date.getDate()).padStart(2, '0'); | ||
return `${year}-${month}-${day}`; | ||
} |
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,23 @@ | ||
export default function formatTime(date) { | ||
const seconds = Math.round((Date.now() - date.getTime()) / 1000) | ||
if (seconds < 60) return seconds + ' second' + (seconds === 1 ? '' : 's') | ||
|
||
const minutes = Math.round(seconds / 60) | ||
if (minutes < 60) return minutes + ' minute' + (minutes === 1 ? '' : 's') | ||
|
||
const hours = Math.round(minutes / 60) | ||
if (hours < 24) return hours + ' hour' + (hours === 1 ? '' : 's') | ||
|
||
const days = Math.round(hours / 24) | ||
if (days < 7) return days + ' day' + (days === 1 ? '' : 's') | ||
|
||
const weeks = Math.round(days / 7) | ||
if (weeks < 4) return weeks + ' week' + (weeks === 1 ? '' : 's') | ||
|
||
const months = Math.round(weeks / 4) | ||
if (months < 12) return months + ' month' + (months === 1 ? '' : 's') | ||
|
||
const years = Math.round(months / 12) | ||
|
||
return years + ' year' + (years === 1 ? '' : 's') | ||
} |
Oops, something went wrong.