Skip to content

Commit 99a5fb4

Browse files
committed
refactor: some improvements
1 parent 1caeb50 commit 99a5fb4

File tree

12 files changed

+67
-60
lines changed

12 files changed

+67
-60
lines changed

helpers/api.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const {google} = require('googleapis');
22

3+
/**
4+
* Create google api credentials
5+
*
6+
* @returns {object} google.chat
7+
*/
38
function gAuth() {
49
// Use default credentials (service account)
510
const credentials = new google.auth.GoogleAuth({
@@ -22,13 +27,13 @@ async function callMessageApi(action, request) {
2227
const chatApi = gAuth();
2328
console.log('gapi request', JSON.stringify(request));
2429
let response;
25-
if (action === 'create')
30+
if (action === 'create') {
2631
response = await chatApi.spaces.messages.create(request);
27-
else if (action === 'update')
32+
} else if (action === 'update') {
2833
response = await chatApi.spaces.messages.update(request);
29-
else if (action === 'get')
34+
} else if (action === 'get') {
3035
response = await chatApi.spaces.messages.get(request);
31-
36+
}
3237
console.log('gapi response', JSON.stringify(response));
3338
return response;
3439
}

helpers/option.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ function addOptionToState(option, state, creator = '') {
1212
if (state.choiceCreator === undefined) {
1313
state.choiceCreator = {[choiceLength]: creator};
1414
}
15-
1615
}
1716

1817
exports.addOptionToState = addOptionToState;

helpers/response.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ function buildActionResponse(message, status = 'OK') {
2121
},
2222
};
2323
}
24+
25+
exports.buildActionResponse = buildActionResponse;

helpers/vote.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@
99
* @returns {object} Map of cast votes keyed by choice index
1010
*/
1111
function saveVotes(choice, voter, votes, isAnonymous = false) {
12-
13-
Object.keys(votes).forEach(function(choice_index) {
14-
if (votes[choice_index]) {
15-
const existed = votes[choice_index].findIndex(x => x.uid === voter.uid);
12+
Object.keys(votes).forEach(function(choiceIndex) {
13+
if (votes[choiceIndex]) {
14+
const existed = votes[choiceIndex].findIndex((x) => x.uid === voter.uid);
1615
if (existed > -1) {
17-
votes[choice_index].splice(existed, 1);
16+
votes[choiceIndex].splice(existed, 1);
1817
}
1918
}
20-
2119
});
2220
if (isAnonymous) {
2321
delete voter.name;
2422
}
25-
if (votes[choice])
23+
if (votes[choice]) {
2624
votes[choice].push(voter);
27-
else
25+
} else {
2826
votes[choice] = [voter];
27+
}
2928

3029
return votes;
3130
}
@@ -65,8 +64,9 @@ function choiceSection(i, poll, totalVotes, state, creator = '') {
6564
poll.votes[i] = [];
6665
}
6766
const choiceTag = choice(i, poll.choices[i], poll.votes[i].length, totalVotes, state);
68-
if(creator)
69-
choiceTag.decoratedText.topLabel = 'Added by '+creator
67+
if (creator) {
68+
choiceTag.decoratedText.topLabel = 'Added by '+creator;
69+
}
7070
const section = {
7171
'widgets': [choiceTag],
7272
};
@@ -75,7 +75,7 @@ function choiceSection(i, poll, totalVotes, state, creator = '') {
7575
section.uncollapsibleWidgetsCount = 1;
7676
section.widgets.push({
7777
'textParagraph': {
78-
'text': poll.votes[i].map(u => u.name).join(', '),
78+
'text': poll.votes[i].map((u) => u.name).join(', '),
7979
},
8080
});
8181
}

index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
const {buildConfigurationForm, MAX_NUM_OF_OPTIONS} = require('./config-form');
2-
const {buildVoteCard} = require('./vote-card');
1+
const {buildConfigurationForm, MAX_NUM_OF_OPTIONS} = require('./src/config-form');
2+
const {buildVoteCard} = require('./src/vote-card');
33
const {saveVotes} = require('./helpers/vote');
4-
const {buildAddOptionForm} = require('./add-option-form');
4+
const {buildAddOptionForm} = require('./src/add-option-form');
55
const {callMessageApi} = require('./helpers/api');
66
const {addOptionToState} = require('./helpers/option');
7+
const {buildActionResponse} = require('./helpers/response');
78

89
/**
910
* App entry point.
11+
* @param {object} req - chat event
12+
* @param {object} res - chat event
13+
* @returns {void}
1014
*/
1115
exports.app = async (req, res) => {
1216
if (!(req.method === 'POST' && req.body)) {
@@ -75,7 +79,7 @@ async function startPoll(event) {
7579
// Get the form values
7680
const formValues = event.common?.formInputs;
7781
const topic = formValues?.['topic']?.stringInputs.value[0]?.trim();
78-
const is_anonymous = formValues?.['is_anonymous']?.stringInputs.value[0] ===
82+
const isAnonymous = formValues?.['is_anonymous']?.stringInputs.value[0] ===
7983
'1';
8084
const choices = [];
8185
const votes = {};
@@ -113,7 +117,7 @@ async function startPoll(event) {
113117
author: event.user,
114118
choices: choices,
115119
votes: votes,
116-
anon: is_anonymous,
120+
anon: isAnonymous,
117121
});
118122
const message = {
119123
cardsV2: [pollCard],
@@ -162,13 +166,13 @@ function recordVote(event) {
162166
*
163167
* @param {object} event the event object from Google Chat.
164168
*
165-
* @return {object} open a dialog.
169+
* @returns {object} open a dialog.
166170
*/
167171
function addOptionForm(event) {
168-
169-
const stateJson = event.message.cardsV2[0].card.sections[0].widgets[0].decoratedText.button.onClick.action.parameters[0].value;
172+
const card = event.message.cardsV2[0].card;
173+
const stateJson = card.sections[0].widgets[0].decoratedText.button.onClick.action.parameters[0].value;
170174
const state = JSON.parse(stateJson);
171-
const dialog = buildAddOptionForm(state, event.message.thread);
175+
const dialog = buildAddOptionForm(state);
172176
return {
173177
actionResponse: {
174178
type: 'DIALOG',
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ function buildAddOptionForm(state) {
3636
{
3737
key: 'state',
3838
value: JSON.stringify(state),
39-
}
40-
],
39+
}],
4140
},
4241
},
4342
},
File renamed without changes.

vote-card.js renamed to src/vote-card.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {choiceSection} = require('./helpers/vote');
1+
const {choiceSection} = require('../helpers/vote');
22

33
/**
44
* Builds the card header including the question and author details.
@@ -35,7 +35,7 @@ function buildVoteCard(poll) {
3535
return sum + vote.length;
3636
}, 0);
3737
for (let i = 0; i < poll.choices.length; ++i) {
38-
const creator = poll.choiceCreator?.[i]
38+
const creator = poll.choiceCreator?.[i];
3939
const section = choiceSection(i, poll, totalVotes, state, creator);
4040
sections.push(section);
4141
}

tests/add-option-form.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {buildAddOptionForm} = require('../add-option-form');
1+
const {buildAddOptionForm} = require('../src/add-option-form');
22
const {addOptionToState} = require('../helpers/option');
33
const {dummyPoll} = require('./dummy');
44
const json = require('./json/add_option_form.json');
@@ -7,7 +7,6 @@ test('build add option form', () => {
77
const dialog = buildAddOptionForm({
88
topic: 'Who is the most handsome AI?',
99
});
10-
const json = require('./json/add_option_form.json');
1110
expect(dialog).toStrictEqual(json);
1211
});
1312

tests/config-form.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const {buildConfigurationForm} = require('../config-form');
1+
const {buildConfigurationForm} = require('../src/config-form');
22

33
test('build configuration form', () => {
44
const dialog = buildConfigurationForm({
5-
topic: "Who is the most handsome AI?",
5+
topic: 'Who is the most handsome AI?',
66
choices: [],
77
});
8-
const json = require('./json/configuration_form.json')
8+
const json = require('./json/configuration_form.json');
99
expect(dialog).toStrictEqual(json);
1010
});

0 commit comments

Comments
 (0)