Completed
Push — master ( 4e1210...2759f8 )
by Muhammad Dyas
16s queued 13s
created

handlers.js   A

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 47
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 28
mnd 0
bc 0
fnc 2
dl 0
loc 47
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A ➔ updateWinnerCardHandler 0 12 1
A ➔ createMessageFromNameListHandler 0 19 1
1
import {buildMessageBody, buildNameListSection, buildNameListWinnerSection} from './src/helpers/components.js';
2
import {createMessage, updateMessage} from './src/helpers/api.js';
3
import {getRandomWinners} from './src/helpers/utils.js';
4
import {delayUpdateMessage} from './src/helpers/task.js';
5
6
/**
7
 * @param {object} requestBody - list of names
8
 * @returns {Promise<void>} update message
9
 */
10
export async function updateWinnerCardHandler(requestBody) {
11
  const names = requestBody.names;
12
  const winner = getRandomWinners(names);
13
  const messageText = `Congratulations! We have shuffled the list of names and the winner is *${winner.join(',')}*.`;
14
  const message = buildMessageBody(buildNameListWinnerSection(names, winner), messageText);
15
  const request = {
16
    name: requestBody.messageId,
17
    requestBody: message,
18
    updateMask: 'text,cardsV2',
19
  };
20
  await updateMessage(request);
21
}
22
23
/**
24
 * @param {array} names - list of name that will be shuffled
25
 * @param {string} space - google chat space name
26
 * @param {string} thread - chat thread/parent
27
 * @returns {Promise<void>} will post the message to google API
28
 */
29
export async function createMessageFromNameListHandler(names, space, thread = null) {
30
  const cardSection = buildNameListSection(names);
31
  const message = buildMessageBody(cardSection);
32
33
  const request = {
34
    parent: space,
35
    threadKey: thread,
36
    requestBody: message,
37
    messageReplyOption: 'REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD',
38
  };
39
  const apiResponse = await createMessage(request);
40
41
  const messageId = apiResponse.data.name;
42
  const payload = {
43
    messageId,
44
    names: names,
45
  };
46
  await delayUpdateMessage(JSON.stringify(payload));
47
}
48