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.
- Loading branch information
Showing
11 changed files
with
152 additions
and
12 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
staff/miguel-arias/project/api/handlers/delayTaskHandler.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,36 @@ | ||
/* 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 sessionProfileId = payload.sub */ | ||
|
||
const { taskId, date } = req.body | ||
|
||
try { | ||
await logic.delayTask(taskId, date) | ||
} catch (error) { | ||
let status = 500 | ||
|
||
/* if (error instanceof JsonWebTokenError) { | ||
status = 401 | ||
error = new TokenError(error.message) | ||
} */ | ||
|
||
if (error instanceof NotFoundError) | ||
status = 404 | ||
|
||
if (error instanceof ContentError || error instanceof TypeError) | ||
status = 406 | ||
|
||
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
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,30 @@ | ||
import { validate, errors } from 'com' | ||
const { SystemError, NotFoundError } = errors | ||
|
||
import { Task } from '../data/models.js' | ||
|
||
function assignTask(taskId, date) { | ||
validate.id(taskId, 'task id') | ||
|
||
return (async () => { | ||
let task | ||
try { | ||
task = await Task.findById(taskId) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!task) | ||
throw new NotFoundError('task not found') | ||
|
||
task.date = date | ||
|
||
try { | ||
await task.save() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
})() | ||
} | ||
|
||
export default assignTask |
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 } = errors | ||
|
||
import session from './session' | ||
|
||
const delayTask = (taskId, date) => { | ||
validate.id(taskId, 'task id') | ||
|
||
const req = { | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
/* Authorization: `Bearer ${session.profileToken}` */ | ||
}, | ||
body: JSON.stringify({ taskId, date }) | ||
} | ||
|
||
return (async () => { | ||
let res | ||
try { | ||
res = await fetch(`${import.meta.env.VITE_API_URL}/tasks/delay`, req) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!res.ok) { | ||
let body | ||
|
||
try { | ||
body = await res.json() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
throw new errors[body.error](body.message) | ||
} | ||
})() | ||
} | ||
|
||
export default delayTask |
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