-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactions-migration.js
156 lines (148 loc) Β· 3.8 KB
/
reactions-migration.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const worksheetsMock = [
{
id: 1,
reactions: [
{ userId: 6471254, reaction: "π" },
{ userId: 7134896, reaction: "π" },
{ userId: 7188825, reaction: "π€©" },
{ userId: 7188825, reaction: "β
" },
{ userId: 6471254, reaction: "π€©" },
{ userId: 6383798, reaction: "π§ " },
{ userId: 3375102, reaction: "π§ " },
{ userId: 3375102, reaction: "π" },
{ userId: 3375102, reaction: "π€©" },
],
},
{
id: 2,
reactions: [{ userId: 6471254, reaction: "π" }],
},
{
id: 3,
reactions: null,
},
{
id: 4,
reactions: [],
},
{
id: 5,
reactions: [
{ userId: 3375102, reaction: "π§ " },
{ userId: 3375102, reaction: "π" },
{ userId: 3375102, reaction: "π€©" },
],
},
];
/**
* get user by id from db
*/
async function getUserFromDB(userId) {
// mock
return Promise.resolve({
id: userId,
imagePath: `http://s3.smth.com/image-${userId}.url`,
firstName: ["Alan", "Zoe", 'Ivan', 'Derek', 'Dan'][Math.floor(5 * Math.random())],
lastName: ["Brown", "Mitch", 'Silverstone', 'Flint', 'Reynolds'][Math.floor(5 * Math.random())],
});
// not mock
// return await knex
// .select("id", "first_name,", "last_name", "image_path")
// .from("us_user")
// .where({ id: userId })
// .map((rawUserRecord) => ({
// firstName: rawUserRecord.first_name,
// lastName: rawUserRecord.last_name,
// imagePath: rawUserRecord.image_path,
// }));
}
/**
* get list of worksheets where reactions has been set
*/
async function getWorksheetsWithNonNullReactionsFromDB() {
// mock
return Promise.resolve(worksheetsMock);
// not mock
// return await knex
// .select("id", "reactions")
// .from("ws_wroksheet")
// .havingNotNull("reactions");
}
/**
* update worksheet reactions by workshhet id
*/
async function saveWorksheetReactionsInDB(worksheet) {
// mock
return Promise.resolve({ success: true });
// not mock
// return await knex("ws_worksheet")
// .update({ reactions: worksheet.reactions })
// .where({ id: worksheet.id });
}
/**
* returns the list of the last pushed reactions for every user
* with reaction order preservation
*/
function getUniqueUserReactions(reactions) {
let usedUserIds = {};
return reactions
.reverse()
.filter((reaction) => {
let isUsed = !(reaction.userId in usedUserIds);
usedUserIds[reaction.userId] = true;
return isUsed;
})
.reverse();
}
/**
* enhance previos reaction records with additional data from user
*/
async function enhanceReactions(reactions) {
for await (let reaction of reactions) {
let user = await getUserFromDB(reaction.userId);
reaction = enhanceReaction(reaction, user);
}
return reactions;
}
/**
* enhance previos reaction record with additional data from user, date
*/
function enhanceReaction(reaction, user) {
let { imagePath, firstName, lastName } = user;
return Object.assign(reaction, {
imagePath,
firstName,
lastName,
date: Date.now(),
});
}
/**
* cases when we dont need a migration of reactions list
*/
function isMigrationRequired(worksheet) {
let reactions = worksheet.reactions;
if (reactions === null) {
return false;
}
if (Array.isArray(reactions) && !reactions.length) {
return false;
}
return true;
}
/**
* main
*/
getWorksheetsWithNonNullReactionsFromDB()
.then(async (worksheets) => {
for await (let worksheet of worksheets) {
if (isMigrationRequired(worksheet)) {
let uniqueReactions = getUniqueUserReactions(worksheet.reactions);
worksheet.reactions = await enhanceReactions(uniqueReactions);
await saveWorksheetReactionsInDB(worksheet);
}
}
console.log(JSON.stringify(worksheets));
})
.catch((err) => {
console.log(err);
});