Skip to content

Commit

Permalink
adapting logics to echoes b00tc4mp#426
Browse files Browse the repository at this point in the history
  • Loading branch information
pankelix committed Mar 11, 2024
1 parent 17e1d0a commit dd1ac5a
Show file tree
Hide file tree
Showing 30 changed files with 291 additions and 69 deletions.
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MONGODB_URL = mongodb://127.0.0.1:27017/project2
MONGODB_URL = mongodb://127.0.0.1:27017/project
TEST_MONGODB_URL = mongodb://127.0.0.1:27017/test
PORT = 8000
JWT_SECRET = Cristiano.
Expand Down
3 changes: 3 additions & 0 deletions staff/miguel-arias/project/api/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ const task = new Schema({
delay: {
type: Number,
default: 0
},
oldId: {
type: String
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default async (req, res) => {

try {
await logic.changePincode(profileId, oldPincode, newPincode)

res.status(204).send()
} catch (error) {
let status = 500

Expand All @@ -31,6 +33,4 @@ export default async (req, res) => {

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 @@ -15,6 +15,8 @@ export default async (req, res) => {

try {
await logic.changeProfileColor(profileId, color)

res.status(204).send()
} catch (error) {
let status = 500

Expand All @@ -31,6 +33,4 @@ export default async (req, res) => {

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 @@ -17,6 +17,8 @@ export default async (req, res) => {

try {
await logic.completeTask(sessionProfileId, taskId, pincode, date)

res.status(204).send()
} catch (error) {
let status = 500

Expand All @@ -25,6 +27,9 @@ export default async (req, res) => {
error = new TokenError(error.message)
}

if (error instanceof PermissionError)
status = 401

if (error instanceof NotFoundError)
status = 404

Expand All @@ -34,5 +39,4 @@ export default async (req, res) => {
res.status(status).json({ error: error.constructor.name, message: error.message })
}

res.status(204).send()
}
4 changes: 2 additions & 2 deletions staff/miguel-arias/project/api/handlers/delayTaskHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default async (req, res) => {

try {
await logic.delayTask(profileId, taskId, date)

res.status(204).send()
} catch (error) {
let status = 500

Expand All @@ -33,6 +35,4 @@ export default async (req, res) => {

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 @@ -15,6 +15,8 @@ export default async (req, res) => {

try {
await logic.deleteProfile(sessionProfileId, profileId)

res.status(204).send()
} catch (error) {
let status = 500

Expand All @@ -31,6 +33,4 @@ export default async (req, res) => {

res.status(status).json({ error: error.constructor.name, message: error.message })
}

res.status(204).send()
}
4 changes: 2 additions & 2 deletions staff/miguel-arias/project/api/handlers/deleteRoomHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default async (req, res) => {

try {
await logic.deleteRoom(profileId, roomId)

res.status(204).send()
} catch (error) {
let status = 500

Expand All @@ -31,6 +33,4 @@ export default async (req, res) => {

res.status(status).json({ error: error.constructor.name, message: error.message })
}

res.status(204).send()
}
4 changes: 2 additions & 2 deletions staff/miguel-arias/project/api/handlers/deleteTaskHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default async (req, res) => {

try {
await logic.deleteTask(profileId, taskId)

res.status(204).send()
} catch (error) {
let status = 500

Expand All @@ -31,6 +33,4 @@ export default async (req, res) => {

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 @@ -15,6 +15,8 @@ export default async (req, res) => {

try {
await logic.deleteTemplate(profileId, templateId)

res.status(204).send()
} catch (error) {
let status = 500

Expand All @@ -31,6 +33,4 @@ export default async (req, res) => {

res.status(status).json({ error: error.constructor.name, message: error.message })
}

res.status(204).send()
}
4 changes: 2 additions & 2 deletions staff/miguel-arias/project/api/handlers/editRoleHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default async (req, res) => {

try {
await logic.editRole(sessionProfileId, profileId, role)

res.status(204).send()
} catch (error) {
let status = 500

Expand All @@ -33,6 +35,4 @@ export default async (req, res) => {

res.status(status).json({ error: error.constructor.name, message: error.message })
}

res.status(204).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 @@ -24,6 +24,7 @@ import registerProfileHandler from './registerProfileHandler.js'
import changeProfileColorHandler from './changeProfileColorHandler.js'
import changePincodeHandler from './changePincodeHandler.js'
import uploadAvatarHandler from './uploadAvatarHandler.js'
import materializeTaskHandler from './materializeTaskHandler.js'

export {
registerHomeHandler,
Expand Down Expand Up @@ -52,4 +53,5 @@ export {
changeProfileColorHandler,
changePincodeHandler,
uploadAvatarHandler,
materializeTaskHandler,
}
40 changes: 40 additions & 0 deletions staff/miguel-arias/project/api/handlers/materializeTaskHandler.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 homeId = payload.sub

const { task } = req.body
try {
const materializedTaskId = await logic.materializeTask(homeId, task)

res.json(materializedTaskId)
} 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

if (error instanceof DuplicityError)
status = 409

res.status(status).json({ error: error.constructor.name, message: error.message })
}

res.status(201).send()
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default async (req, res) => {

try {
await logic.redeemPoints(sessionProfileId, profileId, points)

res.status(204).send()
} catch (error) {
let status = 500

Expand All @@ -33,6 +35,4 @@ export default async (req, res) => {

res.status(status).json({ error: error.constructor.name, message: error.message })
}

res.status(204).send()
}
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 @@ -33,6 +33,7 @@ import {
changeProfileColorHandler,
changePincodeHandler,
uploadAvatarHandler,
materializeTaskHandler,
} from './handlers/index.js'

mongoose.connect(process.env.MONGODB_URL)
Expand Down Expand Up @@ -72,6 +73,8 @@ mongoose.connect(process.env.MONGODB_URL)

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

server.post('/tasks/:taskId', jsonBodyParser, materializeTaskHandler)

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

server.patch('/tasks/:taskId/assign', jsonBodyParser, assignTaskHandler)
Expand Down
19 changes: 15 additions & 4 deletions staff/miguel-arias/project/api/logic/completeTask.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import bcrypt from 'bcryptjs'

import { validate, errors } from 'com'
const { SystemError, NotFoundError, CredentialsError } = errors
const { SystemError, NotFoundError, CredentialsError, PermissionError } = errors

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

function completeTask(profileId, taskId, pincode, date) {
validate.id(profileId, 'profile id')
Expand Down Expand Up @@ -46,11 +46,22 @@ function completeTask(profileId, taskId, pincode, date) {
if (!task)
throw new NotFoundError('task not found')

if (task.done === true)
throw new PermissionError("this task is already done")

date = new Date(date)

const newDate = date.getDate() + task.template.periodicity
/* if (date < task.date)
throw new ContentError("tasks can't be completed before their due date") */

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

task.date = newDate

task.done = true

task.date = date.setDate(newDate)
task.assignee = undefined

profile.points += task.template.points

Expand Down
2 changes: 1 addition & 1 deletion staff/miguel-arias/project/api/logic/completeTask.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import completeTask from './completeTask.js'
try {
await mongoose.connect('mongodb://127.0.0.1:27017/project')

await completeTask('65da45873f666061bc54cf3a', '65da45873f666061bc54cf52', '1234', '2024-03-04')
await completeTask('65da45873f666061bc54cf3a', '65ee329a9003ad5ee73ac9cc', '1234', '2024-03-04')
//sessionProfileId, taskId, pincode, date

console.log('task completed')
Expand Down
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 @@ -24,6 +24,7 @@ import registerProfile from './registerProfile.js'
import changeProfileColor from './changeProfileColor.js'
import changePincode from './changePincode.js'
import uploadAvatar from './uploadAvatar.js'
import materializeTask from './materializeTask.js'

const logic = {
registerHome,
Expand Down Expand Up @@ -52,6 +53,7 @@ const logic = {
changeProfileColor,
changePincode,
uploadAvatar,
materializeTask,
}

export default logic
41 changes: 41 additions & 0 deletions staff/miguel-arias/project/api/logic/materializeTask.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 { dayStart } from '@formkit/tempo'

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

function materializeTask(homeId, task) {
validate.id(homeId, 'home id')
validate.object(task)

return (async () => {
let home
try {
home = await Home.findById(homeId).lean()
} catch (error) {
throw new SystemError(error.message)
}

if (!home)
throw new NotFoundError('home not found')

let date = new Date(task.date)
const today = dayStart(new Date())

if (date < today)
throw new ContentError('date must be after today')

try {
const materializedTask = await Task.create({ home: homeId, template: task.template._id, date: date, oldId: task.id })

return materializedTask._id.toString()
} catch (error) {
throw new SystemError(error.message)
}
})()
}

export default materializeTask
20 changes: 20 additions & 0 deletions staff/miguel-arias/project/api/logic/materializeTask.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import dotenv from 'dotenv'
dotenv.config()

import mongoose from 'mongoose'
import materializeTask from './materializeTask.js'

(async () => {
try {
await mongoose.connect('mongodb://127.0.0.1:27017/project')

const task = await materializeTask('65da45873f666061bc54cf1b', { home: '65da45873f666061bc54cf1b', template: { home: "65da45873f666061bc54cf1b", name: 'dust', periodicity: 4, points: 5, rooms: ['65da45873f666061bc54cf28', '65da45873f666061bc54cf32', '65da45873f666061bc54cf38'], _id: "65da45873f666061bc54cf44" }, assignee: '', done: false, date: "2024-03-23T22:59:59.999Z", id: "65da45873f666061bc54cf52_1" })
// homeId, task

console.log('task registered', task)

await mongoose.disconnect()
} catch (error) {
console.log(error)
}
})()
Loading

0 comments on commit dd1ac5a

Please sign in to comment.