-
Notifications
You must be signed in to change notification settings - Fork 0
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
Implement community board (user queries/mutations) [5/7] #87
Changes from all commits
c541920
6ac7a63
e3601a2
d38532a
9d90039
14a4ab8
7cc3f3a
38aa425
fd106f6
6bf0a2f
dbc08d3
1e1c323
b8f6c8b
f257b73
de73d2f
861e796
c898bef
74f743e
88ce176
6e45574
95e109d
e133b65
c4401cc
d2b12f6
fa00457
90957cd
1e989af
d12a835
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { Article } from '../entities/Article'; | ||
import ArticleRepo from './ArticleRepo'; | ||
import { PublicationSlug } from '../common/types'; | ||
import { OrganizationSlug, PublicationSlug } from '../common/types'; | ||
import { User, UserModel } from '../entities/User'; | ||
import { Magazine } from '../entities/Magazine'; | ||
import MagazineRepo from './MagazineRepo'; | ||
|
||
import { Flyer } from '../entities/Flyer'; | ||
import FlyerRepo from './FlyerRepo'; | ||
/** | ||
* Create new user associated with deviceToken and followedPublicationsSlugs of deviceType. | ||
*/ | ||
|
@@ -65,6 +66,37 @@ const unfollowPublication = async (uuid: string, pubSlug: string): Promise<User> | |
return user; | ||
}; | ||
|
||
/** | ||
* Adds organization slug to user's followedOrganizations. | ||
* Requires: the user is not already following the organization. | ||
*/ | ||
const followOrganization = async (uuid: string, slug: string): Promise<User> => { | ||
const user = await UserModel.findOne({ uuid }); | ||
|
||
if (user) { | ||
user.followedOrganizations.push(new OrganizationSlug(slug)); | ||
return user.save(); | ||
} | ||
Comment on lines
+76
to
+79
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. do we need this if statement? 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. i think it might return null otherwise |
||
return user; | ||
}; | ||
|
||
/** | ||
* Deletes organization slug from user's followedOrganizations. | ||
*/ | ||
const unfollowOrganization = async (uuid: string, orgSlug: string): Promise<User> => { | ||
const user = await UserModel.findOne({ uuid }); | ||
if (user) { | ||
const orgSlugs = user.followedOrganizations.map((orgSlugObj) => orgSlugObj.slug); | ||
const orgIndex = orgSlugs.indexOf(orgSlug); | ||
|
||
if (orgIndex === -1) return user; | ||
|
||
user.followedOrganizations.splice(orgIndex, 1); | ||
return user.save(); | ||
} | ||
Comment on lines
+88
to
+96
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. same here 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. should be checking if user is null or not |
||
return user; | ||
}; | ||
|
||
const getUserByUUID = async (uuid: string): Promise<User> => { | ||
return UserModel.findOne({ uuid }); | ||
}; | ||
|
@@ -79,41 +111,70 @@ const getUsersFollowingPublication = async (pubSlug: string): Promise<User[]> => | |
return matchedUsers; | ||
}; | ||
|
||
/** | ||
* Return all users who follow an organization | ||
*/ | ||
const getUsersFollowingOrganization = async (orgSlug: string): Promise<User[]> => { | ||
const matchedUsers = await UserModel.find({ | ||
followedOrganizations: { $elemMatch: { slug: orgSlug } }, | ||
}); | ||
return matchedUsers; | ||
}; | ||
|
||
/** | ||
* Add article to a user's readArticles | ||
*/ | ||
const appendReadArticle = async (uuid: string, articleID: string): Promise<User> => { | ||
const user = await UserModel.findOne({ uuid }); | ||
|
||
if (!user) return user; | ||
if (user) { | ||
const article = await ArticleRepo.getArticleByID(articleID); | ||
const checkDuplicates = (prev: boolean, cur: Article) => prev || cur.id === articleID; | ||
|
||
const article = await ArticleRepo.getArticleByID(articleID); | ||
const checkDuplicates = (prev: boolean, cur: Article) => prev || cur.id === articleID; | ||
if (article && !user.readArticles.reduce(checkDuplicates, false)) { | ||
user.readArticles.push(article); | ||
} | ||
|
||
if (article && !user.readArticles.reduce(checkDuplicates, false)) { | ||
user.readArticles.push(article); | ||
user.save(); | ||
} | ||
return user; | ||
}; | ||
|
||
/** | ||
* Add a flyer to a user's readFlyers | ||
*/ | ||
const appendReadFlyer = async (uuid: string, flyerID: string): Promise<User> => { | ||
const user = await UserModel.findOne({ uuid }); | ||
|
||
if (user) { | ||
const flyer = await FlyerRepo.getFlyerByID(flyerID); | ||
const checkDuplicates = (prev: boolean, cur: Flyer) => prev || cur.id === flyerID; | ||
|
||
if (flyer && !user.readFlyers.reduce(checkDuplicates, false)) { | ||
user.readFlyers.push(flyer); | ||
} | ||
|
||
user.save(); | ||
user.save(); | ||
} | ||
return user; | ||
}; | ||
|
||
/** | ||
* Add a magazine to a user's readMagazines | ||
* Add a magazine to a user's read | ||
*/ | ||
const appendReadMagazine = async (uuid: string, magazineID: string): Promise<User> => { | ||
const user = await UserModel.findOne({ uuid }); | ||
|
||
if (!user) return user; | ||
if (user) { | ||
const magazine = await MagazineRepo.getMagazineByID(magazineID); | ||
const checkDuplicates = (prev: boolean, cur: Magazine) => prev || cur.id === magazineID; | ||
|
||
const magazine = await MagazineRepo.getMagazineByID(magazineID); | ||
const checkDuplicates = (prev: boolean, cur: Magazine) => prev || cur.id === magazineID; | ||
if (magazine && !user.readMagazines.reduce(checkDuplicates, false)) { | ||
user.readMagazines.push(magazine); | ||
} | ||
|
||
if (magazine && !user.readMagazines.reduce(checkDuplicates, false)) { | ||
user.readMagazines.push(magazine); | ||
user.save(); | ||
} | ||
|
||
user.save(); | ||
return user; | ||
}; | ||
|
||
|
@@ -148,10 +209,14 @@ const incrementBookmarks = async (uuid: string): Promise<User> => { | |
export default { | ||
appendReadArticle, | ||
appendReadMagazine, | ||
appendReadFlyer, | ||
createUser, | ||
followPublication, | ||
unfollowOrganization, | ||
followOrganization, | ||
getUserByUUID, | ||
getUsersFollowingPublication, | ||
getUsersFollowingOrganization, | ||
incrementBookmarks, | ||
incrementShoutouts, | ||
unfollowPublication, | ||
|
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.
love that we appdev members know the alphabetical order