Skip to content

Commit cf0aadb

Browse files
committed
PM-1073 - rework structure for webhooks
1 parent 2e9532e commit cf0aadb

8 files changed

+43
-27
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Provider } from '@nestjs/common';
2+
import { PaymentHandler } from './payment.handler';
3+
import { getWebhooksEventHandlersProvider } from '../../webhooks.event-handlers.provider';
4+
5+
export const TrolleyWebhookHandlers: Provider[] = [
6+
getWebhooksEventHandlersProvider(
7+
'trolleyHandlerFns',
8+
'TrolleyWebhookHandlers',
9+
),
10+
11+
PaymentHandler,
12+
{
13+
provide: 'TrolleyWebhookHandlers',
14+
useFactory: (paymentHandler: PaymentHandler) => [paymentHandler],
15+
inject: [PaymentHandler],
16+
},
17+
];

src/api/webhooks/trolley-handlers/payment.handler.ts renamed to src/api/webhooks/trolley/handlers/payment.handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@nestjs/common';
2-
import { TrolleyWebhookEvent } from '../webhooks.types';
3-
import { WebhookEvent } from './decorators';
2+
import { WebhookEvent } from '../../webhooks.decorators';
3+
import { TrolleyWebhookEvent } from '../trolley.types';
44

55
@Injectable()
66
export class PaymentHandler {

src/api/webhooks/trolley.service.ts renamed to src/api/webhooks/trolley/trolley.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class TrolleyService {
120120
const { model, action, body } = payload;
121121
const handler = this.handlers.get(`${model}.${action}`);
122122
if (!handler) {
123-
throw new Error('Webhook event handler not found!');
123+
throw new Error('Event handler not found!');
124124
}
125125

126126
await handler(body);

src/api/webhooks/webhooks.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
ConflictException,
99
} from '@nestjs/common';
1010
import { ApiTags } from '@nestjs/swagger';
11-
import { TrolleyService } from './trolley.service';
11+
import { TrolleyService } from './trolley/trolley.service';
1212
import { Public } from 'src/core/auth/decorators';
1313

1414
@Public()
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { Provider } from '@nestjs/common';
2-
import { PaymentHandler } from './payment.handler';
3-
import { TrolleyWebhookEvent } from '../webhooks.types';
41
import { Reflector } from '@nestjs/core';
5-
import { WEBHOOK_EVENT_METADATA_KEY } from './decorators';
2+
import { WEBHOOK_EVENT_METADATA_KEY } from './webhooks.decorators';
63

74
/**
85
* Factory function to create a map of Trolley webhook event handlers.
@@ -18,8 +15,8 @@ import { WEBHOOK_EVENT_METADATA_KEY } from './decorators';
1815
* @returns A `Map` where the keys are `TrolleyWebhookEvent` types and the values are
1916
* bound handler functions for those events.
2017
*/
21-
const trolleyHandlerFnsFactory = (reflector: Reflector, handlerClasses) => {
22-
const handlersMap = new Map<TrolleyWebhookEvent, () => void>();
18+
const whEventHandlersFactory = (reflector: Reflector, handlerClasses) => {
19+
const handlersMap = new Map<string, () => void>();
2320

2421
for (const handlerClass of handlerClasses) {
2522
const prototype = Object.getPrototypeOf(handlerClass);
@@ -29,7 +26,7 @@ const trolleyHandlerFnsFactory = (reflector: Reflector, handlerClasses) => {
2926
continue;
3027
}
3128

32-
const eventTypes = reflector.get<TrolleyWebhookEvent[]>(
29+
const eventTypes = reflector.get<string[]>(
3330
WEBHOOK_EVENT_METADATA_KEY,
3431
method,
3532
);
@@ -46,16 +43,18 @@ const trolleyHandlerFnsFactory = (reflector: Reflector, handlerClasses) => {
4643
return handlersMap;
4744
};
4845

49-
export const TrolleyWebhookHandlersProviders: Provider[] = [
50-
PaymentHandler,
51-
{
52-
provide: 'TrolleyWebhookHandlers',
53-
useFactory: (paymentHandler: PaymentHandler) => [paymentHandler],
54-
inject: [PaymentHandler],
55-
},
56-
{
57-
provide: 'trolleyHandlerFns',
58-
useFactory: trolleyHandlerFnsFactory,
59-
inject: [Reflector, 'TrolleyWebhookHandlers'],
60-
},
61-
];
46+
/**
47+
* Creates a provider object for webhook event handlers.
48+
*
49+
* @param provide - The token that will be used to provide the dependency.
50+
* @param handlersKey - The key used to identify the specific handlers to inject.
51+
* @returns An object defining the provider with a factory function and its dependencies.
52+
*/
53+
export const getWebhooksEventHandlersProvider = (
54+
provide: string,
55+
handlersKey: string,
56+
) => ({
57+
provide,
58+
useFactory: whEventHandlersFactory,
59+
inject: [Reflector, handlersKey],
60+
});

src/api/webhooks/webhooks.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Module } from '@nestjs/common';
2-
import { TrolleyService } from './trolley.service';
32
import { WebhooksController } from './webhooks.controller';
4-
import { TrolleyWebhookHandlersProviders } from './trolley-handlers';
3+
import { TrolleyService } from './trolley/trolley.service';
4+
import { TrolleyWebhookHandlers } from './trolley/handlers';
55

66
@Module({
77
imports: [],
88
controllers: [WebhooksController],
9-
providers: [...TrolleyWebhookHandlersProviders, TrolleyService],
9+
providers: [...TrolleyWebhookHandlers, TrolleyService],
1010
})
1111
export class WebhooksModule {}

0 commit comments

Comments
 (0)