unpublish fix
This commit is contained in:
parent
0c2433cac6
commit
acfbc80d1a
@ -2,7 +2,7 @@ import { Controller, Post, Body, Logger } from '@nestjs/common';
|
|||||||
import { StrapiService } from './strapi.service';
|
import { StrapiService } from './strapi.service';
|
||||||
|
|
||||||
interface WebhookBody {
|
interface WebhookBody {
|
||||||
event: 'entry.create' | 'entry.update' | 'entry.delete' | 'entry.publish';
|
event: 'entry.create' | 'entry.update' | 'entry.delete' | 'entry.publish' | 'entry.unpublish';
|
||||||
model: string;
|
model: string;
|
||||||
entry: {
|
entry: {
|
||||||
documentId: string;
|
documentId: string;
|
||||||
@ -91,6 +91,7 @@ export class StrapiController {
|
|||||||
'entry.update',
|
'entry.update',
|
||||||
'entry.delete',
|
'entry.delete',
|
||||||
'entry.publish',
|
'entry.publish',
|
||||||
|
'entry.unpublish',
|
||||||
];
|
];
|
||||||
if (!validEvents.includes(event)) {
|
if (!validEvents.includes(event)) {
|
||||||
this.logger.warn(`Invalid event type: ${event}`);
|
this.logger.warn(`Invalid event type: ${event}`);
|
||||||
@ -102,7 +103,8 @@ export class StrapiController {
|
|||||||
| 'entry.create'
|
| 'entry.create'
|
||||||
| 'entry.update'
|
| 'entry.update'
|
||||||
| 'entry.delete'
|
| 'entry.delete'
|
||||||
| 'entry.publish',
|
| 'entry.publish'
|
||||||
|
| 'entry.unpublish',
|
||||||
{ documentId, model },
|
{ documentId, model },
|
||||||
);
|
);
|
||||||
return { message: 'Webhook processed successfully' };
|
return { message: 'Webhook processed successfully' };
|
||||||
|
|||||||
@ -115,9 +115,12 @@ export class StrapiService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async syncSingleArticle(strapiId: string): Promise<void> {
|
async syncSingleArticle(
|
||||||
|
strapiId: string,
|
||||||
|
event?: 'entry.create' | 'entry.update' | 'entry.delete' | 'entry.publish' | 'entry.unpublish',
|
||||||
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
this.logger.log(`Syncing single article from Strapi: ${strapiId}`);
|
this.logger.log(`Syncing single article from Strapi: ${strapiId}, event: ${event}`);
|
||||||
|
|
||||||
const response = await lastValueFrom(
|
const response = await lastValueFrom(
|
||||||
this.httpService.get<StrapiResponse<StrapiArticle>>(
|
this.httpService.get<StrapiResponse<StrapiArticle>>(
|
||||||
@ -129,14 +132,26 @@ export class StrapiService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const strapiArticle = response.data.data;
|
const strapiArticle = response.data.data;
|
||||||
|
|
||||||
|
// Determine status based on publishedAt and event type
|
||||||
|
let status: ArticleStatus;
|
||||||
|
if (event === 'entry.unpublish') {
|
||||||
|
// If it's an unpublish event, always mark as draft
|
||||||
|
status = ArticleStatus.DRAFT;
|
||||||
|
} else if (strapiArticle.publishedAt) {
|
||||||
|
// If publishedAt exists, it's published
|
||||||
|
status = ArticleStatus.PUBLISHED;
|
||||||
|
} else {
|
||||||
|
// Otherwise it's a draft
|
||||||
|
status = ArticleStatus.DRAFT;
|
||||||
|
}
|
||||||
|
|
||||||
const articleData: Partial<CreateArticleDto> = {
|
const articleData: Partial<CreateArticleDto> = {
|
||||||
title: strapiArticle.title,
|
title: strapiArticle.title,
|
||||||
excerpt: strapiArticle.description,
|
excerpt: strapiArticle.description,
|
||||||
content: strapiArticle.content,
|
content: strapiArticle.content,
|
||||||
slug: strapiArticle.slug,
|
slug: strapiArticle.slug,
|
||||||
status: strapiArticle.publishedAt
|
status,
|
||||||
? ArticleStatus.PUBLISHED
|
|
||||||
: ArticleStatus.DRAFT,
|
|
||||||
tags: [],
|
tags: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -144,13 +159,34 @@ export class StrapiService {
|
|||||||
strapiArticle.documentId,
|
strapiArticle.documentId,
|
||||||
articleData,
|
articleData,
|
||||||
);
|
);
|
||||||
this.logger.log(`Successfully synced article: ${strapiArticle.title}`);
|
this.logger.log(`Successfully synced article: ${strapiArticle.title} with status: ${status}`);
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
this.logger.error(
|
// If we get a 404 and it's an unpublish event, we can still mark it as draft
|
||||||
`Failed to sync article ${strapiId} from Strapi`,
|
if (event === 'entry.unpublish' && error.response?.status === 404) {
|
||||||
error,
|
this.logger.log(`Article ${strapiId} not found in Strapi, marking as draft based on unpublish event`);
|
||||||
);
|
|
||||||
throw error;
|
// Try to update the article status to draft directly
|
||||||
|
try {
|
||||||
|
const articleData: Partial<CreateArticleDto> = {
|
||||||
|
status: ArticleStatus.DRAFT,
|
||||||
|
};
|
||||||
|
|
||||||
|
await this.articlesService.syncFromStrapi(strapiId, articleData);
|
||||||
|
this.logger.log(`Marked article ${strapiId} as draft based on unpublish event`);
|
||||||
|
} catch (syncError) {
|
||||||
|
this.logger.error(
|
||||||
|
`Failed to mark article ${strapiId} as draft:`,
|
||||||
|
syncError,
|
||||||
|
);
|
||||||
|
throw syncError;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.logger.error(
|
||||||
|
`Failed to sync article ${strapiId} from Strapi`,
|
||||||
|
error,
|
||||||
|
);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,9 +230,12 @@ export class StrapiService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async syncSingleLiveBlog(strapiId: string): Promise<void> {
|
async syncSingleLiveBlog(
|
||||||
|
strapiId: string,
|
||||||
|
event?: 'entry.create' | 'entry.update' | 'entry.delete' | 'entry.publish' | 'entry.unpublish',
|
||||||
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
this.logger.log(`Syncing single live blog from Strapi: ${strapiId}`);
|
this.logger.log(`Syncing single live blog from Strapi: ${strapiId}, event: ${event}`);
|
||||||
|
|
||||||
const response = await lastValueFrom(
|
const response = await lastValueFrom(
|
||||||
this.httpService.get<StrapiResponse<StrapiLiveBlog>>(
|
this.httpService.get<StrapiResponse<StrapiLiveBlog>>(
|
||||||
@ -208,24 +247,55 @@ export class StrapiService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const strapiLiveBlog = response.data.data;
|
const strapiLiveBlog = response.data.data;
|
||||||
|
|
||||||
|
// For live blogs, we use the status from Strapi directly
|
||||||
|
// but we might want to handle unpublish events differently
|
||||||
|
let status = this.mapStrapiStatusToLiveBlogStatus(strapiLiveBlog.status);
|
||||||
|
|
||||||
|
// If it's an unpublish event, set to draft
|
||||||
|
if (event === 'entry.unpublish') {
|
||||||
|
status = LiveBlogStatus.DRAFT;
|
||||||
|
}
|
||||||
|
|
||||||
const liveBlogData: Partial<CreateLiveBlogDto> = {
|
const liveBlogData: Partial<CreateLiveBlogDto> = {
|
||||||
title: strapiLiveBlog.title,
|
title: strapiLiveBlog.title,
|
||||||
description: strapiLiveBlog.description,
|
description: strapiLiveBlog.description,
|
||||||
slug: strapiLiveBlog.slug,
|
slug: strapiLiveBlog.slug,
|
||||||
status: this.mapStrapiStatusToLiveBlogStatus(strapiLiveBlog.status),
|
status,
|
||||||
};
|
};
|
||||||
|
|
||||||
await this.liveBlogService.syncFromStrapi(
|
await this.liveBlogService.syncFromStrapi(
|
||||||
strapiLiveBlog.documentId,
|
strapiLiveBlog.documentId,
|
||||||
liveBlogData,
|
liveBlogData,
|
||||||
);
|
);
|
||||||
this.logger.log(`Successfully synced live blog: ${strapiLiveBlog.title}`);
|
this.logger.log(`Successfully synced live blog: ${strapiLiveBlog.title} with status: ${status}`);
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
this.logger.error(
|
// If we get a 404 and it's an unpublish event, we can still mark it as draft
|
||||||
`Failed to sync live blog ${strapiId} from Strapi`,
|
if (event === 'entry.unpublish' && error.response?.status === 404) {
|
||||||
error,
|
this.logger.log(`Live blog ${strapiId} not found in Strapi, marking as draft based on unpublish event`);
|
||||||
);
|
|
||||||
throw error;
|
// Try to update the live blog status to draft directly
|
||||||
|
try {
|
||||||
|
const liveBlogData: Partial<CreateLiveBlogDto> = {
|
||||||
|
status: LiveBlogStatus.DRAFT,
|
||||||
|
};
|
||||||
|
|
||||||
|
await this.liveBlogService.syncFromStrapi(strapiId, liveBlogData);
|
||||||
|
this.logger.log(`Marked live blog ${strapiId} as draft based on unpublish event`);
|
||||||
|
} catch (syncError) {
|
||||||
|
this.logger.error(
|
||||||
|
`Failed to mark live blog ${strapiId} as draft:`,
|
||||||
|
syncError,
|
||||||
|
);
|
||||||
|
throw syncError;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.logger.error(
|
||||||
|
`Failed to sync live blog ${strapiId} from Strapi`,
|
||||||
|
error,
|
||||||
|
);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,7 +315,7 @@ export class StrapiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async handleWebhook(
|
async handleWebhook(
|
||||||
event: 'entry.create' | 'entry.update' | 'entry.delete' | 'entry.publish',
|
event: 'entry.create' | 'entry.update' | 'entry.delete' | 'entry.publish' | 'entry.unpublish',
|
||||||
data: { documentId: string; model?: string },
|
data: { documentId: string; model?: string },
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
@ -259,9 +329,9 @@ export class StrapiService {
|
|||||||
|
|
||||||
// Route to appropriate sync method based on model
|
// Route to appropriate sync method based on model
|
||||||
if (data.model === 'article') {
|
if (data.model === 'article') {
|
||||||
await this.syncSingleArticle(data.documentId);
|
await this.syncSingleArticle(data.documentId, event);
|
||||||
} else if (data.model === 'live-blog') {
|
} else if (data.model === 'live-blog') {
|
||||||
await this.syncSingleLiveBlog(data.documentId);
|
await this.syncSingleLiveBlog(data.documentId, event);
|
||||||
} else {
|
} else {
|
||||||
this.logger.warn(`Unknown model type in webhook: ${data.model}`);
|
this.logger.warn(`Unknown model type in webhook: ${data.model}`);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user