-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactions-service.js
97 lines (90 loc) · 2.41 KB
/
reactions-service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module.exports = {
/**
*
*/
validateReaction: function (reaction) {
const VALID_REACTIONS = ["💗", "👌", "🤩", "✅", "🧠", "👶"];
if (VALID_REACTIONS.includes(reaction)) {
return true;
} else {
throw new Error(`Invalid reaction string ${reaction}`);
}
},
/**
*
*/
getUpdatedReactionsWithUserReaction: function (
worksheetReactions,
userReaction,
user
) {
// check if a reactions array if defined
if (!Array.isArray(worksheetReactions)) {
worksheetReactions = [];
} else {
WorksheetService.removeUserReaction(worksheetReactions, user.id);
}
let reactionObject = WorksheetService.getReactionObject(user, userReaction);
worksheetReactions.push(reactionObject);
return worksheetReactions;
},
/**
* returns reaction object composed of user data, date, reaction string
*/
removeUserReaction: function (worksheetReactions, userId) {
let userReactionIndex = WorksheetService.getUserReactionIndex(
worksheetReactions,
userId
);
// remove existing user reaction
if (userReactionIndex > -1) {
worksheetReactions.splice(userReactionIndex, 1);
}
return worksheetReactions;
},
/**
* returns reaction object composed of user data, date, reaction string
*/
getReactionObject: function (user, userReaction) {
return {
reaction: userReaction,
userId: user.id,
firstName: user.firstName,
lastName: user.lastName,
imagePath: user.imagePath,
date: Date.now(),
};
},
/**
*
*/
getUserReactionIndex: function (reactions, userId) {
return reactions.findIndex((reaction) => reaction.userId === userId);
},
/**
*
*/
getWorksheetReactionsCount: function (worksheet) {
return worksheet && worksheet.reactions
? worksheet.reactions.reduce(
(reactionsMap, reaction) => ({
...reactionsMap,
...(reactionsMap[reaction.reaction]
? { [reaction.reaction]: reactionsMap[reaction.reaction] + 1 }
: { [reaction.reaction]: 1 }),
}),
{}
)
: {};
},
/**
*
*/
getUserWorksheetReactions: function (user, worksheet) {
return user && user.id && worksheet && worksheet.reactions
? worksheet.reactions
.filter((reaction) => reaction.userId === user.id)
.map((reaction) => reaction.reaction)
: [];
},
};