lms-sync 1.0.14 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/apiConnections/mapping.js +1033 -497
- package/apiConnections/mappings-old.js +1517 -0
- package/apiConnections/tablePick.js +1 -1
- package/app.js +6 -4
- package/models/campuses.model.js +16 -0
- package/package.json +1 -1
@@ -0,0 +1,1517 @@
|
|
1
|
+
const {db, schema} = require('../models')
|
2
|
+
const logger = require('../utils/logger')
|
3
|
+
const api = require('../apiConnections/apiConnects')
|
4
|
+
const moment = require('moment')
|
5
|
+
const { Picked } = require('./semesterPick')
|
6
|
+
|
7
|
+
const {
|
8
|
+
AcademicDepartments,
|
9
|
+
AcademicYears,
|
10
|
+
Campuses,
|
11
|
+
Colleges,
|
12
|
+
Courses,
|
13
|
+
EnrolledStudents,
|
14
|
+
Instructors,
|
15
|
+
Rooms,
|
16
|
+
Sections,
|
17
|
+
Students,
|
18
|
+
Subjects,
|
19
|
+
Schedules,
|
20
|
+
Users,
|
21
|
+
Semesters
|
22
|
+
|
23
|
+
} = db
|
24
|
+
|
25
|
+
const server = {
|
26
|
+
|
27
|
+
async Campus(){
|
28
|
+
try {
|
29
|
+
console.log("");
|
30
|
+
console.log("########################################################")
|
31
|
+
console.log(" Campuses Migration ")
|
32
|
+
console.log("########################################################")
|
33
|
+
console.log("");
|
34
|
+
|
35
|
+
const campusApi = await api.Campus()
|
36
|
+
const userMail = await Users.findOne({ where: { email: 'ccci_integrator@gmail.com' } });
|
37
|
+
|
38
|
+
let createdCounter = 0
|
39
|
+
let errorCounter = 0
|
40
|
+
let uniqueErrors = new Set()
|
41
|
+
|
42
|
+
const db_schema = schema
|
43
|
+
// let maxIds = await Campuses.max('_id'); = await Campuses.max('_id'); // Fetch maximum _id directly from the campuses model
|
44
|
+
// let maxId = maxIds|| 0;
|
45
|
+
// await db.sequelize.query(`ALTER SEQUENCE ${db_schema}."Campuses__id_seq" RESTART WITH ${maxId + 1}`);
|
46
|
+
|
47
|
+
for (let i = 0; i < campusApi.length; i++){
|
48
|
+
try {
|
49
|
+
|
50
|
+
let _campus = campusApi[i]
|
51
|
+
|
52
|
+
let modifiedById = userMail._id
|
53
|
+
let createdById = userMail._id
|
54
|
+
|
55
|
+
let [record, created] = await Campuses.findOrCreate({
|
56
|
+
where:{
|
57
|
+
name: _campus?.name,
|
58
|
+
code: _campus?.code
|
59
|
+
},
|
60
|
+
defaults:{
|
61
|
+
email: _campus?.email,
|
62
|
+
address: _campus?.address,
|
63
|
+
modifiedById,
|
64
|
+
createdById
|
65
|
+
}
|
66
|
+
})
|
67
|
+
if (created){
|
68
|
+
logger.info(`New campus record created ${createdCounter + 1}/${createdCounter}: [${record.code}] - ${record.name} `)
|
69
|
+
createdCounter++;
|
70
|
+
}
|
71
|
+
else {
|
72
|
+
const errorMsg = `Record campus already exists: [${record.code}] - ${record.name} `;
|
73
|
+
if (!uniqueErrors.has(errorMsg)) {
|
74
|
+
logger.error(`${errorCounter + 1}/${errorCounter + 1} ${errorMsg}`);
|
75
|
+
uniqueErrors.add(errorMsg);
|
76
|
+
errorCounter++;
|
77
|
+
}
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
} catch (error) {
|
82
|
+
logger.error(`Error occurred: ${error}`);
|
83
|
+
logger.error(`Error occurred: ${error.stack}`);
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
let items = createdCounter + errorCounter
|
88
|
+
console.log("");
|
89
|
+
console.log("########################################################")
|
90
|
+
console.log("")
|
91
|
+
logger.info(' Campus Data Migration Completed');
|
92
|
+
logger.info(`Total successful Campus records created: ${createdCounter}/${createdCounter}`);
|
93
|
+
logger.info(`Total Campus Error Records: ${errorCounter}/${errorCounter}`);
|
94
|
+
logger.info(`Total Record Fetched: ${items} `)
|
95
|
+
console.log("")
|
96
|
+
console.log("########################################################")
|
97
|
+
console.log("")
|
98
|
+
|
99
|
+
let totalSummary = {
|
100
|
+
itemLength: items,
|
101
|
+
error: errorCounter,
|
102
|
+
success: createdCounter
|
103
|
+
}
|
104
|
+
return totalSummary
|
105
|
+
|
106
|
+
} catch (error) {
|
107
|
+
logger.error(`Error occurred: ${error}`);
|
108
|
+
logger.error(`Error occurred: ${error.stack}`);
|
109
|
+
|
110
|
+
}
|
111
|
+
},
|
112
|
+
|
113
|
+
async College(){
|
114
|
+
try {
|
115
|
+
console.log("");
|
116
|
+
console.log("########################################################")
|
117
|
+
console.log(" Colleges Migration ")
|
118
|
+
console.log("########################################################")
|
119
|
+
console.log("");
|
120
|
+
|
121
|
+
const collegeApi = await api.College()
|
122
|
+
const campusApi = await api.Campus()
|
123
|
+
|
124
|
+
const campusSeq = await Campuses.findAll()
|
125
|
+
const userMail = await Users.findOne({ where: { email: 'ccci_integrator@gmail.com' } });
|
126
|
+
|
127
|
+
let createdCounter = 0
|
128
|
+
let errorCounter = 0
|
129
|
+
let uniqueErrors = new Set()
|
130
|
+
|
131
|
+
const db_schema = schema
|
132
|
+
// let maxIds = await Campuses.max('_id'); = await Colleges.max('_id'); // Fetch maximum _id directly from the Colleges model
|
133
|
+
// let maxId = maxIds|| 0;
|
134
|
+
// await db.sequelize.query(`ALTER SEQUENCE ${db_schema}."Colleges__id_seq" RESTART WITH ${maxId + 1}`);
|
135
|
+
|
136
|
+
for (let i = 0; i < collegeApi.length; i++){
|
137
|
+
try {
|
138
|
+
let _college = collegeApi[i]
|
139
|
+
|
140
|
+
let campusMap = campusApi.find(a => a.id === _college?.campus_id)
|
141
|
+
|
142
|
+
let _campus = campusSeq.find(b => b.name === campusMap?.name)
|
143
|
+
|
144
|
+
let campusDefault = 999999
|
145
|
+
|
146
|
+
let modifiedById = userMail._id
|
147
|
+
let createdById = userMail._id
|
148
|
+
|
149
|
+
let [record, created] = await Colleges.findOrCreate({
|
150
|
+
|
151
|
+
where:{
|
152
|
+
name: _college?.name,
|
153
|
+
code: _college?.code
|
154
|
+
},
|
155
|
+
defaults:{
|
156
|
+
campusId: _campus?._id??campusDefault,
|
157
|
+
modifiedById,
|
158
|
+
createdById
|
159
|
+
}
|
160
|
+
})
|
161
|
+
if (created){
|
162
|
+
logger.info(`New college record created ${createdCounter + 1}/${createdCounter + 1}: [${record.code}] - ${record.name} `)
|
163
|
+
createdCounter++;
|
164
|
+
}
|
165
|
+
else {
|
166
|
+
const errorMsg = `Record college already exists: [${record.code}] - ${record.name} `
|
167
|
+
|
168
|
+
if (!uniqueErrors.has(errorMsg)) {
|
169
|
+
logger.error(`${errorCounter + 1}/${errorCounter + 1} ${errorMsg}`);
|
170
|
+
uniqueErrors.add(errorMsg);
|
171
|
+
errorCounter++;
|
172
|
+
}
|
173
|
+
}
|
174
|
+
|
175
|
+
} catch (error) {
|
176
|
+
logger.error(`Error occurred: ${error}`);
|
177
|
+
logger.error(`Error occurred: ${error.stack}`);
|
178
|
+
}
|
179
|
+
}
|
180
|
+
|
181
|
+
let items = createdCounter + errorCounter
|
182
|
+
|
183
|
+
console.log("");
|
184
|
+
console.log("########################################################")
|
185
|
+
console.log("")
|
186
|
+
logger.info(' College Data Migration Completed');
|
187
|
+
logger.info(`Total successful College records created: ${createdCounter}/${createdCounter}`);
|
188
|
+
logger.info(`Total College Error Records: ${errorCounter}/${errorCounter}`);
|
189
|
+
logger.info(`Total Record Fetched: ${items} `)
|
190
|
+
console.log("")
|
191
|
+
console.log("########################################################")
|
192
|
+
console.log("")
|
193
|
+
|
194
|
+
let totalSummary = {
|
195
|
+
itemLength: items,
|
196
|
+
error: errorCounter,
|
197
|
+
success: createdCounter
|
198
|
+
}
|
199
|
+
return totalSummary
|
200
|
+
|
201
|
+
} catch (error) {
|
202
|
+
logger.error(`Error occurred: ${error}`);
|
203
|
+
logger.error(`Error occurred: ${error.stack}`);
|
204
|
+
|
205
|
+
}
|
206
|
+
},
|
207
|
+
|
208
|
+
async AcademicDepartment(){
|
209
|
+
try {
|
210
|
+
console.log("");
|
211
|
+
console.log("########################################################")
|
212
|
+
console.log(" Academic Departments Migration ")
|
213
|
+
console.log("########################################################")
|
214
|
+
console.log("");
|
215
|
+
|
216
|
+
const departmentApi = await api.Department()
|
217
|
+
const campusApi = await api.Campus()
|
218
|
+
const collegeApi = await api.College()
|
219
|
+
|
220
|
+
|
221
|
+
const campusSeq = await Campuses.findAll()
|
222
|
+
const collegeSeq = await Colleges.findAll()
|
223
|
+
const userMail = await Users.findOne({ where: { email: 'ccci_integrator@gmail.com' } });
|
224
|
+
|
225
|
+
let createdCounter = 0
|
226
|
+
let errorCounter = 0
|
227
|
+
let uniqueErrors = new Set()
|
228
|
+
|
229
|
+
const db_schema = schema
|
230
|
+
// let maxIds = await Campuses.max('_id'); = await AcademicDepartments.max('_id'); // Fetch maximum _id directly from the AcademicDepartments model
|
231
|
+
// let maxId = maxIds|| 0;
|
232
|
+
// await db.sequelize.query(`ALTER SEQUENCE ${db_schema}."AcademicDepartments__id_seq" RESTART WITH ${maxId + 1}`);
|
233
|
+
for (let i = 0; i < departmentApi.length; i++){
|
234
|
+
try {
|
235
|
+
let _acadDept = departmentApi[i]
|
236
|
+
|
237
|
+
|
238
|
+
let _college = collegeSeq.find( col => col.code === _acadDept?.code)
|
239
|
+
|
240
|
+
let college = collegeApi.find( cols => cols.code === _acadDept?.code)
|
241
|
+
|
242
|
+
let _campus = campusApi.find(cam => cam.id === college?.campus_id)
|
243
|
+
|
244
|
+
let campus = campusSeq.find(camp => camp.code === _campus?.code)
|
245
|
+
|
246
|
+
let campusDefault = 999999
|
247
|
+
let collegeDefault = 999999
|
248
|
+
let chairPersonDefault = 999999
|
249
|
+
|
250
|
+
let modifiedById = userMail._id
|
251
|
+
let createdById = userMail._id
|
252
|
+
|
253
|
+
|
254
|
+
let [record, created] = await AcademicDepartments.findOrCreate({
|
255
|
+
|
256
|
+
where:{
|
257
|
+
name: _acadDept?.name,
|
258
|
+
code: _acadDept?.code
|
259
|
+
},
|
260
|
+
defaults:{
|
261
|
+
collegeId: _college?._id??collegeDefault,
|
262
|
+
campusId: campus?._id??campusDefault,
|
263
|
+
chairPersonId: chairPersonDefault,
|
264
|
+
modifiedById,
|
265
|
+
createdById
|
266
|
+
}
|
267
|
+
})
|
268
|
+
if (created){
|
269
|
+
logger.info(`New academic department record created ${createdCounter + 1}/${createdCounter + 1}: [${record.code}] - ${record.name} `)
|
270
|
+
createdCounter++;
|
271
|
+
}
|
272
|
+
else {
|
273
|
+
const errorMsg = `Record academic department already exists: [${record.code}] - ${record.name} `
|
274
|
+
|
275
|
+
if (!uniqueErrors.has(errorMsg)) {
|
276
|
+
logger.error(`${errorCounter + 1}/${errorCounter + 1} ${errorMsg}`);
|
277
|
+
uniqueErrors.add(errorMsg);
|
278
|
+
errorCounter++;
|
279
|
+
}
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
} catch (error) {
|
284
|
+
logger.error(`Error occurred: ${error}`);
|
285
|
+
logger.error(`Error occurred: ${error.stack}`);
|
286
|
+
}
|
287
|
+
}
|
288
|
+
|
289
|
+
let items = createdCounter + errorCounter
|
290
|
+
|
291
|
+
console.log("");
|
292
|
+
console.log("########################################################")
|
293
|
+
console.log("")
|
294
|
+
logger.info(' Academic Department Data Migration Completed');
|
295
|
+
logger.info(`Total successful Academic Department records created: ${createdCounter}/${createdCounter}`);
|
296
|
+
logger.info(`Total Academic Department Error Records: ${errorCounter}/${errorCounter}`);
|
297
|
+
logger.info(`Total Record Fetched: ${items} `)
|
298
|
+
console.log("")
|
299
|
+
console.log("########################################################")
|
300
|
+
console.log("")
|
301
|
+
|
302
|
+
let totalSummary = {
|
303
|
+
itemLength: items,
|
304
|
+
error: errorCounter,
|
305
|
+
success: createdCounter
|
306
|
+
}
|
307
|
+
return totalSummary
|
308
|
+
|
309
|
+
} catch (error) {
|
310
|
+
logger.error(`Error occurred: ${error}`);
|
311
|
+
logger.error(`Error occurred: ${error.stack}`);
|
312
|
+
|
313
|
+
}
|
314
|
+
},
|
315
|
+
|
316
|
+
async Course(){
|
317
|
+
try {
|
318
|
+
console.log("");
|
319
|
+
console.log("########################################################")
|
320
|
+
console.log(" Courses Migration ")
|
321
|
+
console.log("########################################################")
|
322
|
+
console.log("");
|
323
|
+
|
324
|
+
const studentApi = (await api.Student()).data
|
325
|
+
|
326
|
+
const courseApi = await api.Course()
|
327
|
+
const collegeApi = await api.College()
|
328
|
+
const departmentApi = await api.Department()
|
329
|
+
|
330
|
+
const acadDeptSeq = await AcademicDepartments.findAll()
|
331
|
+
|
332
|
+
const campusSeq = await Campuses.findAll()
|
333
|
+
const userMail = await Users.findOne({ where: { email: 'ccci_integrator@gmail.com' } });
|
334
|
+
|
335
|
+
let createdCounter = 0
|
336
|
+
let errorCounter = 0
|
337
|
+
let uniqueErrors = new Set()
|
338
|
+
|
339
|
+
const db_schema = schema
|
340
|
+
// let maxIds = await Campuses.max('_id'); = await Courses.max('_id'); // Fetch maximum _id directly from the Courses model
|
341
|
+
// let maxId = maxIds|| 0;
|
342
|
+
// await db.sequelize.query(`ALTER SEQUENCE ${db_schema}."Courses__id_seq" RESTART WITH ${maxId + 1}`);
|
343
|
+
|
344
|
+
for (let i = 0; i < studentApi.length; i++){
|
345
|
+
try {
|
346
|
+
|
347
|
+
let course = studentApi[i]
|
348
|
+
let courseCode = courseApi.find(crs => crs.name === course?.course)
|
349
|
+
let campus = campusSeq.find(camp => camp.name === course?.campus)
|
350
|
+
|
351
|
+
let college = collegeApi.find(col => col.name === course?.college)
|
352
|
+
let department = departmentApi.find(dep => dep.name === college?.name)
|
353
|
+
let _department = acadDeptSeq.find(ad => ad.name === department?.name)
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
let modifiedById = userMail._id
|
360
|
+
let createdById = userMail._id
|
361
|
+
|
362
|
+
|
363
|
+
let [record, created] = await Courses.findOrCreate({
|
364
|
+
where:{
|
365
|
+
name: course?.course,
|
366
|
+
code: courseCode?.code,
|
367
|
+
major: course?.major,
|
368
|
+
campusId: campus?._id??null,
|
369
|
+
departmentId: _department?._id??null,
|
370
|
+
modifiedById,
|
371
|
+
createdById
|
372
|
+
},
|
373
|
+
defaults:{
|
374
|
+
|
375
|
+
|
376
|
+
}
|
377
|
+
})
|
378
|
+
|
379
|
+
if (created) {
|
380
|
+
logger.info(`New course record created ${createdCounter +1}/${createdCounter +1}: [${record.code}] - ${record.name}, Major in ${record.major}`)
|
381
|
+
createdCounter++
|
382
|
+
} else {
|
383
|
+
|
384
|
+
const errorMsg = `Record course already exists: [${record.code}] - ${record.name}, Major in ${record.major}`;
|
385
|
+
|
386
|
+
if (!uniqueErrors.has(errorMsg)) {
|
387
|
+
logger.error(`${errorCounter + 1}/${errorCounter + 1} ${errorMsg}`);
|
388
|
+
uniqueErrors.add(errorMsg);
|
389
|
+
errorCounter++;
|
390
|
+
}
|
391
|
+
|
392
|
+
}
|
393
|
+
|
394
|
+
} catch (error) {
|
395
|
+
logger.error(`Error occurred: ${error}`);
|
396
|
+
logger.error(`Error occurred: ${error.stack}`);
|
397
|
+
}
|
398
|
+
}
|
399
|
+
|
400
|
+
let items = createdCounter + errorCounter
|
401
|
+
|
402
|
+
|
403
|
+
console.log("");
|
404
|
+
console.log("########################################################")
|
405
|
+
console.log("")
|
406
|
+
logger.info(' Course Data Migration Completed');
|
407
|
+
logger.info(`Total successful Course records created: ${createdCounter}/${createdCounter}`);
|
408
|
+
logger.info(`Total Course Error Records: ${errorCounter}/${errorCounter}`);
|
409
|
+
logger.info(`Total Record Fetched: ${items} `)
|
410
|
+
console.log("")
|
411
|
+
console.log("########################################################")
|
412
|
+
console.log("")
|
413
|
+
|
414
|
+
let totalSummary = {
|
415
|
+
itemLength: items,
|
416
|
+
error: errorCounter,
|
417
|
+
success: createdCounter
|
418
|
+
}
|
419
|
+
|
420
|
+
uniqueErrors.clear();
|
421
|
+
|
422
|
+
return totalSummary
|
423
|
+
|
424
|
+
} catch (error) {
|
425
|
+
logger.error(`Error occurred: ${error}`);
|
426
|
+
logger.error(`Error occurred: ${error.stack}`);
|
427
|
+
}
|
428
|
+
},
|
429
|
+
|
430
|
+
async Room(){
|
431
|
+
try {
|
432
|
+
console.log("");
|
433
|
+
console.log("########################################################")
|
434
|
+
console.log(" Rooms Migration ")
|
435
|
+
console.log("########################################################")
|
436
|
+
console.log("");
|
437
|
+
|
438
|
+
const subjectRoom = await api.Subject()
|
439
|
+
const roomApi = await api.Room()
|
440
|
+
|
441
|
+
const subjectSeq = await Subjects.findAll()
|
442
|
+
const campusSeq = await Campuses.findAll()
|
443
|
+
const departmentSeq = await AcademicDepartments.findAll()
|
444
|
+
|
445
|
+
const userMail = await Users.findOne({ where: { email: 'ccci_integrator@gmail.com' } });
|
446
|
+
|
447
|
+
let createdCounter = 0
|
448
|
+
let errorCounter = 0
|
449
|
+
let uniqueErrors = new Set()
|
450
|
+
|
451
|
+
|
452
|
+
const db_schema = schema
|
453
|
+
// let maxIds = await Campuses.max('_id'); = await Rooms.max('_id'); // Fetch maximum _id directly from the Rooms model
|
454
|
+
// let maxId = maxIds|| 0;
|
455
|
+
// await db.sequelize.query(`ALTER SEQUENCE ${db_schema}."Rooms__id_seq" RESTART WITH ${maxId + 1}`);
|
456
|
+
|
457
|
+
|
458
|
+
for (let i = 0; i < roomApi.length; i++){
|
459
|
+
try {
|
460
|
+
let _room = roomApi[i]
|
461
|
+
|
462
|
+
let _subject = subjectRoom.find(r => r.room_id === _room?.id)
|
463
|
+
// console.log('_subject :>> ', _subject);
|
464
|
+
|
465
|
+
let _subjects = subjectSeq.find(s => s.name === _subject?.name)
|
466
|
+
// console.log('_subjects :>> ', _subjects);
|
467
|
+
|
468
|
+
|
469
|
+
let _department = departmentSeq.find(dep => dep._id === _subjects?.departmentId)
|
470
|
+
// console.log('_department :>> ', _department);
|
471
|
+
|
472
|
+
|
473
|
+
let _campus = campusSeq.find (cam => cam._id === _department?.campusId)
|
474
|
+
// console.log('_campus :>> ', _campus);
|
475
|
+
|
476
|
+
|
477
|
+
let capacity = 50
|
478
|
+
|
479
|
+
let campusDefault = "MSC - Boac Campus"
|
480
|
+
|
481
|
+
let _campusDefault = campusSeq.find(ccc => ccc.name === campusDefault)
|
482
|
+
|
483
|
+
// return console.log('campusDefault :>> ', campusDefault);
|
484
|
+
|
485
|
+
let modifiedById = userMail._id
|
486
|
+
let createdById = userMail._id
|
487
|
+
|
488
|
+
let [record, created] = await Rooms.findOrCreate({
|
489
|
+
where:{
|
490
|
+
name: _room?.name,
|
491
|
+
campusId: _campus?._id??_campusDefault?._id??null,
|
492
|
+
},
|
493
|
+
defaults:{
|
494
|
+
capacity,
|
495
|
+
modifiedById,
|
496
|
+
createdById
|
497
|
+
|
498
|
+
}
|
499
|
+
})
|
500
|
+
if (created){
|
501
|
+
logger.info(`New room record created ${createdCounter + 1}/${createdCounter + 1}: ${record.name} `)
|
502
|
+
createdCounter++;
|
503
|
+
}
|
504
|
+
else {
|
505
|
+
const errorMsg = `Record room already exists: ${record.name}`;
|
506
|
+
|
507
|
+
if (!uniqueErrors.has(errorMsg)) {
|
508
|
+
logger.error(`${errorCounter + 1}/${errorCounter + 1} ${errorMsg}`);
|
509
|
+
uniqueErrors.add(errorMsg);
|
510
|
+
errorCounter++;
|
511
|
+
}
|
512
|
+
|
513
|
+
}
|
514
|
+
|
515
|
+
} catch (error) {
|
516
|
+
logger.error(`Error occurred: ${error}`);
|
517
|
+
logger.error(`Error occurred: ${error.stack}`);
|
518
|
+
}
|
519
|
+
}
|
520
|
+
|
521
|
+
let items = createdCounter + errorCounter
|
522
|
+
|
523
|
+
console.log("");
|
524
|
+
console.log("########################################################")
|
525
|
+
console.log("")
|
526
|
+
logger.info(' Room Data Migration Completed');
|
527
|
+
logger.info(`Total successful Room records created: ${createdCounter}/${createdCounter}`);
|
528
|
+
logger.info(`Total Room Error Records: ${errorCounter}/${errorCounter}`);
|
529
|
+
logger.info(`Total Record Fetched: ${items} `)
|
530
|
+
console.log("")
|
531
|
+
console.log("########################################################")
|
532
|
+
console.log("")
|
533
|
+
|
534
|
+
let totalSummary = {
|
535
|
+
itemLength: items,
|
536
|
+
error: errorCounter,
|
537
|
+
success: createdCounter
|
538
|
+
}
|
539
|
+
return totalSummary
|
540
|
+
|
541
|
+
} catch (error) {
|
542
|
+
logger.error(`Error occurred: ${error}`);
|
543
|
+
logger.error(`Error occurred: ${error.stack}`);
|
544
|
+
|
545
|
+
}
|
546
|
+
},
|
547
|
+
|
548
|
+
async Instructor(){
|
549
|
+
try {
|
550
|
+
console.log("");
|
551
|
+
console.log("########################################################")
|
552
|
+
console.log(" Instructors Migration ")
|
553
|
+
console.log("########################################################")
|
554
|
+
console.log("");
|
555
|
+
|
556
|
+
const instructorApi = await api.Instructor()
|
557
|
+
const departmentApi = await api.Department()
|
558
|
+
|
559
|
+
const departmentSeq = await AcademicDepartments.findAll()
|
560
|
+
const campusSeq = await Campuses.findAll()
|
561
|
+
const userMail = await Users.findOne({ where: { email: 'ccci_integrator@gmail.com' } });
|
562
|
+
|
563
|
+
let createdCounter = 0
|
564
|
+
let errorCounter = 0
|
565
|
+
let uniqueErrors = new Set()
|
566
|
+
|
567
|
+
|
568
|
+
const db_schema = schema
|
569
|
+
// let maxIds = await Campuses.max('_id'); = await Instructors.max('_id'); // Fetch maximum _id directly from the Instructors model
|
570
|
+
// let maxId = maxIds|| 0;
|
571
|
+
// await db.sequelize.query(`ALTER SEQUENCE ${db_schema}."Instructors__id_seq" RESTART WITH ${maxId + 1}`);
|
572
|
+
|
573
|
+
|
574
|
+
for (let i = 0; i < instructorApi.length; i++){
|
575
|
+
try {
|
576
|
+
let _instructor = instructorApi[i]
|
577
|
+
|
578
|
+
let acadDepartmentMap = departmentApi.find(a => a.name === _instructor?.department_name)
|
579
|
+
|
580
|
+
let _acadDepartment = departmentSeq.find(b => b.name === acadDepartmentMap?.name)
|
581
|
+
|
582
|
+
let campusMap = campusSeq.find(c => c._id === _acadDepartment?.campusId)
|
583
|
+
|
584
|
+
let academicDepartmentDefault = 999999
|
585
|
+
|
586
|
+
let modifiedById = userMail._id
|
587
|
+
let createdById = userMail._id
|
588
|
+
|
589
|
+
|
590
|
+
let campusDefault = "MSC - Boac Campus"
|
591
|
+
let _campusDefault = campusSeq.find(ccc => ccc.name === campusDefault)
|
592
|
+
|
593
|
+
let [record, created] = await Instructors.findOrCreate({
|
594
|
+
where:{
|
595
|
+
firstName: _instructor?.first_name,
|
596
|
+
lastName: _instructor?.last_name,
|
597
|
+
middleName: _instructor?.middle_name,
|
598
|
+
employeeNumber: _instructor?.faculty_id,
|
599
|
+
departmentId: _acadDepartment?._id??academicDepartmentDefault,
|
600
|
+
campusId: campusMap?._id??_campusDefault?._id,
|
601
|
+
modifiedById,
|
602
|
+
createdById,
|
603
|
+
// employeeId: record?._id??null,
|
604
|
+
|
605
|
+
|
606
|
+
},
|
607
|
+
defaults:{
|
608
|
+
|
609
|
+
}
|
610
|
+
})
|
611
|
+
if (created){
|
612
|
+
logger.info(`New instructor record created ${createdCounter + 1}/${createdCounter + 1}: [${record.employeeNumber}] - ${record.lastName}, ${record.firstName} `)
|
613
|
+
createdCounter++;
|
614
|
+
}
|
615
|
+
else {
|
616
|
+
const errorMsg = `Record instructor already exists: [${record.employeeNumber}] - ${record.lastName}, ${record.firstName}`;
|
617
|
+
|
618
|
+
if (!uniqueErrors.has(errorMsg)) {
|
619
|
+
logger.error(`${errorCounter + 1}/${errorCounter + 1} ${errorMsg}`);
|
620
|
+
uniqueErrors.add(errorMsg);
|
621
|
+
errorCounter++;
|
622
|
+
}
|
623
|
+
|
624
|
+
}
|
625
|
+
|
626
|
+
} catch (error) {
|
627
|
+
logger.error(`Error occurred: ${error}`);
|
628
|
+
logger.error(`Error occurred: ${error.stack}`);
|
629
|
+
}
|
630
|
+
}
|
631
|
+
|
632
|
+
let items = createdCounter + errorCounter
|
633
|
+
|
634
|
+
console.log("");
|
635
|
+
console.log("########################################################")
|
636
|
+
console.log("")
|
637
|
+
logger.info(' Instructors Data Migration Completed');
|
638
|
+
logger.info(`Total successful Instructors records created: ${createdCounter}/${createdCounter}`);
|
639
|
+
logger.info(`Total Instructors Error Records: ${errorCounter}/${errorCounter}`);
|
640
|
+
logger.info(`Total Record Fetched: ${items} `)
|
641
|
+
console.log("")
|
642
|
+
console.log("########################################################")
|
643
|
+
console.log("")
|
644
|
+
|
645
|
+
let totalSummary = {
|
646
|
+
itemLength: items,
|
647
|
+
error: errorCounter,
|
648
|
+
success: createdCounter
|
649
|
+
}
|
650
|
+
return totalSummary
|
651
|
+
|
652
|
+
} catch (error) {
|
653
|
+
logger.error(`Error occurred: ${error}`);
|
654
|
+
logger.error(`Error occurred: ${error.stack}`);
|
655
|
+
|
656
|
+
}
|
657
|
+
},
|
658
|
+
|
659
|
+
async AcademicYear(){
|
660
|
+
try {
|
661
|
+
console.log("");
|
662
|
+
console.log("########################################################")
|
663
|
+
console.log(" Academic Years Migration ")
|
664
|
+
console.log("########################################################")
|
665
|
+
console.log("");
|
666
|
+
|
667
|
+
const acadYearApi = await api.AcademicYear()
|
668
|
+
const userMail = await Users.findOne({ where: { email: 'ccci_integrator@gmail.com' } });
|
669
|
+
|
670
|
+
let createdCounter = 0
|
671
|
+
let errorCounter = 0
|
672
|
+
let uniqueErrors = new Set()
|
673
|
+
|
674
|
+
const { acadYearResult, currentSchoolYear } = acadYearApi;
|
675
|
+
|
676
|
+
|
677
|
+
const db_schema = schema
|
678
|
+
// let maxIds = await Campuses.max('_id'); = await AcademicYears.max('_id'); // Fetch maximum _id directly from the AcademicYears model
|
679
|
+
// let maxId = maxIds|| 0;
|
680
|
+
// await db.sequelize.query(`ALTER SEQUENCE ${db_schema}."AcademicYears__id_seq" RESTART WITH ${maxId + 1}`);
|
681
|
+
|
682
|
+
|
683
|
+
for (let i = 0; i < acadYearResult.length; i++){
|
684
|
+
try {
|
685
|
+
let _acadyear = acadYearResult[i]
|
686
|
+
// console.log('_acadyear :>> ', _acadyear);
|
687
|
+
|
688
|
+
let [from, to] = _acadyear.year.split('-')
|
689
|
+
|
690
|
+
to = /^\d+$/.test(to) ? parseInt(to, 10) : null;
|
691
|
+
from = /^\d+$/.test(from) ? parseInt(from, 10) : null;
|
692
|
+
|
693
|
+
let defaultDate = new Date()
|
694
|
+
let currentYear = defaultDate.getFullYear()
|
695
|
+
let previousYear = currentYear - 1
|
696
|
+
|
697
|
+
let isDefault = false
|
698
|
+
if(from == previousYear && to == currentYear) isDefault = true
|
699
|
+
|
700
|
+
let modifiedById = userMail._id
|
701
|
+
let createdById = userMail._id
|
702
|
+
|
703
|
+
let [record, created] = await AcademicYears.findOrCreate({
|
704
|
+
where:{
|
705
|
+
from,
|
706
|
+
to
|
707
|
+
},
|
708
|
+
defaults:{
|
709
|
+
modifiedById,
|
710
|
+
createdById,
|
711
|
+
default: isDefault
|
712
|
+
}
|
713
|
+
})
|
714
|
+
if (created){
|
715
|
+
logger.info(`New academic year record created ${createdCounter + 1}/${createdCounter + 1}: ${record.from} - ${record.to} `)
|
716
|
+
createdCounter++;
|
717
|
+
}
|
718
|
+
else {
|
719
|
+
const errorMsg = `Record academic year already exists: ${record.from} - ${record.to}`;
|
720
|
+
if (!uniqueErrors.has(errorMsg)) {
|
721
|
+
logger.error(`${errorCounter + 1}/${errorCounter + 1} ${errorMsg}`);
|
722
|
+
uniqueErrors.add(errorMsg);
|
723
|
+
errorCounter++;
|
724
|
+
}
|
725
|
+
}
|
726
|
+
|
727
|
+
} catch (error) {
|
728
|
+
logger.error(`Error occurred: ${error}`);
|
729
|
+
logger.error(`Error occurred: ${error.stack}`);
|
730
|
+
}
|
731
|
+
}
|
732
|
+
|
733
|
+
let items = createdCounter + errorCounter
|
734
|
+
|
735
|
+
console.log("");
|
736
|
+
console.log("########################################################")
|
737
|
+
console.log("")
|
738
|
+
logger.info(' Academic Years Data Migration Completed');
|
739
|
+
logger.info(`Total successful Academic Years records created: ${createdCounter}/${createdCounter}`);
|
740
|
+
logger.info(`Total Academic Years Error Records: ${errorCounter}/${errorCounter}`);
|
741
|
+
logger.info(`Total Record Fetched: ${items} `)
|
742
|
+
console.log("")
|
743
|
+
console.log("########################################################")
|
744
|
+
console.log("")
|
745
|
+
|
746
|
+
let totalSummary = {
|
747
|
+
itemLength: items,
|
748
|
+
error: errorCounter,
|
749
|
+
success: createdCounter
|
750
|
+
}
|
751
|
+
return totalSummary
|
752
|
+
|
753
|
+
} catch (error) {
|
754
|
+
logger.error(`Error occurred: ${error}`);
|
755
|
+
logger.error(`Error occurred: ${error.stack}`);
|
756
|
+
|
757
|
+
}
|
758
|
+
},
|
759
|
+
|
760
|
+
async Subject(){
|
761
|
+
try {
|
762
|
+
console.log("");
|
763
|
+
console.log("########################################################")
|
764
|
+
console.log(" Subjects Migration ")
|
765
|
+
console.log("########################################################")
|
766
|
+
console.log("");
|
767
|
+
|
768
|
+
const studentSubject = (await api.Student()).data
|
769
|
+
|
770
|
+
const subjectApi = await api.Subject()
|
771
|
+
|
772
|
+
const departmentSeq = await AcademicDepartments.findAll()
|
773
|
+
const userMail = await Users.findOne({ where: { email: 'ccci_integrator@gmail.com' } });
|
774
|
+
|
775
|
+
let createdCounter = 0
|
776
|
+
let errorCounter = 0
|
777
|
+
let uniqueErrors = new Set()
|
778
|
+
|
779
|
+
|
780
|
+
const db_schema = schema
|
781
|
+
// let maxIds = await Campuses.max('_id'); = await Subjects.max('_id'); // Fetch maximum _id directly from the Subjects model
|
782
|
+
// let maxId = maxIds|| 0;
|
783
|
+
// await db.sequelize.query(`ALTER SEQUENCE ${db_schema}."Subjects__id_seq" RESTART WITH ${maxId + 1}`);
|
784
|
+
|
785
|
+
|
786
|
+
for (let i = 0; i < studentSubject.length; i++){
|
787
|
+
try {
|
788
|
+
let _subject = studentSubject[i]
|
789
|
+
|
790
|
+
let _department = departmentSeq.find(dep => dep.name === _subject?.college)
|
791
|
+
|
792
|
+
let _subs = studentSubject[i].subjects_enrolled;
|
793
|
+
|
794
|
+
|
795
|
+
for (let j = 0; j < _subs.length; j++){
|
796
|
+
|
797
|
+
let subject = _subs[j].subject_code
|
798
|
+
|
799
|
+
let _subjects = subjectApi.find(sub => sub.code === subject)
|
800
|
+
|
801
|
+
// return console.log('_subjects :>> ', _subjects);
|
802
|
+
|
803
|
+
|
804
|
+
if (!subject) {
|
805
|
+
// console.error(`Subject with code ${subjects} not found.`);
|
806
|
+
continue;
|
807
|
+
}
|
808
|
+
if (!_subjects) {
|
809
|
+
// console.error(`Subject with code ${subjects} not found.`);
|
810
|
+
continue;
|
811
|
+
}
|
812
|
+
let subjectType = "LECTURE"
|
813
|
+
let modifiedById = userMail._id
|
814
|
+
let createdById = userMail._id
|
815
|
+
|
816
|
+
|
817
|
+
|
818
|
+
let [record, created] = await Subjects.findOrCreate({
|
819
|
+
where:{
|
820
|
+
code:_subjects?.code,
|
821
|
+
name:_subjects?.name,
|
822
|
+
units:_subjects?.units,
|
823
|
+
//add null here
|
824
|
+
departmentId: _department?._id??null,
|
825
|
+
subjectType,
|
826
|
+
modifiedById,
|
827
|
+
createdById
|
828
|
+
},
|
829
|
+
defaults:{
|
830
|
+
|
831
|
+
}
|
832
|
+
})
|
833
|
+
if (created){
|
834
|
+
logger.info(`New subject record created ${createdCounter + 1}/${createdCounter + 1}: [${record.code}], ${record.name} `)
|
835
|
+
createdCounter++;
|
836
|
+
}
|
837
|
+
else {
|
838
|
+
const errorMsg = `Record subject already exists: [${record.code}], ${record.name}`;
|
839
|
+
|
840
|
+
if (!uniqueErrors.has(errorMsg)) {
|
841
|
+
logger.error(`${errorCounter + 1}/${errorCounter + 1} ${errorMsg}`);
|
842
|
+
uniqueErrors.add(errorMsg);
|
843
|
+
errorCounter++;
|
844
|
+
}
|
845
|
+
}
|
846
|
+
}
|
847
|
+
|
848
|
+
|
849
|
+
} catch (error) {
|
850
|
+
logger.error(`Error occurred: ${error}`);
|
851
|
+
logger.error(`Error occurred: ${error.stack}`);
|
852
|
+
}
|
853
|
+
}
|
854
|
+
|
855
|
+
let items = createdCounter + errorCounter
|
856
|
+
|
857
|
+
console.log("");
|
858
|
+
console.log("########################################################")
|
859
|
+
console.log("")
|
860
|
+
logger.info(' Subjects Data Migration Completed');
|
861
|
+
logger.info(`Total successful Subjects records created: ${createdCounter}/${createdCounter}`);
|
862
|
+
logger.info(`Total Subjects Error Records: ${errorCounter}/${errorCounter}`);
|
863
|
+
logger.info(`Total Record Fetched: ${items} `)
|
864
|
+
console.log("")
|
865
|
+
console.log("########################################################")
|
866
|
+
console.log("")
|
867
|
+
|
868
|
+
let totalSummary = {
|
869
|
+
itemLength: items,
|
870
|
+
error: errorCounter,
|
871
|
+
success: createdCounter
|
872
|
+
}
|
873
|
+
return totalSummary
|
874
|
+
|
875
|
+
} catch (error) {
|
876
|
+
logger.error(`Error occurred: ${error}`);
|
877
|
+
logger.error(`Error occurred: ${error.stack}`);
|
878
|
+
|
879
|
+
}
|
880
|
+
},
|
881
|
+
|
882
|
+
async Schedule(){
|
883
|
+
try {
|
884
|
+
console.log("");
|
885
|
+
console.log("########################################################")
|
886
|
+
console.log(" Schedules Migration ")
|
887
|
+
console.log("########################################################")
|
888
|
+
console.log("");
|
889
|
+
|
890
|
+
const scheduleApi = await api.Schedule()
|
891
|
+
const userMail = await Users.findOne({ where: { email: 'ccci_integrator@gmail.com' } });
|
892
|
+
|
893
|
+
let createdCounter = 0
|
894
|
+
let errorCounter = 0
|
895
|
+
let uniqueErrors = new Set()
|
896
|
+
|
897
|
+
|
898
|
+
const db_schema = schema
|
899
|
+
// let maxIds = await Campuses.max('_id'); = await Schedules.max('_id'); // Fetch maximum _id directly from the Schedules model
|
900
|
+
// let maxId = maxIds|| 0;
|
901
|
+
// await db.sequelize.query(`ALTER SEQUENCE ${db_schema}."Schedules__id_seq" RESTART WITH ${maxId + 1}`);
|
902
|
+
|
903
|
+
for (let i = 0; i < scheduleApi.length; i++){
|
904
|
+
try {
|
905
|
+
let _schedule = scheduleApi[i]
|
906
|
+
|
907
|
+
const timeRegex = /(\d{1,2}:\d{2} [APMapm]{2}) - (\d{1,2}:\d{2} [APMapm]{2})/
|
908
|
+
const matchTime = _schedule.schedule.match(timeRegex)
|
909
|
+
|
910
|
+
if (!matchTime) {
|
911
|
+
// console.error(`Invalid schedule format, check the record:${_schedule.schedule}`);
|
912
|
+
continue;
|
913
|
+
}
|
914
|
+
|
915
|
+
const [_, startTime, endTime] = matchTime;
|
916
|
+
const stFormat = moment(startTime, 'h:mm A').format('HH:mm:ss')
|
917
|
+
const etFormat = moment(endTime, 'h:mm A').format('HH:mm:ss')
|
918
|
+
|
919
|
+
|
920
|
+
let dayHandler = _schedule.schedule.split(' ')
|
921
|
+
|
922
|
+
const dayMappings = {
|
923
|
+
'M': 'Monday',
|
924
|
+
'T': 'Tuesday',
|
925
|
+
'W': 'Wednesday',
|
926
|
+
'Th': 'Thursday',
|
927
|
+
'F': 'Friday',
|
928
|
+
'S': 'Saturday',
|
929
|
+
'Su': 'Sunday'
|
930
|
+
};
|
931
|
+
|
932
|
+
let modifiedById = userMail._id
|
933
|
+
let createdById = userMail._id
|
934
|
+
|
935
|
+
|
936
|
+
let from = null
|
937
|
+
let to = null
|
938
|
+
let days = null
|
939
|
+
|
940
|
+
if (dayHandler.length > 1) {
|
941
|
+
days = dayHandler[0].split('').map(day => dayMappings[day]).join(', ');
|
942
|
+
} else {
|
943
|
+
days = dayMappings[dayHandler[0]];
|
944
|
+
}
|
945
|
+
|
946
|
+
|
947
|
+
let [record, created] = await Schedules.findOrCreate({
|
948
|
+
where:{
|
949
|
+
name: _schedule?.schedule,
|
950
|
+
from : stFormat??from,
|
951
|
+
to : etFormat??to,
|
952
|
+
days
|
953
|
+
},
|
954
|
+
defaults:{
|
955
|
+
modifiedById,
|
956
|
+
createdById
|
957
|
+
}
|
958
|
+
})
|
959
|
+
if (created){
|
960
|
+
logger.info(`New schedule record created ${createdCounter + 1}/${createdCounter + 1}: [${record.from} - ${record.to}] ${record.name} `)
|
961
|
+
createdCounter++;
|
962
|
+
}
|
963
|
+
else {
|
964
|
+
const errorMsg = `Record schedule already exists: [${record.from} - ${record.to}] ${record.name}`;
|
965
|
+
|
966
|
+
if (!uniqueErrors.has(errorMsg)) {
|
967
|
+
logger.error(`${errorCounter + 1}/${errorCounter + 1} ${errorMsg}`);
|
968
|
+
uniqueErrors.add(errorMsg);
|
969
|
+
errorCounter++;
|
970
|
+
}
|
971
|
+
}
|
972
|
+
|
973
|
+
} catch (error) {
|
974
|
+
logger.error(`Error occurred: ${error}`);
|
975
|
+
logger.error(`Error occurred: ${error.stack}`);
|
976
|
+
}
|
977
|
+
}
|
978
|
+
|
979
|
+
let items = createdCounter + errorCounter
|
980
|
+
|
981
|
+
console.log("");
|
982
|
+
console.log("########################################################")
|
983
|
+
console.log("")
|
984
|
+
logger.info(' Schedule Data Migration Completed');
|
985
|
+
logger.info(`Total successful Schedule records created: ${createdCounter}/${createdCounter}`);
|
986
|
+
logger.info(`Total Schedule Error Records: ${errorCounter}/${errorCounter}`);
|
987
|
+
logger.info(`Total Record Fetched: ${items} `)
|
988
|
+
console.log("")
|
989
|
+
console.log("########################################################")
|
990
|
+
console.log("")
|
991
|
+
|
992
|
+
let totalSummary = {
|
993
|
+
itemLength: items,
|
994
|
+
error: errorCounter,
|
995
|
+
success: createdCounter
|
996
|
+
}
|
997
|
+
return totalSummary
|
998
|
+
|
999
|
+
} catch (error) {
|
1000
|
+
logger.error(`Error occurred: ${error}`);
|
1001
|
+
logger.error(`Error occurred: ${error.stack}`);
|
1002
|
+
}
|
1003
|
+
},
|
1004
|
+
|
1005
|
+
async Student(){
|
1006
|
+
try {
|
1007
|
+
console.log("");
|
1008
|
+
console.log("########################################################")
|
1009
|
+
console.log(" Students Migration ")
|
1010
|
+
console.log("########################################################")
|
1011
|
+
console.log("");
|
1012
|
+
|
1013
|
+
const studentApi = await api.Student()
|
1014
|
+
|
1015
|
+
const campusSeq = await Campuses.findAll()
|
1016
|
+
const courseSeq = await Courses.findAll()
|
1017
|
+
const collegeSeq = await Colleges.findAll()
|
1018
|
+
const semesterSeq = await Semesters.findAll()
|
1019
|
+
const acadYearSeq = await AcademicYears.findAll()
|
1020
|
+
|
1021
|
+
const userMail = await Users.findOne({ where: { email: 'ccci_integrator@gmail.com' } });
|
1022
|
+
|
1023
|
+
let createdCounter = 0
|
1024
|
+
let errorCounter = 0
|
1025
|
+
let uniqueErrors = new Set()
|
1026
|
+
|
1027
|
+
const emailDomain = 'default.msc.edu.ph';
|
1028
|
+
|
1029
|
+
|
1030
|
+
const db_schema = schema
|
1031
|
+
// let maxIds = await Campuses.max('_id'); = await Students.max('_id'); // Fetch maximum _id directly from the Students model
|
1032
|
+
// let maxId = maxIds|| 0;
|
1033
|
+
// await db.sequelize.query(`ALTER SEQUENCE ${db_schema}."Students__id_seq" RESTART WITH ${maxId + 1}`);
|
1034
|
+
|
1035
|
+
const {data} = studentApi
|
1036
|
+
for (let i = 0; i < data.length; i++){
|
1037
|
+
try {
|
1038
|
+
|
1039
|
+
let _student = data[i]
|
1040
|
+
|
1041
|
+
|
1042
|
+
let _campus = campusSeq.find(a => a.name === _student?.campus)
|
1043
|
+
let _course = courseSeq.find(b => b.name === _student?.course)
|
1044
|
+
let _college = collegeSeq.find(c => c.name === _student?.college)
|
1045
|
+
|
1046
|
+
const getLastName = _student?.last_name.toLowerCase().replace(/\s/g, '') ?? "";
|
1047
|
+
|
1048
|
+
let email = `${_student?.student_number}_${getLastName}@${emailDomain}`;
|
1049
|
+
|
1050
|
+
let modifiedById = userMail._id
|
1051
|
+
let createdById = userMail._id
|
1052
|
+
|
1053
|
+
let campusDefault = 999999
|
1054
|
+
let courseDefault = 999999
|
1055
|
+
let collegeDefault = 999999
|
1056
|
+
|
1057
|
+
|
1058
|
+
let fromPrompts = await Picked()
|
1059
|
+
|
1060
|
+
|
1061
|
+
// let semester = "2nd Semester"
|
1062
|
+
let semester = fromPrompts.semester
|
1063
|
+
let yearsFrom = fromPrompts.yearFrom
|
1064
|
+
let yearsTo = fromPrompts.yearTo
|
1065
|
+
|
1066
|
+
let _semester = semesterSeq.find(sem => sem.code === semester)
|
1067
|
+
let _acadYear = acadYearSeq.find(yr => yr.from === parseInt(yearsFrom) && yr.to === parseInt(yearsTo))
|
1068
|
+
|
1069
|
+
// return
|
1070
|
+
let [record, created] = await Students.findOrCreate({
|
1071
|
+
where:{
|
1072
|
+
studentNumber: _student?.student_number,
|
1073
|
+
firstName: _student?.first_name,
|
1074
|
+
lastName: _student?.last_name,
|
1075
|
+
middleName: _student?.middle_name,
|
1076
|
+
},
|
1077
|
+
defaults:{
|
1078
|
+
email: email,
|
1079
|
+
courseId: _course?._id??courseDefault,
|
1080
|
+
campusId: _campus?._id??campusDefault,
|
1081
|
+
collegeId: _college?._id??collegeDefault,
|
1082
|
+
admittedYearId: _acadYear?._id ?? null,
|
1083
|
+
admittedSemId: _semester?._id ?? null,
|
1084
|
+
modifiedById,
|
1085
|
+
createdById
|
1086
|
+
}
|
1087
|
+
})
|
1088
|
+
if (created){
|
1089
|
+
logger.info(`New student record created ${createdCounter + 1}/${createdCounter + 1}: [${record.studentNumber}] ${record.lastName}, ${record.firstName} ${record.middleName} `)
|
1090
|
+
createdCounter++;
|
1091
|
+
}
|
1092
|
+
else {
|
1093
|
+
|
1094
|
+
const errorMsg = `Record student already exists: [${record.studentNumber}] ${record.lastName}, ${record.firstName} ${record.middleName}`;
|
1095
|
+
|
1096
|
+
if (!uniqueErrors.has(errorMsg)) {
|
1097
|
+
logger.error(`${errorCounter + 1}/${errorCounter + 1} ${errorMsg}`);
|
1098
|
+
uniqueErrors.add(errorMsg);
|
1099
|
+
errorCounter++;
|
1100
|
+
}
|
1101
|
+
|
1102
|
+
}
|
1103
|
+
|
1104
|
+
} catch (error) {
|
1105
|
+
logger.error(`Error occurred: ${error}`);
|
1106
|
+
logger.error(`Error occurred: ${error.stack}`);
|
1107
|
+
}
|
1108
|
+
}
|
1109
|
+
|
1110
|
+
let items = createdCounter + errorCounter
|
1111
|
+
|
1112
|
+
console.log("");
|
1113
|
+
console.log("########################################################")
|
1114
|
+
console.log("")
|
1115
|
+
logger.info(' Student Data Migration Completed');
|
1116
|
+
logger.info(`Total successful Student records created: ${createdCounter}/${createdCounter}`);
|
1117
|
+
logger.info(`Total Student Error Records: ${errorCounter}/${errorCounter}`);
|
1118
|
+
logger.info(`Total Record Fetched: ${items} `)
|
1119
|
+
console.log("")
|
1120
|
+
console.log("########################################################")
|
1121
|
+
console.log("")
|
1122
|
+
|
1123
|
+
let totalSummary = {
|
1124
|
+
itemLength: items,
|
1125
|
+
error: errorCounter,
|
1126
|
+
success: createdCounter
|
1127
|
+
}
|
1128
|
+
return totalSummary
|
1129
|
+
|
1130
|
+
} catch (error) {
|
1131
|
+
logger.error(`Error occurred: ${error}`);
|
1132
|
+
logger.error(`Error occurred: ${error.stack}`);
|
1133
|
+
|
1134
|
+
}
|
1135
|
+
},
|
1136
|
+
|
1137
|
+
async Section() {
|
1138
|
+
try {
|
1139
|
+
console.log("");
|
1140
|
+
console.log("########################################################");
|
1141
|
+
console.log(" Sections Migration ");
|
1142
|
+
console.log("########################################################");
|
1143
|
+
console.log("");
|
1144
|
+
|
1145
|
+
const student = (await api.Student()).data;
|
1146
|
+
|
1147
|
+
const subjectApi = await api.Subject();
|
1148
|
+
const instructorApi = await api.Instructor()
|
1149
|
+
|
1150
|
+
const subjectSeq = await Subjects.findAll();
|
1151
|
+
const instructorSeq = await Instructors.findAll()
|
1152
|
+
const courseSeq = await Courses.findAll()
|
1153
|
+
const campusSeq = await Campuses.findAll()
|
1154
|
+
const semesterSeq = await Semesters.findAll()
|
1155
|
+
const acadYearSeq = await AcademicYears.findAll()
|
1156
|
+
const acadDeptSeq = await AcademicDepartments.findAll()
|
1157
|
+
|
1158
|
+
|
1159
|
+
const userMail = await Users.findOne({ where: { email: 'ccci_integrator@gmail.com' } });
|
1160
|
+
|
1161
|
+
let createdCounter = 0
|
1162
|
+
let errorCounter = 0
|
1163
|
+
let uniqueErrors = new Set()
|
1164
|
+
|
1165
|
+
|
1166
|
+
const db_schema = schema
|
1167
|
+
// let maxIds = await Campuses.max('_id'); = await Sections.max('_id'); // Fetch maximum _id directly from the Sections model
|
1168
|
+
// let maxId = maxIds|| 0;
|
1169
|
+
// await db.sequelize.query(`ALTER SEQUENCE ${db_schema}."Sections__id_seq" RESTART WITH ${maxId + 1}`);
|
1170
|
+
|
1171
|
+
|
1172
|
+
for (let i = 0; i < student.length; i++) {
|
1173
|
+
try {
|
1174
|
+
|
1175
|
+
let _sec = student[i]
|
1176
|
+
|
1177
|
+
let _course = courseSeq.find(crs => crs.name === _sec?.course)
|
1178
|
+
let _campus = campusSeq.find(cam => cam.name === _sec?.campus)
|
1179
|
+
let _section = student[i].subjects_enrolled;
|
1180
|
+
|
1181
|
+
|
1182
|
+
for (let j = 0; j < _section.length; j++) {
|
1183
|
+
let sections = _section[j].section_name
|
1184
|
+
let subjects = _section[j].subject_code;
|
1185
|
+
let instructors = _section[j].faculty_id
|
1186
|
+
|
1187
|
+
let _subject = subjectApi.find(sub => sub.code === subjects);
|
1188
|
+
|
1189
|
+
let _instructor = instructorApi.find( ins => ins.faculty_id === instructors)
|
1190
|
+
|
1191
|
+
let _subjects = subjectSeq.find( subs => subs.code === _subject?.code)
|
1192
|
+
|
1193
|
+
let _instructors = instructorSeq.find(inst => inst.employeeNumber === _instructor?.faculty_id)
|
1194
|
+
|
1195
|
+
let _insdepartment = acadDeptSeq.find(idep => idep.name === _instructor?.department_name)
|
1196
|
+
|
1197
|
+
|
1198
|
+
if (!_subject) {
|
1199
|
+
// console.error(`Subject with code ${subjects} not found.`);
|
1200
|
+
continue;
|
1201
|
+
}
|
1202
|
+
|
1203
|
+
if (!_instructor) {
|
1204
|
+
// console.error(`Instructor with faculty_id ${instructors} not found.`);
|
1205
|
+
continue;
|
1206
|
+
}
|
1207
|
+
|
1208
|
+
let code = sections ?? 'CODE'
|
1209
|
+
code = code.split(/\s+/).map((word) => word.charAt(0).toUpperCase()).join('')
|
1210
|
+
|
1211
|
+
let fromPrompts = await Picked()
|
1212
|
+
let semester = fromPrompts.semester
|
1213
|
+
let yearsFrom = fromPrompts.yearFrom
|
1214
|
+
let yearsTo = fromPrompts.yearTo
|
1215
|
+
let _semester = semesterSeq.find(sem => sem.code === semester)
|
1216
|
+
let _acadYear = acadYearSeq.find(yr => yr.from === parseInt(yearsFrom) && yr.to === parseInt(yearsTo))
|
1217
|
+
|
1218
|
+
let modifiedById = userMail._id;
|
1219
|
+
let createdById = userMail._id;
|
1220
|
+
|
1221
|
+
let defaultSub = 99999
|
1222
|
+
let defaultInst = 99999
|
1223
|
+
|
1224
|
+
let classType = 'SYNCHRONOUS'
|
1225
|
+
|
1226
|
+
let [record, created] = await Sections.findOrCreate({
|
1227
|
+
where: {
|
1228
|
+
name: sections,
|
1229
|
+
code,
|
1230
|
+
subjectId: _subjects?._id??defaultSub??null,
|
1231
|
+
instructorId: _instructors?._id??defaultInst??null,
|
1232
|
+
classType,
|
1233
|
+
semesterId: _semester?._id??null,
|
1234
|
+
academicYearId: _acadYear?._id??null,
|
1235
|
+
courseId: _course?._id??null,
|
1236
|
+
campusId: _campus?._id??null,
|
1237
|
+
departmentId: _insdepartment?._id??null,
|
1238
|
+
modifiedById,
|
1239
|
+
createdById,
|
1240
|
+
|
1241
|
+
},
|
1242
|
+
defaults: {
|
1243
|
+
// classType,
|
1244
|
+
// semesterId: _semester?._id??null,
|
1245
|
+
// academicYearId: _acadYear?._id??null,
|
1246
|
+
// courseId: _course?._id??null,
|
1247
|
+
// campusId: _campus?._id??null,
|
1248
|
+
// modifiedById,
|
1249
|
+
// createdById,
|
1250
|
+
|
1251
|
+
}
|
1252
|
+
});
|
1253
|
+
|
1254
|
+
if (created) {
|
1255
|
+
logger.info(`New section record created ${createdCounter + 1}/${createdCounter + 1}: ${record.name} `);
|
1256
|
+
createdCounter++;
|
1257
|
+
} else {
|
1258
|
+
|
1259
|
+
const errorMsg = `Record section already exists: ${record.name} `;
|
1260
|
+
|
1261
|
+
if (!uniqueErrors.has(errorMsg)) {
|
1262
|
+
logger.error(`${errorCounter + 1}/${errorCounter + 1} ${errorMsg}`);
|
1263
|
+
uniqueErrors.add(errorMsg);
|
1264
|
+
errorCounter++;
|
1265
|
+
}
|
1266
|
+
}
|
1267
|
+
}
|
1268
|
+
} catch (error) {
|
1269
|
+
logger.error(`Error occurred: ${error}`);
|
1270
|
+
logger.error(`Error occurred: ${error.stack}`);
|
1271
|
+
}
|
1272
|
+
}
|
1273
|
+
|
1274
|
+
let items = createdCounter + errorCounter
|
1275
|
+
|
1276
|
+
console.log("");
|
1277
|
+
console.log("########################################################");
|
1278
|
+
console.log("");
|
1279
|
+
logger.info(' Section Data Migration Completed');
|
1280
|
+
logger.info(`Total successful Section records created: ${createdCounter}/${createdCounter}`);
|
1281
|
+
logger.info(`Total Section Error Records: ${errorCounter}/${errorCounter}`);
|
1282
|
+
logger.info(`Total Record Fetched: ${items} `)
|
1283
|
+
console.log("");
|
1284
|
+
console.log("########################################################");
|
1285
|
+
console.log("");
|
1286
|
+
|
1287
|
+
let totalSummary = {
|
1288
|
+
itemLength: items,
|
1289
|
+
error: errorCounter,
|
1290
|
+
success: createdCounter
|
1291
|
+
};
|
1292
|
+
return totalSummary;
|
1293
|
+
|
1294
|
+
} catch (error) {
|
1295
|
+
logger.error(`Error occurred: ${error}`);
|
1296
|
+
logger.error(`Error occurred: ${error.stack}`);
|
1297
|
+
}
|
1298
|
+
},
|
1299
|
+
|
1300
|
+
|
1301
|
+
async EnrolledStudent(){
|
1302
|
+
try {
|
1303
|
+
console.log("");
|
1304
|
+
console.log("########################################################")
|
1305
|
+
console.log(" Enrolled Students Migration ")
|
1306
|
+
console.log("########################################################")
|
1307
|
+
console.log("");
|
1308
|
+
|
1309
|
+
const studentApi = (await api.Student()).data
|
1310
|
+
const instructorApi = await api.Instructor()
|
1311
|
+
|
1312
|
+
const studentSeq = await Students.findAll()
|
1313
|
+
const courseSeq = await Courses.findAll()
|
1314
|
+
const campusSeq = await Campuses.findAll()
|
1315
|
+
const acadYearSeq = await AcademicYears.findAll()
|
1316
|
+
const semesterSeq = await Semesters.findAll()
|
1317
|
+
const sectionSeq = await Sections.findAll()
|
1318
|
+
const instructorSeq = await Instructors.findAll()
|
1319
|
+
|
1320
|
+
const userMail = await Users.findOne({ where: { email: 'ccci_integrator@gmail.com' } });
|
1321
|
+
|
1322
|
+
let createdCounter = 0
|
1323
|
+
let errorCounter = 0
|
1324
|
+
let uniqueErrors = new Set()
|
1325
|
+
|
1326
|
+
|
1327
|
+
const db_schema = schema
|
1328
|
+
// let maxIds = await Campuses.max('_id'); = await EnrolledStudents.max('_id'); // Fetch maximum _id directly from the EnrolledStudents model
|
1329
|
+
// let maxId = maxIds|| 0;
|
1330
|
+
// await db.sequelize.query(`ALTER SEQUENCE ${db_schema}."EnrolledStudents__id_seq" RESTART WITH ${maxId + 1}`);
|
1331
|
+
|
1332
|
+
|
1333
|
+
for (let i = 0; i < studentApi.length; i++){
|
1334
|
+
try {
|
1335
|
+
|
1336
|
+
let _student = studentApi[i]
|
1337
|
+
|
1338
|
+
let student = studentSeq.find(st => st.studentNumber === _student?.student_number)
|
1339
|
+
|
1340
|
+
let campus = campusSeq.find(ca => ca.name === _student?.campus)
|
1341
|
+
let course = courseSeq.find(cr => cr.name === _student?.course)
|
1342
|
+
|
1343
|
+
let _students = studentApi[i].subjects_enrolled
|
1344
|
+
|
1345
|
+
let fromPrompts = await Picked()
|
1346
|
+
|
1347
|
+
let semester = fromPrompts.semester
|
1348
|
+
let yearsFrom = fromPrompts.yearFrom
|
1349
|
+
let yearsTo = fromPrompts.yearTo
|
1350
|
+
let _semester = semesterSeq.find(sem => sem.code === semester)
|
1351
|
+
let _acadYear = acadYearSeq.find(yr => yr.from === parseInt(yearsFrom) && yr.to === parseInt(yearsTo))
|
1352
|
+
|
1353
|
+
|
1354
|
+
let modifiedById = userMail._id;
|
1355
|
+
let createdById = userMail._id;
|
1356
|
+
|
1357
|
+
for (let j = 0; j < _students.length; j++){
|
1358
|
+
|
1359
|
+
let instrcutor = _students[j].faculty_id
|
1360
|
+
let sections = _students[j].section_name
|
1361
|
+
let fGrade = _students[j].final_grade
|
1362
|
+
let rmarks = _students[j].remarks
|
1363
|
+
|
1364
|
+
|
1365
|
+
let _inst = instructorApi.find(i => i.faculty_id === instrcutor)
|
1366
|
+
|
1367
|
+
|
1368
|
+
let _instructor = instructorSeq.find(ins => ins.employeeNumber === _inst?.faculty_id)
|
1369
|
+
|
1370
|
+
let _section = sectionSeq.find(sec => sec.name === sections)
|
1371
|
+
|
1372
|
+
|
1373
|
+
if (!_inst) {
|
1374
|
+
continue;
|
1375
|
+
}
|
1376
|
+
|
1377
|
+
if (!_student) {
|
1378
|
+
continue;
|
1379
|
+
}
|
1380
|
+
|
1381
|
+
if (!student) {
|
1382
|
+
continue;
|
1383
|
+
}
|
1384
|
+
|
1385
|
+
|
1386
|
+
let totalPoints = 100
|
1387
|
+
let finalG = fGrade
|
1388
|
+
let gradePoints = ((-4.0 / totalPoints) * parseFloat (finalG) + 5.0).toFixed(2)
|
1389
|
+
let graded = false
|
1390
|
+
if (gradePoints){
|
1391
|
+
graded = true
|
1392
|
+
}
|
1393
|
+
else if (!gradePoints){
|
1394
|
+
gradePoints = 0
|
1395
|
+
}
|
1396
|
+
let remarks = rmarks?.toUpperCase()??null
|
1397
|
+
|
1398
|
+
// return
|
1399
|
+
let [record, created] = await EnrolledStudents.findOrCreate({
|
1400
|
+
where:{
|
1401
|
+
studentId: student?._id,
|
1402
|
+
courseId: course?._id,
|
1403
|
+
instructorId: _instructor?._id??null,
|
1404
|
+
sectionId: _section?._id??null,
|
1405
|
+
campusId: campus?._id,
|
1406
|
+
academicYearId: _acadYear?._id,
|
1407
|
+
semesterId: _semester?._id,
|
1408
|
+
finalGrade: gradePoints,
|
1409
|
+
graded,
|
1410
|
+
remarks,
|
1411
|
+
createdById,
|
1412
|
+
modifiedById,
|
1413
|
+
},
|
1414
|
+
default:{
|
1415
|
+
|
1416
|
+
}
|
1417
|
+
})
|
1418
|
+
|
1419
|
+
if (created) {
|
1420
|
+
logger.info(`New enrolled student record created ${createdCounter + 1}/${createdCounter + 1}: studentId = [${record.studentId}], sectionId = [${record.sectionId}], instructorId = [${record.instructorId}]`);
|
1421
|
+
createdCounter++;
|
1422
|
+
} else {
|
1423
|
+
|
1424
|
+
const errorMsg = `Record enrolled student already exists: studentId = [${record.studentId}], sectionId = [${record.sectionId}], instructorId = [${record.instructorId}]`;
|
1425
|
+
|
1426
|
+
if (!uniqueErrors.has(errorMsg)) {
|
1427
|
+
logger.error(`${errorCounter + 1}/${errorCounter + 1} ${errorMsg}`);
|
1428
|
+
uniqueErrors.add(errorMsg);
|
1429
|
+
errorCounter++;
|
1430
|
+
}
|
1431
|
+
}
|
1432
|
+
}
|
1433
|
+
} catch (error) {
|
1434
|
+
logger.error(`Error occurred: ${error}`);
|
1435
|
+
logger.error(`Error occurred: ${error.stack}`);
|
1436
|
+
}
|
1437
|
+
}
|
1438
|
+
|
1439
|
+
let items = createdCounter + errorCounter
|
1440
|
+
|
1441
|
+
console.log("");
|
1442
|
+
console.log("########################################################")
|
1443
|
+
console.log("")
|
1444
|
+
logger.info(' Enrolled Student Data Migration Completed');
|
1445
|
+
logger.info(`Total successful Enrolled Student records created: ${createdCounter}/${createdCounter}`);
|
1446
|
+
logger.info(`Total Enrolled Student Error Records: ${errorCounter}/${errorCounter}`);
|
1447
|
+
logger.info(`Total Record Fetched: ${items} `)
|
1448
|
+
console.log("")
|
1449
|
+
console.log("########################################################")
|
1450
|
+
console.log("")
|
1451
|
+
|
1452
|
+
let totalSummary = {
|
1453
|
+
itemLength: items,
|
1454
|
+
error: errorCounter,
|
1455
|
+
success: createdCounter
|
1456
|
+
}
|
1457
|
+
return totalSummary
|
1458
|
+
|
1459
|
+
} catch (error) {
|
1460
|
+
logger.error(`Error occurred: ${error}`);
|
1461
|
+
logger.error(`Error occurred: ${error.stack}`);
|
1462
|
+
|
1463
|
+
}
|
1464
|
+
},
|
1465
|
+
}
|
1466
|
+
|
1467
|
+
async function runServerFunctions() {
|
1468
|
+
const results = {};
|
1469
|
+
try {
|
1470
|
+
for (const funcKey in server) {
|
1471
|
+
if(typeof server[funcKey] === 'function'){
|
1472
|
+
const funcName = server[funcKey].name;
|
1473
|
+
results[funcName] = await server[funcKey]();
|
1474
|
+
}
|
1475
|
+
}
|
1476
|
+
} catch (error) {
|
1477
|
+
logger.error(`Error occurred: ${error.stack || error}`);
|
1478
|
+
}
|
1479
|
+
console.log("");
|
1480
|
+
console.log("########################################################");
|
1481
|
+
console.log(" TOTAL SUMMARY");
|
1482
|
+
console.log("########################################################");
|
1483
|
+
|
1484
|
+
Object.entries(results).map(async ([key, value]) =>{
|
1485
|
+
console.log("");
|
1486
|
+
console.log(`${key.toUpperCase()}:`);
|
1487
|
+
logger.info(`total Success ${key} Record: ${value?.success??""}/${value?.itemLength??""}`);
|
1488
|
+
logger.info(`total Error ${key} Record: ${value?.error??""}/${value?.itemLength??""}`);
|
1489
|
+
console.log("");
|
1490
|
+
});
|
1491
|
+
|
1492
|
+
console.log("########################################################");
|
1493
|
+
console.log(" END OF MIGRATION");
|
1494
|
+
console.log("########################################################");
|
1495
|
+
}
|
1496
|
+
|
1497
|
+
|
1498
|
+
|
1499
|
+
|
1500
|
+
// module.exports = server
|
1501
|
+
|
1502
|
+
module.exports = {
|
1503
|
+
serverTable: server,
|
1504
|
+
Campus: server.Campus,
|
1505
|
+
College: server.College,
|
1506
|
+
AcademicDepartments: server.AcademicDepartment,
|
1507
|
+
Course: server.Course,
|
1508
|
+
Room: server.Room,
|
1509
|
+
Instructor: server.Instructor,
|
1510
|
+
AcademicYear: server.AcademicYear,
|
1511
|
+
Subject: server.Subject,
|
1512
|
+
Schedule: server.Schedule,
|
1513
|
+
Student: server.Student,
|
1514
|
+
Section: server.Section,
|
1515
|
+
EnrolledStudent: server.EnrolledStudent,
|
1516
|
+
Server: runServerFunctions,
|
1517
|
+
}
|