Skip to content

Commit

Permalink
add more tailwind to components b00tc4mp#449; fix compenetration prob…
Browse files Browse the repository at this point in the history
…lems between completeTask and materializeTask b00tc4mp#427
  • Loading branch information
pankelix committed Mar 15, 2024
1 parent 05cfbd4 commit 00786d0
Show file tree
Hide file tree
Showing 43 changed files with 905 additions and 450 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/project
MONGODB_URL = mongodb://127.0.0.1:27017/project2
TEST_MONGODB_URL = mongodb://127.0.0.1:27017/test
PORT = 8000
JWT_SECRET = Cristiano.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import jwt from 'jsonwebtoken'
const { JsonWebTokenError } = jwt

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

import logic from '../logic/index.js'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export default async (req, res) => {
const payload = jwt.verify(token, process.env.JWT_SECRET)
const homeId = payload.sub

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

res.json(materializedTaskId)
} catch (error) {
Expand Down
78 changes: 78 additions & 0 deletions staff/miguel-arias/project/api/logic/completeTask.1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import bcrypt from 'bcryptjs'

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

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

function completeTask(profileId, taskId, pincode, date) {
validate.id(profileId, 'profile id')
validate.id(taskId, 'task id')
validate.pincode(pincode)
validate.date(date)

return (async () => {
debugger
let profile
try {
profile = await Profile.findById(profileId)
} catch (error) {
throw new SystemError(error.message)
}

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

if (profile.role !== 'admin')
throw new PermissionError('profile is not admin')

let match
try {
match = await bcrypt.compare(pincode, profile.pincode)
} catch (error) {
throw new SystemError(error.message)
}

if (!match)
throw new CredentialsError('pincode not correct')

let task
try {
task = await Task.findById(taskId).populate('template')
} catch (error) {
throw new SystemError(error.message)
}

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

if (task.done === true)
throw new ContentError('this task is already done')

date = new Date(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)

task.date = newDate

task.done = true

task.assignee = undefined

profile.points += task.template.points

try {
await task.save()
await profile.save()
} catch (error) {
throw new SystemError(error.message)
}
})()
}

export default completeTask
35 changes: 25 additions & 10 deletions staff/miguel-arias/project/api/logic/completeTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,39 @@ function completeTask(profileId, taskId, pincode, date) {
if (task.done === true)
throw new ContentError('this task is already done')

date = new Date(date)
let completionDate = new Date(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)
/* if (completionDate < task.completionDate)
throw new ContentError("tasks can't be completed before their due completionDate") */

task.date = newDate
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})
} catch (error) {
throw new SystemError(error.message)
}

const newCompletionDate = new Date(completionDate);
newCompletionDate.setDate(newCompletionDate.getDate() + task.template.periodicity)

task.done = true
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})
} catch (error) {
throw new SystemError(error.message)
}

task.assignee = undefined
try {
await Task.findByIdAndDelete(taskId)
} catch (error) {
throw new SystemError(error.message)
}

profile.points += task.template.points

