Skip to content

Commit

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

gh-21

## Description

Please include a summary of the change and which issue is fixed. Please
also include relevant motivation and context. List any dependencies that
are required for this change.

Fixes # (issue)

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Intermediate change (work in progress)

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration

- [ ] Test A
- [ ] Test B

## Checklist:

- [ ] Performed a self-review of my own code
- [ ] npm test passes on your machine
- [ ] New tests added or existing tests modified to cover all changes
- [ ] Code conforms with the style guide
- [ ] API Documentation in code was updated
- [ ] Any dependent changes have been merged and published in downstream
modules
  • Loading branch information
Tyagi-Sunny authored Dec 13, 2024
1 parent 5e03dcb commit 944a5c3
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 944a5c3

Please sign in to comment.