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
9 changed files
with
156 additions
and
38 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
staff/miguel-arias/project/api/handlers/assignTaskHandler.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,45 @@ | ||
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 { profileId, taskId } = req.body | ||
|
||
let realProfileId = profileId | ||
|
||
if (profileId === null) | ||
realProfileId = sessionProfileId | ||
|
||
try { | ||
await logic.assignTask(taskId, realProfileId) | ||
} 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 DuplicityError) | ||
status = 409 | ||
|
||
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,41 @@ | ||
import { validate, errors } from 'com' | ||
const { SystemError, NotFoundError } = errors | ||
|
||
import { Task, Profile } from '../data/models.js' | ||
|
||
function assignTask(taskId, profileId) { | ||
validate.id(taskId, 'task id') | ||
validate.id(profileId, 'profile 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') | ||
|
||
let profile | ||
try { | ||
profile = await Profile.findById(profileId).lean() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!profile) | ||
throw new NotFoundError('profile not found') | ||
|
||
task.assignee = profileId | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import mongoose from 'mongoose' | ||
import assignTask from './assignTask.js' | ||
|
||
(async () => { | ||
try { | ||
await mongoose.connect('mongodb://127.0.0.1:27017/project') | ||
|
||
await assignTask('65dd06642ddc33bb0d3bfbee', '65da45873f666061bc54cf3c') | ||
|
||
console.log('task assigned') | ||
|
||
await mongoose.disconnect() | ||
} catch (error) { | ||
console.log(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
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