forked from b00tc4mp/isdi-parttime-202309
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adapting logics to echoes b00tc4mp#426
- Loading branch information
Showing
30 changed files
with
291 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,9 @@ const task = new Schema({ | |
delay: { | ||
type: Number, | ||
default: 0 | ||
}, | ||
oldId: { | ||
type: String | ||
} | ||
}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
staff/miguel-arias/project/api/handlers/materializeTaskHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
staff/miguel-arias/project/api/logic/materializeTask.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
})() |
Oops, something went wrong.