Completed
Push — master ( c98494...fb1643 )
by Muhammad Dyas
28s queued 16s
created

handlers.js ➔ helpCommandHandler   A

Complexity

Conditions 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 17
rs 10
c 0
b 0
f 0
cc 1
1
import {buildMessageBody, buildNameListSection, buildNameListWinnerSection} from './helpers/components.js';
2
import {createMessage, updateMessage} from './helpers/api.js';
3
import {getRandomWinners} from './helpers/utils.js';
4
import {delayUpdateMessage} from './helpers/task.js';
5
import {buildActionResponse} from './helpers/response.js';
6
7
/**
8
 * @param {object} requestBody - list of names
9
 * @returns {Promise<void>} update message
10
 */
11
export async function updateWinnerCardHandler(requestBody) {
12
  const names = requestBody.names;
13
  const winner = getRandomWinners(names);
14
  const messageText = `Congratulations! We have shuffled the list of names and the winner is *${winner.join(',')}*.`;
15
  const message = buildMessageBody(buildNameListWinnerSection(names, winner), messageText);
16
  const request = {
17
    name: requestBody.messageId,
18
    requestBody: message,
19
    updateMask: 'text,cardsV2',
20
  };
21
  await updateMessage(request);
22
}
23
24
/**
25
 * @param {array} names - list of name that will be shuffled
26
 * @param {string} space - google chat space name
27
 * @param {string} thread - chat thread/parent
28
 * @returns {Promise<void>} will post the message to google API
29
 */
30
export async function createMessageFromNameListHandler(names, space, thread = null) {
31
  const cardSection = buildNameListSection(names);
32
  const message = buildMessageBody(cardSection);
33
34
  const request = {
35
    parent: space,
36
    threadKey: thread,
37
    requestBody: message,
38
    messageReplyOption: 'REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD',
39
  };
40
  const apiResponse = await createMessage(request);
41
42
  const messageId = apiResponse.data.name;
43
  const payload = {
44
    messageId,
45
    names: names,
46
  };
47
  await delayUpdateMessage(JSON.stringify(payload));
48
}
49
50
/**
51
 * @param {object} event - request body
52
 * @returns {{actionResponse: {type: string}, text: string}} - new message action response
53
 */
54
export function helpCommandHandler(event) {
55
  const message = `Hi ${event.user.displayName}
56
  Here are the list of available commands:
57
  */shuffle* Opens a dialog where you can input the items/names to be shuffled. By default, it is pre-filled with the list of members in the current space.
58
  */shuffle_members* Quickly shuffle all members of the current space.
59
  */config* Displays the current configuration dialog.
60
  
61
  You can also shuffle by mentioning this app, for example: *@Shuffle Maven "Zubair Manta" "Hasan Star" "Kawasaki Honda"* 
62
  `;
63
  return {
64
    thread: event.message.thread,
65
    actionResponse: {
66
      type: 'NEW_MESSAGE',
67
    },
68
    text: message,
69
  };
70
}
71
72
/**
73
 *
74
 * @param {object} event - request body
75
 * @returns {{actionResponse: {type: string}}|{actionResponse: {type: string}}} - dialog action response
76
 */
77
export function configCommandHandler(event) {
0 ignored issues
show
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
78
  const message = {
79
    'sections': [
80
      {
81
        'widgets': [
82
          {
83
            'textParagraph': {
84
              'text': 'Here are the current settings of your space. Please note that you cannot update these settings directly. If you need to make changes, please contact us at [email protected] for assistance.',
85
            },
86
          },
87
          {
88
            'textInput': {
89
              'label': 'Shuffle delay (in seconds)',
90
              'type': 'SINGLE_LINE',
91
              'name': 'fieldName',
92
              'hintText': 'The delay before the items/names shuffled',
93
              'value': '10',
94
            },
95
          },
96
          {
97
            'textInput': {
98
              'label': 'Default shuffle count',
99
              'type': 'SINGLE_LINE',
100
              'name': 'shuffle_count',
101
              'hintText': '',
102
              'value': '1',
103
            },
104
          },
105
        ],
106
      },
107
    ],
108
  };
109
  return buildActionResponse('DIALOG', message);
110
}
111