Skip to content

Commit

Permalink
164796907-feature(user-read-stats): refactor code
Browse files Browse the repository at this point in the history
Remove unnecessary util functions and tests
Simplify feature

[#164796907]
  • Loading branch information
micah-akpan committed Apr 24, 2019
1 parent 25f61e8 commit 2f8bbe6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 42 deletions.
10 changes: 2 additions & 8 deletions src/controllers/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import {
omitProps,
sendResponse,
handleDBErrors,
addArticleToReadHistory,
removeDuplicateObject
addArticleToReadHistory
} from '../utils';
import { findAndCount } from '../utils/query';

Expand Down Expand Up @@ -345,13 +344,8 @@ export const getReadingStats = async (req, res) => {
}],
attributes: { exclude: ['createdAt', 'updatedAt'] }
});
let readArticles = userReadHistory
.map(history => history.article)
.map(article => article.get({ plain: 'true' }));

// By stripping out duplicate articles
// we will return accurate article read statistics of this user
readArticles = removeDuplicateObject(readArticles, 'id');
const readArticles = userReadHistory.map(history => history.article);

return res.status(200).json({
status: 'success',
Expand Down
25 changes: 5 additions & 20 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,28 +180,13 @@ export const handleDBErrors = (error, { req, Sequelize }, cb) => {
*/
export const addArticleToReadHistory = async (articleId, userId) => {
try {
const articleReadHistory = await ArticleReadHistory.create({ articleId, userId });
let articleReadHistory = await ArticleReadHistory.findOne({ where: { articleId, userId } });
if (!articleReadHistory) {
articleReadHistory = await ArticleReadHistory.create({ articleId, userId });
return articleReadHistory;
}
return articleReadHistory;
} catch (error) {
throw error;
}
};

/**
* @description Removes duplicated object using attribute `prop`
* @function removeDuplicateObject
* @param {Array} array array of objects
* @param {String} prop the prop to use as the criteria for uniqueness
* @returns {Array} Returns an array of non-duplicated records (non-duplicated by `prop`)
*/
export const removeDuplicateObject = (array, prop) => {
const arrayOfUniqs = [], uniq = {};
array.forEach((obj) => {
uniq[obj[prop]] = obj;
});
for (const entry of Object.entries(uniq)) {
const [, value] = entry;
arrayOfUniqs.push(value);
}
return arrayOfUniqs;
};
15 changes: 1 addition & 14 deletions tests/unit/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
errorResponseFormat,
generateDummyWords,
handleDBErrors,
checkIDParamType,
removeDuplicateObject
checkIDParamType
} from '../../src/utils';
import { invalidArticleUUID } from '../mock/bookmark';

Expand Down Expand Up @@ -90,16 +89,4 @@ describe('Util test', () => {
});
});
});

describe('remove duplicated records', () => {
it('should remove duplicated objects from array', () => {
const dicts = [{ id: 1 }, { id: 3 }, { id: 1 }];
removeDuplicateObject(dicts, 'id').should.deep.equal([{ id: 1 }, { id: 3 }]);
});

it('should remove duplicated objects from array', () => {
const dicts = [{ id: 1 }, { id: 2 }, { id: 2 }];
removeDuplicateObject(dicts, 'id').should.deep.equal([{ id: 1 }, { id: 2 }]);
});
});
});

0 comments on commit 2f8bbe6

Please sign in to comment.