Skip to content

Commit

Permalink
separate assignTask into 2 logics (assignTask and takeTask) b00tc4mp#426
Browse files Browse the repository at this point in the history
; fix calendar view styles overlapping b00tc4mp#449
  • Loading branch information
pankelix committed Mar 16, 2024
1 parent 00786d0 commit 46e564a
Show file tree
Hide file tree
Showing 49 changed files with 254 additions and 105 deletions.
4 changes: 1 addition & 3 deletions staff/miguel-arias/project/api/handlers/assignTaskHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ export default async (req, res) => {
const payload = jwt.verify(token, process.env.JWT_SECRET)
const sessionProfileId = payload.sub

const { taskId } = req.params

const { profileId } = req.body
const { taskId, profileId } = req.params

try {
await logic.assignTask(sessionProfileId, taskId, profileId)
Expand Down
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 @@ -9,6 +9,7 @@ import authenticateProfileHandler from './authenticateProfileHandler.js'
import retrieveRoleHandler from './retrieveRoleHandler.js'
import createTaskHandler from './createTaskHandler.js'
import assignTaskHandler from './assignTaskHandler.js'
import takeTaskHandler from './takeTaskHandler.js'
import delayTaskHandler from './delayTaskHandler.js'
import deleteTaskHandler from './deleteTaskHandler.js'
import completeTaskHandler from './completeTaskHandler.js'
Expand Down Expand Up @@ -39,6 +40,7 @@ export {
retrieveRoleHandler,
createTaskHandler,
assignTaskHandler,
takeTaskHandler,
delayTaskHandler,
deleteTaskHandler,
completeTaskHandler,
Expand Down
40 changes: 40 additions & 0 deletions staff/miguel-arias/project/api/handlers/takeTaskHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import jwt from 'jsonwebtoken'
const { JsonWebTokenError } = jwt

import { errors } from 'com'
const { NotFoundError, ContentError, TokenError, DuplicityError } = 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 } = req.params

try {
await logic.takeTask(sessionProfileId, 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 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()
}
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 @@ -18,6 +18,7 @@ import {
retrieveProfileTasksHandler,
deleteTaskHandler,
assignTaskHandler,
takeTaskHandler,
delayTaskHandler,
completeTaskHandler,
retrieveTemplatesHandler,
Expand Down Expand Up @@ -80,7 +81,9 @@ mongoose.connect(process.env.MONGODB_URL)

server.post('/tasks/:taskId/delete', deleteTaskHandler)

server.patch('/tasks/:taskId/assign', jsonBodyParser, assignTaskHandler)
server.patch('/tasks/:taskId/take', takeTaskHandler)

server.patch('/tasks/:taskId/assign/:profileId', assignTaskHandler)

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

Expand Down
51 changes: 21 additions & 30 deletions staff/miguel-arias/project/api/logic/assignTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { validate, errors } from 'com'
const { SystemError, NotFoundError, PermissionError } = errors

import { Task, Profile } from '../data/models.js'
debugger

function assignTask(sessionProfileId, taskId, profileId) {
validate.id(sessionProfileId, 'session profile id')
validate.id(taskId, 'task id')

if (profileId !== null)
validate.id(profileId, 'profile id')
validate.id(profileId, 'profile id')

return (async () => {
let task
Expand All @@ -21,37 +19,30 @@ function assignTask(sessionProfileId, taskId, profileId) {
if (!task)
throw new NotFoundError('task not found')

let assignToSelf = false
if (profileId === null)
assignToSelf = true

let profile

if (profileId === null) {
try {
profile = await Profile.findById(sessionProfileId).lean()
} catch (error) {
throw new SystemError(error.message)
}
let sessionProfile
try {
sessionProfile = await Profile.findById(sessionProfileId).lean()
} catch (error) {
throw new SystemError(error.message)
}

if (!profile)
throw new NotFoundError('session profile not found')
debugger
if (profile.role !== 'admin'/* && assignToSelf !== true */)
throw new PermissionError('profile is not admin')
if (!sessionProfile)
throw new NotFoundError('session profile not found')

} else if (profileId) {
try {
profile = await Profile.findById(profileId).lean()
} catch (error) {
throw new SystemError(error.message)
}
if (sessionProfile.role !== 'admin')
throw new PermissionError('session profile is not admin')

if (!profile)
throw new NotFoundError('profile not found')
let assignedProfile
try {
assignedProfile = await Profile.findById(profileId).lean()
} catch (error) {
throw new SystemError(error.message)
}

task.assignee = profile._id.toString()
if (!assignedProfile)
throw new NotFoundError('profile not found')

task.assignee = profileId

try {
await task.save()
Expand Down
4 changes: 2 additions & 2 deletions staff/miguel-arias/project/api/logic/assignTask.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Profile, Template, Task } from '../data/models.js'

import { errors } from 'com'
const { NotFoundError, PermissionError } = errors
debugger

describe('assignTask', () => {
before(() => mongoose.connect('mongodb://127.0.0.1:27017/spec'))

Expand All @@ -33,7 +33,7 @@ describe('assignTask', () => {
await assignTask(sessionProfileId, taskId, profileId)

const taskFound = await Task.findById(taskId)
debugger

expect(taskFound.assignee._id.toString()).to.equal(profileId)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Home, Profile } from '../data/models.js'
import { errors } from 'com'
import authenticateProfile from './authenticateProfile.js'
const { NotFoundError, CredentialsError } = errors
debugger

describe('authenticateProfile', () => {
before(async () => await mongoose.connect('mongodb://127.0.0.1:27017/spec'))

Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/changePincode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('changePincode', () => {
expect(error.message).to.equal('profile not found')
}
})
debugger

it('fails on wrong pincode', async () => {
const pincode = random.pincode()

Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/changeProfileColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { validate, errors } from 'com'
const { SystemError, NotFoundError } = errors

import { Profile } from '../data/models.js'
debugger

function changeProfileColor(profileId, color) {
validate.id(profileId, 'profile id')
validate.object(color, 'color')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('changeProfileColor', () => {
await changeProfileColor(profileId, color)

const foundProfile = await Profile.findById(profileId)
debugger

expect(foundProfile._doc.color.name).to.equal(color.name)
expect(foundProfile._doc.color.code).to.equal(color.code)
})
Expand Down
4 changes: 2 additions & 2 deletions staff/miguel-arias/project/api/logic/completeTask.1.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function completeTask(profileId, taskId, pincode, date) {
validate.date(date)

return (async () => {
debugger

let profile
try {
profile = await Profile.findById(profileId)
Expand Down Expand Up @@ -54,7 +54,7 @@ function completeTask(profileId, taskId, pincode, date) {

/* if (date < task.date)
throw new ContentError("tasks can't be completed before their due date") */
debugger

const newDate = new Date(date);
newDate.setDate(newDate.getDate() + task.template.periodicity)

Expand Down
6 changes: 3 additions & 3 deletions staff/miguel-arias/project/api/logic/completeTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function completeTask(profileId, taskId, pincode, date) {
validate.date(date)

return (async () => {
debugger

let profile
try {
profile = await Profile.findById(profileId)
Expand Down Expand Up @@ -57,7 +57,7 @@ function completeTask(profileId, taskId, pincode, date) {

let completedTask
try {
completedTask = await Task.create({home: task.home._id.toString(), template: task.template._id.toString(), assignee : task.assignee ? task.assignee._id.toString(): null, done: true, date: completionDate})
completedTask = await Task.create({ home: task.home._id.toString(), template: task.template._id.toString(), assignee: task.assignee ? task.assignee._id.toString() : null, done: true, date: completionDate, delay: task.delay })
} catch (error) {
throw new SystemError(error.message)
}
Expand All @@ -67,7 +67,7 @@ function completeTask(profileId, taskId, pincode, date) {

let nextTask
try {
nextTask = await Task.create({home: task.home._id.toString(), template: task.template._id.toString(), assignee : task.assignee ? task.assignee._id.toString(): null, done: false, date: newCompletionDate})
nextTask = await Task.create({ home: task.home._id.toString(), template: task.template._id.toString(), assignee: task.assignee ? task.assignee._id.toString() : null, done: false, date: newCompletionDate })
} catch (error) {
throw new SystemError(error.message)
}
Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/completeTask.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Profile, Template, Task } from '../data/models.js'
import { errors } from 'com'
import { ContentError } from 'com/errors.js'
const { NotFoundError, CredentialsError, PermissionError } = errors
debugger

describe('completeTask', () => {
before(() => mongoose.connect('mongodb://127.0.0.1:27017/spec'))

Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/createTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { dayStart } from '@formkit/tempo'

import { Home, Template, Task } from '../data/models.js'
import { ContentError } from 'com/errors.js'
debugger


function createTask(homeId, templateId, date) {
validate.id(homeId, 'home id')
Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/createTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { validate, errors } from 'com'
const { SystemError, NotFoundError, ContentError } = errors

import { Home, Room, Template } from '../data/models.js'
debugger


function createTemplate(homeId, name, periodicityNumber, periodicityRange, rooms, points) {
validate.id(homeId, 'home id')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('createTemplate', async () => {
points = Number(points.replace(0, 1))

await createTemplate(homeId, name, periodicityNumber, periodicityRange, rooms, points)
debugger

try {
const template = await Template.findOne({ home: homeId })

Expand Down
12 changes: 6 additions & 6 deletions staff/miguel-arias/project/api/logic/delayTask.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { Profile, Template, Task } from '../data/models.js'

import { errors } from 'com'
const { NotFoundError, PermissionError } = errors
debugger

describe('delayTask', () => {
before(() => mongoose.connect('mongodb://127.0.0.1:27017/spec'))

beforeEach(() => Promise.all([Profile.deleteMany(), Task.deleteMany()]))

it.skip('succeeds on existing profile and template', async () => {
it('succeeds on existing profile and template', async () => {
const profile = await Profile.create({ home: random.id(), name: random.name(), pincode: random.pincode() })

const profileId = profile._id.toString()
Expand All @@ -30,17 +30,17 @@ describe('delayTask', () => {
const task = await Task.create({ home: random.id(), template: templateId, assignee: random.id() })

const taskId = task._id.toString()
debugger
expect(task.date).to.equal(new Date())

expect(task.date.toLocaleDateString('en-CA')).to.equal(new Date().toLocaleDateString('en-CA'))
expect(task.delay).to.equal(0)

const date = (addDay(new Date(), 1))

await delayTask(profileId, taskId, date)

const taskFound = await Template.findById(templateId)
const taskFound = await Task.findById(taskId)

expect(taskFound.date).to.equal(date)
expect(taskFound.date.toLocaleDateString('en-CA')).to.equal(date.toLocaleDateString('en-CA'))
expect(taskFound.delay).to.equal(1)
})

Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/deleteProfile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Profile, Task } from '../data/models.js'

import { errors } from 'com'
const { NotFoundError, PermissionError } = errors
debugger

describe('deleteProfile', () => {
before(() => mongoose.connect('mongodb://127.0.0.1:27017/spec'))

Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/deleteRoom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Profile, Room, Template } from '../data/models.js'

import { errors } from 'com'
const { NotFoundError, PermissionError } = errors
debugger

describe('deleteRoom', () => {
before(() => mongoose.connect('mongodb://127.0.0.1:27017/spec'))

Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/deleteTask.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Profile, Task } from '../data/models.js'
import { validate, errors } from 'com'
const { SystemError, NotFoundError, PermissionError } = errors
debugger

function deleteTask(profileId, taskId) {
validate.id(profileId, 'profile id')
validate.id(taskId, 'task id')
Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/deleteTask.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Profile, Task } from '../data/models.js'

import { errors } from 'com'
const { NotFoundError, PermissionError } = errors
debugger

describe('deleteTask', () => {
before(() => mongoose.connect('mongodb://127.0.0.1:27017/spec'))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Profile, Template, Task } from '../data/models.js'

import { errors } from 'com'
const { NotFoundError, PermissionError } = errors
debugger

describe('deleteTemplate', () => {
before(() => mongoose.connect('mongodb://127.0.0.1:27017/spec'))

Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/editTemplate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Profile, Template } from '../data/models.js'

import { errors } from 'com'
const { NotFoundError, PermissionError, ContentError } = errors
debugger

describe('editTemplate', () => {
before(() => mongoose.connect('mongodb://127.0.0.1:27017/spec'))

Expand Down
Loading

0 comments on commit 46e564a

Please sign in to comment.