diff --git a/README.md b/README.md index 2ece100..98dda73 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Note the format of the date (`YYYY-MM-DD`) and the fact that multiple chores can ##### Service account -Create a service account using [these instructions](https://developers.google.com/android/management/service-account). Once it's created, share the previously created spreadsheet with the service account's address. View-only permissions should be fine; we're only reading data from the sheet. +Create a service account using [these instructions](https://developers.google.com/android/management/service-account). Once it's created, share the previously created spreadsheet with the service account's address. You will need both Read and Write permissions (`https://www.googleapis.com/auth/spreadsheets`) for this to work. #### Your server diff --git a/server.js b/server.js index e2d9af3..2a8dad3 100644 --- a/server.js +++ b/server.js @@ -46,7 +46,7 @@ app.action( }, async ({ action, body }) => { if (!action || !body || !body.user || !body.channel || !body.message) return; - markChoreDone(action.action_id, body.user.id, body.channel.id, body.message.ts, + await markChoreDone(action.action_id, body.user, body.channel.id, body.message.ts, body.message.blocks); } ); diff --git a/utilities/getChores.js b/utilities/getChores.js index 04890c2..b43ac1b 100644 --- a/utilities/getChores.js +++ b/utilities/getChores.js @@ -1,4 +1,4 @@ -//node pacakges +//node packages const { google } = require("googleapis"); const moment = require("moment-timezone"); require("dotenv").config(); diff --git a/utilities/markChoreDone.js b/utilities/markChoreDone.js index d2bf5f9..6ed4dd1 100644 --- a/utilities/markChoreDone.js +++ b/utilities/markChoreDone.js @@ -1,26 +1,86 @@ +//node packages +const {google} = require("googleapis"); +require("dotenv").config(); + //local packages const { - app: { - client: { - chat: { update } + app: { + client: { + chat: { update } + } } - } } = require("./bolt.js"); +const privatekey = require("../keys/sheets-api.json"); //globals const TOKEN = process.env.SLACK_BOT_TOKEN; +const SPREADSHEET_ID = process.env.SPREADSHEET_ID; +const GDRIVE_EMAIL = process.env.GDRIVE_EMAIL; + +const jwtClient = new google.auth.JWT( + privatekey.client_email, + null, + privatekey.private_key, + ["https://www.googleapis.com/auth/spreadsheets"], + GDRIVE_EMAIL +); + +const setCompletedBy = async (user, date, choretext) => { + const sheets = google.sheets({ + version: "v4", + auth: jwtClient + }); + + jwtClient.authorize(err => console.log(err ? err : "Successfully connected!")); + + try { + const {data} = await sheets.spreadsheets.values.get({ + spreadsheetId: SPREADSHEET_ID, + range: "A2:B" + }); + for (let n = 0; n < data.values.length; n++) { + if (data.values[n][1] === choretext && data.values[n][0] === date) { + await sheets.spreadsheets.values.update({ + spreadsheetId: SPREADSHEET_ID, + range: `Sheet1!C${n + 2}:D${n + 2}`, + valueInputOption: 'USER_ENTERED', + resource: {values: [[user.name, user.id]]} + }); + break; + } + } + } catch (error) { + console.error(error); + } +}; //helper functions const crossOffAndTag = (user, index, blocks) => { - const choreText = blocks[index + 2].text.text.replace(">", ""); - blocks[index + 2].text.text = `>~${choreText}~ Completed by <@${user}>`; - delete blocks[index + 2].accessory; - return blocks; + const choreText = blocks[index + 2].text.text.replace(">", ""); + blocks[index + 2].text.text = `>~${choreText}~ Completed by <@${user}>`; + delete blocks[index + 2].accessory; + return blocks; +}; + +const markChoreDone = async (index, user, channel, ts, initialBlocks) => { + await setCompletedBy(user, formatDate(new Date(ts * 1000)), initialBlocks[parseInt(index) + 2] + .text.text.replace(">", "")); + const blocks = crossOffAndTag(user.id, parseInt(index), initialBlocks); + update({ token: TOKEN, channel, ts, blocks }); }; -const markChoreDone = (index, user, channel, ts, initialBlocks) => { - const blocks = crossOffAndTag(user, parseInt(index), initialBlocks); - update({ token: TOKEN, channel, ts, blocks }); +const formatDate = (date) => { + var d = new Date(date), + month = '' + (d.getMonth() + 1), + day = '' + d.getDate(), + year = d.getFullYear(); + + if (month.length < 2) + month = '0' + month; + if (day.length < 2) + day = '0' + day; + + return [year, month, day].join('-'); }; module.exports = { markChoreDone }; diff --git a/utilities/notify.js b/utilities/notify.js index 3458851..44ab42c 100644 --- a/utilities/notify.js +++ b/utilities/notify.js @@ -51,7 +51,7 @@ const postToSlack = chores => postSlackMessage("Today's chores have been posted! buildMarkdownSection(randomPhrase("greeting")), buildMarkdownSection("Tonight's chores:"), ...chores.map((c, i) => buildChoreElement(c, i)), - buildMarkdownSection(randomPhrase("request") + " If you have any questions " + + buildMarkdownSection(randomPhrase("request") + " If you have any questions " + ":thinking_face: please reach out to the vice president!") ]);