Skip to content

Commit 06b4d50

Browse files
authored
Merge pull request #61 from eldimious/feature/NAS-0_eslint
NAS-0: lint files
2 parents 6db61a8 + f135148 commit 06b4d50

File tree

17 files changed

+171
-273
lines changed

17 files changed

+171
-273
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ coverage
3030
.nyc_output
3131
# Compiled binary addons (http://nodejs.org/api/addons.html)
3232
build/Release
33+
dist/

src/common/constants/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const MAX_CONSECUTIVE_FAILS_BY_USERNAME = 10;
1212
const PRODUCTION_ENV = 'production';
1313
const VERBOSE_LOGGING_LVL = 'verbose';
1414
const INFO_LOGGING_LVL = 'info';
15+
const DEFAULT_PAGINATION_LIMIT = 25;
16+
const MAX_PAGINATION_LIMIT = 100;
17+
const DEFAULT_PAGINATION_PAGE = 1;
1518

1619
module.exports = {
1720
USER_TOKEN_EXPIRATION,
@@ -21,4 +24,7 @@ module.exports = {
2124
PRODUCTION_ENV,
2225
VERBOSE_LOGGING_LVL,
2326
INFO_LOGGING_LVL,
27+
DEFAULT_PAGINATION_LIMIT,
28+
MAX_PAGINATION_LIMIT,
29+
DEFAULT_PAGINATION_PAGE,
2430
};

src/common/logging/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const logger = winston.createLogger({
4040
module.exports = {
4141
requestLogger,
4242
errorLogger,
43+
raw: logger,
4344
error: logger.error.bind(logger),
4445
warn: logger.warn.bind(logger),
4546
info: logger.info.bind(logger),

src/common/utils/helper/index.js

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,7 @@
1-
const isObject = obj => (typeof obj === 'object' && obj !== null) || typeof obj === 'function';
2-
3-
4-
const getConstructorName = function getConstructorName(obj) {
5-
return (obj.constructor && obj.constructor.name) ? obj.constructor.name : '';
6-
};
7-
8-
9-
const isDate = d => d instanceof Date;
10-
11-
12-
const isEmpty = o => Object.keys(o).length === 0;
13-
14-
15-
const properObject = o => isObject(o) && !o.hasOwnProperty ? { ...o } : o;
16-
17-
18-
const AsyncFunction = (async () => {}).constructor;
19-
20-
21-
const isFunctionConstructor = (f) => {
22-
if (f.constructor === Function || f.constructor === AsyncFunction) {
23-
return true;
24-
}
25-
return false;
26-
};
27-
28-
const isArgumentFunction = (f) => {
29-
if (!f || typeof f !== 'function' || !isFunctionConstructor(f)) {
30-
return false;
31-
}
32-
return true;
33-
};
34-
35-
36-
const isFunction = (obj) => {
37-
return !!(obj && obj.constructor && obj.call && obj.apply);
38-
};
39-
40-
41-
const getDiffValuesFromObjects = (lhs, rhs) => {
42-
if (lhs === rhs) return {};
43-
if (!isObject(lhs) || !isObject(rhs)) return rhs;
44-
const l = properObject(lhs);
45-
const r = properObject(rhs);
46-
const deletedValues = Object.keys(l).reduce((acc, key) => {
47-
return r.hasOwnProperty(key) ? acc : { ...acc, [key]: undefined };
48-
}, {});
49-
if (isDate(l) || isDate(r)) {
50-
if (l.valueOf() == r.valueOf()) return {};
51-
return r;
52-
}
53-
return Object.keys(r).reduce((acc, key) => {
54-
if (!l.hasOwnProperty(key)) return { ...acc, [key]: r[key] };
55-
const difference = getDiffValuesFromObjects(l[key], r[key]);
56-
if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc;
57-
return { ...acc, [key]: difference };
58-
}, deletedValues);
59-
};
60-
61-
62-
const compareObjects = function compareObjects(obj1, obj2) {
63-
return Object.keys(obj1).every(k1 =>
64-
Object.keys(obj2).some(k2 =>
65-
obj1[k1] === obj2[k2],
66-
),
67-
);
68-
};
69-
70-
71-
const getElementsToSort = (a, b, prop, type) => {
72-
if (type === 'date') {
73-
return {
74-
keyA: new Date(a[prop]),
75-
keyB: new Date(b[prop]),
76-
};
77-
}
78-
return {
79-
keyA: a[prop],
80-
keyB: b[prop],
81-
};
82-
};
83-
84-
85-
const sortArrayDescBy = (prop, type) => (a, b) => {
86-
const { keyA, keyB } = getElementsToSort(a, b, prop, type);
87-
if (keyA > keyB) return -1;
88-
if (keyA < keyB) return 1;
89-
return 0;
90-
};
91-
92-
93-
const sortArrayAscBy = (prop, type) => (a, b) => {
94-
const { keyA, keyB } = getElementsToSort(a, b, prop, type);
95-
if (keyA < keyB) return -1;
96-
if (keyA > keyB) return 1;
97-
return 0;
98-
};
99-
100-
101-
const removeDuplicateFromArray = arr => Array.from(new Set(arr));
102-
1031
function getRetryAfterSeconds(msBeforeNext) {
1042
return Math.round(msBeforeNext / 1000) || 1;
1053
}
1064

1075
module.exports = {
108-
isObject,
109-
isArgumentFunction,
110-
isFunction,
111-
getConstructorName,
112-
getDiffValuesFromObjects,
113-
compareObjects,
114-
sortArrayDescBy,
115-
sortArrayAscBy,
116-
removeDuplicateFromArray,
1176
getRetryAfterSeconds,
1187
};

src/common/utils/workerProcesses/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ const setupWorkerProcesses = () => {
1717
// these references can be used to receive messages from workers
1818
workers.push(cluster.fork());
1919
// to receive messages from worker process
20-
workers[i].on('message', message => logging.info(`worker message: ${message}`));
20+
workers[i].on('message', (message) => logging.info(`worker message: ${message}`));
2121
}
2222
// process is clustered on a core and process id is assigned
23-
cluster.on('online', worker => logging.info(`Worker ${worker.process.pid} is online`));
24-
cluster.on('listening', worker => logging.info(`Worker ${worker.process.pid} is listening`));
23+
cluster.on('online', (worker) => logging.info(`Worker ${worker.process.pid} is online`));
24+
cluster.on('listening', (worker) => logging.info(`Worker ${worker.process.pid} is listening`));
2525
// if any of the worker process dies then start a new one by simply forking another one
2626
cluster.on('exit', (worker, code, signal) => {
2727
logging.info(`Worker ${worker.process.pid} died with code: ${code} and signal: ${signal}`);
@@ -31,7 +31,7 @@ const setupWorkerProcesses = () => {
3131
const newWorker = cluster.fork();
3232
workers.push(newWorker);
3333
// to receive messages from new worker process
34-
workers[workers.length - 1].on('message', message => logging.info(`worker message: ${message}`));
34+
workers[workers.length - 1].on('message', (message) => logging.info(`worker message: ${message}`));
3535
}
3636
});
3737
};
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const postSchema = require('./Post');
22
const userSchema = require('./User');
33

4-
5-
module.exports.create = mongoose => ({
4+
module.exports.create = (mongoose) => ({
65
Post: postSchema(mongoose),
76
User: userSchema(mongoose),
87
});

src/data/repositories/posts/index.js

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ const DEFAULT_PAGINATION_CONTENT = {
1717
data: [],
1818
};
1919

20-
2120
const handleUsersPaginationResponse = (response) => {
2221
if (!response.docs || response.docs.length <= 0) {
2322
return DEFAULT_PAGINATION_CONTENT;
2423
}
2524
const postsList = {
26-
data: response.docs.map(doc => mapper.toDomainModel(doc, PostDomainModel)),
25+
data: response.docs.map((doc) => mapper.toDomainModel(doc, PostDomainModel)),
2726
pagination: {
2827
total: response.total,
2928
limit: response.limit,
@@ -34,14 +33,13 @@ const handleUsersPaginationResponse = (response) => {
3433
return postsList;
3534
};
3635

37-
const getPaginationOptions = options => ({
36+
const getPaginationOptions = (options) => ({
3837
lean: true,
3938
page: options.page || 1,
4039
limit: options.limit || 25,
4140
sort: { created: -1 },
4241
});
4342

44-
4543
const getQueryObject = (options) => {
4644
const queries = {
4745
userId: options.userId,
@@ -55,52 +53,37 @@ const getQueryObject = (options) => {
5553
return queries;
5654
};
5755

58-
5956
const postStore = {
6057
async listUserPosts(options) {
61-
try {
62-
const { Post: postSchema } = this.getSchemas();
63-
const docs = await postSchema.paginate(getQueryObject(options), getPaginationOptions(options));
64-
return handleUsersPaginationResponse(docs);
65-
} catch (error) {
66-
throw error;
67-
}
58+
const { Post: postSchema } = this.getSchemas();
59+
const docs = await postSchema.paginate(getQueryObject(options), getPaginationOptions(options));
60+
return handleUsersPaginationResponse(docs);
6861
},
6962
async createUserPost(options) {
70-
try {
71-
const { Post: postSchema } = this.getSchemas();
72-
const newPost = new postSchema({
73-
userId: options.userId,
74-
imageUrl: options.imageUrl,
75-
description: options.description,
76-
publisher: options.publisher,
77-
});
78-
const doc = await newPost.save();
79-
return mapper.toDomainModel(doc, PostDomainModel);
80-
} catch (error) {
81-
throw error;
82-
}
63+
const { Post: postSchema } = this.getSchemas();
64+
const newPost = new postSchema({
65+
userId: options.userId,
66+
imageUrl: options.imageUrl,
67+
description: options.description,
68+
publisher: options.publisher,
69+
});
70+
const doc = await newPost.save();
71+
return mapper.toDomainModel(doc, PostDomainModel);
8372
},
8473
async getUserPost(options) {
85-
try {
86-
const { Post: postSchema } = this.getSchemas();
87-
const doc = await postSchema.findOne({ userId: options.userId, _id: options.postId }).lean().exec();
88-
if (!doc) {
89-
throw new errors.NotFound(`Post with id ${options.postId} not found.`);
90-
}
91-
return mapper.toDomainModel(doc, PostDomainModel);
92-
} catch (error) {
93-
throw error;
74+
const { Post: postSchema } = this.getSchemas();
75+
const doc = await postSchema.findOne({ userId: options.userId, _id: options.postId }).lean().exec();
76+
if (!doc) {
77+
throw new errors.NotFound(`Post with id ${options.postId} not found.`);
9478
}
79+
return mapper.toDomainModel(doc, PostDomainModel);
9580
},
9681
};
9782

98-
9983
module.exports.init = ({ Post }) => Object.assign(Object.create(postStore), {
10084
getSchemas() {
10185
return {
10286
Post,
10387
};
10488
},
10589
});
106-

src/data/repositories/recourceLimiterRepository/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ module.exports.init = function init() {
2323

2424
return {
2525
maxConsecutiveFailsByUsername: MAX_CONSECUTIVE_FAILS_BY_USERNAME,
26-
getUserKeyForFailedLogin: async usernameKey => limiterUserConsecutiveFailsByUsername.get(usernameKey),
27-
consumeUserPointsForFailedLogin: async usernameKey => limiterUserConsecutiveFailsByUsername.consume(usernameKey),
28-
deleteUserKeyForFailedLogin: async usernameKey => limiterUserConsecutiveFailsByUsername.delete(usernameKey),
26+
getUserKeyForFailedLogin: async (usernameKey) => limiterUserConsecutiveFailsByUsername.get(usernameKey),
27+
consumeUserPointsForFailedLogin: async (usernameKey) => limiterUserConsecutiveFailsByUsername.consume(usernameKey),
28+
deleteUserKeyForFailedLogin: async (usernameKey) => limiterUserConsecutiveFailsByUsername.delete(usernameKey),
2929
};
3030
};

src/data/repositories/users/index.js

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,35 @@ const userStore = {
2121
email,
2222
password,
2323
}) {
24-
try {
25-
const {
26-
User: userSchema,
27-
} = this.getSchemas();
28-
const newUser = new userSchema({
29-
name,
30-
surname,
31-
username,
32-
email,
33-
password,
34-
});
35-
const userDoc = await newUser.save();
36-
return mapper.toDomainModel(userDoc, UserDomainModel);
37-
} catch (error) {
38-
throw error;
39-
}
24+
const {
25+
User: userSchema,
26+
} = this.getSchemas();
27+
const newUser = new userSchema({
28+
name,
29+
surname,
30+
username,
31+
email,
32+
password,
33+
});
34+
const userDoc = await newUser.save();
35+
return mapper.toDomainModel(userDoc, UserDomainModel);
4036
},
4137

4238
async getUser({
4339
email,
4440
userId,
4541
}) {
46-
try {
47-
const { User: userSchema } = this.getSchemas();
48-
const userDoc = await userSchema.findOne(queryForGetUser({
49-
email,
50-
userId,
51-
}))
52-
.lean()
53-
.exec();
54-
if (!userDoc) {
55-
throw new errors.NotFound('User not found.');
56-
}
57-
return mapper.toDomainModel(userDoc, UserDomainModel);
58-
} catch (error) {
59-
throw error;
42+
const { User: userSchema } = this.getSchemas();
43+
const userDoc = await userSchema.findOne(queryForGetUser({
44+
email,
45+
userId,
46+
}))
47+
.lean()
48+
.exec();
49+
if (!userDoc) {
50+
throw new errors.NotFound('User not found.');
6051
}
52+
return mapper.toDomainModel(userDoc, UserDomainModel);
6153
},
6254
};
6355

0 commit comments

Comments
 (0)