Skip to content

Commit

Permalink
added reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
tigransimonyan committed Dec 17, 2023
1 parent a81b305 commit 7e7973c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
30 changes: 19 additions & 11 deletions src/api/reactions/controller.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import Reaction from './model';
import { getPageData } from './helper';

import { asyncRoute } from '../../services/express';
const VIEW = 'view';

export const reactOrView = asyncRoute(async (req, res) => {
export const add = asyncRoute(async (req, res) => {
const fingerprint = req.headers.fingerprint;
const pageId = req.body.pageId || req.query.pageId;
const reaction = (req.body.reaction || VIEW).slice(0, 20); // Just in case, limit characters by 20
const reaction = req.body.reaction.slice(0, 20); // Just in case, limit characters by 20

if (!fingerprint) {
return res.status(500).send('Fingerprint required for reacting.');
Expand All @@ -16,17 +14,27 @@ export const reactOrView = asyncRoute(async (req, res) => {
const searchCondition = { pageId, fingerprint };
const recordInDB = await Reaction.findOne(searchCondition);

const newReaction = recordInDB && recordInDB.reaction === reaction ? VIEW : reaction;
if (recordInDB && reaction !== VIEW) {
recordInDB.reaction = newReaction;
await recordInDB.save();
}

if (!recordInDB) {
await new Reaction({ ...searchCondition, reaction }).save();
await Reaction.create({ ...searchCondition, reaction });
} else {
if (recordInDB.reaction === reaction) {
await Reaction.deleteOne(recordInDB);
} else {
recordInDB.reaction = reaction;
await recordInDB.save();
}
}

const reactions = await getPageData(searchCondition);

return res.status(200).json(reactions);
});

export const list = asyncRoute(async (req, res) => {
const fingerprint = req.headers.fingerprint;
const pageId = req.body.pageId || req.query.pageId;

const reactions = await getPageData({ pageId, fingerprint });

return res.status(200).json(reactions);
});
13 changes: 7 additions & 6 deletions src/api/reactions/helper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Reaction from './model';

export const getPageData = async ({ fingerprint, pageId }) => {
const userReactionPromise = Reaction.findOne({
fingerprint,
pageId
}).select('reaction -_id');

const userReactionPromise = Reaction.findOne({ fingerprint, pageId }).select('reaction -_id');
const pageViewsPromise = Reaction.find({ pageId }).count();
const aggregationPromise = Reaction.aggregate()
.match({
pageId
Expand All @@ -13,11 +15,10 @@ export const getPageData = async ({ fingerprint, pageId }) => {
count: { $count: {} }
});

const [aggregation, userReaction, pageViews] = await Promise.all([
const [aggregation, userReaction] = await Promise.all([
aggregationPromise,
userReactionPromise,
pageViewsPromise
userReactionPromise
]);

return { aggregation, userReaction, pageViews };
return { aggregation, userReaction };
};
6 changes: 3 additions & 3 deletions src/api/reactions/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Router } from 'express';
import { reactOrView } from './controller';
import { list, add } from './controller';

const router = new Router();

router.post('/', reactOrView);
router.get('/', reactOrView);
router.post('/', add);
router.get('/', list);

export default router;

0 comments on commit 7e7973c

Please sign in to comment.