Skip to content

Commit

Permalink
fix(tenant-management): add post webhook handler
Browse files Browse the repository at this point in the history
add post webhook handler

21
  • Loading branch information
Tyagi-Sunny committed Dec 12, 2024
1 parent 5e03dcb commit 7d6c5b6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {ILogger, LOGGER, STATUS_CODE} from '@sourceloop/core';
import {createHmac, randomBytes} from 'crypto';
import {TenantMgmtServiceApplication} from '../../application';
import {TenantStatus} from '../../enums';
import {WEBHOOK_CONFIG} from '../../keys';
import {PostWebhookHandlerServiceKey, WEBHOOK_CONFIG} from '../../keys';
import {
ContactRepository,
ResourceRepository,
Expand All @@ -24,6 +24,7 @@ describe('WebhookController', () => {
let tenantRepo: TenantRepository;
let contactRepo: ContactRepository;
let webhookPayload: WebhookPayload;
let postWebhookHandlerServiceStub: sinon.SinonStub;
const nonExistantTenant = 'non-existant-tenant';
const notifStub = sinon.stub();

Expand All @@ -49,6 +50,11 @@ describe('WebhookController', () => {
createNotification: notifStub,
getTemplateByName: (name: string) => testTemplates[name],
});

postWebhookHandlerServiceStub = sinon.stub();
app.bind(PostWebhookHandlerServiceKey).to({
postWebhookHandler: postWebhookHandlerServiceStub,
});
});

after(async () => {
Expand All @@ -70,6 +76,19 @@ describe('WebhookController', () => {
});

describe('Common', () => {
it('should call postWebhookHandler on successful webhook processing', async () => {
const headers = await buildHeaders(webhookPayload);
await client
.post('/webhook')
.set(webhookConfig.signatureHeaderName, headers.signature)
.set(webhookConfig.timestampHeaderName, headers.timestamp)
.send(webhookPayload)
.expect(STATUS_CODE.NO_CONTENT);

// Verify that postWebhookHandler is called
sinon.assert.calledOnce(postWebhookHandlerServiceStub);
sinon.assert.calledWith(postWebhookHandlerServiceStub, webhookPayload);
});
it('should return 204 status for a webhook call with valid token', async () => {
const headers = await buildHeaders(webhookPayload);
await client
Expand Down
6 changes: 6 additions & 0 deletions services/tenant-management-service/src/keys.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {VerifyFunction} from 'loopback4-authentication';
import {
IPostWebhookHandlerService,
ITenantManagementServiceConfig,
LeadUser,
ResourceProvisionedWebhookPayload,
WebhookConfig,
WebhookNotificationServiceType,
} from './types';
Expand Down Expand Up @@ -74,3 +76,7 @@ export const WebhookNotificationService =
export const EventConnectorBinding = BindingKey.create<
IEventConnector<unknown>
>('arc-saas.services.tenant-management.event-connector');

export const PostWebhookHandlerServiceKey = BindingKey.create<
IPostWebhookHandlerService<ResourceProvisionedWebhookPayload>
>('services.PostWebhookHandlerService');
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
} from '../../types';
import {CryptoHelperService} from '../crypto-helper.service';
import {NotificationService} from '../notifications';
import {IPostWebhookHandlerService} from '../../types/i-post-webhook-handler-service.interface';
import {PostWebhookHandlerServiceKey} from '../../keys';

/**
* Handler for provisioning webhooks.
Expand All @@ -41,6 +43,8 @@ export class ProvisioningWebhookHandler implements IWebhookHandler {
public tenantRepository: TenantRepository,
@inject('services.NotificationService')
private notificationService: NotificationService,
@inject(PostWebhookHandlerServiceKey)
private postWebhookHandlerService: IPostWebhookHandlerService<ResourceProvisionedWebhookPayload>,
@service(CryptoHelperService)
private cryptoHelperService: CryptoHelperService,
@inject(LOGGER.LOGGER_INJECT)
Expand Down Expand Up @@ -97,6 +101,7 @@ export class ProvisioningWebhookHandler implements IWebhookHandler {
{transaction},
);
}
await this.postWebhookHandlerService.postWebhookHandler(payload);

await transaction.commit();
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {ResourceProvisionedWebhookPayload} from './webhook-payload.type';

export interface IPostWebhookHandlerService<
T extends ResourceProvisionedWebhookPayload,
> {
postWebhookHandler(dto: T): Promise<void>;
}
1 change: 1 addition & 0 deletions services/tenant-management-service/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ export * from './resource.type';
export * from './i-provisioning-service.interface';
export * from './i-subscription.interface';
export * from './i-event-connector.interface';
export * from './i-post-webhook-handler-service.interface';

0 comments on commit 7d6c5b6

Please sign in to comment.