module.exports = { checkPublish: { task: async ({ strapi }: { strapi: any }) => { try { // Safety check: verify the content type exists before proceeding if (!strapi.contentTypes['api::blog.blog']) { console.log('Blog content type not found. Skipping scheduled publish task.'); return; } const now = new Date(); console.log('[Cron] Checking for scheduled blog posts to publish at:', now.toISOString()); try { const drafts = await strapi.documents('api::blog.blog').findMany({ filters: { publish_at: { $lte: now }, auto_publish: true }, status: 'draft', }); console.log(`[Cron] Found ${drafts?.length || 0} blog posts to publish`); for (const draft of drafts) { try { console.log('[Cron] Publishing entry:', draft.documentId); // Update the auto_publish flag first await strapi.documents('api::blog.blog').update({ documentId: draft.documentId, data: { auto_publish: false }, status: 'draft', }); // Then publish the blog post await strapi.documents('api::blog.blog').publish({ documentId: draft.documentId, }); console.log('[Cron] Successfully published blog post:', draft.documentId); } catch (postError) { console.error(`[Cron] Error publishing blog post ${draft.documentId}:`, postError); // Continue with next post even if one fails } } } catch (queryError) { console.error('[Cron] Error querying drafts:', queryError); } } catch (error) { console.error('[Cron] Uncaught error in checkPublish task:', error); } }, options: { rule: '*/10 * * * *', // Run every 10 minutes }, }, };