diff --git a/app/views/index.jade b/app/views/index.jade index 6d9cd57..b4abef7 100644 --- a/app/views/index.jade +++ b/app/views/index.jade @@ -10,6 +10,10 @@ html(lang="en", ng-app="biomed", ng-controller="biomed.PageCtrl") a.brand(href='/', target='_self') Atlantic Biomedical progress-panel ul.nav.pull-right(ng-controller='biomed.AccountCtrl') + li + a(href='https://docs.google.com/a/atlanticbiomedical.com/forms/d/16tPHoTkEQB7fqXxBDZk_7s-WLz6a3eXVBWiUyA0fntM/viewform', target='_blank') + i.icon-thumbs-down + | Incident li a(href='http://atlanticbiomedical.com/ticket/', target='_blank') i.icon-fire @@ -47,4 +51,4 @@ html(lang="en", ng-app="biomed", ng-controller="biomed.PageCtrl") | Workorders .container-fluid ng-view - !{js} \ No newline at end of file + !{js} diff --git a/config/calendar.js b/config/calendar.js index e73625e..0ee81da 100644 --- a/config/calendar.js +++ b/config/calendar.js @@ -2,6 +2,7 @@ var googleapis = require('googleapis'), sprintf = require('sprintf'), OAuth2Client = googleapis.OAuth2Client; +var apiClient; module.exports = function(config) { @@ -15,14 +16,15 @@ module.exports = function(config) { var opts = { baseDiscoveryUrl: 'https://www.googleapis.com/discovery/v1/apis/' }; - return { scheduleEvent: function(event, callback) { console.log("schedule event"); api(function(client, callback) { - var resource = buildResource(event); + var resource = buildResource(event, {}); + console.log("Insert Google Calendar"); + console.log(resource); var request = client.calendar.events.insert({ calendarId: 'primary', @@ -38,34 +40,47 @@ module.exports = function(config) { updateEvent: function(event, callback) { api(function(client, callback) { - var resource = buildResource(event); - - var request = client.calendar.events.update({ + var getRequest = client.calendar.events.get({ calendarId: 'primary', - eventId: event.eventId, - resource: resource + eventId: event.eventId }); + + getRequest.withAuthClient(oauth2Client).execute(function(err, result) { + console.log("get result"); + console.log(err); + console.log(result); - request.withAuthClient(oauth2Client).execute(function(err, result) { - callback(err, result); + var resource = buildResource(event, result); + if ('sequence' in resource) { + resource.sequence += 1; + } else { + resource.sequence = 1; + } + + var updateRequest = client.calendar.events.update({ + calendarId: 'primary', + eventId: event.eventId, + resource: buildResource(event, result) + }); + + updateRequest.withAuthClient(oauth2Client).execute(function(err, result) { + callback(err, result); + }); }); }, callback); } }; - function buildResource(event) { - var resource = { - summary: event.summary, - description: event.description, - location: event.location, - start: { - dateTime: event.start - }, - end: { - dateTime: event.end - }, - attendees: [] - }; + function buildResource(event, resource) { + console.log(event.start); + console.log(event.end); + + resource.summary = event.summary; + resource.description = event.description; + resource.location = event.location; + resource.start = { dateTime: event.start }; + resource.end = { dateTime: event.end }; + resource.attendees = []; event.attendees.forEach(function(attendee) { resource.attendees.push({ @@ -77,20 +92,30 @@ module.exports = function(config) { } function api(workorder, callback) { - googleapis - .discover('calendar', 'v3') - .execute(function(err, client) { - if (err) return callback(err); + var handler = function(client) { + workorder(client, function(err, result) { + if (oauth2Client.credentials.access_token != config.auth.accessToken) { + console.log("Updating access token"); + config.auth.accessToken = oauth2Client.credentials.access_token; + } - workorder(client, function(err, result) { - if (oauth2Client.credentials.access_token != config.auth.accessToken) { - console.log("Updating access token"); - config.auth.accessToken = oauth2Client.credentials.access_token; - } - - callback(err, result); - }); + callback(err, result); }); - } + }; + console.log(apiClient); + if (apiClient) { + console.log("Using cached api client"); + handler(apiClient); + } else { + console.log("Getting api client"); + googleapis.discover('calendar', 'v3').execute(function(err, client) { + if (err) return callback(err); + apiClient = client; + + handler(apiClient); + }); + } + + } }; diff --git a/public/partials/workorders/edit.html b/public/partials/workorders/edit.html index 01a4a2c..596f81c 100644 --- a/public/partials/workorders/edit.html +++ b/public/partials/workorders/edit.html @@ -25,6 +25,7 @@ + @@ -204,4 +205,4 @@ No history is currently available. - \ No newline at end of file +