Skip to content
Open
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
104 changes: 57 additions & 47 deletions lib/services/ActivityService.js
Original file line number Diff line number Diff line change
@@ -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;
68 changes: 39 additions & 29 deletions lib/services/AuthenticationService.js
Original file line number Diff line number Diff line change
@@ -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;
module.exports = AuthenticationService;
158 changes: 81 additions & 77 deletions lib/services/CommentService.js
Original file line number Diff line number Diff line change
@@ -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;
Loading