Skip to content

Commit

Permalink
fix: returned.timestamp not being set
Browse files Browse the repository at this point in the history
  • Loading branch information
ArmaanKatyal committed Aug 19, 2023
1 parent 5ab4d69 commit bb81c71
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
10 changes: 6 additions & 4 deletions backend/src/controllers/dish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import {
import { CustomRequest } from '../middlewares/auth'
import Logger from '../utils/logger'
import { verifyIfUserAdmin } from '../services/users'
import { getTransaction, registerTransaction, getTransactionBydishId } from '../services/transactions'
import { registerTransaction, getLatestTransaction, getLatestTransactionBydishId } from '../services/transactions'
import { getQrCode } from '../services/qrCode'
import { db } from '../services/firebase'
import nodeConfig from 'config'
import { time } from 'console'

export const getDishes = async (req: Request, res: Response) => {
let userClaims = (req as CustomRequest).firebase
Expand Down Expand Up @@ -199,7 +200,8 @@ export const borrowDish = async (req: Request, res: Response) => {
},
userId: userClaims.uid,
returned: {
condition: Condition.alright
condition: Condition.alright,
timestamp: '',
},
timestamp: new Date().toISOString(),
}
Expand Down Expand Up @@ -284,7 +286,7 @@ export const returnDish = async (req: Request, res: Response) => {
}

// update the existing transaction with the returned property
ongoingTransaction = await getTransaction(userClaims, parseInt(qid, 10))
ongoingTransaction = await getLatestTransaction(userClaims, parseInt(qid, 10))
if (!ongoingTransaction) {
Logger.error({
module: 'dish.controller',
Expand Down Expand Up @@ -333,7 +335,7 @@ export const returnDish = async (req: Request, res: Response) => {
})
return res.status(400).json({ error: 'operation_not_allowed', message: 'Dish not borrowed' })
}
ongoingTransaction = await getTransactionBydishId(userClaims, id!)
ongoingTransaction = await getLatestTransactionBydishId(userClaims, id!)
if (!ongoingTransaction) {
Logger.error({
module: 'dish.controller',
Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type Transaction = {
userId: string
returned: {
condition: string
timestamp?: Date
timestamp?: string
}
timestamp: string
}
6 changes: 4 additions & 2 deletions backend/src/services/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ export const registerTransaction = async (transaction: Transaction) => {
}
}

export const getTransaction = async (userClaims: DecodedIdToken, qid: number) => {
export const getLatestTransaction = async (userClaims: DecodedIdToken, qid: number) => {
let transactionQuery = await db
.collection(nodeConfig.get('collections.transactions'))
.where('userId', '==', userClaims.uid)
.where('dish.qid', '==', qid)
.where('returned.timestamp', '==', '')
.get()
if (transactionQuery.empty) {
return null
Expand All @@ -74,11 +75,12 @@ export const getTransaction = async (userClaims: DecodedIdToken, qid: number) =>
}
}

export const getTransactionBydishId = async (userClaims: DecodedIdToken, dishId: string) => {
export const getLatestTransactionBydishId = async (userClaims: DecodedIdToken, dishId: string) => {
let snapshot = await db
.collection(nodeConfig.get('collections.transactions'))
.where('userId', '==', userClaims.uid)
.where('dish.id', '==', dishId)
.where('returned.timestamp', '==', '')
.get()
if (snapshot.empty) {
return null
Expand Down

0 comments on commit bb81c71

Please sign in to comment.