Skip to content

Commit

Permalink
implement logics of delay, delete and complete tasks b00tc4mp#426
Browse files Browse the repository at this point in the history
  • Loading branch information
pankelix committed Feb 28, 2024
1 parent 7403862 commit cb8720f
Show file tree
Hide file tree
Showing 20 changed files with 391 additions and 41 deletions.
4 changes: 4 additions & 0 deletions staff/miguel-arias/project/api/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ const profile = new Schema({
role: {
type: String,
default: 'User'
},
points: {
type: Number,
default: 0
}
})

Expand Down
38 changes: 38 additions & 0 deletions staff/miguel-arias/project/api/handlers/completeTaskHandler.js
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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ export default async (req, res) => {
res.status(status).json({ error: error.constructor.name, message: error.message })
}

res.status(201).send()
res.status(204).send()
}
36 changes: 36 additions & 0 deletions staff/miguel-arias/project/api/handlers/deleteTaskHandler.js
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()
}
4 changes: 4 additions & 0 deletions staff/miguel-arias/project/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import retrieveRoleHandler from './retrieveRoleHandler.js'
import createTaskHandler from './createTaskHandler.js'
import assignTaskHandler from './assignTaskHandler.js'
import delayTaskHandler from './delayTaskHandler.js'
import deleteTaskHandler from './deleteTaskHandler.js'
import completeTaskHandler from './completeTaskHandler.js'

export {
registerHomeHandler,
Expand All @@ -22,4 +24,6 @@ export {
createTaskHandler,
assignTaskHandler,
delayTaskHandler,
deleteTaskHandler,
completeTaskHandler,
}
3 changes: 3 additions & 0 deletions staff/miguel-arias/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
createTaskHandler,
assignTaskHandler,
delayTaskHandler,
deleteTaskHandler
} from './handlers/index.js'

mongoose.connect(process.env.MONGODB_URL)
Expand All @@ -40,6 +41,8 @@ mongoose.connect(process.env.MONGODB_URL)

server.patch('/tasks/delay', jsonBodyParser, delayTaskHandler)

server.post('/tasks/delete', jsonBodyParser, deleteTaskHandler)

server.get('/tasks', retrieveTasksHandler)

server.get('/profiles', retrieveProfilesHandler)
Expand Down
62 changes: 62 additions & 0 deletions staff/miguel-arias/project/api/logic/completeTask.js
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
17 changes: 17 additions & 0 deletions staff/miguel-arias/project/api/logic/completeTask.test.js
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)
}
})()
16 changes: 16 additions & 0 deletions staff/miguel-arias/project/api/logic/delayTask.test.js
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)
}
})()
38 changes: 38 additions & 0 deletions staff/miguel-arias/project/api/logic/deleteTask.js
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
16 changes: 16 additions & 0 deletions staff/miguel-arias/project/api/logic/deleteTask.test.js
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)
}
})()
2 changes: 2 additions & 0 deletions staff/miguel-arias/project/api/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import retrieveRole from './retrieveRole.js'
import createTask from './createTask.js'
import assignTask from './assignTask.js'
import delayTask from './delayTask.js'
import deleteTask from './deleteTask.js'

const logic = {
registerHome,
Expand All @@ -22,6 +23,7 @@ const logic = {
createTask,
assignTask,
delayTask,
deleteTask
}

export default logic
Loading

0 comments on commit cb8720f

Please sign in to comment.