Blame view

app/lib/index.js 5.96 KB
f6a4415a   Apichat.Tum   start smart-rms-c...
1
2
3
'use strict';

const google = require('googleapis');
d360cefc   TUM.Apichat   move save google ...
4
const googleAuth = require('google-auth-library');
f6a4415a   Apichat.Tum   start smart-rms-c...
5
6
const calendar = google.calendar('v3');
const fs = require('fs');
f6a4415a   Apichat.Tum   start smart-rms-c...
7
const path = require('path');
a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
8
9
10
const yamlConfig = require('node-yaml-config');
const config = yamlConfig.load(path.join(__dirname, '/../../config/config.yml'));
const CALENDAR_ID = config.calendarID
77034810   Apichat.Tum   fix REST API
11

d360cefc   TUM.Apichat   move save google ...
12
13
14
const SCOPES = ['https://www.googleapis.com/auth/calendar'];
const TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/';
const TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json';
fbe7f7a7   Apichat.Tum   add api google
15
16
17
18
19
20
21
22
23

function hasTimezone(timezone) {
    if (timezone) {
        return timezone
    } else {
        return 'Asia/Bangkok'
    }
}

f3f14fef   TUM.Apichat   add value config ...
24
function getNewToken(oauth2Client, callback) {
fbe7f7a7   Apichat.Tum   add api google
25
26
27
28
29
30
31
    var authUrl = oauth2Client.generateAuthUrl({
        access_type: 'offline',
        scope: SCOPES
    });
    // console.log('Authorize this app by visiting this url: ', authUrl);
    return authUrl;
}
f6a4415a   Apichat.Tum   start smart-rms-c...
32

d360cefc   TUM.Apichat   move save google ...
33
function setNewToken(code) {
f6a4415a   Apichat.Tum   start smart-rms-c...
34
35
36
    oauth2Client.getToken(code, function (err, token) {
        if (err) {
            console.log('Error while trying to retrieve access token', err);
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
37
38
39
40
41
42
43
44
            return;
        }
        oauth2Client.credentials = token;
        storeToken(token);
        callback(oauth2Client);
    })
}

cf86e9a3   Apichat.Tum   - ui with oauth2 ...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function storeToken(token) {
    try {
        fs.mkdirSync(TOKEN_DIR);
    } catch (err) {
        if (err.code != 'EEXIST') {
            throw err;
        }
    }
    fs.writeFile(TOKEN_PATH, JSON.stringify(token));
    console.log('Token stored to ' + TOKEN_PATH);
}

module.exports = {
    authorize: (callback) => {

        fs.readFile('client_secret.json', (err, content) => {
            if (err) return callback(err);

            let credentials = JSON.parse(content);
            var clientSecret = credentials.installed.client_secret;
            var clientId = credentials.installed.client_id;
d360cefc   TUM.Apichat   move save google ...
66
67
68
            var redirectUrl = credentials.installed.redirect_uris[0];
            var auth = new googleAuth();
            var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
69

d360cefc   TUM.Apichat   move save google ...
70
71
72
73
            fs.readFile(TOKEN_PATH, (err, token) => {
                if (err) {
                    return callback(null, null, getNewToken(oauth2Client, callback));
                } else {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
74
                    oauth2Client.credentials = JSON.parse(token);
d360cefc   TUM.Apichat   move save google ...
75
                    return callback(null, oauth2Client);
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
76
                }
d360cefc   TUM.Apichat   move save google ...
77
            });
48a9a246   Apichat.Tum   fix auth
78
        });
d360cefc   TUM.Apichat   move save google ...
79
80
81
82
83
84
85
86
87
88
89
90
91

    },

    setNewToken: (code, callback) => {
        fs.readFile('client_secret.json', (err, content) => {
            if (err) return callback(err);

            let credentials = JSON.parse(content);
            var clientSecret = credentials.installed.client_secret;
            var clientId = credentials.installed.client_id;
            var redirectUrl = credentials.installed.redirect_uris[0];
            var auth = new googleAuth();
            var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
92
93

            fs.readFile(TOKEN_PATH, (err, token) => {
f6a4415a   Apichat.Tum   start smart-rms-c...
94
                if (err) {
f6a4415a   Apichat.Tum   start smart-rms-c...
95
                    oauth2Client.getToken(code, function (err, token) {
fbe7f7a7   Apichat.Tum   add api google
96
97
                        if (err) {
                            console.log('Error while trying to retrieve access token', err);
f3f14fef   TUM.Apichat   add value config ...
98
                            return;
fbe7f7a7   Apichat.Tum   add api google
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
                        } else {
                            oauth2Client.credentials = token;
                            storeToken(token);
                            return callback(null, oauth2Client);
                        }

                    })
                } else {
                    oauth2Client.credentials = JSON.parse(token);
                    return callback(null, oauth2Client);
                }
            });
        });
    },

    listEvents: (auth, callback) => {
f6a4415a   Apichat.Tum   start smart-rms-c...
115
        calendar.events.list({
a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
116
            auth: auth,
f6a4415a   Apichat.Tum   start smart-rms-c...
117
118
            calendarId: CALENDAR_ID || 'primary',
            timeMin: (new Date()).toISOString(),
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
119
            maxResults: 50,
fbe7f7a7   Apichat.Tum   add api google
120
            singleEvents: true,
f3f14fef   TUM.Apichat   add value config ...
121
            orderBy: 'startTime'
fbe7f7a7   Apichat.Tum   add api google
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
        }, (err, response) => {
            if (err) return callback(err);

            return callback(null, response);
        });
    },

    createEvent: (options, callback) => {
        calendar.events.insert({
            auth: options.auth,
            calendarId: CALENDAR_ID || 'primary',
            resource: options
        }, (err, response) => {
            if (err) return callback(err);

            return callback(null, response);
        });
    },

    deleteEvent: (options, callback) => {
        calendar.events.delete({
            auth: options.auth,
            calendarId: CALENDAR_ID || 'primary',
            eventId: options.eventId
        }, (err, response) => {
            if (err) return callback(err);

48a9a246   Apichat.Tum   fix auth
149
            return callback(null, response);
fbe7f7a7   Apichat.Tum   add api google
150
151
152
153
        });
    },

    eventBuilder: (payload) => {
48a9a246   Apichat.Tum   fix auth
154
        var buildPayload = {}
fbe7f7a7   Apichat.Tum   add api google
155
156
157
158
159
160
        try {
            buildPayload.summary = payload.summary
            buildPayload.description = payload.description
            buildPayload.start = {
                dateTime: payload.startDate,
                timezone: hasTimezone(payload.startTimeZone)
48a9a246   Apichat.Tum   fix auth
161

fbe7f7a7   Apichat.Tum   add api google
162
            }
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
163
164
165
            buildPayload.end = {
                dateTime: payload.endDate,
                timeZone: hasTimezone(payload.endTimeZone)
f6a4415a   Apichat.Tum   start smart-rms-c...
166
167
168
            }
            if (payload.email) {
                buildPayload.attendees = [{ email: payload.email }]
fbe7f7a7   Apichat.Tum   add api google
169
            }
f6a4415a   Apichat.Tum   start smart-rms-c...
170
171
172
173
            if (payload.reminders) {
                buildPayload.reminders = {
                    useDefault: false,
                    overrides: [
a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
174
175
                        {
                            method: 'email',
f6a4415a   Apichat.Tum   start smart-rms-c...
176
177
178
179
                            minutes: 24 * 60
                        }
                    ]
                }
a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
180
            }
f6a4415a   Apichat.Tum   start smart-rms-c...
181
            if (payload.extendedProperties) {
a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
182
183
184
185
186
187
188
189
190
                buildPayload.extendedProperties = payload.extendedProperties
            }
        } catch (error) {
            console.error(error.message)
        } finally {
            return buildPayload
        }
    }
}
f6a4415a   Apichat.Tum   start smart-rms-c...

