Completed
Push — master ( 8e01eb...25ec60 )
by Muhammad Dyas
17s queued 12s
created

src/helpers/state.ts   A

Complexity

Total Complexity 34
Complexity/F 4.86

Size

Lines of Code 65
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 34
eloc 46
mnd 27
bc 27
fnc 7
dl 0
loc 65
rs 9.68
bpm 3.8571
cpm 4.8571
noi 0
c 0
b 0
f 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
C state.ts ➔ getStateFromCardWhenHasHeader 0 4 10
A state.ts ➔ addOptionToState 0 16 2
A state.ts ➔ getStateFromCardName 0 4 2
A state.ts ➔ getStateFromCard 0 5 4
A state.ts ➔ getStateFromParameter 0 5 3
C state.ts ➔ getStateFromCardWhenNoHeader 0 3 10
A state.ts ➔ getConfigFromInput 0 16 3
1
import {ClosableType, PollConfig, PollFormInputs, PollState} from './interfaces';
2
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
3
import {MAX_NUM_OF_OPTIONS} from '../config/default';
4
5
/**
6
 * Add a new option to the state(like DB)
7
 *
8
 * @param {string} option - the new option name
9
 * @param {object} state - the current message state
10
 * @param {string} creator - who add the new option
11
 * @returns {void} card
12
 */
13
export function addOptionToState(option: string, state: PollState, creator = '') {
14
  const choiceLength = state.choices.length;
15
  state.choices.push(option);
16
  if (state.choiceCreator === undefined) {
17
    state.choiceCreator = {[choiceLength]: creator};
18
  } else {
19
    state.choiceCreator[choiceLength] = creator;
20
  }
21
}
22
23
export function getStateFromCard(event: chatV1.Schema$DeprecatedEvent) {
24
  const card = event.message?.cardsV2?.[0]?.card as chatV1.Schema$GoogleAppsCardV1Card;
25
  return getStateFromCardName(card) || getStateFromParameter(event) || getStateFromCardWhenNoHeader(card) ||
26
    getStateFromCardWhenHasHeader(card);
27
}
28
29
export function getConfigFromInput(formValues: PollFormInputs) {
30
  const state: PollConfig = {topic: '', choices: []};
31
32
  state.topic = formValues.topic.stringInputs!.value![0]!.trim();
33
  state.anon = formValues.is_anonymous.stringInputs!.value![0] === '1';
34
  state.optionable = formValues.allow_add_option.stringInputs!.value![0] === '1';
35
  state.type = parseInt(formValues.type.stringInputs!.value![0]) as ClosableType;
36
37
  for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) {
38
    const choice = formValues[`option${i}`]!.stringInputs!.value![0]!.trim();
39
    if (choice) {
40
      state.choices.push(choice);
41
    }
42
  }
43
  return state;
44
}
45
46
function getStateFromCardWhenNoHeader(card: chatV1.Schema$GoogleAppsCardV1Card) {
47
  return card?.sections?.[0].widgets?.[0].decoratedText?.button?.onClick?.action?.parameters?.[0]?.value;
48
}
49
50
function getStateFromCardWhenHasHeader(card: chatV1.Schema$GoogleAppsCardV1Card) {
51
  // when has header the first section is header
52
  return card?.sections?.[1].widgets?.[0].decoratedText?.button?.onClick?.action?.parameters?.[0]?.value;
53
}
54
55
function getStateFromCardName(card: chatV1.Schema$GoogleAppsCardV1Card) {
56
  // when has header the first section is header
57
  return card?.name;
58
}
59
60
function getStateFromParameter(event: chatV1.Schema$DeprecatedEvent) {
61
  const parameters = event.common?.parameters;
62
63
  return parameters?.['state'];
64
}
65