Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webhooks): add postgres persistence to clients and requests (feature branch) #108

Merged
merged 7 commits into from
Nov 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove unneeded dependencies
Signed-off-by: david <david@umaproject.org>
daywiss authored and amateima committed Nov 26, 2024
commit 7f636dc276c9855ec2efd9da4d7d59aacb28be51
3 changes: 1 addition & 2 deletions packages/webhooks/package.json
Original file line number Diff line number Diff line change
@@ -25,8 +25,7 @@
"express": "^4.19.2",
"express-bearer-token": "^3.0.0",
"redis": "^4.7.0",
"superstruct": "2.0.3-1",
"typeorm": "^0.3.20"
"superstruct": "2.0.3-1"
},
"exports": {
".": "./dist/index.js"
3 changes: 1 addition & 2 deletions packages/webhooks/src/database/webhookClientRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DataSource } from "typeorm";
import { entities } from "@repo/indexer-database";
import { entities, DataSource } from "@repo/indexer-database";

// This class is intended to store integration clients allowed to use the webhook service.
export class WebhookClientRepository {
14 changes: 7 additions & 7 deletions packages/webhooks/src/database/webhookRequestRepository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { DataSource } from "typeorm";
import { WebhookRequest } from "../types";
import { entities } from "@repo/indexer-database";
import { entities, DataSource } from "@repo/indexer-database";

export class WebhookRequestRepository {
private repository;
@@ -9,7 +7,7 @@ export class WebhookRequestRepository {
this.repository = this.dataSource.getRepository(entities.WebhookRequest);
}

public async register(webhook: WebhookRequest): Promise<void> {
public async register(webhook: entities.WebhookRequest): Promise<void> {
const existingWebhook = await this.repository.findOne({
where: { id: webhook.id },
});
@@ -31,17 +29,19 @@ export class WebhookRequestRepository {

public async getWebhook(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public async getWebhook(
public async getWebhookRequest(

webhookId: string,
): Promise<WebhookRequest | undefined> {
): Promise<entities.WebhookRequest | undefined> {
return (
(await this.repository.findOne({ where: { id: webhookId } })) ?? undefined
);
}

public async listWebhooks(): Promise<WebhookRequest[]> {
public async listWebhooks(): Promise<entities.WebhookRequest[]> {
return this.repository.find();
}

public async filterWebhooks(filter: string): Promise<WebhookRequest[]> {
public async filterWebhooks(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name is a bit misleading because it suggests that you can filter webhooks by custom fields. It should sound more like getWebhookRequestsByFilter

filter: string,
): Promise<entities.WebhookRequest[]> {
return this.repository.find({ where: { filter } });
}

8 changes: 4 additions & 4 deletions packages/webhooks/src/eventProcessors/depositStatus.ts
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ export class DepositStatusProcessor implements IEventProcessor {
private postgres: DataSource;
// Type shoudl be uniqe across all event processors, this is to avoid colliding with multiple
// processors writing to the same tables
public type = "DepositStatus";
static type = "DepositStatus";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hardcoded strings should really be avoided. If this is common to all event processors, should we add the type field tot the IEventProcessor interface?
And also making it to have the WebhookTypes type


constructor(deps: Dependencies) {
this.webhookRequests = new WebhookRequestRepository(deps.postgres);
@@ -40,7 +40,7 @@ export class DepositStatusProcessor implements IEventProcessor {
}
private async _write(event: DepositStatusEvent): Promise<void> {
const filter = customId(
this.type,
DepositStatusProcessor.type,
event.originChainId,
event.depositTxHash,
);
@@ -65,13 +65,13 @@ export class DepositStatusProcessor implements IEventProcessor {
params: DepositStatusFilter,
): Promise<string> {
const id = customId(
this.type,
DepositStatusProcessor.type,
url,
params.originChainId,
params.depositTxHash,
);
const filter = customId(
this.type,
DepositStatusProcessor.type,
params.originChainId,
params.depositTxHash,
);
2 changes: 0 additions & 2 deletions packages/webhooks/src/factory.ts
Original file line number Diff line number Diff line change
@@ -4,9 +4,7 @@ import { DataSource } from "@repo/indexer-database";
import { Logger } from "winston";
import { WebhookNotifier } from "./notifier";
import { DepositStatusProcessor } from "./eventProcessors";
import { WebhookRequestRepository } from "./database/webhookRequestRepository";
import { WebhookRouter } from "./router";
import { entities } from "@repo/indexer-database";

export enum WebhookTypes {
DepositStatus = "DepositStatus",
6 changes: 0 additions & 6 deletions packages/webhooks/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import * as ss from "superstruct";

export interface WebhookRequest {
id: string;
url: string;
filter: string;
}

export interface IEventProcessor {
write(event: JSONValue): void;
register(url: string, params: JSONValue): Promise<string>;
57 changes: 24 additions & 33 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.