diff --git a/app/controllers/home.controller.js b/app/controllers/home.controller.js index 2c03ddc..0a9bcf1 100644 --- a/app/controllers/home.controller.js +++ b/app/controllers/home.controller.js @@ -51,14 +51,14 @@ function create(req, res) { let endDate = payload.endDate lib.authorize((err, auth) => { + let options = lib.eventBuilder(payload) if (err) { res.send(err) + } else { + options.auth = auth } - // let options = lib.eventBuilder(payload) - // options.auth = auth - - lib.createEvent(auth, payload, (err, result) => { + lib.createEvent(options, (err, result) => { if (err) { res.send(err) } else { diff --git a/app/lib/index.js b/app/lib/index.js index 3a356b3..0e8824b 100644 --- a/app/lib/index.js +++ b/app/lib/index.js @@ -3,22 +3,21 @@ const google = require('googleapis'); const googleAuth = require('google-auth-library'); const calendar = google.calendar('v3'); - const fs = require('fs'); +const path = require('path'); +const yamlConfig = require('node-yaml-config'); +const config = yamlConfig.load(path.join(__dirname, '/../../config/config.yml')); +const CALENDAR_ID = config.calendarID -const SCOPES = [process.env.SCOPES]; +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'; module.exports = { - authorize: (callback) => { - fs.readFile('client_secret.json', function processClientSecrets(err, content) { - if (err) { - console.log('Error loading client secret file: ' + err); - return; - } + fs.readFile('client_secret.json', (err, content) => { + if (err) return callback(err); let credentials = JSON.parse(content); var clientSecret = credentials.installed.client_secret; @@ -27,71 +26,80 @@ module.exports = { var auth = new googleAuth(); var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); - // Check if we have previously stored a token. - fs.readFile(TOKEN_PATH, function (err, token) { - if (err) { - return callback(err); - } else { - oauth2Client.credentials = JSON.parse(token); - return callback(null, oauth2Client); - } - }); - - }); - - }, + fs.readFile(TOKEN_PATH, (err, token) => { + if (err) return callback(err); - getNewToken: (oauth2Client, callback) => { - var authUrl = oauth2Client.generateAuthUrl({ - access_type: 'offline', - scope: SCOPES - }); - console.log('Authorize this app by visiting this url: ', authUrl); - var rl = readline.createInterface({ - input: process.stdin, - output: process.stdout - }); - rl.question('Enter the code from that page here: ', function (code) { - rl.close(); - oauth2Client.getToken(code, function (err, token) { - if (err) { - console.log('Error while trying to retrieve access token', err); - return; - } - oauth2Client.credentials = token; - storeToken(token); - callback(oauth2Client); + oauth2Client.credentials = JSON.parse(token); + return callback(null, oauth2Client); }); }); + }, listEvents: (auth, callback) => { calendar.events.list({ auth: auth, - calendarId: process.env.CALENDAR_ID, + calendarId: CALENDAR_ID || 'primary', timeMin: (new Date()).toISOString(), maxResults: 50, singleEvents: true, orderBy: 'startTime' }, (err, response) => { - if (err) { - return callback(err); - } + if (err) return callback(err); + return callback(null, response); }); }, - createEvent: (auth, event, callback) => { - + createEvent: (options, callback) => { calendar.events.insert({ - auth: auth, - calendarId: process.env.CALENDAR_ID, - resource: event, - }, (err, event) => { - if (err) { - return callback('There was an error contacting the Calendar service: ' + err); - } - return callback(null, event.htmlLink); + 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); + + return callback(null, response); }); + }, + + eventBuilder: (payload) => { + + return { + summary: payload.summary, + description: payload.description, + start: { + dateTime: payload.startDate, + timeZone: 'Asia/Bangkok' + }, + end: { + dateTime: payload.endDate, + timeZone: 'Asia/Bangkok' + }, + attendees: [ + { email: payload.email } + ], + reminders: { + useDefault: false, + overrides: [ + { + method: 'email', + minutes: 24 * 60 + } + ] + } + } } } \ No newline at end of file diff --git a/config/config.yml b/config/config.yml index 5312f8f..b5e64bf 100644 --- a/config/config.yml +++ b/config/config.yml @@ -1,6 +1,5 @@ ##### deverlopment Config localhost: - skipLogin: true debug: true server: url: 'localhost' @@ -15,4 +14,5 @@ localhost: user: pass: timerecheck: '10000' #millisecond - apitimeout: 3000 #millisecond \ No newline at end of file + apitimeout: 3000 #millisecond + calendarID: 'rvmbg3kg7uqninf7n3au1ku4mc@group.calendar.google.com' \ No newline at end of file diff --git a/quickstart.js b/quickstart.js index 96b2678..2d87381 100644 --- a/quickstart.js +++ b/quickstart.js @@ -7,7 +7,7 @@ var googleAuth = require('google-auth-library'); // If modifying these scopes, delete your previously saved credentials // at ~/.credentials/calendar-nodejs-quickstart.json -var SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']; +var SCOPES = ['https://www.googleapis.com/auth/calendar']; var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/'; var TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json'; -- libgit2 0.21.2