All files / core/services WebhookService.ts

100% Statements 15/15
85.71% Branches 6/7
100% Functions 6/6
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30  2x 2x   2x 2x 2x     8x     1x 1x     1x 1x     1x 1x     2x 2x     2x  
import crypto from "crypto";
import { Webhook } from "@/core/models/Webhook";
import { WebhookRemote } from "@/infra/http/WebhookRemote";
 
export class WebhookService {
  public constructor(
    private readonly remote: WebhookRemote
  ) {}
 
  public async create(data: { endpoint: string }): Promise<Webhook> {
    const response = await this.remote.create(data);
    return response.data;
  }
 
  public async find(data?: { endpoint?: string | null }): Promise<Array<Webhook>> {
    const response = await this.remote.find(data);
    return response.data;
  }
 
  public async delete(webhookId: number): Promise<Webhook> {
    const response = await this.remote.delete(webhookId);
    return response.data;
  }
 
  public verifySignature(payload: any, xWebhookSignature: string, signature: string): boolean {
    const computedSignature = crypto.createHmac("sha256", signature).update(JSON.stringify(payload)).digest("hex");
    return xWebhookSignature == computedSignature;
  }
}