From 3787224d5e2efc915006ca15f476af6aa8d5729e Mon Sep 17 00:00:00 2001 From: Uryn Dmytro Date: Tue, 18 Oct 2022 20:05:26 +0300 Subject: [PATCH 1/3] google sheets --- .gitignore | 0 scripts/google-sheets/appsscript.json | 6 ++ scripts/google-sheets/getSessionToken.js | 13 +++ scripts/google-sheets/publish.js | 14 ++++ scripts/google-sheets/retrieveConfig.js | 15 ++++ scripts/google-sheets/sync.js | 101 +++++++++++++++++++++++ 6 files changed, 149 insertions(+) create mode 100644 .gitignore create mode 100644 scripts/google-sheets/appsscript.json create mode 100644 scripts/google-sheets/getSessionToken.js create mode 100644 scripts/google-sheets/publish.js create mode 100644 scripts/google-sheets/retrieveConfig.js create mode 100644 scripts/google-sheets/sync.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 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() +} From 37a15b123322bd2abe2850de9d693210ca99c3d5 Mon Sep 17 00:00:00 2001 From: Uryn Dmytro Date: Tue, 18 Oct 2022 20:13:03 +0300 Subject: [PATCH 2/3] updated .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index e69de29..1c126bc 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,3 @@ +/scripts/google-sheets/.clasp.json +.idea/ +.DS_Store \ No newline at end of file From 913281f65ac67af5c669a297bd387b0e11a43044 Mon Sep 17 00:00:00 2001 From: Uryn Dmytro Date: Wed, 19 Oct 2022 13:53:02 +0300 Subject: [PATCH 3/3] updated .gitignore --- .gitignore | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 1c126bc..af13701 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ -/scripts/google-sheets/.clasp.json -.idea/ -.DS_Store \ No newline at end of file +/scripts/google-sheets/.clasp.json \ No newline at end of file