Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint for retrieve liked reviews of current user #319

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
Loading