Skip to content

Commit 456d8de

Browse files
authored
fix: add condition logic to return dish (#232)
* fix: add condition logic to return * fix: default dish condition * fix: update readme for allowed conditions
1 parent 8274bc7 commit 456d8de

File tree

6 files changed

+61
-20
lines changed

6 files changed

+61
-20
lines changed

backend/README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,16 @@ The dish routes are defined in the `src/routes/dish.ts` file. The routes are mou
119119
body:
120120
```
121121
returned: {
122-
broken: boolean, * required
123-
lost: boolean, * required
122+
condition: string, * required
123+
}
124+
```
125+
allowed values for condition:
126+
```
127+
Condition {
128+
'small_crack_chip',
129+
'large_crack_chunk',
130+
'shattered',
131+
'alright',
124132
}
125133
```
126134
@@ -144,6 +152,15 @@ The dish routes are defined in the `src/routes/dish.ts` file. The routes are mou
144152
condition: string, * required
145153
}
146154
```
155+
allowed values for condition:
156+
```
157+
Condition {
158+
'small_crack_chip',
159+
'large_crack_chunk',
160+
'shattered',
161+
'alright',
162+
}
163+
```
147164
148165
### Transactions
149166
The transaction routes are defined in the `src/routes/transaction.ts` file. The routes are mounted on the `/transactions` path. The routes are:

backend/src/controllers/dish.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Request, Response } from 'express'
2-
import { Dish } from '../models/dish'
2+
import { Condition, Dish } from '../models/dish'
33
import { Transaction } from '../models/transaction'
44
import {
55
getDish,
@@ -12,6 +12,7 @@ import {
1212
getUserDishesSimple,
1313
validateReturnDishRequestBody,
1414
getDishById,
15+
validateUpdateConditonRequestBody,
1516
} from '../services/dish'
1617
import { CustomRequest } from '../middlewares/auth'
1718
import Logger from '../utils/logger'
@@ -198,8 +199,7 @@ export const borrowDish = async (req: Request, res: Response) => {
198199
},
199200
userId: userClaims.uid,
200201
returned: {
201-
broken: false,
202-
lost: false,
202+
condition: Condition.alright
203203
},
204204
timestamp: new Date().toISOString(),
205205
}
@@ -240,13 +240,13 @@ export const returnDish = async (req: Request, res: Response) => {
240240
if (validation.error) {
241241
Logger.error({
242242
module: 'dish.controller',
243-
message: 'No values for broken or lost provided',
243+
message: 'No values for condition provided',
244244
statusCode: 400,
245245
})
246246

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

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

297-
await updateBorrowedStatus(associatedDish, userClaims, false)
297+
await updateBorrowedStatus(associatedDish, userClaims, false, condition)
298298

299299
await db
300300
.collection(nodeConfig.get('collections.transactions'))
301301
.doc(ongoingTransaction.id)
302302
.update({
303303
returned: {
304-
broken,
305-
lost,
304+
condition,
306305
timestamp: new Date().toISOString(),
307306
},
308307
})
@@ -353,8 +352,7 @@ export const returnDish = async (req: Request, res: Response) => {
353352
.doc(ongoingTransaction.id)
354353
.update({
355354
returned: {
356-
broken,
357-
lost,
355+
condition,
358356
timestamp: new Date().toISOString(),
359357
},
360358
})
@@ -389,6 +387,18 @@ export const updateDishCondition = async (req: Request, res: Response) => {
389387
return res.status(400).json({ error: 'bad_request', message: 'dish_id not provided' })
390388
}
391389

390+
let validation = validateUpdateConditonRequestBody(req.body)
391+
if (validation.error) {
392+
Logger.error({
393+
module: 'dish.controller',
394+
error: validation.error,
395+
message: 'No values for condition provided',
396+
statusCode: 400,
397+
})
398+
399+
return res.status(400).json({ error: 'bad_request', message: 'validation for condition failed' })
400+
}
401+
392402
let condition = req.body.condition
393403
if (!condition) {
394404
Logger.error({

backend/src/models/dish.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export enum DishStatus {
2626
available = 'available',
2727
}
2828

29+
export enum Condition {
30+
smallChip = 'small_crack_chip',
31+
largeCrack = 'large_crack_chunk',
32+
shattered = 'shattered',
33+
alright = 'alright',
34+
}
35+
2936
export type DishTableVM = {
3037
id: string
3138
type: string

backend/src/models/transaction.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ export type Transaction = {
88
}
99
userId: string
1010
returned: {
11-
broken: boolean
12-
lost: boolean
11+
condition: string
1312
timestamp?: Date
1413
}
1514
timestamp: string

backend/src/services/dish.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Joi from 'joi'
22
import { DecodedIdToken } from 'firebase-admin/lib/auth/token-verifier'
3-
import { Dish, DishSimple, DishStatus, DishTableVM } from '../models/dish'
3+
import { Condition, Dish, DishSimple, DishStatus, DishTableVM } from '../models/dish'
44
import { Transaction } from '../models/transaction'
55
import { db } from './firebase'
66
import Logger from '../utils/logger'
@@ -222,18 +222,26 @@ export const validateDishRequestBody = (dish: Dish) => {
222222

223223
export const validateReturnDishRequestBody = (dish: Dish) => {
224224
const schema = Joi.object({
225-
broken: Joi.boolean().required(),
226-
lost: Joi.boolean().required(),
225+
condition: Joi.string().valid(Condition.smallChip, Condition.largeCrack, Condition.shattered, Condition.alright).required(),
227226
}).required()
228227

229228
return schema.validate(dish)
230229
}
231230

232-
export const updateBorrowedStatus = async (dish: Dish, userClaims: DecodedIdToken, borrowed: boolean) => {
231+
export const validateUpdateConditonRequestBody = (body: Object) => {
232+
const schema = Joi.object({
233+
condition: Joi.string().valid(Condition.smallChip, Condition.largeCrack, Condition.shattered, Condition.alright).required(),
234+
}).required()
235+
236+
return schema.validate(body)
237+
}
238+
239+
export const updateBorrowedStatus = async (dish: Dish, userClaims: DecodedIdToken, borrowed: boolean, condition?: string) => {
233240
// when borrowing, set userId and increase timesBorrowed
234241
let timesBorrowed = borrowed ? dish.timesBorrowed + 1 : dish.timesBorrowed
235242
let userId = borrowed ? userClaims.uid : null
236243
await db.collection('dishes').doc(dish.id).update({
244+
condition: condition ? condition : dish.condition,
237245
borrowed,
238246
timesBorrowed,
239247
userId,

testbench/scripts/return-dish.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ curl -i -X POST \
99
-H "x-api-key: test" \
1010
-H "session-token: $SESSION_TOKEN" \
1111
-H "Content-Type: application/json" \
12-
-d "{ \"returned\" : {\"broken\" : false, \"lost\" : \"false\"} }" \
12+
-d "{ \"returned\" : {\"condition\" : \"small_crack_chip\"} }" \
1313
http://localhost:8080/api/dish/return?qid=$QID

0 commit comments

Comments
 (0)