Skip to content

Commit

Permalink
add delay 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 a7ad45e commit 037b015
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 12 deletions.
36 changes: 36 additions & 0 deletions staff/miguel-arias/project/api/handlers/delayTaskHandler.js
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()
}
2 changes: 2 additions & 0 deletions staff/miguel-arias/project/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import authenticateProfileHandler from './authenticateProfileHandler.js'
import retrieveRoleHandler from './retrieveRoleHandler.js'
import createTaskHandler from './createTaskHandler.js'
import assignTaskHandler from './assignTaskHandler.js'
import delayTaskHandler from './delayTaskHandler.js'

export {
registerHomeHandler,
Expand All @@ -20,4 +21,5 @@ export {
retrieveRoleHandler,
createTaskHandler,
assignTaskHandler,
delayTaskHandler,
}
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 @@ -16,6 +16,7 @@ import {
retrieveRoleHandler,
createTaskHandler,
assignTaskHandler,
delayTaskHandler,
} from './handlers/index.js'

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

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

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

server.get('/tasks', retrieveTasksHandler)

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

const logic = {
registerHome,
Expand All @@ -20,6 +21,7 @@ const logic = {
retrieveRole,
createTask,
assignTask,
delayTask,
}

export default logic
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/retrieveTasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function retrieveTasks(homeId) {

let tasks
try {
tasks = await Task.find({ home: homeId }).populate('template', '-__v').select('-__v').lean()
tasks = await Task.find({ home: homeId }).populate('template', '-__v').select('-__v').sort({ date: 1 }).lean()
} catch (error) {
throw new SystemError(error.message)
}
Expand Down
41 changes: 33 additions & 8 deletions staff/miguel-arias/project/app/src/components/Calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function Calendar(props) {
const refreshTasks = async () => {
try {
const tasks = await props.loadTasks()
// tasks = await logic.orderTasks(tasks)
setTasks(tasks)
} catch (error) {
context.handleError(error)
Expand All @@ -65,7 +66,7 @@ function Calendar(props) {
setView('react-to-task-view')
}

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

Expand Down Expand Up @@ -97,10 +98,6 @@ function Calendar(props) {
}

const handleAssignThisTask = async (taskId, profileId) => {
/* if (typeof profileId !== 'string')
profileId = null
if (typeof taskId !== 'string')
taskId = null */
try {
await logic.assignTask(taskId, profileId)
refreshTasks()
Expand All @@ -114,6 +111,26 @@ function Calendar(props) {
setView('assign-task-view')
}

const onDelayTaskClick = () => {
setView('delay-task-view')
}

const handleDelaySubmit = async (event) => {
event.preventDefault()
const date = event.target.delayDate.value
const dateObject = date ? new Date(date) : null

const taskId = event.nativeEvent.submitter.value
try {
await logic.delayTask(taskId, dateObject)
refreshTasks()
setView(null)
} catch (error) {
context.handleError(error)
}

}

return <Container>
<h1>Tasks</h1>

Expand All @@ -125,24 +142,32 @@ function Calendar(props) {
{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 !== null && <Button onClick={onDelayTaskClick}>Delay this task</Button>}
{role === 'admin' && <Button>Edit this task</Button>}
{role === 'admin' && <Button>Delete this task</Button>}
{role !== null && <Button onClick={onCloseClick}>Close</Button>}
{role !== null && <Button onClick={onCancelClick}>Cancel</Button>}
</Container>}

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

{view === 'delay-task-view' && <Container>
<Form onSubmit={handleDelaySubmit}>
<Input id='delayDate' type={'date'} required={true}></Input>
<Button type='submit' value={task}>Delay</Button>
<Button onClick={onCancelClick}>Cancel</Button>
</Form>
</Container>}

{view === 'new-task-view' && <Container>
<Button onClick={handleNewTaskClick}>Create new task</Button>
<Button onClick={handleProposeTaskClick}>Propose task</Button>
</Container>}

{view === 'propose-task' && <Container>
<Form onSubmit={handleProposeTaskSubmit}>
<Input id='date' type={'date'} placeholder={'Date'} required={true}></Input>
<Input id='date' type={'date'} required={true}></Input>
{templates.map(template => <Button name='template' type='submit' value={template.id}><Template key={template.id} template={template} /></Button>)}
</Form>
</Container>}
Expand Down
4 changes: 2 additions & 2 deletions staff/miguel-arias/project/app/src/logic/assignTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import session from './session'

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

validate.id(taskId, 'taskId')
validate.id(taskId, 'task id')

const req = {
method: 'PATCH',
Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/app/src/logic/createTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { SystemError } = errors
import session from './session'

const createTask = (templateId, date) => {
validate.id(templateId, 'templateId')
validate.id(templateId, 'template id')
validate.date(date)

const req = {
Expand Down
40 changes: 40 additions & 0 deletions staff/miguel-arias/project/app/src/logic/delayTask.js
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
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 @@ -10,6 +10,7 @@ import loginProfile from './loginProfile.js'
import retrieveRole from './retrieveRole.js'
import createTask from './createTask.js'
import assignTask from './assignTask.js'
import delayTask from './delayTask.js'

const logic = {
registerHome,
Expand All @@ -24,6 +25,7 @@ const logic = {
retrieveRole,
createTask,
assignTask,
delayTask,
}

export default logic

0 comments on commit 037b015

Please sign in to comment.