Skip to content

Commit

Permalink
add api for retrieve likes reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
thuypham03 committed Nov 9, 2023
1 parent 728f3fd commit 616a86c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
30 changes: 29 additions & 1 deletion backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
*/
Expand Down
4 changes: 2 additions & 2 deletions backend/src/firebase-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit 616a86c

Please sign in to comment.