a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API

77034810   Apichat.Tum   fix REST API

7598c58f   Apichat.Tum   add eventTypeID /...

77034810   Apichat.Tum   fix REST API

a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API

cf86e9a3   Apichat.Tum   - ui with oauth2 ...

77034810   Apichat.Tum   fix REST API

cf86e9a3   Apichat.Tum   - ui with oauth2 ...

21a1ff19   Apichat.Tum   fix follow smartr...

77034810   Apichat.Tum   fix REST API

77034810   Apichat.Tum   fix REST API

21a1ff19   Apichat.Tum   fix follow smartr...

77034810   Apichat.Tum   fix REST API

21a1ff19   Apichat.Tum   fix follow smartr...

77034810   Apichat.Tum   fix REST API

21a1ff19   Apichat.Tum   fix follow smartr...

77034810   Apichat.Tum   fix REST API

7598c58f   Apichat.Tum   add eventTypeID /...

77034810   Apichat.Tum   fix REST API

1244ae71   Apichat.Tum   new response

77034810   Apichat.Tum   fix REST API

cf86e9a3   Apichat.Tum   - ui with oauth2 ...

77034810   Apichat.Tum   fix REST API

fbe7f7a7   Apichat.Tum   add api google

77034810   Apichat.Tum   fix REST API

fbe7f7a7   Apichat.Tum   add api google

77034810   Apichat.Tum   fix REST API

fbe7f7a7   Apichat.Tum   add api google

77034810   Apichat.Tum   fix REST API

fbe7f7a7   Apichat.Tum   add api google

cf86e9a3   Apichat.Tum   - ui with oauth2 ...

77034810   Apichat.Tum   fix REST API

fbe7f7a7   Apichat.Tum   add api google

cf86e9a3   Apichat.Tum   - ui with oauth2 ...

21a1ff19   Apichat.Tum   fix follow smartr...

cf86e9a3   Apichat.Tum   - ui with oauth2 ...

21a1ff19   Apichat.Tum   fix follow smartr...

cf86e9a3   Apichat.Tum   - ui with oauth2 ...

7598c58f   Apichat.Tum   add eventTypeID /...

1244ae71   Apichat.Tum   new response

cf86e9a3   Apichat.Tum   - ui with oauth2 ...

a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API

cf86e9a3   Apichat.Tum   - ui with oauth2 ...

a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API

fbe7f7a7   Apichat.Tum   add api google

f3f14fef   TUM.Apichat   add value config ...

fbe7f7a7   Apichat.Tum   add api google

f6a4415a   Apichat.Tum   start smart-rms-c...

fbe7f7a7   Apichat.Tum   add api google