Passed
Pull Request — master (#20)
by Muhammad Dyas
01:34
created

src/handlers/TaskHandler.ts   A

Complexity

Total Complexity 13
Complexity/F 3.25

Size

Lines of Code 75
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 59
mnd 9
bc 9
fnc 4
dl 0
loc 75
rs 10
bpm 2.25
cpm 3.25
noi 0
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A TaskHandler.remindAll 0 13 1
A TaskHandler.getStateFromMessageId 0 13 5
B TaskHandler.process 0 21 6
A TaskHandler.updatePollMessage 0 10 1
1
import {PollState, TaskEvent} from '../helpers/interfaces';
2
import {callMessageApi} from '../helpers/api';
3
import {getStateFromCardName} from '../helpers/state';
4
import PollCard from '../cards/PollCard';
5
6
export default class TaskHandler {
7
  event: TaskEvent;
8
9
  public constructor(event: TaskEvent) {
10
    this.event = event;
11
  }
12
13
  async process(): Promise<void> {
14
    this.event.state = await this.getStateFromMessageId();
15
    switch (this.event.action) {
16
      case 'close_poll':
17
        if (!this.event.state.closedTime || this.event.state.closedTime > Date.now()) {
18
          this.event.state.closedTime = Date.now();
19
        }
20
        this.event.state.closedBy = 'scheduled auto-close';
21
        const apiResponse = await this.updatePollMessage(this.event.state);
22
        if (apiResponse?.status !== 200) {
23
          throw new Error('Error when closing message');
24
        }
25
        break;
26
      case 'remind_all':
27
        if (this.event.state.closedTime && this.event.state.closedTime > Date.now()) {
28
          await this.remindAll();
29
        }
30
        break;
31
      default:
32
        console.log('unknown task');
33
    }
34
  }
35
36
  async getStateFromMessageId(): Promise<PollState> {
37
    const request = {
38
      name: this.event.id,
39
    };
40
    const apiResponse = await callMessageApi('get', request);
41
    const currentState = getStateFromCardName(apiResponse.data!.cardsV2?.[0].card ?? {});
42
    if (!currentState) {
43
      throw new Error('State not found');
44
    }
45
    this.event.space = apiResponse.data!.space;
46
    this.event.thread = apiResponse.data!.thread;
47
    return JSON.parse(currentState) as PollState;
48
  }
49
50
  async updatePollMessage(currentState: PollState) {
51
    const localeTimezone = {locale: 'en', offset: 0, id: 'UTC'};
52
    const cardMessage = new PollCard(currentState, localeTimezone).createMessage();
53
    const request = {
54
      name: this.event.id,
55
      requestBody: cardMessage,
56
      updateMask: 'cardsV2',
57
    };
58
    return await callMessageApi('update', request);
59
  }
60
61
  async remindAll() {
62
    const text = `<users/all>, The poll with the topic *${this.event.state!.topic}*  is reaching its finale. Please wrap up your voting now.`;
63
64
    const request = {
65
      name: this.event.id,
66
      parent: this.event.space!.name,
67
      messageReplyOption: 'REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD',
68
      requestBody: {
69
        text, thread: this.event.thread,
70
      },
71
    };
72
    return await callMessageApi('create', request);
73
  }
74
}
75