lms-sync 1.0.0

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.
@@ -0,0 +1,268 @@
1
+ const axios = require('axios')
2
+ const axiosRetry = require('axios-retry')
3
+ const fs = require('fs')
4
+ const path = require('path')
5
+
6
+
7
+ const Apis = {
8
+ async ApiConnections() {
9
+ try {
10
+ const api = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'login.json')));
11
+ const instance = axios.create({
12
+ baseURL: api.baseURL
13
+ });
14
+ // console.log('api , instance :>> ', api , instance);
15
+ return {instance, api}
16
+ } catch (error) {
17
+ console.error('Axios Api Error:', error)
18
+ throw error
19
+
20
+ }
21
+ },
22
+
23
+ //sequence 1
24
+ async Login() {
25
+ try {
26
+ const apis = await this.ApiConnections()
27
+ const loginResult = await apis.instance.post('/login', apis.api)
28
+
29
+ // console.log('loginResult.data.access_token :>> ', loginResult.data.access_token);
30
+ return {login: loginResult.data.access_token, apis:apis}
31
+
32
+ } catch (error) {
33
+ console.error('Login Api Error:', error)
34
+ throw error
35
+ }
36
+ },
37
+
38
+ //sequence 2
39
+ async Campus() {
40
+ try {
41
+ const { login, apis } = await this.Login();
42
+ const campusResult = await apis.instance.get('/campuses', {
43
+ headers: {
44
+ 'Authorization': `Bearer ${login}`
45
+ }
46
+ });
47
+ // console.log('campusResult.data :>> ', campusResult.data);
48
+ return campusResult.data
49
+ } catch (error) {
50
+ console.error('Campus Api Error:', error);
51
+ }
52
+ },
53
+
54
+ //sequence 3
55
+ async College(){
56
+ try {
57
+ const { login, apis } = await this.Login();
58
+ const collegeResult = await apis.instance.get('/colleges', {
59
+ headers: {
60
+ 'Authorization': `Bearer ${login}`
61
+ }
62
+ });
63
+ // console.log('collegeResult.data :>> ', collegeResult.data);
64
+ return collegeResult.data
65
+ } catch (error) {
66
+ console.error('College Api Error:', error);
67
+ }
68
+ },
69
+
70
+ //sequence 4
71
+ async Department(){
72
+ try {
73
+ const { login, apis } = await this.Login();
74
+ const departmentResult = await apis.instance.get('/departments', {
75
+ headers: {
76
+ 'Authorization': `Bearer ${login}`
77
+ }
78
+ });
79
+ // console.log('departmentResult.data :>> ', departmentResult.data);
80
+ return departmentResult.data
81
+ } catch (error) {
82
+ console.error('Department Api Error:', error);
83
+ }
84
+ },
85
+
86
+ //sequence 5
87
+ async Course(){
88
+ try {
89
+ const { login, apis } = await this.Login();
90
+ const courseResult = await apis.instance.get('/courses', {
91
+ headers: {
92
+ 'Authorization': `Bearer ${login}`
93
+ }
94
+ });
95
+ // console.log('courseResult.data :>> ', courseResult.data);
96
+ return courseResult.data
97
+ } catch (error) {
98
+ console.error('Course Api Error:', error);
99
+ }
100
+ },
101
+
102
+ //sequence 6
103
+ async Room(){
104
+ try {
105
+ const { login, apis } = await this.Login();
106
+ const roomResult = await apis.instance.get('/rooms', {
107
+ headers: {
108
+ 'Authorization': `Bearer ${login}`
109
+ }
110
+ });
111
+ // console.log('roomResult.data :>> ', roomResult.data);
112
+ return roomResult.data
113
+ } catch (error) {
114
+ console.error('Room Api Error:', error);
115
+ }
116
+ },
117
+
118
+ //sequence 7
119
+ async Instructor(){
120
+ try {
121
+ const { login, apis } = await this.Login();
122
+ const instructorResult = await apis.instance.get('/instructors', {
123
+ headers: {
124
+ 'Authorization': `Bearer ${login}`
125
+ }
126
+ });
127
+ // console.log('instructorResult.data :>> ', instructorResult.data);
128
+ return instructorResult.data
129
+ } catch (error) {
130
+ console.error('Instructor Api Error:', error);
131
+ }
132
+ },
133
+
134
+ //sequence 8
135
+ async AcademicYear(){
136
+ try {
137
+ //fetch the access token from the login
138
+ const { login, apis } = await this.Login();
139
+
140
+ //get the current date
141
+ let defaultDate = new Date()
142
+
143
+ //get the current year
144
+ let currentYear = defaultDate.getFullYear()
145
+
146
+ //get the current year and minus it to 1 for the previous year
147
+ let previousYear = currentYear - 1
148
+
149
+ //join the previous year and the current year
150
+ let currentSchoolYear = previousYear + '-' + currentYear
151
+
152
+ const acadYearResult = await apis.instance.get('/academic-year', {
153
+ headers: {
154
+ 'Authorization': `Bearer ${login}`
155
+ }
156
+ });
157
+
158
+ // console.log('acadYearResult.data :>> ', acadYearResult.data);
159
+ return {
160
+ acadYearResult:acadYearResult.data,
161
+ currentSchoolYear: currentSchoolYear
162
+ }
163
+ } catch (error) {
164
+ console.error('Academic Year Api Error:', error)
165
+ }
166
+ },
167
+
168
+ //sequence 9
169
+ async Subject(){
170
+ try {
171
+ const { login, apis } = await this.Login();
172
+ const subjectResult = await apis.instance.get('/subjects?academic_year_id=6124', {
173
+ headers: {
174
+ 'Authorization': `Bearer ${login}`
175
+ }
176
+ });
177
+ // console.log('subjectResult.data :>> ', subjectResult.data);
178
+ return subjectResult.data
179
+ } catch (error) {
180
+ console.error('Instructor Api Error:', error);
181
+ }
182
+ },
183
+
184
+ //sequence 10
185
+ async Schedule(){
186
+ try {
187
+ //fetch the access token from the login
188
+ const { login, apis } = await this.Login();
189
+
190
+ const schedule = await apis.instance.get('/subjects?academic_year_id=6124',
191
+ {
192
+ headers: {
193
+ 'Authorization': `Bearer ${login}`
194
+ }
195
+ })
196
+
197
+ // console.log('schedule.data :>> ', schedule.data);
198
+ return schedule.data
199
+ } catch (error) {
200
+ console.error('Schedule Api Error:', error);
201
+ }
202
+ },
203
+
204
+ //sequence 11
205
+ async Student(){
206
+ try {
207
+ const { login, apis } = await this.Login();
208
+ const {currentSchoolYear} = await this.AcademicYear()
209
+ const student = await apis.instance.post('/students/enrolled',
210
+ {
211
+ "school_year": `${currentSchoolYear}`,
212
+ "school_term": "2nd Semester",
213
+ "limit": 999999,
214
+ },
215
+ {
216
+ headers:{
217
+ 'Authorization': `Bearer ${login}`
218
+ },
219
+ }
220
+ )
221
+ // console.log('student.data :>> ', student.data);
222
+ return student.data
223
+ } catch (error) {
224
+ console.error('Student Api Error:', error);
225
+ }
226
+ },
227
+
228
+ //sequence 12
229
+ async Section(){
230
+ try {
231
+ const { login, apis } = await this.Login();
232
+ const {currentSchoolYear} = await this.AcademicYear()
233
+ const sectionResult = await apis.instance.post('/sections',
234
+ {
235
+ "school_year": `${currentSchoolYear}`,
236
+ "school_term": "2nd Semester",
237
+ "limit": 999999,
238
+ },
239
+ {
240
+ headers:{
241
+ 'Authorization': `Bearer ${login}`
242
+ },
243
+ }
244
+ )
245
+ // console.log('sectionResult.data :>> ', sectionResult.data);
246
+ return sectionResult.data
247
+ } catch (error) {
248
+ console.error('Student Api Error:', error);
249
+ }
250
+ },
251
+ }
252
+
253
+ module.exports = {
254
+ ApiConnections: Apis.ApiConnections,
255
+ Login: Apis.Login,
256
+ Campus: Apis.Campus,
257
+ College: Apis.College,
258
+ Department: Apis.Department,
259
+ Course: Apis.Course,
260
+ Room: Apis.Room,
261
+ Instructor: Apis.Instructor,
262
+ AcademicYear: Apis.AcademicYear,
263
+ Subject: Apis.Subject,
264
+ Schedule: Apis.Schedule,
265
+ Student: Apis.Student,
266
+ Section: Apis.Section
267
+
268
+ }