feat: complete scheduled publishing, security remediation and DEV_TICKETS implementation
This commit is contained in:
1 parent
d6d4158cbe
commit
efb3c473c4
35 files changed
+685
-185
No files matched your search
@@ -1,11 +1,20 @@
|
||||
import { Injectable, NotFoundException, OnModuleInit } from '@nestjs/common';
|
||||
import {
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
OnModuleInit,
|
||||
OnModuleDestroy,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { PaginationDto } from '../common/dto/pagination.dto';
|
||||
import { RevalidationService } from '../common/revalidation/revalidation.service';
|
||||
|
||||
@Injectable()
|
||||
export class BlogsService implements OnModuleInit {
|
||||
export class BlogsService implements OnModuleInit, OnModuleDestroy {
|
||||
private readonly logger = new Logger(BlogsService.name);
|
||||
private schedulerTimer: NodeJS.Timeout | null = null;
|
||||
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private revalidationService: RevalidationService,
|
||||
@@ -13,6 +22,21 @@ export class BlogsService implements OnModuleInit {
|
||||
|
||||
async onModuleInit() {
|
||||
await this.seedDefaultCategories();
|
||||
// Start background scheduled publishing worker (every 60 seconds)
|
||||
this.schedulerTimer = setInterval(() => {
|
||||
this.publishDueScheduledBlogs().catch((err) => {
|
||||
this.logger.error('Error in publishDueScheduledBlogs worker:', err);
|
||||
});
|
||||
}, 60000);
|
||||
// Run an initial check on startup
|
||||
this.publishDueScheduledBlogs().catch(() => {});
|
||||
}
|
||||
|
||||
onModuleDestroy() {
|
||||
if (this.schedulerTimer) {
|
||||
clearInterval(this.schedulerTimer);
|
||||
this.schedulerTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
private async seedDefaultCategories() {
|
||||
@@ -22,7 +46,11 @@ export class BlogsService implements OnModuleInit {
|
||||
{ name: 'سلامت گوارش و متابولیسم', slug: 'digestion', order: 2 },
|
||||
{ name: 'مفاصل، استخوان و اسکلت', slug: 'joints-bones', order: 3 },
|
||||
{ name: 'پوست، مو و آلرژی', slug: 'skin-coat', order: 4 },
|
||||
{ name: 'ایمنی، پیشگیری و مراقبت بالینی', slug: 'health-prevention', order: 5 },
|
||||
{
|
||||
name: 'ایمنی، پیشگیری و مراقبت بالینی',
|
||||
slug: 'health-prevention',
|
||||
order: 5,
|
||||
},
|
||||
];
|
||||
|
||||
for (const cat of defaultCategories) {
|
||||
@@ -33,7 +61,10 @@ export class BlogsService implements OnModuleInit {
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[BlogsService] Failed to seed default blog categories:', err);
|
||||
console.warn(
|
||||
'[BlogsService] Failed to seed default blog categories:',
|
||||
err,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,6 +146,7 @@ export class BlogsService implements OnModuleInit {
|
||||
categoryId,
|
||||
authorId,
|
||||
publishedAt,
|
||||
scheduledAt,
|
||||
readingTime,
|
||||
...rest
|
||||
} = data;
|
||||
@@ -138,15 +170,34 @@ export class BlogsService implements OnModuleInit {
|
||||
}
|
||||
|
||||
const targetAuthorId = authorId || currentUserId;
|
||||
const now = new Date();
|
||||
const parsedScheduledAt = scheduledAt ? new Date(scheduledAt) : null;
|
||||
const isFutureSchedule = parsedScheduledAt && parsedScheduledAt > now;
|
||||
|
||||
let finalStatus = rest.status || 'PUBLISHED';
|
||||
let finalIsPublished =
|
||||
rest.isPublished !== undefined ? rest.isPublished : true;
|
||||
let finalPublishedAt = publishedAt ? new Date(publishedAt) : null;
|
||||
|
||||
if (isFutureSchedule) {
|
||||
finalStatus = 'SCHEDULED';
|
||||
finalIsPublished = false;
|
||||
finalPublishedAt = null;
|
||||
} else if (parsedScheduledAt && parsedScheduledAt <= now) {
|
||||
finalStatus = 'PUBLISHED';
|
||||
finalIsPublished = true;
|
||||
finalPublishedAt = parsedScheduledAt;
|
||||
} else if (!finalPublishedAt && finalStatus === 'PUBLISHED') {
|
||||
finalPublishedAt = now;
|
||||
}
|
||||
|
||||
const createPayload: Prisma.BlogCreateInput = {
|
||||
...rest,
|
||||
status: finalStatus,
|
||||
isPublished: finalIsPublished,
|
||||
readingTime: readingTime ? Number(readingTime) : 3,
|
||||
publishedAt: publishedAt
|
||||
? new Date(publishedAt)
|
||||
: rest.status === 'PUBLISHED'
|
||||
? new Date()
|
||||
: null,
|
||||
publishedAt: finalPublishedAt,
|
||||
scheduledAt: parsedScheduledAt,
|
||||
author: { connect: { id: targetAuthorId } },
|
||||
...(categoryId ? { category: { connect: { id: categoryId } } } : {}),
|
||||
...(tagConnectOrCreate.length > 0
|
||||
@@ -177,18 +228,17 @@ export class BlogsService implements OnModuleInit {
|
||||
categoryId,
|
||||
authorId,
|
||||
publishedAt,
|
||||
scheduledAt,
|
||||
readingTime,
|
||||
...rest
|
||||
} = data;
|
||||
|
||||
const now = new Date();
|
||||
const updatePayload: Prisma.BlogUpdateInput = {
|
||||
...rest,
|
||||
...(readingTime !== undefined
|
||||
? { readingTime: Number(readingTime) }
|
||||
: {}),
|
||||
...(publishedAt !== undefined
|
||||
? { publishedAt: publishedAt ? new Date(publishedAt) : null }
|
||||
: {}),
|
||||
...(authorId ? { author: { connect: { id: authorId } } } : {}),
|
||||
...(categoryId !== undefined
|
||||
? categoryId
|
||||
@@ -197,6 +247,24 @@ export class BlogsService implements OnModuleInit {
|
||||
: {}),
|
||||
};
|
||||
|
||||
if (scheduledAt !== undefined) {
|
||||
const parsedScheduledAt = scheduledAt ? new Date(scheduledAt) : null;
|
||||
updatePayload.scheduledAt = parsedScheduledAt;
|
||||
|
||||
if (parsedScheduledAt && parsedScheduledAt > now) {
|
||||
updatePayload.status = 'SCHEDULED';
|
||||
updatePayload.isPublished = false;
|
||||
updatePayload.publishedAt = null;
|
||||
} else if (parsedScheduledAt && parsedScheduledAt <= now) {
|
||||
updatePayload.status = 'PUBLISHED';
|
||||
updatePayload.isPublished = true;
|
||||
updatePayload.publishedAt = parsedScheduledAt;
|
||||
updatePayload.createdAt = parsedScheduledAt;
|
||||
}
|
||||
} else if (publishedAt !== undefined) {
|
||||
updatePayload.publishedAt = publishedAt ? new Date(publishedAt) : null;
|
||||
}
|
||||
|
||||
if (Array.isArray(tagNames)) {
|
||||
const tagConnectOrCreate = [];
|
||||
for (const tName of tagNames) {
|
||||
@@ -367,4 +435,61 @@ export class BlogsService implements OnModuleInit {
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishes blogs that have reached their scheduled publication date
|
||||
*/
|
||||
async publishDueScheduledBlogs(): Promise<number> {
|
||||
const now = new Date();
|
||||
const dueBlogs = await this.prisma.blog.findMany({
|
||||
where: {
|
||||
status: 'SCHEDULED',
|
||||
scheduledAt: { lte: now },
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
slug: true,
|
||||
title: true,
|
||||
scheduledAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!dueBlogs || dueBlogs.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
this.logger.log(
|
||||
`Found ${dueBlogs.length} due scheduled blog(s) to publish automatically.`,
|
||||
);
|
||||
|
||||
let publishedCount = 0;
|
||||
for (const blog of dueBlogs) {
|
||||
try {
|
||||
const publishDate = blog.scheduledAt || now;
|
||||
await this.prisma.blog.update({
|
||||
where: { id: blog.id },
|
||||
data: {
|
||||
status: 'PUBLISHED',
|
||||
isPublished: true,
|
||||
publishedAt: publishDate,
|
||||
createdAt: publishDate,
|
||||
},
|
||||
});
|
||||
|
||||
publishedCount++;
|
||||
this.logger.log(
|
||||
`Successfully published scheduled blog [${blog.title}] (${blog.slug})`,
|
||||
);
|
||||
|
||||
// Immediate cache & sitemap revalidation
|
||||
await this.revalidationService.revalidateBlog(blog.slug);
|
||||
} catch (err: any) {
|
||||
this.logger.error(
|
||||
`Failed to publish scheduled blog [${blog.id}] (${blog.slug}): ${err?.message}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return publishedCount;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user