try {
await task.save()
await completedTask.save()
await nextTask.save()
await profile.save()
} catch (error) {
throw new SystemError(error.message)
Expand Down
6 changes: 3 additions & 3 deletions staff/miguel-arias/project/api/logic/completeTask.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import completeTask from './completeTask.js'
(async () => {
const date = '2024-03-01'
try {
await mongoose.connect('mongodb://127.0.0.1:27017/project')
await mongoose.connect('mongodb://127.0.0.1:27017/test')

await completeTask('65da45873f666061bc54cf3a', '65ee329a9003ad5ee73ac9cc', '1234', '2024-03-04')
//sessionProfileId, taskId, pincode, date
await completeTask('65f4797be26873325602dda6', '65f4797be26873325602ddd9', '1234', '2024-03-14')
//sessionProfileId, taskId, pincode, completionDate

console.log('task completed')

Expand Down
9 changes: 5 additions & 4 deletions staff/miguel-arias/project/api/logic/materializeTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import { Home, Task } from '../data/models.js'
import { ContentError } from 'com/errors.js'
debugger

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

return (async () => {
let home
Expand All @@ -22,14 +23,14 @@ function materializeTask(homeId, task) {
if (!home)
throw new NotFoundError('home not found')

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

if (today > date)
if (today > materializationDate)
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 })
const materializedTask = await Task.create({ home: homeId, template: task.template._id, date: materializationDate, oldId: task.id })

return materializedTask._id.toString()
} catch (error) {
Expand Down
41 changes: 21 additions & 20 deletions staff/miguel-arias/project/api/logic/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Home, Room, Profile, Template, Task } from '../data/models.js'

(async () => {
try {
await mongoose.connect('mongodb://127.0.0.1:27017/project')
await mongoose.connect('mongodb://127.0.0.1:27017/test')
await Home.deleteMany()
await Room.deleteMany()
await Profile.deleteMany()
Expand Down Expand Up @@ -65,10 +65,10 @@ import { Home, Room, Profile, Template, Task } from '../data/models.js'
let michael
let john
try {
peter = await registerProfile(mansion.id, 'Peter Pan', '1234', 'green', 'admin')
wendy = await registerProfile(mansion.id, 'Wendy Darling', '1234', 'blue', 'admin')
michael = await registerProfile(mansion.id, 'Michael Darling', '1234', 'white')
john = await registerProfile(mansion.id, 'John Darling', '1234', 'pink')
peter = await registerProfile(mansion.id, 'Peter Pan', '1234', { name: 'color1', code: 'code1' }, 'admin')
wendy = await registerProfile(mansion.id, 'Wendy Darling', '1234', { name: 'color2', code: 'code2' }, 'admin')
michael = await registerProfile(mansion.id, 'Michael Darling', '1234', { name: 'color3', code: 'code3' })
john = await registerProfile(mansion.id, 'John Darling', '1234', { name: 'color4', code: 'code4' })
} catch (error) {
console.log(error)
}
Expand All @@ -82,26 +82,27 @@ import { Home, Room, Profile, Template, Task } from '../data/models.js'
let changeSheets2
let cleanOven
try {
cleanShower = await createTemplate(mansion.id, 'clean-shower', [kidsBathroom.id, adultsBathroom.id], 14, 15)
dust = await createTemplate(mansion.id, 'dust', [livingRoom.id, hall.id, office.id], 4, 5)
doDishes = await createTemplate(mansion.id, 'do-dishes', [kitchen.id], 1, 2)
lawnRaking = await createTemplate(mansion.id, 'lawn-raking', [terrace.id, yard.id], 28, 15)
changeSheets = await createTemplate(mansion.id, 'change-sheets', [kidsBedroom.id], 7, 2)
changeSheets2 = await createTemplate(mansion.id, 'change-sheets', [adultsBedroom.id], 7, 2)
cleanOven = await createTemplate(mansion.id, 'clean-oven', [kitchen.id], 21, 10)
//homeId, name, periodicityNumber, periodicityRange, rooms, points
cleanShower = await createTemplate(mansion.id, 'clean-shower', 1, 'week', [kidsBathroom.id, adultsBathroom.id], 15)
dust = await createTemplate(mansion.id, 'dust', 2, 'week', [livingRoom.id, hall.id, office.id], 5)
doDishes = await createTemplate(mansion.id, 'do-dishes', 6, 'day', [kitchen.id], 2)
lawnRaking = await createTemplate(mansion.id, 'lawn-raking', 4, 'week', [terrace.id, yard.id], 15)
changeSheets = await createTemplate(mansion.id, 'change-sheets', 3, 'day', [kidsBedroom.id], 2)
changeSheets2 = await createTemplate(mansion.id, 'change-sheets', 2, 'week', [adultsBedroom.id], 2)
cleanOven = await createTemplate(mansion.id, 'clean-oven', 3, 'week', [kitchen.id], 10)
} catch (error) {
console.log(error)
}

//Task

await createTask(mansion.id, cleanShower.id, peter.id)
await createTask(mansion.id, dust.id, wendy.id)
await createTask(mansion.id, doDishes.id, michael.id)
await createTask(mansion.id, lawnRaking.id, john.id)
await createTask(mansion.id, changeSheets.id, michael.id)
await createTask(mansion.id, changeSheets2.id, peter.id)
await createTask(mansion.id, cleanOven.id, wendy.id)
//homeId, templateId, date
await createTask(mansion.id, cleanShower.id, '2024-03-15')
await createTask(mansion.id, dust.id, '2024-03-16')
await createTask(mansion.id, doDishes.id, '2024-03-17')
await createTask(mansion.id, lawnRaking.id, '2024-03-18')
await createTask(mansion.id, changeSheets.id, '2024-03-19')
await createTask(mansion.id, changeSheets2.id, '2024-03-20')
await createTask(mansion.id, cleanOven.id, '2024-03-21')

console.log('database populated')

Expand Down
3 changes: 0 additions & 3 deletions staff/miguel-arias/project/api/logic/retrieveProfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ function retrieveProfiles(homeId) {
throw new SystemError(error.message)
}

if (profiles.length === 0)
throw new NotFoundError('profile not found')

profiles.forEach(profile => {
profile.id = profile._id.toString()
delete profile._id
Expand Down
14 changes: 0 additions & 14 deletions staff/miguel-arias/project/api/logic/retrieveProfiles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,5 @@ describe('retrieveProfiles', () => {
}
})

it('fails on non-existing profile', async () => {
const home = await Home.create({ name: random.name(), email: random.email(), password: random.password() })

const homeId = home._id.toString()

try {
await retrieveProfiles(homeId)
throw new Error('should not reach this point')
} catch (error) {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('profile not found')
}
})

after(async () => await mongoose.disconnect())
})
3 changes: 0 additions & 3 deletions staff/miguel-arias/project/api/logic/retrieveRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ function retrieveRooms(homeId) {
throw new SystemError(error.message)
}

if (rooms.length === 0)
throw new NotFoundError('room not found')

rooms.forEach(room => {
room.id = room._id.toString()
delete room._id
Expand Down
14 changes: 0 additions & 14 deletions staff/miguel-arias/project/api/logic/retrieveRooms.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,5 @@ describe('retrieveRooms', () => {
}
})

it('fails on non-existing room', async () => {
const home = await Home.create({ name: random.name(), email: random.email(), password: random.password() })

const homeId = home._id.toString()

try {
await retrieveRooms(homeId)
throw new Error('should not reach this point')
} catch (error) {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('room not found')
}
})

after(async () => await mongoose.disconnect())
})
Loading

0 comments on commit 00786d0

Please sign in to comment.