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

BaseHandler.getUserTimezone   B

Complexity

Conditions 6

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 10
rs 8.6666
c 0
b 0
f 0
cc 6
1
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
2
import {LocaleTimezone} from '../helpers/interfaces';
3
4
export default abstract class BaseHandler {
5
  protected event: chatV1.Schema$DeprecatedEvent;
6
7
  constructor(event: chatV1.Schema$DeprecatedEvent) {
8
    this.event = event;
9
  }
10
11
  protected getAnnotations(): chatV1.Schema$Annotation[] {
12
    return this.event.message?.annotations ?? [];
13
  }
14
15
  protected getAnnotationByType(type: string): chatV1.Schema$Annotation | undefined {
16
    return this.getAnnotations().find((annotation) => annotation.type === type);
17
  }
18
19
  protected getUserTimezone(): LocaleTimezone {
20
    if (this.event.common?.timeZone?.id) {
21
      const locale = this.event.common.userLocale;
22
      const id = this.event.common.timeZone.id;
23
      const offset = this.event.common.timeZone.offset ?? 0;
24
      return {locale, id, offset};
25
    }
26
27
    return {'locale': 'en', 'offset': 0, 'id': 'UTC'};
28
  }
29
30
  public abstract process(): chatV1.Schema$Message | Promise<chatV1.Schema$Message>;
31
}
32