• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

safe-global / safe-events-service / 6195845991

15 Sep 2023 08:36AM UTC coverage: 89.356%. Remained the same
6195845991

push

github

Uxio0
Set version 0.4.0

69 of 78 branches covered (0.0%)

Branch coverage included in aggregate %.

250 of 279 relevant lines covered (89.61%)

6.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.7
/src/routes/webhook/webhook.service.ts
1
import { Inject, Injectable, Logger } from '@nestjs/common';
8✔
2
import { Cache } from 'cache-manager';
3
import { InjectRepository } from '@nestjs/typeorm';
8✔
4
import { Repository } from 'typeorm';
8✔
5
import { Webhook } from './entities/webhook.entity';
8✔
6
import { CACHE_MANAGER } from '@nestjs/cache-manager';
8✔
7
import { ConfigService } from '@nestjs/config';
8✔
8
import { HttpService } from '@nestjs/axios';
8✔
9
import { of, catchError, firstValueFrom } from 'rxjs';
8✔
10
import { AxiosError, AxiosResponse } from 'axios';
11
import { TxServiceEvent } from '../events/event.dto';
12

13
@Injectable()
14
export class WebhookService {
8✔
15
  private readonly logger = new Logger(WebhookService.name);
14✔
16

17
  constructor(
18
    @InjectRepository(Webhook)
19
    private readonly WebHooksRepository: Repository<Webhook>,
14✔
20
    @Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
14✔
21
    private readonly configService: ConfigService,
14✔
22
    private readonly httpService: HttpService,
14✔
23
  ) {}
24

25
  /**
26
   *
27
   * @returns webhooks cache ttl from `WEBHOOKS_CACHE_TTL`, if not defined 300_000 ms (5 seconds)
28
   */
29
  getWebhooksCacheTTL(): number {
30
    return this.configService.get('WEBHOOKS_CACHE_TTL') ?? 300_000;
6!
31
  }
32

33
  findAllActive(): Promise<Webhook[]> {
34
    return this.WebHooksRepository.findBy({ isActive: true });
×
35
  }
36

37
  async getCachedActiveWebhooks(): Promise<Webhook[]> {
38
    const key = 'webhooks';
8✔
39
    const value = await this.cacheManager.get<Webhook[] | null>('webhooks');
8✔
40
    if (value != null) {
8✔
41
      this.logger.debug('Webhooks cached');
2✔
42
      return value;
2✔
43
    } else {
44
      this.logger.debug('Webhooks not cached, fetching them');
6✔
45
      const webhooks = await this.findAllActive();
6✔
46
      this.cacheManager.set(key, webhooks, this.getWebhooksCacheTTL());
6✔
47
      return webhooks;
6✔
48
    }
49
  }
50

51
  async postEveryWebhook(
52
    parsedMessage: TxServiceEvent,
53
  ): Promise<(AxiosResponse | undefined)[]> {
54
    const webhooks: Webhook[] = await this.getCachedActiveWebhooks();
6✔
55
    const responses: Promise<AxiosResponse | undefined>[] = webhooks
6✔
56
      .filter((webhook: Webhook) => {
57
        return webhook.isEventRelevant(parsedMessage);
8✔
58
      })
59
      .map((webhook: Webhook) => {
60
        this.logger.debug(
6✔
61
          `Sending ${JSON.stringify(parsedMessage)} to ${webhook.url}`,
62
        );
63
        return this.postWebhook(
6✔
64
          parsedMessage,
65
          webhook.url,
66
          webhook.authorization,
67
        );
68
      });
69
    return Promise.all(responses);
6✔
70
  }
71

72
  postWebhook(
73
    parsedMessage: TxServiceEvent,
74
    url: string,
75
    authorization: string,
76
  ): Promise<AxiosResponse | undefined> {
77
    const headers = authorization ? { Authorization: authorization } : {};
4✔
78
    return firstValueFrom(
4✔
79
      this.httpService.post(url, parsedMessage, { headers }).pipe(
80
        catchError((error: AxiosError) => {
81
          this.logger.error(`Error sending event to ${url}`, error);
×
82
          return of(undefined);
×
83
        }),
84
      ),
85
    );
86
  }
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc