feat(api): add Riot webhook
This commit is contained in:
1 parent
d6f52a8b15
commit
9f6974c577
2 files changed
+109
No files matched your search
@@ -0,0 +1,60 @@
|
||||
import type { Request } from 'express';
|
||||
import { RiotWebhookController } from './riot-webhook.controller';
|
||||
|
||||
type RiotWebhookRequest = Request & {
|
||||
rawBody?: Buffer;
|
||||
};
|
||||
|
||||
describe('RiotWebhookController', () => {
|
||||
let controller: RiotWebhookController;
|
||||
|
||||
beforeEach(() => {
|
||||
controller = new RiotWebhookController();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('logs the incoming webhook payload and returns an acknowledgement', () => {
|
||||
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => undefined);
|
||||
const dirSpy = jest.spyOn(console, 'dir').mockImplementation(() => undefined);
|
||||
const request = {
|
||||
method: 'POST',
|
||||
originalUrl: '/riot/webhooks/tournament',
|
||||
protocol: 'https',
|
||||
hostname: 'example.test',
|
||||
ip: '127.0.0.1',
|
||||
ips: ['127.0.0.1'],
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
query: {
|
||||
shard: 'kr',
|
||||
},
|
||||
params: {},
|
||||
body: {
|
||||
eventType: 'MATCH_FINISHED',
|
||||
},
|
||||
rawBody: Buffer.from('{"eventType":"MATCH_FINISHED"}', 'utf8'),
|
||||
} as unknown as RiotWebhookRequest;
|
||||
|
||||
const response = controller.captureTournamentWebhook(request);
|
||||
|
||||
expect(response.ok).toBe(true);
|
||||
expect(response.message).toBe('Riot webhook received.');
|
||||
expect(response.receivedAt).toEqual(expect.any(String));
|
||||
expect(logSpy).toHaveBeenCalledTimes(2);
|
||||
expect(dirSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
url: '/riot/webhooks/tournament',
|
||||
body: {
|
||||
eventType: 'MATCH_FINISHED',
|
||||
},
|
||||
rawBodyText: '{"eventType":"MATCH_FINISHED"}',
|
||||
}),
|
||||
{ depth: null },
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,49 @@
|
||||
import {
|
||||
All,
|
||||
Controller,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Req,
|
||||
} from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import { AllowPublicAccess } from '../auth/allow-public-access.decorator';
|
||||
|
||||
type RiotWebhookRequest = Request & {
|
||||
rawBody?: Buffer;
|
||||
};
|
||||
|
||||
@Controller('riot')
|
||||
export class RiotWebhookController {
|
||||
@All('webhooks/tournament')
|
||||
@AllowPublicAccess()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public captureTournamentWebhook(@Req() req: RiotWebhookRequest) {
|
||||
const receivedAt = new Date().toISOString();
|
||||
const rawBodyText = req.rawBody?.toString('utf8') ?? null;
|
||||
const logPayload = {
|
||||
receivedAt,
|
||||
method: req.method,
|
||||
url: req.originalUrl,
|
||||
protocol: req.protocol,
|
||||
hostname: req.hostname,
|
||||
ip: req.ip,
|
||||
ips: req.ips,
|
||||
headers: req.headers,
|
||||
query: req.query,
|
||||
params: req.params,
|
||||
body: req.body,
|
||||
rawBodyLength: req.rawBody?.length ?? null,
|
||||
rawBodyText,
|
||||
};
|
||||
|
||||
console.log('\n[RIOT TOURNAMENT WEBHOOK] Incoming request');
|
||||
console.dir(logPayload, { depth: null });
|
||||
console.log('[RIOT TOURNAMENT WEBHOOK] End request log\n');
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
message: 'Riot webhook received.',
|
||||
receivedAt,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user