Skip to content

Commit

Permalink
fix: add condition logic to return dish (#232)
Browse files Browse the repository at this point in the history
* fix: add condition logic to return

* fix: default dish condition

* fix: update readme for allowed conditions
  • Loading branch information
ArmaanKatyal authored Aug 9, 2023
1 parent 8274bc7 commit 456d8de
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 20 deletions.
21 changes: 19 additions & 2 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,16 @@ The dish routes are defined in the `src/routes/dish.ts` file. The routes are mou
body:
```
returned: {
broken: boolean, * required
lost: boolean, * required
condition: string, * required
}
```
allowed values for condition:
```
Condition {
'small_crack_chip',
'large_crack_chunk',
'shattered',
'alright',
}
```
Expand All @@ -144,6 +152,15 @@ The dish routes are defined in the `src/routes/dish.ts` file. The routes are mou
condition: string, * required
}
```
allowed values for condition:
```
Condition {
'small_crack_chip',
'large_crack_chunk',
'shattered',
'alright',
}
```
### Transactions
The transaction routes are defined in the `src/routes/transaction.ts` file. The routes are mounted on the `/transactions` path. The routes are:
Expand Down
32 changes: 21 additions & 11 deletions backend/src/controllers/dish.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express'
import { Dish } from '../models/dish'
import { Condition, Dish } from '../models/dish'
import { Transaction } from '../models/transaction'
import {
getDish,
Expand All @@ -12,6 +12,7 @@ import {
getUserDishesSimple,
validateReturnDishRequestBody,
getDishById,
validateUpdateConditonRequestBody,
} from '../services/dish'
import { CustomRequest } from '../middlewares/auth'
import Logger from '../utils/logger'
Expand Down Expand Up @@ -198,8 +199,7 @@ export const borrowDish = async (req: Request, res: Response) => {
},
userId: userClaims.uid,
returned: {
broken: false,
lost: false,
condition: Condition.alright
},
timestamp: new Date().toISOString(),
}
Expand Down Expand Up @@ -240,13 +240,13 @@ export const returnDish = async (req: Request, res: Response) => {
if (validation.error) {
Logger.error({
module: 'dish.controller',
message: 'No values for broken or lost provided',
message: 'No values for condition provided',
statusCode: 400,
})

return res.status(400).json({ error: 'bad_request', message: 'no values for broken or lost provided' })
return res.status(400).json({ error: 'bad_request', message: 'no values for condition provided' })
}
let { broken, lost } = req.body.returned
let { condition } = req.body.returned

let userClaims = (req as CustomRequest).firebase
try {
Expand Down Expand Up @@ -294,15 +294,14 @@ export const returnDish = async (req: Request, res: Response) => {
return res.status(400).json({ error: 'operation_not_allowed', message: 'Transaction not found' })
}

await updateBorrowedStatus(associatedDish, userClaims, false)
await updateBorrowedStatus(associatedDish, userClaims, false, condition)

await db
.collection(nodeConfig.get('collections.transactions'))
.doc(ongoingTransaction.id)
.update({
returned: {
broken,
lost,
condition,
timestamp: new Date().toISOString(),
},
})
Expand Down Expand Up @@ -353,8 +352,7 @@ export const returnDish = async (req: Request, res: Response) => {
.doc(ongoingTransaction.id)
.update({
returned: {
broken,
lost,
condition,
timestamp: new Date().toISOString(),
},
})
Expand Down Expand Up @@ -389,6 +387,18 @@ export const updateDishCondition = async (req: Request, res: Response) => {
return res.status(400).json({ error: 'bad_request', message: 'dish_id not provided' })
}

let validation = validateUpdateConditonRequestBody(req.body)
if (validation.error) {
Logger.error({
module: 'dish.controller',
error: validation.error,
message: 'No values for condition provided',
statusCode: 400,
})

return res.status(400).json({ error: 'bad_request', message: 'validation for condition failed' })
}

let condition = req.body.condition
if (!condition) {
Logger.error({
Expand Down
7 changes: 7 additions & 0 deletions backend/src/models/dish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export enum DishStatus {
available = 'available',
}

export enum Condition {
smallChip = 'small_crack_chip',
largeCrack = 'large_crack_chunk',
shattered = 'shattered',
alright = 'alright',
}

export type DishTableVM = {
id: string
type: string
Expand Down
3 changes: 1 addition & 2 deletions backend/src/models/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export type Transaction = {
}
userId: string
returned: {
broken: boolean
lost: boolean
condition: string
timestamp?: Date
}
timestamp: string
Expand Down
16 changes: 12 additions & 4 deletions backend/src/services/dish.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Joi from 'joi'
import { DecodedIdToken } from 'firebase-admin/lib/auth/token-verifier'
import { Dish, DishSimple, DishStatus, DishTableVM } from '../models/dish'
import { Condition, Dish, DishSimple, DishStatus, DishTableVM } from '../models/dish'
import { Transaction } from '../models/transaction'
import { db } from './firebase'
import Logger from '../utils/logger'
Expand Down Expand Up @@ -222,18 +222,26 @@ export const validateDishRequestBody = (dish: Dish) => {

export const validateReturnDishRequestBody = (dish: Dish) => {
const schema = Joi.object({
broken: Joi.boolean().required(),
lost: Joi.boolean().required(),
condition: Joi.string().valid(Condition.smallChip, Condition.largeCrack, Condition.shattered, Condition.alright).required(),
}).required()

return schema.validate(dish)
}

export const updateBorrowedStatus = async (dish: Dish, userClaims: DecodedIdToken, borrowed: boolean) => {
export const validateUpdateConditonRequestBody = (body: Object) => {
const schema = Joi.object({
condition: Joi.string().valid(Condition.smallChip, Condition.largeCrack, Condition.shattered, Condition.alright).required(),
}).required()

return schema.validate(body)
}

export const updateBorrowedStatus = async (dish: Dish, userClaims: DecodedIdToken, borrowed: boolean, condition?: string) => {
// when borrowing, set userId and increase timesBorrowed
let timesBorrowed = borrowed ? dish.timesBorrowed + 1 : dish.timesBorrowed
let userId = borrowed ? userClaims.uid : null
await db.collection('dishes').doc(dish.id).update({
condition: condition ? condition : dish.condition,
borrowed,
timesBorrowed,
userId,
Expand Down
2 changes: 1 addition & 1 deletion testbench/scripts/return-dish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ curl -i -X POST \
-H "x-api-key: test" \
-H "session-token: $SESSION_TOKEN" \
-H "Content-Type: application/json" \
-d "{ \"returned\" : {\"broken\" : false, \"lost\" : \"false\"} }" \
-d "{ \"returned\" : {\"condition\" : \"small_crack_chip\"} }" \
http://localhost:8080/api/dish/return?qid=$QID

0 comments on commit 456d8de

Please sign in to comment.