Skip to content

Commit

Permalink
assign task logic b00tc4mp#426
Browse files Browse the repository at this point in the history
  • Loading branch information
pankelix committed Feb 27, 2024
1 parent bfdbba5 commit 80313ca
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 38 deletions.
45 changes: 45 additions & 0 deletions staff/miguel-arias/project/api/handlers/assignTaskHandler.js
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()
}
4 changes: 3 additions & 1 deletion staff/miguel-arias/project/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import retrieveTemplatesHandler from './retrieveTemplatesHandler.js'
import authenticateProfileHandler from './authenticateProfileHandler.js'
import retrieveRoleHandler from './retrieveRoleHandler.js'
import createTaskHandler from './createTaskHandler.js'
import assignTaskHandler from './assignTaskHandler.js'

export {
registerHomeHandler,
Expand All @@ -17,5 +18,6 @@ export {
retrieveTemplatesHandler,
authenticateProfileHandler,
retrieveRoleHandler,
createTaskHandler
createTaskHandler,
assignTaskHandler,
}
5 changes: 4 additions & 1 deletion staff/miguel-arias/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
retrieveTemplatesHandler,
authenticateProfileHandler,
retrieveRoleHandler,
createTaskHandler
createTaskHandler,
assignTaskHandler,
} from './handlers/index.js'

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

server.post('/tasks', jsonBodyParser, createTaskHandler)

server.patch('/tasks', jsonBodyParser, assignTaskHandler)

server.get('/tasks', retrieveTasksHandler)

server.get('/profiles', retrieveProfilesHandler)
Expand Down
41 changes: 41 additions & 0 deletions staff/miguel-arias/project/api/logic/assignTask.js
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
16 changes: 16 additions & 0 deletions staff/miguel-arias/project/api/logic/assignTask.test.js
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)
}
})()
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 @@ -7,6 +7,7 @@ import retrieveTemplates from './retrieveTemplates.js'
import authenticateProfile from './authenticateProfile.js'
import retrieveRole from './retrieveRole.js'
import createTask from './createTask.js'
import assignTask from './assignTask.js'

const logic = {
registerHome,
Expand All @@ -18,6 +19,7 @@ const logic = {
authenticateProfile,
retrieveRole,
createTask,
assignTask,
}

export default logic
23 changes: 14 additions & 9 deletions staff/miguel-arias/project/app/src/components/Calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ function Calendar(props) {
refreshTemplates()
}, [props.stamp])

const openModal = (taskId) => {
const handleOnTaskClick = (taskId) => {
setTask(taskId)
setView('react-to-task-view')
}

const closeModal = () => {
const onCloseClick = () => {
setView(null)
}

Expand All @@ -90,16 +90,21 @@ function Calendar(props) {
try {
await logic.createTask(templateId, dateObject)
refreshTasks()
setView(null)
} catch (error) {
context.handleError(error)
}
}

const handleAssignThisTask = async (profileId) => {
if (typeof profileId !== 'string')
const handleAssignThisTask = async (taskId, profileId) => {
/* if (typeof profileId !== 'string')
profileId = null
if (typeof taskId !== 'string')
taskId = null */
try {
await logic.assignTask(profileId)
await logic.assignTask(taskId, profileId)
refreshTasks()
setView(null)
} catch (error) {
context.handleError(error)
}
Expand All @@ -114,20 +119,20 @@ function Calendar(props) {

<Button>Filter</Button>

{tasks.map(task => <Task key={task.id} task={task} profileName={profiles.map(profile => task.assignee === profile.id ? profile.name : '')} onTaskClick={openModal} />)}
{tasks.map(task => <Task key={task.id} task={task} profileName={profiles.map(profile => task.assignee === profile.id ? profile.name : '')} onTaskClick={handleOnTaskClick} />)}

{view === 'react-to-task-view' && <Container>
{role !== null && <Button onClick={handleAssignThisTask}>Assign this task</Button>}
{role !== null && <Button onClick={() => handleAssignThisTask(task, null)}>Assign this task</Button>}
{role === 'admin' && <Button onClick={handleAssignThisTaskTo}>Assign this task to...</Button>}
{role !== null && <Button>Complete this task</Button>}
{role !== null && <Button>Delay this task</Button>}
{role === 'admin' && <Button>Edit this task</Button>}
{role === 'admin' && <Button>Delete this task</Button>}
{role !== null && <Button onClick={closeModal}>Close</Button>}
{role !== null && <Button onClick={onCloseClick}>Close</Button>}
</Container>}

{view === 'assign-task-view' && <Container>
{profiles.map(profile => <Button onClick={() => handleAssignThisTask(profile.id)}>{profile.name}</Button>)}
{profiles.map(profile => <Button onClick={() => handleAssignThisTask(task, profile.id)}>{profile.name}</Button>)}
</Container>}

{view === 'new-task-view' && <Container>
Expand Down
56 changes: 29 additions & 27 deletions staff/miguel-arias/project/app/src/logic/assignTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,41 @@ const { SystemError } = errors

import session from './session'

const createTask = (templateId, date) => {
validate.id(templateId, 'templateId')
validate.date(date)
const assignTask = (taskId, profileId) => {
if (profileId !== null)
validate.id(profileId, 'profileId')

validate.id(taskId, 'taskId')

const req = {
method: 'POST', // PAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAATCHHHHHH!!
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.token}`
Authorization: `Bearer ${session.profileToken}`
},
body: JSON.stringify({ templateId, date })
body: JSON.stringify({ taskId, profileId })
}

return (async () => {
let res
try {
res = await fetch(`${import.meta.env.VITE_API_URL}/tasks`, 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)
}
})()
return (async () => {
let res
try {
res = await fetch(`${import.meta.env.VITE_API_URL}/tasks`, 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 createTask
export default assignTask
2 changes: 2 additions & 0 deletions staff/miguel-arias/project/app/src/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import retrieveTemplates from './retrieveTemplates.js'
import loginProfile from './loginProfile.js'
import retrieveRole from './retrieveRole.js'
import createTask from './createTask.js'
import assignTask from './assignTask.js'

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

export default logic

0 comments on commit 80313ca

Please sign in to comment.