From 616a86c4d68cffe217118068fd373dd9bbd473fe Mon Sep 17 00:00:00 2001 From: thuypham03 Date: Thu, 9 Nov 2023 17:28:06 -0500 Subject: [PATCH] add api for retrieve likes reviews --- backend/src/app.ts | 30 +++++++++++++++++++++++++++- backend/src/firebase-config/index.ts | 4 ++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/backend/src/app.ts b/backend/src/app.ts index 29eae48a..febbe82f 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -13,7 +13,7 @@ import { ApartmentWithLabel, ApartmentWithId, } from '@common/types/db-types'; -import { db, FieldValue } from './firebase-config'; +import { db, FieldValue, FieldPath } from './firebase-config'; import { Faq } from './firebase-config/types'; import authenticate from './auth'; @@ -82,6 +82,34 @@ app.get('/api/review/:status', async (req, res) => { res.status(200).send(JSON.stringify(reviews)); }); +/** + * Return list of reviews that user marked as helpful (like) + */ +app.get('/api/review/like', authenticate, async (req, res) => { + if (!req.user) throw new Error('not authenticated'); + const { uid } = req.user; + const likesDoc = await likesCollection.doc(uid).get(); + + if (likesDoc.exists) { + const data = likesDoc.data(); + if (data) { + const reviewIds = Object.keys(data); + const matchingReviews: ReviewWithId[] = []; + const querySnapshot = await reviewCollection + .where(FieldPath.documentId(), 'in', reviewIds) + .get(); + querySnapshot.forEach((doc) => { + const reviewData = doc.data(); + matchingReviews.push({ ...reviewData, id: doc.id } as ReviewWithId); + }); + res.status(200).send(JSON.stringify(matchingReviews)); + return; + } + } + + res.status(200).send(JSON.stringify([])); +}); + /** * Takes in the location type in the URL and returns the number of reviews made forr that location */ diff --git a/backend/src/firebase-config/index.ts b/backend/src/firebase-config/index.ts index aa657e27..f12d6494 100644 --- a/backend/src/firebase-config/index.ts +++ b/backend/src/firebase-config/index.ts @@ -18,6 +18,6 @@ admin.initializeApp({ const db = admin.firestore(); const auth = admin.auth(); -const { FieldValue } = admin.firestore; +const { FieldValue, FieldPath } = admin.firestore; -export { db, auth, FieldValue }; +export { db, auth, FieldValue, FieldPath };