-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetch.ts
198 lines (179 loc) · 5.15 KB
/
fetch.ts
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
import fetch from "node-fetch";
import csv from "csvtojson";
import * as fastcsv from "fast-csv";
import fs from "fs";
import { env } from "process";
//const csv = require("csvtojson");
import dotenv from "dotenv";
//const dotenv = require("dotenv");
dotenv.config();
const auth_token = process.env.AUTH_TOKEN;
console.log(auth_token);
const mosyle_token = process.env.MOSYLE_TOKEN;
export interface Student {
id: number;
email: string;
first_name: string;
last_name: string;
archived: boolean;
gender: string;
timezone: string;
created_at: string;
updated_at: string;
birthday: string;
photo_url: string;
student_id: string;
homeroom_advisor_id: number;
attendance_start_date: string;
class_grade: string;
class_grade_number: number;
program: string;
program_code: string;
ib_group_id: number;
year_group_id: number;
parent_ids?: number[] | null;
street_address: string;
street_address_ii: string;
city: string;
zipcode: string;
country: string;
nationalities?: string[] | null;
languages?: null[] | null;
graduating_year: number;
}
export interface Parent {
id: number;
email: string;
first_name: string;
last_name: string;
archived: boolean;
timezone: string;
mobile_phone_number: string;
child_ids?: number[] | null;
children?: ChildrenEntity[] | null;
nationalities?: null[] | null;
languages?: null[] | null;
work_phone: string;
}
export interface ChildrenEntity {
id: number;
relationship: Relationship;
}
export interface Relationship {
kind: number;
name: string;
}
export interface Class {
name: string;
section: string;
}
const base_url = "https://api.managebac.com/v2/";
function range(size: number, startAt: number = 0): ReadonlyArray<number> {
return [...Array(size).keys()].map((i) => i + startAt);
}
export const fetchMB = async (url: string): Promise<any> => {
const request = {
method: "GET",
headers: {
"content-type": "application/json",
"auth-token": auth_token,
},
};
console.log(url);
const response = await fetch(url, request);
const json = await response.json();
return json;
};
export const fetchMosyle = async (endpoint): Promise<any> => {
const body = {
accessToken: mosyle_token,
options: {
os: "mac",
},
};
console.log(JSON.stringify(body));
const request = {
method: "POST",
body: JSON.stringify(body),
//headers: { "Content-Type": "application/json" },
};
const url = "https://managerapi.mosyle.com/v2/" + endpoint;
const response = await fetch(url, request);
const json = await response.json();
console.log(json["response"]["devices"]);
return json;
};
export const getInfo = async (id: number, endpoint: string): Promise<any> => {
const url = base_url + endpoint + "/" + id;
const json = await fetchMB(url);
return json;
};
export const getClassList = async (id: number): Promise<any> => {
const url = base_url + "classes" + "/" + id + "/students";
const json = await fetchMB(url);
return json;
};
export const fetchParams = async (
endpoint: string,
params?: URLSearchParams
): Promise<any> => {
const url = base_url + endpoint + "?" + params;
//console.log(url);
//const request = new Request(url, {
const json = await fetchMB(url);
//console.log(json["meta"]["total_pages"]);
const list = json[endpoint];
return list;
};
export const fetchAll = async (
endpoint: string,
endpoint_url = ""
): Promise<any> => {
const url2 = endpoint_url === "" ? endpoint : endpoint_url;
const url = base_url + url2;
const json = await fetchMB(url);
//console.log(json);
const total_pages = json["meta"]["total_pages"];
let list = json[endpoint];
console.log(total_pages);
await Promise.all(
range(total_pages - 1, 2).map(async (p) => {
//for (let p = 2; p < total_pages + 1; p++) {
const pageParams = new URLSearchParams({ page: String(p) });
const page = await fetchParams(url2, pageParams);
list = list.concat(page);
//console.log(page);
})
);
return list;
};
//const classes = [11726302, 11774462, 11774457, 11786518];
//const classes = [];
const getClassesMosyle = async (classes: number[]): Promise<any> => {
//Foreach student, get parent emails and send email
const csvFilePath = "student_mosyle_ids.csv";
const jsonArray = await csv().fromFile(csvFilePath);
const mosyle_ids = jsonArray.map((s) => s["User ID"]);
console.log(mosyle_ids[0]);
console.log(mosyle_ids[1]);
await Promise.all(
classes.map(async (c) => {
const classInfo = await getInfo(c, "classes");
console.log(classInfo);
const classList = await getClassList(c);
console.log(classList);
const student_ids = classList["student_ids"];
console.log(student_ids);
const ps_ids = await Promise.all(
student_ids.map(async (student) => {
const studentInfo = await getInfo(student, "students");
const s = <Student>studentInfo["student"];
console.log(s.first_name + " " + s.last_name);
return mosyle_ids.includes(s.student_id) ? s.student_id : s.email;
})
);
console.log("English 7,2B,,TEACHER_ID,SCHOOL,");
console.log('"' + ps_ids.join(",") + '"');
})
);
};