This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.js
115 lines (95 loc) · 2.88 KB
/
utils.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
const https = require('https');
async function sendMessageToAdminRoom(message) {
const body = JSON.stringify({
msgtype: 'm.text',
body: message
});
const post_options = {
host: 'matrix.org',
path: `/_matrix/client/r0/rooms/${process.env.MATRIX_ADMIN_ROOM}/send/m.room.message?access_token=${process.env.MATRIX_ACCESS_TOKEN}`,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
return new Promise((resolve, reject) => {
const request = https.request(post_options, response => {
const response_data = [];
response.on('data', chunk => {
response_data.push(chunk);
});
if (response.statusCode === 200) {
response.on('end', () => resolve(JSON.parse(Buffer.concat(response_data).toString())));
} else {
response.on('end', () => {
let error = new Error(`Matrix returned status code: ${response.statusCode}`);
error.response = JSON.parse(Buffer.concat(response_data).toString());
reject(error);
});
}
});
request.on('error', reject);
request.write(body);
request.end();
});
}
function validateString(text) {
return text.length <= 64;
}
function generateCsv(members) {
const rows = members.map((member) => {
return [
member.firstName,
member.lastName,
member.studentID,
member.degree,
member.email,
member.department,
member.mobileNumber,
member.groupChat,
];
});
const header = [
'First Name',
'Last Name',
'Student ID',
'Degree',
'Email',
'Department',
'Mobile Number',
'Group Chat',
];
const csvRows = [header, ...rows];
return csvRows.map((row) => row.join(',')).join('\n');
}
function log(message, level = 'INFO') {
console.log(`[${new Date().toISOString()}] [${level.toUpperCase()}] ${message}`);
}
function makeTitleCase(str) {
let words = str.split(/\s+/);
let res = '';
for (let i = 0; i < words.length; i++) {
let word = words[i];
res += word.charAt(0).toUpperCase() + word.slice(1).toLowerCase() + ' ';
}
return res.trimEnd();
}
function standardizePhoneNumber(initialNumber) {
if (!initialNumber)
return initialNumber
if (initialNumber.charAt(0) === '+')
return initialNumber;
if (initialNumber.substring(0, 2) === '90')
return '+' + initialNumber;
if (initialNumber.charAt(0) === '0')
return '+9' + initialNumber;
return '+90' + initialNumber;
}
module.exports = {
sendMessageToAdminRoom,
validateString,
generateCsv,
log,
makeTitleCase,
standardizePhoneNumber
};