placebo.mk/backend/src/modules/strapi.controller.ts
2026-01-29 04:01:55 +01:00

61 lines
1.6 KiB
TypeScript

import { Controller, Post, Body } from '@nestjs/common';
import { StrapiService } from './strapi.service';
interface WebhookBody {
event: 'entry.create' | 'entry.update' | 'entry.delete';
model: string;
entry: {
documentId: string;
};
}
@Controller('webhooks/strapi')
export class StrapiController {
constructor(private readonly strapiService: StrapiService) {}
@Post('article')
async handleArticleWebhook(@Body() body: WebhookBody) {
const { event, model, entry } = body;
if (model !== 'article') {
return { message: 'Ignored: not an article' };
}
await this.strapiService.handleWebhook(event, entry);
return { message: 'Webhook processed successfully' };
}
@Post('sync/all')
async syncAllArticles() {
await this.strapiService.syncArticles();
return { message: 'Articles sync completed' };
}
@Post('live-blog')
async handleLiveBlogWebhook(@Body() body: WebhookBody) {
const { event, model, entry } = body;
if (model !== 'live-blog') {
return { message: 'Ignored: not a live blog' };
}
await this.strapiService.handleWebhook(event, entry);
return { message: 'Live blog webhook processed successfully' };
}
@Post('sync/live-blogs')
async syncAllLiveBlogs() {
await this.strapiService.syncLiveBlogs();
return { message: 'Live blogs sync completed' };
}
@Post('sync/everything')
async syncEverything() {
await Promise.all([
this.strapiService.syncArticles(),
this.strapiService.syncLiveBlogs(),
]);
return { message: 'Full sync completed' };
}
}