forked from b00tc4mp/isdi-parttime-202309
-
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.
add create template and complete task logics b00tc4mp#426
- Loading branch information
Showing
27 changed files
with
430 additions
and
60 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
staff/miguel-arias/project/api/handlers/createTemplateHandler.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,38 @@ | ||
import jwt from 'jsonwebtoken' | ||
const { JsonWebTokenError } = jwt | ||
|
||
import { errors } from 'com' | ||
const { NotFoundError, ContentError, TokenError } = errors | ||
|
||
import logic from '../logic/index.js' | ||
|
||
export default async (req, res) => { | ||
const token = req.headers.authorization.substring(7) | ||
const payload = jwt.verify(token, process.env.JWT_SECRET) | ||
const homeId = payload.sub | ||
|
||
const { name, periodicityNumber, periodicityRange, rooms, points } = req.body | ||
try { | ||
await logic.createTemplate(homeId, name, periodicityNumber, periodicityRange, rooms, points) | ||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof NotFoundError) | ||
status = 404 | ||
|
||
if (error instanceof DuplicityError) | ||
status = 409 | ||
|
||
if (error instanceof ContentError || error instanceof TypeError) | ||
status = 406 | ||
|
||
if (error instanceof JsonWebTokenError) { | ||
status = 401 | ||
error = new TokenError(error.message) | ||
} | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
|
||
res.status(201).send() | ||
} |
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
35 changes: 35 additions & 0 deletions
35
staff/miguel-arias/project/api/handlers/retrieveRoomsHandler.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,35 @@ | ||
import jwt from 'jsonwebtoken' | ||
const { JsonWebTokenError } = jwt | ||
|
||
import { errors } from 'com' | ||
const { NotFoundError, ContentError, TokenError } = errors | ||
|
||
import logic from "../logic/index.js" | ||
|
||
export default async (req, res) => { | ||
try { | ||
const token = req.headers.authorization.substring(7) | ||
const payload = jwt.verify(token, process.env.JWT_SECRET) | ||
const homeId = payload.sub | ||
|
||
const tasks = await logic.retrieveRooms(homeId) | ||
|
||
res.json(tasks) | ||
|
||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof ContentError || error instanceof TypeError) | ||
status = 406 | ||
|
||
if (error instanceof NotFoundError) | ||
status = 404 | ||
|
||
if (error instanceof JsonWebTokenError) { | ||
status = 401 | ||
error = new TokenError(error.message) | ||
} | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.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
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
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,40 @@ | ||
import { validate, errors } from 'com' | ||
const { SystemError, NotFoundError } = errors | ||
|
||
import { Home, Room } from '../data/models.js' | ||
|
||
function retrieveRooms(homeId) { | ||
validate.id(homeId) | ||
|
||
return (async () => { | ||
debugger | ||
let home | ||
try { | ||
home = await Home.findById(homeId).lean() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!home) | ||
throw new NotFoundError('home not found') | ||
|
||
let rooms | ||
try { | ||
rooms = await Room.find({ home: homeId }).select('-__v').lean() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!rooms) | ||
throw new NotFoundError('room not found') | ||
|
||
rooms.forEach(room => { | ||
room.id = room._id.toString() | ||
delete room._id | ||
}) | ||
|
||
return rooms | ||
})() | ||
} | ||
|
||
export default retrieveRooms |
16 changes: 16 additions & 0 deletions
16
staff/miguel-arias/project/api/logic/retrieveRooms.test.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,16 @@ | ||
import mongoose from 'mongoose' | ||
|
||
import retrieveRooms from './retrieveRooms.js' | ||
|
||
(async () => { | ||
try { | ||
await mongoose.connect('mongodb://127.0.0.1:27017/test') | ||
|
||
const rooms = await retrieveRooms('65d79ed33377222a975829fa') | ||
console.log('rooms retrieved', rooms) | ||
|
||
await mongoose.disconnect() | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
})() |
10 changes: 10 additions & 0 deletions
10
staff/miguel-arias/project/api/test/retrieve-profiles.test.sh
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,10 @@ | ||
source colors.sh | ||
|
||
echo -e "${blue}TEST retrieve-profiles${reset}" | ||
|
||
echo | ||
echo -e "${tomato}CASE failure on incorrect token${reset}" | ||
|
||
curl 'http://localhost:8000/profiles' \ | ||
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NWRhNDU4NzNmNjY2MDYxYmM1NGNmMWIiLCJpYXQiOjE3MDkxNjM0MDQsImV4cCI6MTcwOTE2NzAwNH0.CB6vPB5pFCKhViEsEkba9larS6_QpsQaxVXrFZ3-XfY' \ | ||
-v |
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
Oops, something went wrong.