-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
199 lines (159 loc) · 5.17 KB
/
main.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
import moment from "moment"
import fs from "fs"
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
// GET AUTHENTICATION TOKEN
let baseUrl = "https://staging-api-health2023.agileteknik.com/api"
const LOGIN_URL = baseUrl + "/v1/login";
const INSERT_MOOD_URL = baseUrl + "/v2/mood-tracker/moods"
const LIST_MOOD_THIS_MONTH_URL = baseUrl + "/v2/mood-tracker/moods/months/"
const USER_INFO_URL = baseUrl + "/v1/user";
const MAX_REPEAT_CHECK_TOKEN = 3;
const __dirname = dirname(fileURLToPath(import.meta.url));
let tokenFile = __dirname + '/dist/token.txt';
let dataFile = __dirname + '/dist/data.json';
let token = null;
let LoginWithCredential = async () => {
let response = await fetch(LOGIN_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify({
email: process.env.EMAIL_NIKOMOOD,
password: process.env.PASSWORD_NIKOMOOD
})
})
if (!response.ok) {
throw new Error(`Failed to login: ${response.statusText}`);
}
let json = await response.json()
// write token to file
fs.writeFileSync(tokenFile, json.data.access_token);
return json.data.access_token
}
let GetToken = async (force = false) => {
// check if token.txt exists
if (!fs.existsSync(tokenFile) || force === true) {
token = await LoginWithCredential();
} else {
// read token from file
token = fs.readFileSync(tokenFile, 'utf-8');
}
}
let GetListMoodDescriptions = () => {
// read data.json
let data = JSON.parse(fs.readFileSync(dataFile, "utf8"));
return data;
}
let InsertRandomMood = async () => {
let moodTypes = {
1: 'Very Happy',
2: 'Happy',
3: 'Neutral',
4: 'Sad',
5: 'Angry'
}
let activities = {
1: "Belajar",
2: "Hiburan",
3: "Kerja",
4: "Olahraga",
5: "Sosial",
6: "Makan",
7: "Memasak",
8: "Belanja",
9: "Tidur",
10: "Ibadah",
11: "Meditasi",
}
// get random mood & activity
let randomMoodId = Object.keys(moodTypes)[Math.floor(Math.random() * Object.keys(moodTypes).length)];
let randomActivityId = Object.keys(activities)[Math.floor(Math.random() * Object.keys(activities).length)];
let randomMood = moodTypes[randomMoodId];
let randomActivity = activities[randomActivityId];
let allData = GetListMoodDescriptions();
let descriptions = allData[randomMood][randomActivity];
// get random descriptions
let description = descriptions[Math.floor(Math.random() * Object.keys(descriptions).length)]
let today = moment().format("YYYY-MM-DD");
let response = await fetch(INSERT_MOOD_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + token
},
body: JSON.stringify({
date: today,
mood_types_id: randomMoodId,
activities: [randomActivityId],
description: description
})
})
if (!response.ok) {
throw new Error(`Failed to insert mood: ${response.statusText}`);
}
console.log("Mood berhasil dimasukkan");
}
let IsFilledToday = async (today) => {
let response = await fetch(LIST_MOOD_THIS_MONTH_URL + today, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + token
}
})
if (!response.ok) {
throw new Error(`Failed to check if filled today: ${response.statusText}`);
}
let json = await response.json()
let isFilledToday = false;
for (let i = 0; i < json.data.length; i++) {
if (json.data[i].date === today) {
isFilledToday = true;
break;
}
}
if (isFilledToday) {
return true;
} else {
return false;
}
}
let isTokenValid = async (numberOfRepeat = 1) => {
let response = await fetch(USER_INFO_URL, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + token
}
})
if (!response.ok) {
if (numberOfRepeat > MAX_REPEAT_CHECK_TOKEN) throw new Error("Failed to check user account");
await GetToken(true);
return await isTokenValid(numberOfRepeat + 1);
}
let json = await response.json()
return json.data
}
let main = async () => {
// Authentication
await GetToken();
let dataUser = await isTokenValid();
console.log("Who i am ? ", dataUser.name);
// Apakah hari ini sudah isi mood ?
let today = moment().format('YYYY-MM-DD')
// let today = "2023-12-25"; // change this to the desired date
let isFilledToday = await IsFilledToday(today);
console.log("Is filled today?", isFilledToday);
// Jika belum isi, maka insert mood baru
if (!isFilledToday) {
await InsertRandomMood();
}
console.log('-- Thanks for using this service --');
}
main();