Skip to content

Commit bb81c71

Browse files
committed
fix: returned.timestamp not being set
1 parent 5ab4d69 commit bb81c71

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

backend/src/controllers/dish.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ import {
1717
import { CustomRequest } from '../middlewares/auth'
1818
import Logger from '../utils/logger'
1919
import { verifyIfUserAdmin } from '../services/users'
20-
import { getTransaction, registerTransaction, getTransactionBydishId } from '../services/transactions'
20+
import { registerTransaction, getLatestTransaction, getLatestTransactionBydishId } from '../services/transactions'
2121
import { getQrCode } from '../services/qrCode'
2222
import { db } from '../services/firebase'
2323
import nodeConfig from 'config'
24+
import { time } from 'console'
2425

2526
export const getDishes = async (req: Request, res: Response) => {
2627
let userClaims = (req as CustomRequest).firebase
@@ -199,7 +200,8 @@ export const borrowDish = async (req: Request, res: Response) => {
199200
},
200201
userId: userClaims.uid,
201202
returned: {
202-
condition: Condition.alright
203+
condition: Condition.alright,
204+
timestamp: '',
203205
},
204206
timestamp: new Date().toISOString(),
205207
}
@@ -284,7 +286,7 @@ export const returnDish = async (req: Request, res: Response) => {
284286
}
285287

286288
// update the existing transaction with the returned property
287-
ongoingTransaction = await getTransaction(userClaims, parseInt(qid, 10))
289+
ongoingTransaction = await getLatestTransaction(userClaims, parseInt(qid, 10))
288290
if (!ongoingTransaction) {
289291
Logger.error({
290292
module: 'dish.controller',
@@ -333,7 +335,7 @@ export const returnDish = async (req: Request, res: Response) => {
333335
})
334336
return res.status(400).json({ error: 'operation_not_allowed', message: 'Dish not borrowed' })
335337
}
336-
ongoingTransaction = await getTransactionBydishId(userClaims, id!)
338+
ongoingTransaction = await getLatestTransactionBydishId(userClaims, id!)
337339
if (!ongoingTransaction) {
338340
Logger.error({
339341
module: 'dish.controller',

backend/src/models/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type Transaction = {
99
userId: string
1010
returned: {
1111
condition: string
12-
timestamp?: Date
12+
timestamp?: string
1313
}
1414
timestamp: string
1515
}

backend/src/services/transactions.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ export const registerTransaction = async (transaction: Transaction) => {
5252
}
5353
}
5454

55-
export const getTransaction = async (userClaims: DecodedIdToken, qid: number) => {
55+
export const getLatestTransaction = async (userClaims: DecodedIdToken, qid: number) => {
5656
let transactionQuery = await db
5757
.collection(nodeConfig.get('collections.transactions'))
5858
.where('userId', '==', userClaims.uid)
5959
.where('dish.qid', '==', qid)
60+
.where('returned.timestamp', '==', '')
6061
.get()
6162
if (transactionQuery.empty) {
6263
return null
@@ -74,11 +75,12 @@ export const getTransaction = async (userClaims: DecodedIdToken, qid: number) =>
7475
}
7576
}
7677

77-
export const getTransactionBydishId = async (userClaims: DecodedIdToken, dishId: string) => {
78+
export const getLatestTransactionBydishId = async (userClaims: DecodedIdToken, dishId: string) => {
7879
let snapshot = await db
7980
.collection(nodeConfig.get('collections.transactions'))
8081
.where('userId', '==', userClaims.uid)
8182
.where('dish.id', '==', dishId)
83+
.where('returned.timestamp', '==', '')
8284
.get()
8385
if (snapshot.empty) {
8486
return null

0 commit comments

Comments
 (0)