diff --git a/backend/README.md b/backend/README.md index 5960c6c2..59230672 100644 --- a/backend/README.md +++ b/backend/README.md @@ -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', } ``` @@ -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: diff --git a/backend/src/controllers/dish.ts b/backend/src/controllers/dish.ts index 814dd0d5..62fafc21 100644 --- a/backend/src/controllers/dish.ts +++ b/backend/src/controllers/dish.ts @@ -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, @@ -12,6 +12,7 @@ import { getUserDishesSimple, validateReturnDishRequestBody, getDishById, + validateUpdateConditonRequestBody, } from '../services/dish' import { CustomRequest } from '../middlewares/auth' import Logger from '../utils/logger' @@ -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(), } @@ -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 { @@ -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(), }, }) @@ -353,8 +352,7 @@ export const returnDish = async (req: Request, res: Response) => { .doc(ongoingTransaction.id) .update({ returned: { - broken, - lost, + condition, timestamp: new Date().toISOString(), }, }) @@ -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({ diff --git a/backend/src/models/dish.ts b/backend/src/models/dish.ts index 8fd5f2f3..2efd7cbd 100644 --- a/backend/src/models/dish.ts +++ b/backend/src/models/dish.ts @@ -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 diff --git a/backend/src/models/transaction.ts b/backend/src/models/transaction.ts index a8cd179e..a5f68998 100644 --- a/backend/src/models/transaction.ts +++ b/backend/src/models/transaction.ts @@ -8,8 +8,7 @@ export type Transaction = { } userId: string returned: { - broken: boolean - lost: boolean + condition: string timestamp?: Date } timestamp: string diff --git a/backend/src/services/dish.ts b/backend/src/services/dish.ts index 8b831a0b..91f68771 100644 --- a/backend/src/services/dish.ts +++ b/backend/src/services/dish.ts @@ -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' @@ -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, diff --git a/testbench/scripts/return-dish.sh b/testbench/scripts/return-dish.sh index 996bceb0..e7d2e203 100755 --- a/testbench/scripts/return-dish.sh +++ b/testbench/scripts/return-dish.sh @@ -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 \ No newline at end of file