Skip to content

Commit

Permalink
Add endpoint for retrieve liked reviews of current user (#319)
Browse files Browse the repository at this point in the history
* add api for retrieve likes reviews

* filter to approved review only
  • Loading branch information
thuypham03 authored Nov 14, 2023
1 parent 5989088 commit 01d997a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
31 changes: 30 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 @@ -83,6 +83,35 @@ 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)
.where('status', '==', 'APPROVED')
.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 01d997a

Please sign in to comment.