diff --git a/src/repositories/UserRepository.ts b/src/repositories/UserRepository.ts index 4fbe00f..0a026ca 100644 --- a/src/repositories/UserRepository.ts +++ b/src/repositories/UserRepository.ts @@ -1,10 +1,10 @@ -import { PostModel } from 'src/models/PostModel'; -import { AbstractRepository, EntityRepository } from 'typeorm'; +import { PostModel } from "src/models/PostModel"; +import { AbstractRepository, EntityRepository } from "typeorm"; -import { ConflictError } from '../errors'; -import { NotFoundError } from 'routing-controllers'; -import { UserModel } from '../models/UserModel'; -import { Uuid } from '../types'; +import { NotFoundError } from "routing-controllers"; +import { ConflictError } from "../errors"; +import { UserModel } from "../models/UserModel"; +import { Uuid } from "../types"; @EntityRepository(UserModel) export class UserRepository extends AbstractRepository { @@ -19,7 +19,9 @@ export class UserRepository extends AbstractRepository { .getOne(); } - public async getUserWithBlockedInfo(id: Uuid): Promise { + public async getUserWithBlockedInfo( + id: Uuid + ): Promise { return this.repository .createQueryBuilder("user") .leftJoinAndSelect("user.blocking", "user_blocking_users.blocking") @@ -28,7 +30,9 @@ export class UserRepository extends AbstractRepository { .getOne(); } - public async getUserByGoogleId(googleId: Uuid): Promise { + public async getUserByGoogleId( + googleId: Uuid + ): Promise { return await this.repository .createQueryBuilder("user") .where("user.googleId = :googleId", { googleId }) @@ -41,8 +45,19 @@ export class UserRepository extends AbstractRepository { return post; } - public async unsavePost(user: UserModel, post: PostModel): Promise { - user.saved.splice(user.saved.indexOf(post)); + public async unsavePost( + user: UserModel, + post: PostModel + ): Promise { + const postIndex = user.saved.findIndex( + (savedPost) => savedPost.id === post.id + ); + if (postIndex === -1) { + throw new NotFoundError( + "Tried to unsave post that was not found in user's saved posts." + ); + } + user.saved.splice(postIndex, 1); await this.repository.save(user); return post; } @@ -78,28 +93,26 @@ export class UserRepository extends AbstractRepository { familyName: string, photoUrl: string, email: string, - googleId: string, + googleId: string ): Promise { let existingUser = await this.repository - .createQueryBuilder("user") - .where("user.username = :username", { username }) - .orWhere("user.netid = :netid", { netid }) - .orWhere("user.email = :email", { email }) - .orWhere("user.googleId = :googleId", { googleId }) - .getOne(); + .createQueryBuilder("user") + .where("user.username = :username", { username }) + .orWhere("user.netid = :netid", { netid }) + .orWhere("user.email = :email", { email }) + .orWhere("user.googleId = :googleId", { googleId }) + .getOne(); if (existingUser) { if (existingUser.username === username) { - throw new ConflictError('UserModel with same username already exists!'); - } - else if (existingUser.netid === netid) - { - throw new ConflictError('UserModel with same netid already exists!'); - } - else if (existingUser.email === email) { - throw new ConflictError('UserModel with same email already exists!'); - } - else { - throw new ConflictError('UserModel with same google ID already exists!'); + throw new ConflictError("UserModel with same username already exists!"); + } else if (existingUser.netid === netid) { + throw new ConflictError("UserModel with same netid already exists!"); + } else if (existingUser.email === email) { + throw new ConflictError("UserModel with same email already exists!"); + } else { + throw new ConflictError( + "UserModel with same google ID already exists!" + ); } } const adminEmails = process.env.ADMIN_EMAILS?.split(","); @@ -122,7 +135,7 @@ export class UserRepository extends AbstractRepository { username: string | undefined, photoUrl: string | undefined, venmoHandle: string | undefined, - bio: string | undefined, + bio: string | undefined ): Promise { const existingUser = this.repository .createQueryBuilder("user") @@ -130,7 +143,7 @@ export class UserRepository extends AbstractRepository { .getOne(); if (await existingUser) { if (username !== user.username) { - throw new ConflictError('UserModel with same username already exists!'); + throw new ConflictError("UserModel with same username already exists!"); } } @@ -152,26 +165,30 @@ export class UserRepository extends AbstractRepository { public async blockUser( blocker: UserModel, - blocked: UserModel, + blocked: UserModel ): Promise { - if (blocker.blocking === undefined) { blocker.blocking = [blocked]; } - else { blocker.blocking.push(blocked); } + if (blocker.blocking === undefined) { + blocker.blocking = [blocked]; + } else { + blocker.blocking.push(blocked); + } return this.repository.save(blocker); } public async unblockUser( blocker: UserModel, - blocked: UserModel, + blocked: UserModel ): Promise { if (blocker.blocking === undefined) { - throw new NotFoundError("User has not been blocked!") - } - else { + throw new NotFoundError("User has not been blocked!"); + } else { if (!blocker.blocking.find((user) => user.id === blocked.id)) { - throw new NotFoundError("User has not been blocked!") + throw new NotFoundError("User has not been blocked!"); } // remove blocked user from blocking list - blocker.blocking = blocker.blocking.filter((user) => user.id !== blocked.id); + blocker.blocking = blocker.blocking.filter( + (user) => user.id !== blocked.id + ); } return this.repository.save(blocker); } @@ -180,4 +197,4 @@ export class UserRepository extends AbstractRepository { user.isActive = false; return this.repository.save(user); } -} \ No newline at end of file +}