-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathupdateUserProfileInProjects.js
331 lines (272 loc) · 16.1 KB
/
updateUserProfileInProjects.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* name : updateUserProfileInProjects.js
* author : Priyanka Pradeep
* created-date : 10-Nov-2022
* Description : Migration script for update userProfile in project
*/
const path = require("path");
let rootPath = path.join(__dirname, '../../')
require('dotenv').config({ path: rootPath+'/.env' })
let _ = require("lodash");
let mongoUrl = process.env.MONGODB_URL;
let dbName = mongoUrl.split("/").pop();
let url = mongoUrl.split(dbName)[0];
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID;
var fs = require('fs');
const request = require('request');
const userServiceUrl = "http://learner-service:9000";
const endPoint = "/v1/location/search";
(async () => {
let connection = await MongoClient.connect(url, { useNewUrlParser: true });
let db = connection.db(dbName);
try {
let updatedProjectIds = [];
//get all projects
let projectDocument = await db.collection('projects').find({
userRoleInformation: {$exists : true},
userProfile: {$exists : true},
}).project({ "_id": 1}).toArray();
let chunkOfProjectDocument = _.chunk(projectDocument, 10);
let projectIds;
for (let pointerToProject = 0; pointerToProject < chunkOfProjectDocument.length; pointerToProject++) {
projectIds = await chunkOfProjectDocument[pointerToProject].map(
projectDoc => {
return projectDoc._id;
}
);
let projectDocuments = await db.collection('projects').find({
_id: { $in : projectIds }
}).project({
"_id": 1,
"userRoleInformation" : 1,
"userProfile" : 1
}).toArray();
//loop all projects
for ( let count = 0; count < projectDocuments.length; count++ ) {
let project = projectDocuments[count];
let userProfile = project.userProfile;
let updateUserProfileRoleInformation = false; // Flag to see if roleInformation i.e. userProfile.profileUserTypes has to be updated based on userRoleInfromation.roles
if(project.userRoleInformation.role) { // Check if userRoleInformation has role value.
let rolesInUserRoleInformation = project.userRoleInformation.role.split(","); // userRoleInfomration.role can be multiple with comma separated.
let resetCurrentUserProfileRoles = false; // Flag to reset current userProfile.profileUserTypes i.e. if current role in profile is not at all there in userRoleInformation.roles
// Check if userProfile.profileUserTypes exists and is an array of length > 0
if(userProfile.profileUserTypes && Array.isArray(userProfile.profileUserTypes) && userProfile.profileUserTypes.length >0) {
// Loop through current roles in userProfile.profileUserTypes
for (let pointerToCurrentProfileUserTypes = 0; pointerToCurrentProfileUserTypes < userProfile.profileUserTypes.length; pointerToCurrentProfileUserTypes++) {
const currentProfileUserType = userProfile.profileUserTypes[pointerToCurrentProfileUserTypes];
if(currentProfileUserType.subType && currentProfileUserType.subType !== null) { // If the role has a subType
// Check if subType exists in userRoleInformation role, if not means profile data is old and should be reset.
if(!project.userRoleInformation.role.toUpperCase().includes(currentProfileUserType.subType.toUpperCase())) {
resetCurrentUserProfileRoles = true; // Reset userProfile.profileUserTypes
break;
}
} else { // If the role subType is null or is not there
// Check if type exists in userRoleInformation role, if not means profile data is old and should be reset.
if(!project.userRoleInformation.role.toUpperCase().includes(currentProfileUserType.type.toUpperCase())) {
resetCurrentUserProfileRoles = true; // Reset userProfile.profileUserTypes
break;
}
}
}
}
if(resetCurrentUserProfileRoles) { // Reset userProfile.profileUserTypes
userProfile.profileUserTypes = new Array;
}
// Loop through each subRole in userRoleInformation
for (let pointerToRolesInUserInformation = 0; pointerToRolesInUserInformation < rolesInUserRoleInformation.length; pointerToRolesInUserInformation++) {
const subRole = rolesInUserRoleInformation[pointerToRolesInUserInformation];
// Check if userProfile.profileUserTypes exists and is an array of length > 0
if(userProfile.profileUserTypes && Array.isArray(userProfile.profileUserTypes) && userProfile.profileUserTypes.length >0) {
if(!_.find(userProfile.profileUserTypes, { 'type': subRole.toLowerCase() }) && !_.find(userProfile.profileUserTypes, { 'subType': subRole.toLowerCase() })) {
updateUserProfileRoleInformation = true; // Need to update userProfile.profileUserTypes
if(subRole.toUpperCase() === "TEACHER") { // If subRole is not teacher
userProfile.profileUserTypes.push({
"subType" : null,
"type" : "teacher"
})
} else { // If subRole is not teacher
userProfile.profileUserTypes.push({
"subType" : subRole.toLowerCase(),
"type" : "administrator"
})
}
}
} else { // Make a new entry if userProfile.profileUserTypes is empty or does not exist.
updateUserProfileRoleInformation = true; // Need to update userProfile.profileUserTypes
userProfile.profileUserTypes = new Array;
if(subRole.toUpperCase() === "TEACHER") { // If subRole is teacher
userProfile.profileUserTypes.push({
"subType" : null,
"type" : "teacher"
})
} else { // If subRole is not teacher
userProfile.profileUserTypes.push({
"subType" : subRole.toLowerCase(),
"type" : "administrator"
})
}
}
}
}
// Create location only object from userRoleInformation
let userRoleInformationLocationObject = _.omit(project.userRoleInformation, ['role']);
// All location keys from userRoleInformation
let userRoleInfomrationLocationKeys = Object.keys(userRoleInformationLocationObject);
let updateUserProfileLocationInformation = false; // Flag to see if userLocations i.e. userProfile.userLocations has to be updated based on userRoleInfromation location values
// Loop through all location keys.
for (let pointerToUserRoleInfromationLocationKeys = 0; pointerToUserRoleInfromationLocationKeys < userRoleInfomrationLocationKeys.length; pointerToUserRoleInfromationLocationKeys++) {
const locationType = userRoleInfomrationLocationKeys[pointerToUserRoleInfromationLocationKeys]; // e.g. state, district, school
const locationValue = userRoleInformationLocationObject[locationType]; // Location UUID values or school code.
// Check if userProfile.userLocations exists and is an array of length > 0
if(userProfile.userLocations && Array.isArray(userProfile.userLocations) && userProfile.userLocations.length >0) {
if(locationType === "school") { // If location type school exist check if same is there in userProfile.userLocations
if(!_.find(userProfile.userLocations, { 'type': "school", 'code': locationValue })) {
updateUserProfileLocationInformation = true; // School does not exist in userProfile.userLocations, update entire userProfile.userLocations
break;
}
} else { // Check if location type is there in userProfile.userLocations and has same value as userRoleInformation
if(!_.find(userProfile.userLocations, { 'type': locationType, 'id': locationValue })) {
updateUserProfileLocationInformation = true; // Location does not exist in userProfile.userLocations, update entire userProfile.userLocations
break;
}
}
} else {
updateUserProfileLocationInformation = true;
break;
}
}
if(userProfile.userLocations && Array.isArray(userProfile.userLocations) && userProfile.userLocations.length >0) {
if(userProfile.userLocations.length != userRoleInfomrationLocationKeys.length) {
updateUserProfileLocationInformation = true;
}
}
// If userProfile.userLocations has to be updated, get all values and set in userProfile.
if(updateUserProfileLocationInformation) {
//update userLocations in userProfile
let locationIds = [];
let locationCodes = [];
let userLocations = new Array;
userRoleInfomrationLocationKeys.forEach( requestedDataKey => {
if (checkIfValidUUID(userRoleInformationLocationObject[requestedDataKey])) {
locationIds.push(userRoleInformationLocationObject[requestedDataKey]);
} else {
locationCodes.push(userRoleInformationLocationObject[requestedDataKey]);
}
})
//query for fetch location using id
if ( locationIds.length > 0 ) {
let locationQuery = {
"id" : locationIds
}
let entityData = await locationSearch(locationQuery);
if ( entityData.success ) {
userLocations = entityData.data;
}
}
// query for fetch location using code
if ( locationCodes.length > 0 ) {
let codeQuery = {
"code" : locationCodes
}
let entityData = await locationSearch(codeQuery);
if ( entityData.success ) {
userLocations = userLocations.concat(entityData.data);
}
}
if ( userLocations.length > 0 ) {
userProfile["userLocations"] = userLocations;
}
}
//update projects if userProfile role or location information is incorrect
if ( updateUserProfileRoleInformation || updateUserProfileLocationInformation ) {
let updateObject = {
"$set" : {}
};
if(updateUserProfileRoleInformation) {
updateObject["$set"]["userProfile.profileUserTypes"] = userProfile.profileUserTypes;
updateObject["$set"]["userProfile.userRoleMismatchFoundAndUpdated"] = true;
}
if(updateUserProfileLocationInformation) {
updateObject["$set"]["userProfile.userLocations"] = userProfile.userLocations;
updateObject["$set"]["userProfile.userLocationsMismatchFoundAndUpdated"] = true;
}
await db.collection('projects').findOneAndUpdate({
"_id" : project._id
},updateObject);
updatedProjectIds.push(project._id.toString());
}
}
//write updated project ids to file
fs.writeFile(
'updatedProjectIds.json',
JSON.stringify(updatedProjectIds),
function (err) {
if (err) {
console.error('Crap happens');
}
}
);
}
function locationSearch ( filterData ) {
return new Promise(async (resolve, reject) => {
try {
let bodyData={};
bodyData["request"] = {};
bodyData["request"]["filters"] = filterData;
const url = userServiceUrl + endPoint;
const options = {
headers : {
"content-type": "application/json"
},
json : bodyData
};
request.post(url,options,requestCallback);
let result = {
success : true
};
function requestCallback(err, data) {
if (err) {
result.success = false;
} else {
let response = data.body;
if( response.responseCode === "OK" &&
response.result &&
response.result.response &&
response.result.response.length > 0
) {
let entityResult = new Array;
response.result.response.map(entityData => {
let entity = _.omit(entityData, ['identifier']);
entityResult.push(entity);
})
result["data"] = entityResult;
result["count"] = response.result.count;
} else {
result.success = false;
}
}
return resolve(result);
}
setTimeout(function () {
return resolve (result = {
success : false
});
}, 5000);
} catch (error) {
return reject(error);
}
})
}
console.log("Updated Project Count : ", updatedProjectIds.length)
console.log("completed")
connection.close();
}
catch (error) {
console.log(error)
}
})().catch(err => console.error(err));
function checkIfValidUUID(value) {
const regexExp = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi;
return regexExp.test(value);
}