Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Completed-By Column To Google Spreadsheets #15

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
Expand Down
2 changes: 1 addition & 1 deletion utilities/getChores.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//node pacakges
//node packages
zapdos26 marked this conversation as resolved.
Show resolved Hide resolved
const { google } = require("googleapis");
const moment = require("moment-timezone");
require("dotenv").config();
Expand Down
82 changes: 71 additions & 11 deletions utilities/markChoreDone.js
Original file line number Diff line number Diff line change
@@ -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;
zapdos26 marked this conversation as resolved.
Show resolved Hide resolved
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++) {
ddbruce marked this conversation as resolved.
Show resolved Hide resolved
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("&gt;", "");
blocks[index + 2].text.text = `>~${choreText}~ Completed by <@${user}>`;
delete blocks[index + 2].accessory;
return blocks;
const choreText = blocks[index + 2].text.text.replace("&gt;", "");
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("&gt;", ""));
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 };
2 changes: 1 addition & 1 deletion utilities/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
]);

Expand Down