fixed strapi webhook endpoint
This commit is contained in:
1 parent
4ccb65ba88
commit
2ff76ffda5
7 files changed
+689
-16
No files matched your search
@@ -1,8 +1,8 @@
|
||||
import { Controller, Post, Body } from '@nestjs/common';
|
||||
import { Controller, Post, Body, Logger } from '@nestjs/common';
|
||||
import { StrapiService } from './strapi.service';
|
||||
|
||||
interface WebhookBody {
|
||||
event: 'entry.create' | 'entry.update' | 'entry.delete';
|
||||
event: 'entry.create' | 'entry.update' | 'entry.delete' | 'entry.publish';
|
||||
model: string;
|
||||
entry: {
|
||||
documentId: string;
|
||||
@@ -11,17 +11,100 @@ interface WebhookBody {
|
||||
|
||||
@Controller('webhooks/strapi')
|
||||
export class StrapiController {
|
||||
private readonly logger = new Logger(StrapiController.name);
|
||||
|
||||
constructor(private readonly strapiService: StrapiService) {}
|
||||
|
||||
@Post('article')
|
||||
async handleArticleWebhook(@Body() body: WebhookBody) {
|
||||
this.logger.log(`Received article webhook: ${JSON.stringify(body)}`);
|
||||
const { event, model, entry } = body;
|
||||
|
||||
if (model !== 'article') {
|
||||
this.logger.warn(`Ignored: not an article, model: ${model}`);
|
||||
return { message: 'Ignored: not an article' };
|
||||
}
|
||||
|
||||
await this.strapiService.handleWebhook(event, entry);
|
||||
await this.strapiService.handleWebhook(event, {
|
||||
documentId: entry.documentId,
|
||||
model,
|
||||
});
|
||||
return { message: 'Webhook processed successfully' };
|
||||
}
|
||||
|
||||
@Post('live-blog')
|
||||
async handleLiveBlogWebhook(@Body() body: WebhookBody) {
|
||||
this.logger.log(`Received live-blog webhook: ${JSON.stringify(body)}`);
|
||||
const { event, model, entry } = body;
|
||||
|
||||
if (model !== 'live-blog') {
|
||||
this.logger.warn(`Ignored: not a live blog, model: ${model}`);
|
||||
return { message: 'Ignored: not a live blog' };
|
||||
}
|
||||
|
||||
await this.strapiService.handleWebhook(event, {
|
||||
documentId: entry.documentId,
|
||||
model,
|
||||
});
|
||||
return { message: 'Live blog webhook processed successfully' };
|
||||
}
|
||||
|
||||
@Post()
|
||||
async handleGenericWebhook(@Body() body: unknown) {
|
||||
this.logger.log(`Received generic webhook: ${JSON.stringify(body)}`);
|
||||
|
||||
// Type guard to check if body is an object
|
||||
if (typeof body !== 'object' || body === null) {
|
||||
this.logger.warn(`Invalid webhook payload: ${JSON.stringify(body)}`);
|
||||
return { message: 'Invalid payload' };
|
||||
}
|
||||
|
||||
const bodyObj = body as Record<string, unknown>;
|
||||
|
||||
// Try to extract event and model from different possible payload structures
|
||||
const event = (bodyObj.event || bodyObj.type) as string;
|
||||
const model = (bodyObj.model || bodyObj.contentType) as string;
|
||||
const entry = bodyObj.entry || bodyObj.data || bodyObj;
|
||||
|
||||
if (!event || !model) {
|
||||
this.logger.warn(
|
||||
`Cannot process webhook: missing event or model. Payload: ${JSON.stringify(body)}`,
|
||||
);
|
||||
return { message: 'Cannot process: missing event or model' };
|
||||
}
|
||||
|
||||
const entryObj = entry as Record<string, unknown>;
|
||||
const documentId = (entryObj.documentId ||
|
||||
entryObj.id ||
|
||||
(entryObj.document as Record<string, unknown>)?.id) as string;
|
||||
|
||||
if (!documentId) {
|
||||
this.logger.warn(
|
||||
`Cannot process webhook: missing documentId. Payload: ${JSON.stringify(body)}`,
|
||||
);
|
||||
return { message: 'Cannot process: missing documentId' };
|
||||
}
|
||||
|
||||
// Validate event type
|
||||
const validEvents = [
|
||||
'entry.create',
|
||||
'entry.update',
|
||||
'entry.delete',
|
||||
'entry.publish',
|
||||
];
|
||||
if (!validEvents.includes(event)) {
|
||||
this.logger.warn(`Invalid event type: ${event}`);
|
||||
return { message: 'Invalid event type' };
|
||||
}
|
||||
|
||||
await this.strapiService.handleWebhook(
|
||||
event as
|
||||
| 'entry.create'
|
||||
| 'entry.update'
|
||||
| 'entry.delete'
|
||||
| 'entry.publish',
|
||||
{ documentId, model },
|
||||
);
|
||||
return { message: 'Webhook processed successfully' };
|
||||
}
|
||||
|
||||
@@ -31,18 +114,6 @@ export class StrapiController {
|
||||
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();
|
||||
|
||||
@@ -245,7 +245,7 @@ export class StrapiService {
|
||||
}
|
||||
|
||||
async handleWebhook(
|
||||
event: 'entry.create' | 'entry.update' | 'entry.delete',
|
||||
event: 'entry.create' | 'entry.update' | 'entry.delete' | 'entry.publish',
|
||||
data: { documentId: string; model?: string },
|
||||
): Promise<void> {
|
||||
this.logger.log(
|
||||
|
||||
Reference in new issue
Block a user