From a197185ad0a08629b09e0f23684eafceff4da22f Mon Sep 17 00:00:00 2001 From: dahyeon Date: Wed, 15 Apr 2026 05:26:24 -0700 Subject: [PATCH] feat(api): implement Riot tournament webhook endpoint Adds a controller to capture and log incoming webhook notifications from Riot Games. Enables raw body parsing at the application level to facilitate payload processing and future signature verification. --- apps/api/src/main.ts | 4 +- .../riot/riot-webhook.controller.spec.ts | 60 +++++++++++++++++++ .../modules/riot/riot-webhook.controller.ts | 47 +++++++++++++++ apps/api/src/modules/riot/riot.module.ts | 2 + 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 apps/api/src/modules/riot/riot-webhook.controller.spec.ts create mode 100644 apps/api/src/modules/riot/riot-webhook.controller.ts diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index f76bc8d9..5ee0da56 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -2,7 +2,9 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { - const app = await NestFactory.create(AppModule); + const app = await NestFactory.create(AppModule, { + rawBody: true, + }); await app.listen(process.env.PORT ?? 3000); } bootstrap(); diff --git a/apps/api/src/modules/riot/riot-webhook.controller.spec.ts b/apps/api/src/modules/riot/riot-webhook.controller.spec.ts new file mode 100644 index 00000000..b72287fb --- /dev/null +++ b/apps/api/src/modules/riot/riot-webhook.controller.spec.ts @@ -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 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 }, + ); + }); +}); diff --git a/apps/api/src/modules/riot/riot-webhook.controller.ts b/apps/api/src/modules/riot/riot-webhook.controller.ts new file mode 100644 index 00000000..434c15f3 --- /dev/null +++ b/apps/api/src/modules/riot/riot-webhook.controller.ts @@ -0,0 +1,47 @@ +import { + All, + Controller, + HttpCode, + HttpStatus, + Req, +} from '@nestjs/common'; +import type { Request } from 'express'; + +type RiotWebhookRequest = Request & { + rawBody?: Buffer; +}; + +@Controller('riot') +export class RiotWebhookController { + @All('webhooks/tournament') + @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, + }; + } +} diff --git a/apps/api/src/modules/riot/riot.module.ts b/apps/api/src/modules/riot/riot.module.ts index ed53fef2..9f147be5 100644 --- a/apps/api/src/modules/riot/riot.module.ts +++ b/apps/api/src/modules/riot/riot.module.ts @@ -3,9 +3,11 @@ import { SecretModule } from '../global/secret/secret.module'; import { RiotAccountService } from './riot-account.service'; import { RiotClientService } from './riot.client.service'; import { RiotMatchService } from './riot-match.service'; +import { RiotWebhookController } from './riot-webhook.controller'; @Module({ imports: [SecretModule], + controllers: [RiotWebhookController], providers: [RiotClientService, RiotAccountService, RiotMatchService], exports: [RiotClientService, RiotAccountService, RiotMatchService], })