diff --git a/lib/services/ActivityService.js b/lib/services/ActivityService.js
index 3409f9e..072b88f 100644
--- a/lib/services/ActivityService.js
+++ b/lib/services/ActivityService.js
@@ -1,57 +1,67 @@
-const bcrypt = require("bcryptjs");
+const bcrypt = require('bcryptjs');
class ActivityService {
- constructor({ activityRepository, componentRepository, userRepository, commentRepository }) {
- this.activityRepository = activityRepository;
- this.componentRepository = componentRepository;
- this.userRepository = userRepository;
- this.commentRepository = commentRepository;
- }
+ constructor({
+ activityRepository,
+ componentRepository,
+ userRepository,
+ commentRepository
+ }) {
+ this.activityRepository = activityRepository;
+ this.componentRepository = componentRepository;
+ this.userRepository = userRepository;
+ this.commentRepository = commentRepository;
+ }
- async getActivity(userId) {
- try {
- let activities = await this.activityRepository.getCollectionByIdentifier(userId, "owner_id");
- return activities;
- } catch (error) {
- console.log(error);
- }
- }
+ async getActivity(userId) {
+ try {
+ let activities = await this.activityRepository.getCollectionByIdentifier(
+ userId,
+ 'owner_id'
+ );
+ return activities;
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async getByIdentifier(args, parent) {
- try {
- let component = await this.componentRepository.getById(parent.component_id);
- return component;
- } catch (error) {
- console.log(error);
- }
- }
+ async getByIdentifier(args, parent) {
+ try {
+ let component = await this.componentRepository.getById(
+ parent.component_id
+ );
+ return component;
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async getByUserId(args, parent) {
- try {
- let user = await this.userRepository.getById(parent.user_id);
- return user;
- } catch (error) {
- console.log(error);
- }
- }
+ async getByUserId(args, parent) {
+ try {
+ let user = await this.userRepository.getById(parent.user_id);
+ return user;
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async getComment(id) {
- try {
- let comment = await this.commentRepository.getById(id);
- return comment;
- } catch (error) {
- console.log(error);
- }
- }
+ async getComment(id) {
+ try {
+ let comment = await this.commentRepository.getById(id);
+ return comment;
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async createNew(args) {
- try {
- let newActivity = await this.activityRepository.createNew(args);
- return newActivity;
- } catch (error) {
- console.log(error);
- }
- }
+ async createNew(args) {
+ try {
+ let newActivity = await this.activityRepository.createNew(args);
+ return newActivity;
+ } catch (error) {
+ console.log(error);
+ }
+ }
}
module.exports = ActivityService;
diff --git a/lib/services/AuthenticationService.js b/lib/services/AuthenticationService.js
index 5e1ceb9..deeef7c 100644
--- a/lib/services/AuthenticationService.js
+++ b/lib/services/AuthenticationService.js
@@ -1,38 +1,48 @@
-const {promisify} = require('util');
+const { promisify } = require('util');
const jwt = require('jsonwebtoken');
const signJwt = promisify(jwt.sign);
const bcrypt = require('bcryptjs');
class AuthenticationService {
- constructor({JWT_KEY, userRepository}) {
- this.jwtSecretKey = JWT_KEY;
- this.userRepository = userRepository;
- }
+ constructor({ JWT_KEY, userRepository }) {
+ this.jwtSecretKey = JWT_KEY;
+ this.userRepository = userRepository;
+ }
- async authenticate(credentials) {
- try {
- if (!credentials.email || typeof credentials.email !== 'string' || !credentials.password || typeof credentials.password !== 'string') {
- throw new Error('INSUFFICIENT_CREDENTIALS')
- }
- const user = await this.userRepository.getByEmail(credentials.email);
- if (!user) throw new Error('INVALID_CREDENTIALS');
- let secretPass = await bcrypt.hash(credentials.password, 10);
- const isCorrect = await bcrypt.compare(credentials.password, user.hashed_password);
- if (!isCorrect) throw new Error('INVALID_CREDENTIALS');
- let whenIssued = Math.floor(Date.now() / 1000);
- let whenExpires = whenIssued + 86400 * 7;
- return signJwt({
- iss: 'coder-hive',
- aud: 'coder-hive',
- iat: whenIssued,
- exp: whenExpires,
- sub: user.id
- }, this.jwtSecretKey)
- }
- catch (error) {
- throw(error)
- }
+ async authenticate(credentials) {
+ try {
+ if (
+ !credentials.email ||
+ typeof credentials.email !== 'string' ||
+ !credentials.password ||
+ typeof credentials.password !== 'string'
+ ) {
+ throw new Error('INSUFFICIENT_CREDENTIALS');
+ }
+ const user = await this.userRepository.getByEmail(credentials.email);
+ if (!user) throw new Error('INVALID_CREDENTIALS');
+ let secretPass = await bcrypt.hash(credentials.password, 10);
+ const isCorrect = await bcrypt.compare(
+ credentials.password,
+ user.hashed_password
+ );
+ if (!isCorrect) throw new Error('INVALID_CREDENTIALS');
+ let whenIssued = Math.floor(Date.now() / 1000);
+ let whenExpires = whenIssued + 86400 * 7;
+ return signJwt(
+ {
+ iss: 'coder-hive',
+ aud: 'coder-hive',
+ iat: whenIssued,
+ exp: whenExpires,
+ sub: user.id
+ },
+ this.jwtSecretKey
+ );
+ } catch (error) {
+ throw error;
}
+ }
}
-module.exports = AuthenticationService;
\ No newline at end of file
+module.exports = AuthenticationService;
diff --git a/lib/services/CommentService.js b/lib/services/CommentService.js
index e997d6d..3fcdfc3 100644
--- a/lib/services/CommentService.js
+++ b/lib/services/CommentService.js
@@ -1,88 +1,92 @@
-const bcrypt = require("bcryptjs");
+const bcrypt = require('bcryptjs');
class CommentService {
- constructor({ commentRepository, fanRepository, activityRepository }) {
- this.commentRepository = commentRepository;
- this.fanRepository = fanRepository;
- this.activityRepository = activityRepository;
- }
+ constructor({ commentRepository, fanRepository, activityRepository }) {
+ this.commentRepository = commentRepository;
+ this.fanRepository = fanRepository;
+ this.activityRepository = activityRepository;
+ }
- async getByIdentifier(args, parent) {
- try {
- // TODO Authenticate for admin or ownership
- let componentId = parent.id;
- return await this.commentRepository.getCollectionByIdentifierSpecial(
- componentId,
- "component_id",
- "comment"
- );
- } catch (error) {
- console.log(error);
- }
- }
+ async getByIdentifier(args, parent) {
+ try {
+ // TODO Authenticate for admin or ownership
+ let componentId = parent.id;
+ return await this.commentRepository.getCollectionByIdentifierSpecial(
+ componentId,
+ 'component_id',
+ 'comment'
+ );
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async getByUserId(args, parent) {
- try {
- // TODO Authenticate for admin or ownership
- let componentId = parent.id;
- return await this.commentRepository.getCollectionByIdentifierSpecial(
- componentId,
- "user_id",
- "comment"
- );
- } catch (error) {
- console.log(error);
- }
- }
+ async getByUserId(args, parent) {
+ try {
+ // TODO Authenticate for admin or ownership
+ let componentId = parent.id;
+ return await this.commentRepository.getCollectionByIdentifierSpecial(
+ componentId,
+ 'user_id',
+ 'comment'
+ );
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async createNew(args, authenticatedUserId) {
- try {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- if (authenticatedUserId !== args.user_id) throw new Error("UNAUTHORIZED");
- let newComment = await this.commentRepository.createNew(args);
- let fansToAlert = await this.fanRepository.getFansByComponent(args.component_id);
- await fansToAlert.map(async fan => {
- return await this.activityRepository.createNew({
- owner_id: fan.user_id,
- type: "newComment",
- user_id: args.user_id,
- comment_id: newComment.id,
- component_id: args.component_id
- });
- });
+ async createNew(args, authenticatedUserId) {
+ try {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ if (authenticatedUserId !== args.user_id) throw new Error('UNAUTHORIZED');
+ let newComment = await this.commentRepository.createNew(args);
+ let fansToAlert = await this.fanRepository.getFansByComponent(
+ args.component_id
+ );
+ await fansToAlert.map(async fan => {
+ return await this.activityRepository.createNew({
+ owner_id: fan.user_id,
+ type: 'newComment',
+ user_id: args.user_id,
+ comment_id: newComment.id,
+ component_id: args.component_id
+ });
+ });
- return newComment;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ return newComment;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
- async update(args, authenticatedUserId) {
- try {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- let comment = await this.commentRepository.getById(args.id);
- if (authenticatedUserId !== comment.user_id) throw new Error("UNAUTHORIZED");
- let updatedComment = await this.commentRepository.update(args);
- return updatedComment;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ async update(args, authenticatedUserId) {
+ try {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ let comment = await this.commentRepository.getById(args.id);
+ if (authenticatedUserId !== comment.user_id)
+ throw new Error('UNAUTHORIZED');
+ let updatedComment = await this.commentRepository.update(args);
+ return updatedComment;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
- async delete(args, authenticatedUserId) {
- try {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- let comment = await this.commentRepository.getById(args.id);
- if (authenticatedUserId !== comment.user_id) throw new Error("UNAUTHORIZED");
- let deletedComment = await this.commentRepository.hardDelete(args.id);
- return deletedComment;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ async delete(args, authenticatedUserId) {
+ try {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ let comment = await this.commentRepository.getById(args.id);
+ if (authenticatedUserId !== comment.user_id)
+ throw new Error('UNAUTHORIZED');
+ let deletedComment = await this.commentRepository.hardDelete(args.id);
+ return deletedComment;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
}
module.exports = CommentService;
diff --git a/lib/services/ComponentService.js b/lib/services/ComponentService.js
index ae20eb6..603e709 100644
--- a/lib/services/ComponentService.js
+++ b/lib/services/ComponentService.js
@@ -1,151 +1,168 @@
-const bcrypt = require("bcryptjs");
-const cssbeautify = require("cssbeautify");
-const esformatter = require("esformatter");
-const esformatterjsx = require("esformatter-jsx");
+const bcrypt = require('bcryptjs');
+const cssbeautify = require('cssbeautify');
+const esformatter = require('esformatter');
+const esformatterjsx = require('esformatter-jsx');
esformatter.register(esformatterjsx);
class ComponentService {
- constructor({ componentRepository, activityRepository, followRepository, fanRepository }) {
- this.componentRepository = componentRepository;
- this.activityRepository = activityRepository;
- this.followRepository = followRepository;
- this.fanRepository = fanRepository;
- this.update = this.update.bind(this);
- }
+ constructor({
+ componentRepository,
+ activityRepository,
+ followRepository,
+ fanRepository
+ }) {
+ this.componentRepository = componentRepository;
+ this.activityRepository = activityRepository;
+ this.followRepository = followRepository;
+ this.fanRepository = fanRepository;
+ this.update = this.update.bind(this);
+ }
- async getById(id) {
- try {
- // TODO Authenticate for admin or ownership
- const component = await this.componentRepository.getById(id);
- if (component.css) {
- component.css = cssbeautify(component.css, {
- indent: " ",
- autosemicolon: true
- });
- if (component.code) {
- component.code = esformatter.format(component.code);
- }
- }
- return component;
- } catch (error) {
- console.log(error);
- }
- }
+ async getById(id) {
+ try {
+ // TODO Authenticate for admin or ownership
+ const component = await this.componentRepository.getById(id);
+ if (component.css) {
+ component.css = cssbeautify(component.css, {
+ indent: ' ',
+ autosemicolon: true
+ });
+ if (component.code) {
+ component.code = esformatter.format(component.code);
+ }
+ }
+ return component;
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async getAll() {
- try {
- let components = await this.componentRepository.getAll("score");
- // users = users.map(user => {
- // delete user.hashed_password;
- // delete user.email;
- // return user
- // });
- return components;
- } catch (error) {
- console.log(error);
- }
- }
+ async getAll() {
+ try {
+ let components = await this.componentRepository.getAll('score');
+ // users = users.map(user => {
+ // delete user.hashed_password;
+ // delete user.email;
+ // return user
+ // });
+ return components;
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async getByIdentifier(args, parent) {
- try {
- // TODO Authenticate for admin or ownership
- let userId = parent.id;
- return await this.componentRepository.getCollectionByIdentifier(userId, "owner_user_id");
- } catch (error) {
- console.log(error);
- }
- }
+ async getByIdentifier(args, parent) {
+ try {
+ // TODO Authenticate for admin or ownership
+ let userId = parent.id;
+ return await this.componentRepository.getCollectionByIdentifier(
+ userId,
+ 'owner_user_id'
+ );
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async getChildren(thisId) {
- try {
- let children = await this.componentRepository.getCollectionByIdentifier(
- thisId,
- "parent_component_id"
- );
- return children;
- } catch (error) {
- console.log(error);
- }
- }
+ async getChildren(thisId) {
+ try {
+ let children = await this.componentRepository.getCollectionByIdentifier(
+ thisId,
+ 'parent_component_id'
+ );
+ return children;
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async getClones(thisId) {
- try {
- let children = await this.componentRepository.getCollectionByIdentifier(
- thisId,
- "clone_component_id"
- );
- return children;
- } catch (error) {
- console.log(error);
- }
- }
+ async getClones(thisId) {
+ try {
+ let children = await this.componentRepository.getCollectionByIdentifier(
+ thisId,
+ 'clone_component_id'
+ );
+ return children;
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async createNew(args, authenticatedUserId) {
- try {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- if (authenticatedUserId !== args.owner_user_id) throw new Error("UNAUTHORIZED");
- args.code = `class Component extends React.Component {
+ async createNew(args, authenticatedUserId) {
+ try {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ if (authenticatedUserId !== args.owner_user_id)
+ throw new Error('UNAUTHORIZED');
+ args.code = `class Component extends React.Component {
render() {
return (
Your Component Here
)
}
}`;
- args.css = `.new { background-color: blue;
+ args.css = `.new { background-color: blue;
font-size:30px;
color: white;
width: 400px;
text-align: center;
margin: 0 auto;}`;
- let newComponent = await this.componentRepository.createNew(args);
- let followersToAlert = await this.followRepository.getFollowerIds(authenticatedUserId);
- await followersToAlert.map(async follow => {
- return await this.activityRepository.createNew({
- owner_id: follow.follower,
- type: "newComponent",
- user_id: authenticatedUserId,
- component_id: newComponent.id
- });
- });
+ let newComponent = await this.componentRepository.createNew(args);
+ let followersToAlert = await this.followRepository.getFollowerIds(
+ authenticatedUserId
+ );
+ await followersToAlert.map(async follow => {
+ return await this.activityRepository.createNew({
+ owner_id: follow.follower,
+ type: 'newComponent',
+ user_id: authenticatedUserId,
+ component_id: newComponent.id
+ });
+ });
- newComponent = this.update(
- {
- id: newComponent.id,
- component_picture: `https://s3-us-west-1.amazonaws.com/coderhive/component_${newComponent.id}.jpeg`,
- owner_user_id: newComponent.owner_user_id
- },
- authenticatedUserId
- );
- return newComponent;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ newComponent = this.update(
+ {
+ id: newComponent.id,
+ component_picture: `https://s3-us-west-1.amazonaws.com/coderhive/component_${
+ newComponent.id
+ }.jpeg`,
+ owner_user_id: newComponent.owner_user_id
+ },
+ authenticatedUserId
+ );
+ return newComponent;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
- async update(args, authenticatedUserId) {
- try {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- if (authenticatedUserId !== args.owner_user_id) throw new Error("UNAUTHORIZED");
- let updatedComponent = await this.componentRepository.update(args);
- return updatedComponent;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ async update(args, authenticatedUserId) {
+ try {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ if (authenticatedUserId !== args.owner_user_id)
+ throw new Error('UNAUTHORIZED');
+ let updatedComponent = await this.componentRepository.update(args);
+ return updatedComponent;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
- async delete(args, authenticatedUserId) {
- try {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- let toDelete = await this.componentRepository.getById(args.id);
- if (authenticatedUserId !== toDelete.owner_user_id) throw new Error("UNAUTHORIZED");
- let deletedComponent = await this.componentRepository.deleteComponent(args.id);
- return deletedComponent;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ async delete(args, authenticatedUserId) {
+ try {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ let toDelete = await this.componentRepository.getById(args.id);
+ if (authenticatedUserId !== toDelete.owner_user_id)
+ throw new Error('UNAUTHORIZED');
+ let deletedComponent = await this.componentRepository.deleteComponent(
+ args.id
+ );
+ return deletedComponent;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
}
module.exports = ComponentService;
diff --git a/lib/services/ComponentTagService.js b/lib/services/ComponentTagService.js
index 7834605..f818c17 100644
--- a/lib/services/ComponentTagService.js
+++ b/lib/services/ComponentTagService.js
@@ -1,44 +1,44 @@
-const bcrypt = require("bcryptjs");
+const bcrypt = require('bcryptjs');
class ComponentService {
- constructor({ componentTagRepository }) {
- this.componentTagRepository = componentTagRepository;
- }
+ constructor({ componentTagRepository }) {
+ this.componentTagRepository = componentTagRepository;
+ }
- async getTagsByComponent(args, parent) {
- try {
- // TODO Authenticate for admin or ownership
- let componentId = parent.id;
- return await this.componentTagRepository.getTagsByComponent(componentId);
- } catch (error) {
- console.log(error);
- }
- }
+ async getTagsByComponent(args, parent) {
+ try {
+ // TODO Authenticate for admin or ownership
+ let componentId = parent.id;
+ return await this.componentTagRepository.getTagsByComponent(componentId);
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async createNew(args) {
- let existingTagCheck = await this.componentTagRepository.checkForDuplicateTag(
- args.tag_id,
- args.component_id
- );
- existingTagCheck = existingTagCheck[0];
- if (existingTagCheck) throw new Error("DUPLICATE: Identical Tag");
- let newTagRelation = await this.componentTagRepository.createNew(args);
- return newTagRelation;
- }
+ async createNew(args) {
+ let existingTagCheck = await this.componentTagRepository.checkForDuplicateTag(
+ args.tag_id,
+ args.component_id
+ );
+ existingTagCheck = existingTagCheck[0];
+ if (existingTagCheck) throw new Error('DUPLICATE: Identical Tag');
+ let newTagRelation = await this.componentTagRepository.createNew(args);
+ return newTagRelation;
+ }
- async delete(args) {
- try {
- let tag_id = args.tag_id;
- let component_id = args.component_id;
- let deletedTagRelationship = await this.componentTagRepository.hardDeleteByTag(
- tag_id,
- component_id
- );
- return deletedTagRelationship;
- } catch (error) {
- console.log(error);
- }
- }
+ async delete(args) {
+ try {
+ let tag_id = args.tag_id;
+ let component_id = args.component_id;
+ let deletedTagRelationship = await this.componentTagRepository.hardDeleteByTag(
+ tag_id,
+ component_id
+ );
+ return deletedTagRelationship;
+ } catch (error) {
+ console.log(error);
+ }
+ }
}
module.exports = ComponentService;
diff --git a/lib/services/FanService.js b/lib/services/FanService.js
index 516a409..43510e0 100644
--- a/lib/services/FanService.js
+++ b/lib/services/FanService.js
@@ -1,59 +1,68 @@
-const bcrypt = require("bcryptjs");
+const bcrypt = require('bcryptjs');
class FanService {
- constructor({ fanRepository }) {
- this.fanRepository = fanRepository;
- }
+ constructor({ fanRepository }) {
+ this.fanRepository = fanRepository;
+ }
- async getByIdentifier(args, parent) {
- try {
- let componentId = parent.id;
- return await this.fanRepository.getCollectionByIdentifierSpecial(componentId, "component_id");
- } catch (error) {
- console.log(error);
- }
- }
+ async getByIdentifier(args, parent) {
+ try {
+ let componentId = parent.id;
+ return await this.fanRepository.getCollectionByIdentifierSpecial(
+ componentId,
+ 'component_id'
+ );
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async getByUserId(args, parent) {
- try {
- let componentId = parent.id;
- return await this.fanRepository.getCollectionByIdentifierSpecial(componentId, "user_id");
- } catch (error) {
- console.log(error);
- }
- }
+ async getByUserId(args, parent) {
+ try {
+ let componentId = parent.id;
+ return await this.fanRepository.getCollectionByIdentifierSpecial(
+ componentId,
+ 'user_id'
+ );
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async createNew(args, authenticatedUserId) {
- try {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- if (authenticatedUserId !== args.user_id) throw new Error("UNAUTHORIZED");
- let existingFanCheck = await this.fanRepository.checkForDuplicateRelation(
- args.user_id,
- args.component_id
- );
- if (!!existingFanCheck.length)
- throw new Error("DUPLICATE: This user is already a fan of this component.");
- let newFan = await this.fanRepository.createNew(args);
- return newFan;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ async createNew(args, authenticatedUserId) {
+ try {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ if (authenticatedUserId !== args.user_id) throw new Error('UNAUTHORIZED');
+ let existingFanCheck = await this.fanRepository.checkForDuplicateRelation(
+ args.user_id,
+ args.component_id
+ );
+ if (!!existingFanCheck.length)
+ throw new Error(
+ 'DUPLICATE: This user is already a fan of this component.'
+ );
+ let newFan = await this.fanRepository.createNew(args);
+ return newFan;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
- async delete(args, authenticatedUserId) {
- try {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- let fanRelation = await this.fanRepository.getById(args.id);
- if (authenticatedUserId !== fanRelation.user_id) throw new Error("UNAUTHORIZED");
- const fanId = args.id;
- let deletedFan = await this.fanRepository.hardDelete(fanId);
- return deletedFan;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ async delete(args, authenticatedUserId) {
+ try {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ let fanRelation = await this.fanRepository.getById(args.id);
+ if (authenticatedUserId !== fanRelation.user_id)
+ throw new Error('UNAUTHORIZED');
+ const fanId = args.id;
+ let deletedFan = await this.fanRepository.hardDelete(fanId);
+ return deletedFan;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
}
module.exports = FanService;
diff --git a/lib/services/FollowService.js b/lib/services/FollowService.js
index d0e7db0..2ea2682 100644
--- a/lib/services/FollowService.js
+++ b/lib/services/FollowService.js
@@ -1,61 +1,64 @@
-const bcrypt = require("bcryptjs");
+const bcrypt = require('bcryptjs');
class FollowService {
- constructor({ followRepository }) {
- this.followRepository = followRepository;
- }
+ constructor({ followRepository }) {
+ this.followRepository = followRepository;
+ }
- async getFollowers(args, parent) {
- try {
- let userId = parent.id;
- return await this.followRepository.getFollowers(userId);
- } catch (error) {
- console.log(error);
- }
- }
+ async getFollowers(args, parent) {
+ try {
+ let userId = parent.id;
+ return await this.followRepository.getFollowers(userId);
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async getFollowees(args, parent) {
- try {
- let userId = parent.id;
- return await this.followRepository.getFollowees(userId);
- } catch (error) {
- console.log(error);
- }
- }
+ async getFollowees(args, parent) {
+ try {
+ let userId = parent.id;
+ return await this.followRepository.getFollowees(userId);
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async createNew(args, authenticatedUserId) {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- if (authenticatedUserId !== args.follower) throw new Error("UNAUTHORIZED");
- try {
- if (args.follower === args.followee)
- throw new Error("DUPLICATE: Identical Follower and Followee");
- let existingFollowCheck = await this.followRepository.checkForDuplicateFollow(
- args.follower,
- args.followee
- );
- existingFollowCheck = existingFollowCheck[0];
- if (existingFollowCheck) throw new Error("DUPLICATE: Identical Follow");
- let newFollow = await this.followRepository.createNew(args);
- return newFollow;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ async createNew(args, authenticatedUserId) {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ if (authenticatedUserId !== args.follower) throw new Error('UNAUTHORIZED');
+ try {
+ if (args.follower === args.followee)
+ throw new Error('DUPLICATE: Identical Follower and Followee');
+ let existingFollowCheck = await this.followRepository.checkForDuplicateFollow(
+ args.follower,
+ args.followee
+ );
+ existingFollowCheck = existingFollowCheck[0];
+ if (existingFollowCheck) throw new Error('DUPLICATE: Identical Follow');
+ let newFollow = await this.followRepository.createNew(args);
+ return newFollow;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
- async delete(args, authenticatedUserId) {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- if (authenticatedUserId !== args.follower) throw new Error("UNAUTHORIZED");
- try {
- let follower = args.follower;
- let followee = args.followee;
- let deletedFollow = await this.followRepository.hardDeleteFollow(follower, followee);
- return deletedFollow;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ async delete(args, authenticatedUserId) {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ if (authenticatedUserId !== args.follower) throw new Error('UNAUTHORIZED');
+ try {
+ let follower = args.follower;
+ let followee = args.followee;
+ let deletedFollow = await this.followRepository.hardDeleteFollow(
+ follower,
+ followee
+ );
+ return deletedFollow;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
}
module.exports = FollowService;
diff --git a/lib/services/UserService.js b/lib/services/UserService.js
index 2b53945..2177616 100644
--- a/lib/services/UserService.js
+++ b/lib/services/UserService.js
@@ -1,102 +1,104 @@
-const bcrypt = require("bcryptjs");
+const bcrypt = require('bcryptjs');
class UserService {
- constructor({ userRepository }) {
- this.userRepository = userRepository;
- }
+ constructor({ userRepository }) {
+ this.userRepository = userRepository;
+ }
- async getById(id) {
- try {
- // TODO Authenticate for admin or ownership
- let user = await this.userRepository.getById(id);
- delete user.email;
- return user;
- } catch (error) {
- console.log(error);
- }
- }
+ async getById(id) {
+ try {
+ // TODO Authenticate for admin or ownership
+ let user = await this.userRepository.getById(id);
+ delete user.email;
+ return user;
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async getAll() {
- try {
- // if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- // let thisUser = await this.userRepository.getByIdentifier(authenticatedUserId, 'id');
- // if (thisUser.role !== "admin") throw new Error('UNAUTHORIZED');
- let users = await this.userRepository.getAll("experience");
- users = users.map(user => {
- delete user.hashed_password;
- delete user.email;
- return user;
- });
- return users;
- } catch (error) {
- // if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ async getAll() {
+ try {
+ // if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
+ // let thisUser = await this.userRepository.getByIdentifier(authenticatedUserId, 'id');
+ // if (thisUser.role !== "admin") throw new Error('UNAUTHORIZED');
+ let users = await this.userRepository.getAll('experience');
+ users = users.map(user => {
+ delete user.hashed_password;
+ delete user.email;
+ return user;
+ });
+ return users;
+ } catch (error) {
+ // if (error.message === "UNAUTHORIZED") throw error;
+ console.log(error);
+ }
+ }
- async getByIdentifier(args) {
- try {
- // TODO Authenticate for admin or ownership
- let user;
- if (args.email) {
- user = await this.userRepository.getByIdentifier(args.email, "email");
- delete user.hashed_password;
- }
- return user;
- } catch (error) {
- throw error;
- }
- }
+ async getByIdentifier(args) {
+ try {
+ // TODO Authenticate for admin or ownership
+ let user;
+ if (args.email) {
+ user = await this.userRepository.getByIdentifier(args.email, 'email');
+ delete user.hashed_password;
+ }
+ return user;
+ } catch (error) {
+ throw error;
+ }
+ }
- async createNew(args) {
- try {
- if (!args.email) throw new Error("MISSING EMAIL");
- if (!args.password) throw new Error("MISSING PASSWORD");
- if (!args.display_name) throw new Error("MISSING DISPLAY_NAME");
+ async createNew(args) {
+ try {
+ if (!args.email) throw new Error('MISSING EMAIL');
+ if (!args.password) throw new Error('MISSING PASSWORD');
+ if (!args.display_name) throw new Error('MISSING DISPLAY_NAME');
- args.hashed_password = await bcrypt.hash(args.password, 10);
- delete args.password;
- let user = await this.userRepository.createNew(args);
- this.update(
- {
- id: user.id,
- profile_picture: `https://s3-us-west-1.amazonaws.com/coderhive/profile_${user.id}.jpeg`
- },
- user.id
- );
+ args.hashed_password = await bcrypt.hash(args.password, 10);
+ delete args.password;
+ let user = await this.userRepository.createNew(args);
+ this.update(
+ {
+ id: user.id,
+ profile_picture: `https://s3-us-west-1.amazonaws.com/coderhive/profile_${
+ user.id
+ }.jpeg`
+ },
+ user.id
+ );
- return user;
- } catch (error) {
- if (error.message.startsWith("MISSING ")) throw error;
- console.log(error);
- }
- }
+ return user;
+ } catch (error) {
+ if (error.message.startsWith('MISSING ')) throw error;
+ console.log(error);
+ }
+ }
- async update(args, authenticatedUserId) {
- try {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- if (authenticatedUserId !== args.id) throw new Error("UNAUTHORIZED");
- let user = await this.userRepository.update(args);
- delete user.hashed_password;
- return user;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ async update(args, authenticatedUserId) {
+ try {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ if (authenticatedUserId !== args.id) throw new Error('UNAUTHORIZED');
+ let user = await this.userRepository.update(args);
+ delete user.hashed_password;
+ return user;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
- async delete(args, authenticatedUserId) {
- try {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- if (authenticatedUserId !== args.id) throw new Error("UNAUTHORIZED");
- let user = await this.userRepository.delete(args);
- delete user.hashed_password;
- return user;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ async delete(args, authenticatedUserId) {
+ try {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ if (authenticatedUserId !== args.id) throw new Error('UNAUTHORIZED');
+ let user = await this.userRepository.delete(args);
+ delete user.hashed_password;
+ return user;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
}
module.exports = UserService;
diff --git a/lib/services/VoteService.js b/lib/services/VoteService.js
index 5d3f890..351bd72 100644
--- a/lib/services/VoteService.js
+++ b/lib/services/VoteService.js
@@ -1,109 +1,120 @@
-const bcrypt = require("bcryptjs");
+const bcrypt = require('bcryptjs');
class VoteService {
- constructor({
- voteRepository,
- fanRepository,
- activityRepository,
- userRepository,
- componentRepository
- }) {
- this.voteRepository = voteRepository;
- this.fanRepository = fanRepository;
- this.activityRepository = activityRepository;
- this.userRepository = userRepository;
- this.componentRepository = componentRepository;
- }
+ constructor({
+ voteRepository,
+ fanRepository,
+ activityRepository,
+ userRepository,
+ componentRepository
+ }) {
+ this.voteRepository = voteRepository;
+ this.fanRepository = fanRepository;
+ this.activityRepository = activityRepository;
+ this.userRepository = userRepository;
+ this.componentRepository = componentRepository;
+ }
- async getByIdentifier(args, parent) {
- try {
- let componentId = parent.id;
- let output = await this.voteRepository.getCollectionByIdentifierSpecial(
- componentId,
- "component_id",
- "vote"
- );
- return output;
- } catch (error) {
- console.log(error);
- }
- }
+ async getByIdentifier(args, parent) {
+ try {
+ let componentId = parent.id;
+ let output = await this.voteRepository.getCollectionByIdentifierSpecial(
+ componentId,
+ 'component_id',
+ 'vote'
+ );
+ return output;
+ } catch (error) {
+ console.log(error);
+ }
+ }
- async createNew(args, authenticatedUserId) {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- if (authenticatedUserId !== args.user_id) throw new Error("UNAUTHORIZED");
- try {
- let existingVoteCheck = await this.voteRepository.checkForDuplicateRelation(
- args.user_id,
- args.component_id
- );
- existingVoteCheck = existingVoteCheck[0];
- let deletedVote;
- if (existingVoteCheck && existingVoteCheck.vote === args.vote)
- throw new Error("DUPLICATE: Identical Vote");
- else if (existingVoteCheck) {
- deletedVote = await this.voteRepository.hardDelete(existingVoteCheck.id);
- }
- let newVote = await this.voteRepository.createNew(args);
- if (existingVoteCheck) newVote.replacedVoteId === deletedVote.id;
+ async createNew(args, authenticatedUserId) {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ if (authenticatedUserId !== args.user_id) throw new Error('UNAUTHORIZED');
+ try {
+ let existingVoteCheck = await this.voteRepository.checkForDuplicateRelation(
+ args.user_id,
+ args.component_id
+ );
+ existingVoteCheck = existingVoteCheck[0];
+ let deletedVote;
+ if (existingVoteCheck && existingVoteCheck.vote === args.vote)
+ throw new Error('DUPLICATE: Identical Vote');
+ else if (existingVoteCheck) {
+ deletedVote = await this.voteRepository.hardDelete(
+ existingVoteCheck.id
+ );
+ }
+ let newVote = await this.voteRepository.createNew(args);
+ if (existingVoteCheck) newVote.replacedVoteId === deletedVote.id;
- let component = await this.componentRepository.getById(args.component_id);
- component.score += args.vote;
- let ownerUser = await this.userRepository.getById(component.owner_user_id);
- ownerUser.experience += args.vote;
- let updatedUser = await this.userRepository.update({
- id: ownerUser.id,
- experience: ownerUser.experience
- });
- let updatedComponent = await this.componentRepository.update({
- id: component.id,
- score: component.score
- });
+ let component = await this.componentRepository.getById(args.component_id);
+ component.score += args.vote;
+ let ownerUser = await this.userRepository.getById(
+ component.owner_user_id
+ );
+ ownerUser.experience += args.vote;
+ let updatedUser = await this.userRepository.update({
+ id: ownerUser.id,
+ experience: ownerUser.experience
+ });
+ let updatedComponent = await this.componentRepository.update({
+ id: component.id,
+ score: component.score
+ });
- let fansToAlert = await this.fanRepository.getFansByComponent(args.component_id);
- await fansToAlert.map(async fan => {
- return await this.activityRepository.createNew({
- owner_id: fan.user_id,
- type: "newVote",
- user_id: args.user_id,
- component_id: args.component_id
- });
- });
- return newVote;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ let fansToAlert = await this.fanRepository.getFansByComponent(
+ args.component_id
+ );
+ await fansToAlert.map(async fan => {
+ return await this.activityRepository.createNew({
+ owner_id: fan.user_id,
+ type: 'newVote',
+ user_id: args.user_id,
+ component_id: args.component_id
+ });
+ });
+ return newVote;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
- async delete(args, authenticatedUserId) {
- if (!authenticatedUserId) throw new Error("UNAUTHORIZED");
- if (authenticatedUserId !== args.user_id) throw new Error("UNAUTHORIZED");
+ async delete(args, authenticatedUserId) {
+ if (!authenticatedUserId) throw new Error('UNAUTHORIZED');
+ if (authenticatedUserId !== args.user_id) throw new Error('UNAUTHORIZED');
- try {
- let user_id = args.user_id;
- let component_id = args.component_id;
+ try {
+ let user_id = args.user_id;
+ let component_id = args.component_id;
- let deletedVote = await this.voteRepository.hardDeleteByUser(user_id, component_id);
- let component = await this.componentRepository.getById(component_id);
- component.score -= deletedVote.vote;
- let ownerUser = await this.userRepository.getById(component.owner_user_id);
- ownerUser.experience -= deletedVote.vote;
- let updatedUser = await this.userRepository.update({
- id: ownerUser.id,
- experience: ownerUser.experience
- });
- let updatedComponent = await this.componentRepository.update({
- id: component.id,
- score: component.score
- });
+ let deletedVote = await this.voteRepository.hardDeleteByUser(
+ user_id,
+ component_id
+ );
+ let component = await this.componentRepository.getById(component_id);
+ component.score -= deletedVote.vote;
+ let ownerUser = await this.userRepository.getById(
+ component.owner_user_id
+ );
+ ownerUser.experience -= deletedVote.vote;
+ let updatedUser = await this.userRepository.update({
+ id: ownerUser.id,
+ experience: ownerUser.experience
+ });
+ let updatedComponent = await this.componentRepository.update({
+ id: component.id,
+ score: component.score
+ });
- return deletedVote;
- } catch (error) {
- if (error.message === "UNAUTHORIZED") throw error;
- console.log(error);
- }
- }
+ return deletedVote;
+ } catch (error) {
+ if (error.message === 'UNAUTHORIZED') throw error;
+ console.log(error);
+ }
+ }
}
module.exports = VoteService;