Skip to content

Commit

Permalink
add more error handling options b00tc4mp#412
Browse files Browse the repository at this point in the history
  • Loading branch information
pankelix committed Feb 27, 2024
1 parent e38d9a6 commit bfdbba5
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
Empty file.
Empty file.
15 changes: 14 additions & 1 deletion staff/miguel-arias/project/api/logic/createRoom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { validate, errors } from 'com'
const { SystemError } = errors
const { SystemError, NotFoundError } = errors

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

Expand All @@ -8,9 +8,22 @@ function createRoom(homeId, name) {
validate.text(name, 'name')

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')

try {
const room = await Room.create({ home: homeId, name })

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

return room
} catch (error) {
throw new SystemError(error.message)
Expand Down
14 changes: 12 additions & 2 deletions staff/miguel-arias/project/api/logic/createTask.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { validate, errors } from 'com'
const { SystemError } = errors
const { SystemError, NotFoundError } = errors

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

function createTask(homeId, templateId, date/*, assigneeId */) {
validate.id(homeId, 'home id')
validate.id(templateId, 'template id')
/* validate.id(assigneeId, 'assignee id') */

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')

try {
const task = await Task.create({ home: homeId, template: templateId, date: date/*, assignee: assigneeId */ })

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

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

function createTemplate(homeId, name, rooms, periodicity, points) {
validate.id(homeId, 'home id')
Expand All @@ -11,6 +11,16 @@ function createTemplate(homeId, name, rooms, periodicity, points) {
validate.number(points, 'points')

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')

try {
const template = await Template.create({ home: homeId, name, rooms, periodicity, points })

Expand Down
14 changes: 12 additions & 2 deletions staff/miguel-arias/project/api/logic/registerProfile.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import bcrypt from 'bcryptjs'

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

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

function registerProfile(homeId, name, pincode, color, role) {
validate.id(homeId)
validate.text(name, 'name')
validate.pincode(pincode, 'pincode')

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')

try {
const hash = await bcrypt.hash(pincode, 8)

Expand Down

0 comments on commit bfdbba5

Please sign in to comment.