diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af13701 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/scripts/google-sheets/.clasp.json \ No newline at end of file diff --git a/scripts/google-sheets/appsscript.json b/scripts/google-sheets/appsscript.json new file mode 100644 index 0000000..fd2465e --- /dev/null +++ b/scripts/google-sheets/appsscript.json @@ -0,0 +1,6 @@ +{ + "timeZone": "Asia/Jerusalem", + "dependencies": {}, + "exceptionLogging": "STACKDRIVER", + "runtimeVersion": "V8" +} diff --git a/scripts/google-sheets/getSessionToken.js b/scripts/google-sheets/getSessionToken.js new file mode 100644 index 0000000..7c0ceca --- /dev/null +++ b/scripts/google-sheets/getSessionToken.js @@ -0,0 +1,13 @@ +function getSessionToken(apiKey) { + const options = { + method: 'post', + contentType: 'application/json', + headers: { + 'x-api-key': apiKey, + }, + } + const response = UrlFetchApp.fetch('https://api.mapme.com/auth/key', options) + + const parsedResponse = JSON.parse(response) + return parsedResponse.sessionToken +} diff --git a/scripts/google-sheets/publish.js b/scripts/google-sheets/publish.js new file mode 100644 index 0000000..ff714ec --- /dev/null +++ b/scripts/google-sheets/publish.js @@ -0,0 +1,14 @@ +function publish() { + const config = retrieveConfig() + const sessionToken = getSessionToken(config.apiKey) + + const options = { + method: 'post', + contentType: 'application/json', + headers: { + authorization: `Bearer ${sessionToken}`, + }, + } + + UrlFetchApp.fetch(`https://api.mapme.com/api/scenes/${config.sceneId}`, options) +} diff --git a/scripts/google-sheets/retrieveConfig.js b/scripts/google-sheets/retrieveConfig.js new file mode 100644 index 0000000..2b977c0 --- /dev/null +++ b/scripts/google-sheets/retrieveConfig.js @@ -0,0 +1,15 @@ +function retrieveConfig() { + const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('config') + + const SCENE_ID_ROW = 1 + const API_KEY_ROW = 2 + const VALUE_COLUMN = 2 + + const sceneId = sheet.getRange(SCENE_ID_ROW, VALUE_COLUMN) + const apiKey = sheet.getRange(API_KEY_ROW, VALUE_COLUMN) + + return { + sceneId: sceneId.getValue(), + apiKey: apiKey.getValue(), + } +} diff --git a/scripts/google-sheets/sync.js b/scripts/google-sheets/sync.js new file mode 100644 index 0000000..444112b --- /dev/null +++ b/scripts/google-sheets/sync.js @@ -0,0 +1,101 @@ +class Map { + constructor(sheetName) { + this.sheetName = sheetName + const config = retrieveConfig() + this.sceneId = config.sceneId + this.sessionToken = getSessionToken(config.apiKey) + } + + addSection(row) { + const [ + , + status, + shouldDelete, + id, + name, + description, + categories, + address, + latitude, + longitude, + pitch, + zoom, + bearing, + actionText, + actionUrl, + media, + ] = row + + const data = { + sectionData: { + id: Utilities.getUuid(), + name, + description, + address, + mapView: { + center: { + lat: latitude, + lng: longitude, + }, + zoom, + bearing, + pitch, + // 'centerZoom' | 'bounds' | 'autofit' + mode: 'centerZoom', + }, + callToAction: { + url: actionUrl, + title: actionText, + // '_modal' | '_self' + // target: "_modal" + }, + }, + sceneId: this.sceneId, + categoryIds: [], + } + + const options = { + method: 'post', + contentType: 'application/json', + headers: { + authorization: `Bearer ${this.sessionToken}`, + }, + payload: JSON.stringify(data), + } + UrlFetchApp.fetch('https://api.mapme.com/api/sections', options) + } + + sync() { + const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(this.sheetName) + const table = sheet.getDataRange().getValues() + + const VALUES_START_ROW = 5 + const STATUS_COLUMN = 2 + const ERROR_COLUMN = 17 + + const values = table.slice(VALUES_START_ROW) + + for (const i in values) { + const value = values[i] + const [, status] = value + if (status === 'new') { + const row = VALUES_START_ROW + 1 + parseInt(i) + const statusCell = sheet.getRange(row, STATUS_COLUMN) + + try { + this.addSection(value) + statusCell.setValue('sync') + } catch (e) { + statusCell.setValue('error') + const errorCell = sheet.getRange(row, ERROR_COLUMN) + errorCell.setValue(e.message) + } + } + } + } +} + +function sync() { + const map = new Map('ADD or SYNC') + map.sync() +}