From 9790b58efdd0058c5fb9a7cb46a9ae12a82c98c3 Mon Sep 17 00:00:00 2001 From: Spazcool Date: Thu, 14 Dec 2017 13:07:35 +0800 Subject: [PATCH 01/14] REFACTOR: logScanned to catch doubled UUIDs & render doubles on template --- app.js | 26 +++++++++++++++++++------- views/recent.ejs | 12 +++++++++++- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/app.js b/app.js index e63ab03..2a602c0 100644 --- a/app.js +++ b/app.js @@ -8,18 +8,30 @@ const app = express(); const allScans = []; -function logScanned(uuid, fixture) { +function logScanned(uuid, allMatches) { + const now = new Date(); // TRICK: fixture tells if the the uuid was found in database or not if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(uuid)) { return; } - const now = new Date(); - if (!fixture) { - allScans.unshift({ time: now, status: 'missing', uuid }); + allScans.map(item => item.status = (item.uuid === uuid) ? 'fixed' : item.status); + if (allMatches.length > 1) { + allMatches.map((item, index) => + allScans.unshift({ + time: now, + fixture: allMatches[index].fixture ? allMatches[index].fixture : '', + status: allMatches[index].fixture ? '' : 'missing', + uuid, + double: true, + })); return; } - allScans.map(item => item.status = (item.uuid === uuid) ? 'fixed' : item.status); - allScans.unshift({ time: now, fixture, uuid }); + allScans.unshift({ + time: now, + fixture: allMatches[0].fixture ? allMatches[0].fixture : '', + status: allMatches[0].fixture ? '' : 'missing', + uuid, + }); } app.set('view engine', 'ejs'); @@ -65,7 +77,7 @@ app.get('/:uuid', (req, res) => { }); return; } - logScanned(req.params.uuid, matches[0].fixture); + logScanned(req.params.uuid, matches); matches[0].similarItems = searchDatabase({ fixture: matches[0].fixture }, allItems) .filter(item => item.uuid !== matches[0].uuid) .splice(0, 3); diff --git a/views/recent.ejs b/views/recent.ejs index 372cf57..973e3c4 100644 --- a/views/recent.ejs +++ b/views/recent.ejs @@ -3,11 +3,16 @@

Recently scanned QR code

+
<% include ./partials/footer %> From 93da255863d6ce2f9cabc92cd73237dedfa5403f Mon Sep 17 00:00:00 2001 From: Spazcool Date: Wed, 20 Dec 2017 03:20:41 +0800 Subject: [PATCH 06/14] ADD: double UUID conditional without duplicate code --- views/recent.ejs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/views/recent.ejs b/views/recent.ejs index ca2b0b1..dc1f5bd 100644 --- a/views/recent.ejs +++ b/views/recent.ejs @@ -1,21 +1,21 @@ <% include ./partials/header %> -

Recently scanned QR code

+

Recently scanned QR codes

From cae791df155749d80e8470c86dc9cb1ef9bff900 Mon Sep 17 00:00:00 2001 From: Spazcool Date: Wed, 20 Dec 2017 06:03:01 +0800 Subject: [PATCH 07/14] ADD: double UUID conditional without duplicate code --- app.js | 4 ++-- googleSpreadsheet.js | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index fa17176..82580c7 100644 --- a/app.js +++ b/app.js @@ -40,8 +40,8 @@ app.get(['/favicon.ico', '/robots.txt'], (req, res) => { app.get('/search', (req, res) => { loadDatabase((allItems) => { res.render('search', { - matches: searchDatabase(req.query, allItems) - .sort((a, b) => (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1)), + matches: searchDatabase(req.query, allItems).sort((a, b) => + (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1)), }); }); }); diff --git a/googleSpreadsheet.js b/googleSpreadsheet.js index 4a16a68..f32210d 100644 --- a/googleSpreadsheet.js +++ b/googleSpreadsheet.js @@ -1,11 +1,12 @@ const google = require('googleapis'); const keys = require('./config/keys'); -function rowToObject(val, lab) { +function rowToObject(val, lab, index) { const o = {}; for (let i = 0; i < lab.length; i += 1) { o[lab[i]] = val[i]; } + o.cellRef = 'https://docs.google.com/spreadsheets/d/1QHKa3vUpht7zRl_LEzl3BlUbolz3ZiL8yKHzdBL42dY/edit#gid=0&range=A' + index; return o; } @@ -20,8 +21,8 @@ function loadDatabase(callback) { console.log(`The API returned an error: ${err}`); return; } - return callback(response.values.map(row => - rowToObject(row, response.values[0])).splice(1)); + return callback(response.values.map((row, index) => + rowToObject(row, response.values[0], index)).splice(1)); }); } From 5b379120f875d8b6a520a5781ff350bc9c792b40 Mon Sep 17 00:00:00 2001 From: Spazcool Date: Wed, 20 Dec 2017 06:46:07 +0800 Subject: [PATCH 08/14] ADD: link to item row in spreadsheet to both item obj and recent.ejs --- app.js | 4 +++- googleSpreadsheet.js | 4 ++-- views/recent.ejs | 15 +++++++++------ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index 82580c7..8bd2a6c 100644 --- a/app.js +++ b/app.js @@ -26,6 +26,7 @@ function logScanned(uuid, matches) { status: allMatches[0].fixture ? '' : 'missing', uuid, double: allMatches[0].double, + link: allMatches[0].cellRef, }); } @@ -39,9 +40,10 @@ app.get(['/favicon.ico', '/robots.txt'], (req, res) => { app.get('/search', (req, res) => { loadDatabase((allItems) => { + console.log(allItems); res.render('search', { matches: searchDatabase(req.query, allItems).sort((a, b) => - (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1)), + (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1)), }); }); }); diff --git a/googleSpreadsheet.js b/googleSpreadsheet.js index f32210d..d481338 100644 --- a/googleSpreadsheet.js +++ b/googleSpreadsheet.js @@ -6,7 +6,7 @@ function rowToObject(val, lab, index) { for (let i = 0; i < lab.length; i += 1) { o[lab[i]] = val[i]; } - o.cellRef = 'https://docs.google.com/spreadsheets/d/1QHKa3vUpht7zRl_LEzl3BlUbolz3ZiL8yKHzdBL42dY/edit#gid=0&range=A' + index; + o.cellRef = 'https://docs.google.com/spreadsheets/d/1QHKa3vUpht7zRl_LEzl3BlUbolz3ZiL8yKHzdBL42dY/edit#gid=0&range=A' + index + ':T' + index; return o; } @@ -22,7 +22,7 @@ function loadDatabase(callback) { return; } return callback(response.values.map((row, index) => - rowToObject(row, response.values[0], index)).splice(1)); + rowToObject(row, response.values[0], index + 1)).splice(1)); }); } diff --git a/views/recent.ejs b/views/recent.ejs index dc1f5bd..e95b2b1 100644 --- a/views/recent.ejs +++ b/views/recent.ejs @@ -4,16 +4,19 @@
    <% for (var i = 0; i < allScans.length; i++) { %>
  • + <%- allScans[i].time.toTimeString() %> - <% if (allScans[i].fixture) { %> - <%- allScans[i].time.toTimeString() %> - <%- allScans[i].fixture %> :
    + <%- allScans[i].fixture %> <% } else { %> - <%- allScans[i].time.toTimeString() %> - Missing Details! :
    + ??? + <% } %> + :
    + <%- allScans[i].uuid %> + <% if (!allScans[i].fixture) { %> + Missing Details! <% } %> - <%- allScans[i].uuid %> <% if (allScans[i].double) { %> - - Duplicate UUID! - + Duplicate UUID! <% } %>
  • <% } %> From 6f7a97f4a32583a1a62f0719eed786c3f2a504ae Mon Sep 17 00:00:00 2001 From: mpsido Date: Wed, 20 Dec 2017 20:45:46 +0800 Subject: [PATCH 09/14] Refactoring/simplifying code googleSpreadsheet.js --- googleSpreadsheet.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/googleSpreadsheet.js b/googleSpreadsheet.js index d481338..f673677 100644 --- a/googleSpreadsheet.js +++ b/googleSpreadsheet.js @@ -1,13 +1,14 @@ const google = require('googleapis'); const keys = require('./config/keys'); -function rowToObject(val, lab, index) { - const o = {}; - for (let i = 0; i < lab.length; i += 1) { - o[lab[i]] = val[i]; +// creates a dictionary mapping column names with the values +// ex: { floor : 402, business : coworking, etc..} +function spreadsheetValuesToObject(values, columns) { + const formatedRow = {}; + for (let i = 0; i < columns.length; i += 1) { + formatedRow[columns[i]] = values[i]; } - o.cellRef = 'https://docs.google.com/spreadsheets/d/1QHKa3vUpht7zRl_LEzl3BlUbolz3ZiL8yKHzdBL42dY/edit#gid=0&range=A' + index + ':T' + index; - return o; + return formatedRow; } function loadDatabase(callback) { @@ -21,8 +22,10 @@ function loadDatabase(callback) { console.log(`The API returned an error: ${err}`); return; } - return callback(response.values.map((row, index) => - rowToObject(row, response.values[0], index + 1)).splice(1)); + const columns = response.values[0]; + // map function transforms a list into another list using the given lambda function + const formatedRows = response.values.map(row => spreadsheetValuesToObject(row, columns)); + return callback(formatedRows); }); } From 1c1d78ab0c3c2c9c4015d39041d1652dd5b8a927 Mon Sep 17 00:00:00 2001 From: mpsido Date: Wed, 20 Dec 2017 21:31:45 +0800 Subject: [PATCH 10/14] Removing hard url in app.js --- app.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 8bd2a6c..7b430bb 100644 --- a/app.js +++ b/app.js @@ -54,7 +54,7 @@ app.get('/qrlist', (req, res) => { .filter(item => item.uuid !== '') .filter(item => item.uuid !== undefined) .sort((a, b) => (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1)); - qrList.forEach(item => item.qr = qr.imageSync('http://url.coderbunker.com/' + item.uuid, { type: 'svg' })); + qrList.forEach(item => item.qr = qr.imageSync(item.uuid, { type: 'svg' })); res.render('qrList', { matches: qrList }); }); }); @@ -75,6 +75,9 @@ app.get('/:uuid', (req, res) => { return; } logScanned(req.params.uuid, matches); + if (matches.length >= 1) { + console.log(`Too much matches for uuid ${req.params.uuid} length = ${matches.length}`); + } matches[0].similarItems = searchDatabase({ fixture: matches[0].fixture }, allItems) .filter(item => item.uuid !== matches[0].uuid) .splice(0, 3); From d04c2cf048d633fe6906878e3ffdd1fdeed6af80 Mon Sep 17 00:00:00 2001 From: mpsido Date: Wed, 20 Dec 2017 23:55:13 +0800 Subject: [PATCH 11/14] Refactoring, addRecentlyScanned function instead of logScanned --- app.js | 36 +++++++++++++++++------------------- views/recent.ejs | 18 +++++++++--------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/app.js b/app.js index 7b430bb..11ac8f4 100644 --- a/app.js +++ b/app.js @@ -6,28 +6,26 @@ const { loadDatabase, searchDatabase } = require('./googleSpreadsheet'); const app = express(); -const allScans = []; +const allScans = new Map(); -function logScanned(uuid, matches) { - let allMatches = matches; - const now = new Date(); +function addRecentlyScanned(uuid, item, nbFound = 0) { // TRICK: only record uuids if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(uuid)) { return; } - allScans.map(item => item.status = (item.uuid === uuid) ? 'fixed' : item.status); - if (allMatches.length > 1) { - allMatches = allMatches.splice(0, 1); - allMatches[0].double = true; + + const duplicatedItem = item; + + duplicatedItem.time = new Date(); + duplicatedItem.duplicated = nbFound > 1; + + if (nbFound === 0) { + duplicatedItem.fixture = ''; + duplicatedItem.uuid = uuid; + duplicatedItem.status = 'missing'; } - allScans.unshift({ - time: now, - fixture: allMatches[0].fixture ? allMatches[0].fixture : '', - status: allMatches[0].fixture ? '' : 'missing', - uuid, - double: allMatches[0].double, - link: allMatches[0].cellRef, - }); + + allScans.set(uuid, duplicatedItem); } app.set('view engine', 'ejs'); @@ -67,17 +65,17 @@ app.get('/:uuid', (req, res) => { loadDatabase((allItems) => { const matches = searchDatabase(req.params, allItems); if (matches.length === 0) { - logScanned(req.params.uuid, [{ fixture: null }]); + addRecentlyScanned(req.params.uuid, {}); res.status(404).render('notFound', { item: '', id: req.params.uuid, }); return; } - logScanned(req.params.uuid, matches); - if (matches.length >= 1) { + if (matches.length > 1) { console.log(`Too much matches for uuid ${req.params.uuid} length = ${matches.length}`); } + addRecentlyScanned(req.params.uuid, matches[0], matches.length); matches[0].similarItems = searchDatabase({ fixture: matches[0].fixture }, allItems) .filter(item => item.uuid !== matches[0].uuid) .splice(0, 3); diff --git a/views/recent.ejs b/views/recent.ejs index e95b2b1..3d415a0 100644 --- a/views/recent.ejs +++ b/views/recent.ejs @@ -2,21 +2,21 @@

    Recently scanned QR codes

      - <% for (var i = 0; i < allScans.length; i++) { %> + <% for (let item of allScans.values()) { %>
    • - <%- allScans[i].time.toTimeString() %> - - <% if (allScans[i].fixture) { %> - <%- allScans[i].fixture %> + <%- item.time.toTimeString() %> - + <% if (item.fixture) { %> + <%- item.fixture %> <% } else { %> ??? <% } %> :
      - <%- allScans[i].uuid %> - <% if (!allScans[i].fixture) { %> - Missing Details! + <%- item.uuid %> + <% if (!item.fixture) { %> + Missing Details! <% } %> - <% if (allScans[i].double) { %> - Duplicate UUID! + <% if (item.duplicated) { %> + Duplicate UUID! <% } %>
    • <% } %> From 2b946e71c98cdd0d9b75a69934c7c8183b9107dd Mon Sep 17 00:00:00 2001 From: mpsido Date: Wed, 27 Dec 2017 19:07:51 +0800 Subject: [PATCH 12/14] Refactoring, making loadDatabase function invisible from app.js --- app.js | 60 +++++++++++++++++++++----------------------- googleSpreadsheet.js | 31 +++++++++++++++-------- 2 files changed, 49 insertions(+), 42 deletions(-) diff --git a/app.js b/app.js index a2dc626..d239746 100644 --- a/app.js +++ b/app.js @@ -3,7 +3,6 @@ const express = require('express'); const qr = require('qr-image'); const { - loadDatabase, searchDatabase, addMarkdown, addSimilarItems, @@ -13,53 +12,50 @@ const app = express(); const allScans = new Map(); +app.set('view engine', 'ejs'); + +app.use(express.static(`${__dirname}/public`)); + +app.get(['/favicon.ico', '/robots.txt'], (req, res) => { + res.sendStatus(204); +}); + + function addRecentlyScanned(uuid, item, nbFound = 0) { // TRICK: only record uuids if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(uuid)) { return; } - const duplicatedItem = item; - duplicatedItem.time = new Date(); duplicatedItem.duplicated = nbFound > 1; - if (nbFound === 0) { duplicatedItem.fixture = ''; duplicatedItem.uuid = uuid; duplicatedItem.status = 'missing'; } - allScans.set(uuid, duplicatedItem); } -app.set('view engine', 'ejs'); - -app.use(express.static(`${__dirname}/public`)); - -app.get(['/favicon.ico', '/robots.txt'], (req, res) => { - res.sendStatus(204); -}); - app.get('/search', (req, res) => { - loadDatabase((allItems) => { - console.log(allItems); - res.render('search', { - matches: searchDatabase(req.query, allItems).sort((a, b) => - (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1)), - }); - }); + searchDatabase(req.query, queryResult => res.render('search', { + matches: queryResult.sort((a, b) => (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1)), + })); }); app.get('/qrlist', (req, res) => { - loadDatabase((allItems) => { - const qrList = searchDatabase(req.query, allItems) - .filter(item => item.uuid !== '') + function renderQrList(qrList) { + const qrItems = qrList.filter(item => item.uuid !== '') .filter(item => item.uuid !== undefined) .sort((a, b) => (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1)); - qrList.forEach(item => item.qr = qr.imageSync(item.uuid, { type: 'svg' })); - res.render('qrList', { matches: qrList }); - }); + qrItems.forEach((item) => { + const itemWithQr = item; + itemWithQr.qr = qr.imageSync(item.uuid, { type: 'svg' }); + return itemWithQr; + }); + res.render('qrList', { matches: qrItems }); + } + searchDatabase(req.query, queryResult => renderQrList(queryResult)); }); app.get('/recent', (req, res) => { @@ -67,8 +63,8 @@ app.get('/recent', (req, res) => { }); app.get('/:uuid', (req, res) => { - loadDatabase((allItems) => { - const matches = searchDatabase(req.params, allItems); + function renderUuid(uuids) { + let matches = uuids; if (matches.length === 0) { addRecentlyScanned(req.params.uuid, {}); res.status(404).render('notFound', { @@ -80,11 +76,13 @@ app.get('/:uuid', (req, res) => { if (matches.length > 1) { console.log(`Too much matches for uuid ${req.params.uuid} length = ${matches.length}`); } - addMarkdown(matches); - addSimilarItems(matches, allItems); + // copying twice just to remove a lint warning (no-param-reassign): bad bad bad ! + matches = addMarkdown(matches); + matches = addSimilarItems(matches); addRecentlyScanned(req.params.uuid, matches[0], matches.length); res.render('item', matches); - }); + } + searchDatabase(req.params, queryResult => renderUuid(queryResult)); }); app.get('/', (req, res) => { diff --git a/googleSpreadsheet.js b/googleSpreadsheet.js index 1af81f8..f81233f 100644 --- a/googleSpreadsheet.js +++ b/googleSpreadsheet.js @@ -2,6 +2,9 @@ const google = require('googleapis'); const keys = require('./config/keys'); const marked = require('marked'); + +let loadedItems = []; + // creates a dictionary mapping column names with the values // ex: { floor : 402, business : coworking, etc..} function spreadsheetValuesToObject(values, columns) { @@ -24,28 +27,35 @@ function loadDatabase(callback) { return; } const columns = response.values[0]; - // map function transforms a list into another list using the given lambda function - const formatedRows = response.values.map(row => spreadsheetValuesToObject(row, columns)); - return callback(formatedRows); + loadedItems = response.values.map(row => spreadsheetValuesToObject(row, columns)); + return callback(); }); } -function searchDatabase(query, rows) { - let matches = rows; +function searchDatabase(query, callback) { + if (loadedItems.length === 0) { + // if the database was not loaded load it then recall this function with the same parameters + // warning, this causes an infinite loop if the spreadsheet is empty or unreachable + loadDatabase(() => searchDatabase(query, callback)); + return; + } + let queryResult = loadedItems; Object.keys(query).map((key) => { - matches = matches.filter(item => item[key] === query[key]); + queryResult = loadedItems.filter(item => item[key] === query[key]); }); - return matches; + return callback(queryResult); } -function addSimilarItems(obj, allObj) { - obj.similarItems = searchDatabase({ fixture: obj.fixture }, allObj) +function addSimilarItems(items) { + const obj = items; + obj.similarItems = searchDatabase({ fixture: obj.fixture }, loadedItems) .filter(item => item.uuid !== obj.uuid) .splice(0, 3); return obj; } -function addMarkdown(obj) { +function addMarkdown(items) { + const obj = items; obj.HOWTO = marked(obj.HOWTO); obj.details = marked(obj.details); obj.Troubleshooting = marked(obj.Troubleshooting); @@ -53,7 +63,6 @@ function addMarkdown(obj) { } module.exports = { - loadDatabase, searchDatabase, addMarkdown, addSimilarItems, From c9a83ff329aa5e1a4da5df034f4c32a691052b93 Mon Sep 17 00:00:00 2001 From: mpsido Date: Thu, 28 Dec 2017 03:50:15 +0800 Subject: [PATCH 13/14] Fixing regressions --- app.js | 19 ++++++++++--------- googleSpreadsheet.js | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app.js b/app.js index d239746..eed7a33 100644 --- a/app.js +++ b/app.js @@ -63,9 +63,9 @@ app.get('/recent', (req, res) => { }); app.get('/:uuid', (req, res) => { - function renderUuid(uuids) { - let matches = uuids; - if (matches.length === 0) { + function renderUuid(uuidsList) { + let uuids = uuidsList[0]; + if (uuidsList.length === 0) { addRecentlyScanned(req.params.uuid, {}); res.status(404).render('notFound', { item: '', @@ -73,14 +73,15 @@ app.get('/:uuid', (req, res) => { }); return; } - if (matches.length > 1) { - console.log(`Too much matches for uuid ${req.params.uuid} length = ${matches.length}`); + console.log(uuidsList.length); + if (uuidsList.length > 1) { + console.log(`Too much matches for uuid ${req.params.uuid} length = ${uuidsList.length}`); } // copying twice just to remove a lint warning (no-param-reassign): bad bad bad ! - matches = addMarkdown(matches); - matches = addSimilarItems(matches); - addRecentlyScanned(req.params.uuid, matches[0], matches.length); - res.render('item', matches); + uuids = addMarkdown(uuids); + uuids = addSimilarItems(uuids); + addRecentlyScanned(req.params.uuid, uuids, uuidsList.length); + res.render('item', uuids); } searchDatabase(req.params, queryResult => renderUuid(queryResult)); }); diff --git a/googleSpreadsheet.js b/googleSpreadsheet.js index f81233f..9aa778e 100644 --- a/googleSpreadsheet.js +++ b/googleSpreadsheet.js @@ -48,7 +48,7 @@ function searchDatabase(query, callback) { function addSimilarItems(items) { const obj = items; - obj.similarItems = searchDatabase({ fixture: obj.fixture }, loadedItems) + obj.similarItems = loadedItems.filter(item => item.fixture === obj.fixture) .filter(item => item.uuid !== obj.uuid) .splice(0, 3); return obj; From a7622cb329136b33d93701c784607e845ae87dfd Mon Sep 17 00:00:00 2001 From: mpsido Date: Thu, 28 Dec 2017 05:34:51 +0800 Subject: [PATCH 14/14] Duplicate id link back to spreadsheet --- app.js | 2 +- googleSpreadsheet.js | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index eed7a33..220e887 100644 --- a/app.js +++ b/app.js @@ -29,6 +29,7 @@ function addRecentlyScanned(uuid, item, nbFound = 0) { const duplicatedItem = item; duplicatedItem.time = new Date(); duplicatedItem.duplicated = nbFound > 1; + duplicatedItem.link = item.cellRef; if (nbFound === 0) { duplicatedItem.fixture = ''; duplicatedItem.uuid = uuid; @@ -73,7 +74,6 @@ app.get('/:uuid', (req, res) => { }); return; } - console.log(uuidsList.length); if (uuidsList.length > 1) { console.log(`Too much matches for uuid ${req.params.uuid} length = ${uuidsList.length}`); } diff --git a/googleSpreadsheet.js b/googleSpreadsheet.js index 9aa778e..60e4cd4 100644 --- a/googleSpreadsheet.js +++ b/googleSpreadsheet.js @@ -2,16 +2,18 @@ const google = require('googleapis'); const keys = require('./config/keys'); const marked = require('marked'); - +const spreadsheetDataId = '1QHKa3vUpht7zRl_LEzl3BlUbolz3ZiL8yKHzdBL42dY'; +const spreadsheetLink = `https://docs.google.com/spreadsheets/d/${spreadsheetDataId}/edit`; let loadedItems = []; // creates a dictionary mapping column names with the values // ex: { floor : 402, business : coworking, etc..} -function spreadsheetValuesToObject(values, columns) { +function spreadsheetValuesToObject(values, columns, index) { const formatedRow = {}; for (let i = 0; i < columns.length; i += 1) { formatedRow[columns[i]] = values[i]; } + formatedRow.cellRef = `${spreadsheetLink}#gid=0&range=A${index}:T${index}`; return formatedRow; } @@ -19,7 +21,7 @@ function loadDatabase(callback) { const sheets = google.sheets('v4'); sheets.spreadsheets.values.get({ auth: keys.apiKey, - spreadsheetId: '1QHKa3vUpht7zRl_LEzl3BlUbolz3ZiL8yKHzdBL42dY', + spreadsheetId: spreadsheetDataId, range: 'Agora inventory!A:Z', }, (err, response) => { if (err) { @@ -27,7 +29,11 @@ function loadDatabase(callback) { return; } const columns = response.values[0]; - loadedItems = response.values.map(row => spreadsheetValuesToObject(row, columns)); + let i = 0; + loadedItems = response.values.map((row) => { + i += 1; + return spreadsheetValuesToObject(row, columns, i); + }); return callback(); }); }