-
Notifications
You must be signed in to change notification settings - Fork 22
Team matching subscription api #252
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0f20075
feat(pages/api/teammatching/subscription/create.ts): Subscribe to Post
uy-seng d74ae15
feat(pages/api/teammatching/subscription/delete.ts): Unsubscribe post
uy-seng 128fad4
feat(pages/api/teammatching/subscription/get.ts): Get subscriptions
uy-seng 1151d15
fix(pages/api/teammatching/subscription): Add async delete snapshot
uy-seng 0d1cf34
fix(pages/api/teammatching/subscription): Update delete
uy-seng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { firestore } from 'firebase-admin'; | ||
| import { NextApiRequest, NextApiResponse } from 'next'; | ||
| import initializeApi from '../../../../lib/admin/init'; | ||
| import { userIsAuthorized } from '../../../../lib/authorization/check-authorization'; | ||
|
|
||
| initializeApi(); | ||
| const db = firestore(); | ||
|
|
||
| // interface of subscription data that contains the user id and the post id | ||
| interface SubscriptionData { | ||
| userId: string; | ||
| postId: string; | ||
| } | ||
|
|
||
| // create a subscription for user that subscribe them to a post | ||
| async function createSubscription(req: NextApiRequest, res: NextApiResponse) { | ||
| try { | ||
| const subscriptionData: SubscriptionData = JSON.parse(req.body); | ||
| await db.collection('subscriptions').add(subscriptionData); | ||
| return res.status(201).json({ | ||
| msg: 'Subscription created', | ||
| }); | ||
| } catch (error) { | ||
| return res.status(500).json({ | ||
| msg: 'Unexpected error. Please try again later', | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| async function handlePostRequest(req: NextApiRequest, res: NextApiResponse) { | ||
| const userToken = req.headers['authorization'] as string; | ||
| const isAuthorized = await userIsAuthorized(userToken, ['hacker']); | ||
|
|
||
| if (!isAuthorized) { | ||
| return res.status(403).json({ | ||
| statusCode: 403, | ||
| msg: 'Request is not authorized to perform this functionality', | ||
| }); | ||
| } | ||
|
|
||
| return createSubscription(req, res); | ||
| } | ||
|
|
||
| export default function handler(req: NextApiRequest, res: NextApiResponse) { | ||
| const { method } = req; | ||
| switch (method) { | ||
| case 'POST': { | ||
| return handlePostRequest(req, res); | ||
| } | ||
| default: { | ||
| return res.status(404).json({ | ||
| msg: 'Route not found', | ||
| }); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { firestore } from 'firebase-admin'; | ||
| import { NextApiRequest, NextApiResponse } from 'next'; | ||
| import initializeApi from '../../../../lib/admin/init'; | ||
| import { userIsAuthorized } from '../../../../lib/authorization/check-authorization'; | ||
|
|
||
| initializeApi(); | ||
| const db = firestore(); | ||
|
|
||
| // interface of subscription data that contains the user id and the post id | ||
| interface SubscriptionData { | ||
| userId: string; | ||
| postId: string; | ||
| } | ||
|
|
||
| // delete one subscription for user that unsubscribe them to a post | ||
| async function deleteSubscription(req: NextApiRequest, res: NextApiResponse) { | ||
| try { | ||
| const subscriptionData: SubscriptionData = JSON.parse(req.body); | ||
| const snapshot = await db | ||
| .collection('subscriptions') | ||
| .where('userId', '==', subscriptionData.userId) | ||
| .where('postId', '==', subscriptionData.postId) | ||
| .get(); | ||
| if (snapshot.empty) { | ||
| return res.status(404).json({ | ||
| msg: 'Subscription to posting not found', | ||
| }); | ||
| } | ||
|
|
||
| await Promise.all(snapshot.docs.map((doc) => doc.ref.delete())); | ||
|
|
||
| return res.status(200).json({ | ||
| msg: 'Subscription to posting deleted', | ||
| }); | ||
| } catch (error) { | ||
| return res.status(500).json({ | ||
| msg: 'Unexpected error. Please try again later', | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| async function handleDeleteRequest(req: NextApiRequest, res: NextApiResponse) { | ||
| const userToken = req.headers['authorization'] as string; | ||
| const isAuthorized = await userIsAuthorized(userToken, ['hacker']); | ||
|
|
||
| if (!isAuthorized) { | ||
| return res.status(403).json({ | ||
| statusCode: 403, | ||
| msg: 'Request is not authorized to perform this functionality', | ||
| }); | ||
| } | ||
|
|
||
| return deleteSubscription(req, res); | ||
| } | ||
|
|
||
| export default function handler(req: NextApiRequest, res: NextApiResponse) { | ||
| const { method } = req; | ||
| switch (method) { | ||
| case 'POST': { | ||
| return handleDeleteRequest(req, res); | ||
| } | ||
| default: { | ||
| return res.status(404).json({ | ||
| msg: 'Route not found', | ||
| }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { firestore } from 'firebase-admin'; | ||
| import { NextApiRequest, NextApiResponse } from 'next'; | ||
| import initializeApi from '../../../../lib/admin/init'; | ||
| import { userIsAuthorized } from '../../../../lib/authorization/check-authorization'; | ||
|
|
||
| initializeApi(); | ||
| const db = firestore(); | ||
|
|
||
| // get all subscriptions for user | ||
| async function getSubscriptions(req: NextApiRequest, res: NextApiResponse) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^ |
||
| try { | ||
| const userId = req.query.userId as string; | ||
| const snapshot = await db.collection('subscriptions').where('userId', '==', userId).get(); | ||
| if (snapshot.empty) { | ||
| return res.status(404).json({ | ||
| msg: 'Subscriptions not found', | ||
| }); | ||
| } | ||
| const subscriptions = []; | ||
| snapshot.forEach((doc) => { | ||
| subscriptions.push(doc.data()); | ||
| }); | ||
| return res.status(200).json(subscriptions); | ||
| } catch (error) { | ||
| return res.status(500).json({ | ||
| msg: 'Unexpected error. Please try again later', | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| async function handleGetRequest(req: NextApiRequest, res: NextApiResponse) { | ||
| const userToken = req.headers['authorization'] as string; | ||
| const isAuthorized = await userIsAuthorized(userToken, ['hacker']); | ||
|
|
||
| if (!isAuthorized) { | ||
| return res.status(403).json({ | ||
| statusCode: 403, | ||
| msg: 'Request is not authorized to perform this functionality', | ||
| }); | ||
| } | ||
|
|
||
| return getSubscriptions(req, res); | ||
| } | ||
|
|
||
| export default function handler(req: NextApiRequest, res: NextApiResponse) { | ||
| const { method } = req; | ||
| switch (method) { | ||
| case 'GET': { | ||
| return handleGetRequest(req, res); | ||
| } | ||
| default: { | ||
| return res.status(404).json({ | ||
| msg: 'Route not found', | ||
| }); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll need a way to verify whether request initiator matches user id provided in the request body.