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.
implement logics of delay, delete and complete tasks b00tc4mp#426
- Loading branch information
Showing
20 changed files
with
391 additions
and
41 deletions.
There are no files selected for viewing
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
38 changes: 38 additions & 0 deletions
38
staff/miguel-arias/project/api/handlers/completeTaskHandler.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 sessionProfileId = payload.sub | ||
|
||
const { pincode, date } = req.body | ||
|
||
const { taskId } = req.params | ||
|
||
try { | ||
await logic.completeTask(sessionProfileId, taskId, pincode, 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(204).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
36 changes: 36 additions & 0 deletions
36
staff/miguel-arias/project/api/handlers/deleteTaskHandler.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' | ||
|
||
import { errors } from 'com' | ||
const { JsonWebTokenError } = jwt | ||
|
||
import logic from '../logic/index.js' | ||
const { NotFoundError, ContentError, TokenError } = errors | ||
|
||
export default async (req, res) => { | ||
const { taskId } = req.body | ||
|
||
const token = req.headers.authorization.substring(7) | ||
const payload = jwt.verify(token, process.env.JWT_SECRET) | ||
const profileId = payload.sub | ||
|
||
try { | ||
await logic.deleteTask(profileId, taskId) | ||
} 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(204).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,62 @@ | ||
import bcrypt from 'bcryptjs' | ||
|
||
import { validate, errors } from 'com' | ||
const { SystemError, NotFoundError, CredentialsError } = errors | ||
|
||
import { Profile, Task } from '../data/models.js' | ||
|
||
function completeTask(profileId, taskId, pincode, date) { | ||
validate.id(profileId, 'profile id') | ||
validate.id(taskId, 'task id') | ||
validate.pincode(pincode) | ||
|
||
return (async () => { | ||
let profile | ||
try { | ||
profile = await Profile.findById(profileId) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!profile) | ||
throw new NotFoundError('profile not found') | ||
|
||
if (!profile.role !== 'admin') | ||
throw new CredentialsError('this profile is not admin') | ||
|
||
let match | ||
try { | ||
match = await bcrypt.compare(pincode, profile.pincode) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!match) | ||
throw new CredentialsError('pincode not correct') | ||
|
||
let task | ||
try { | ||
task = await Task.findById(taskId).populate('template') | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!task) | ||
throw new NotFoundError('task not found') | ||
|
||
const newDate = date.getDate() + task.template.periodicity + 1 | ||
|
||
task.date = date.setDate(newDate) | ||
|
||
profile.points += task.template.points | ||
|
||
try { | ||
await task.save() | ||
await profile.save() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
})() | ||
} | ||
|
||
export default completeTask |
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,17 @@ | ||
import mongoose from 'mongoose' | ||
import completeTask from './completeTask.js' | ||
|
||
(async () => { | ||
try { | ||
await mongoose.connect('mongodb://127.0.0.1:27017/test') | ||
|
||
await completeTask('65d79ed33377222a97582a18', '65d79ed43377222a97582a2e', '1234', new Date) | ||
//sessionProfileId, taskId, pincode, date | ||
|
||
console.log('task completed') | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import mongoose from 'mongoose' | ||
import delayTask from './delayTask.js' | ||
|
||
(async () => { | ||
try { | ||
await mongoose.connect('mongodb://127.0.0.1:27017/test') | ||
|
||
await delayTask('65d79ed43377222a97582a2e', new Date) | ||
|
||
console.log('task delayed') | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Profile, Task } from '../data/models.js' | ||
import { validate, errors } from 'com' | ||
const { SystemError, NotFoundError } = errors | ||
|
||
function deleteTask(profileId, taskId) { | ||
validate.id(profileId, 'profile id') | ||
validate.id(taskId, 'task id') | ||
|
||
return (async () => { | ||
let profile | ||
try { | ||
profile = await Profile.findById(profileId).lean() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!profile) | ||
throw new NotFoundError('profile not found') | ||
|
||
let task | ||
try { | ||
task = await Task.findById(taskId) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!task) | ||
throw new NotFoundError('task not found') | ||
|
||
try { | ||
task = await Task.findByIdAndDelete(taskId) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
})() | ||
} | ||
|
||
export default deleteTask |
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 deleteTask from './deleteTask.js' | ||
|
||
(async () => { | ||
try { | ||
await mongoose.connect('mongodb://127.0.0.1:27017/test') | ||
|
||
await deleteTask('65d79ed33377222a97582a18', '65d79ed43377222a97582a36') | ||
|
||
console.log('task deleted') | ||
|
||
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
Oops, something went wrong.