Skip to content

Commit 9305b88

Browse files
authored
Merge pull request #62 from techdiary-dev/shoaibsharif/dashboard-54
implement article deletion and synchronization logic in cleanup and action services
2 parents 741dbc2 + bcd2798 commit 9305b88

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

src/backend/services/article-cleanup-service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { and, lt, neq } from "sqlkit";
33
import { persistenceRepository } from "../persistence/persistence-repositories";
44
import { handleActionException } from "./RepositoryException";
5+
import { deleteArticleById } from "./search.service";
56

67
/**
78
* Delete articles that have passed their scheduled deletion time
@@ -10,6 +11,17 @@ export async function deleteExpiredArticles() {
1011
try {
1112
const currentTime = new Date();
1213

14+
const articlesToDelete = await persistenceRepository.article.find({
15+
where: and(
16+
neq("delete_scheduled_at", null),
17+
lt("delete_scheduled_at", currentTime)
18+
),
19+
});
20+
21+
for (const article of articlesToDelete) {
22+
deleteArticleById(article.id);
23+
}
24+
1325
const deleteResult = await persistenceRepository.article.delete({
1426
where: and(
1527
neq("delete_scheduled_at", null),

src/backend/services/article.actions.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import {
55
removeMarkdownSyntax,
66
removeNullOrUndefinedFromObject,
77
} from "@/lib/utils";
8+
import { addDays } from "date-fns";
89
import * as sk from "sqlkit";
910
import { and, desc, eq, like, neq, or } from "sqlkit";
1011
import { z } from "zod";
12+
import { ActionResponse } from "../models/action-contracts";
1113
import { Article, User } from "../models/domain-models";
1214
import { DatabaseTableName } from "../persistence/persistence-contracts";
1315
import { persistenceRepository } from "../persistence/persistence-repositories";
14-
import { handleActionException, ActionException } from "./RepositoryException";
16+
import { ActionException, handleActionException } from "./RepositoryException";
1517
import { ArticleRepositoryInput } from "./inputs/article.input";
18+
import { deleteArticleById, syncArticleById } from "./search.service";
1619
import { authID } from "./session.actions";
1720
import { syncTagsWithArticles } from "./tag.action";
18-
import { addDays } from "date-fns";
19-
import { ActionResponse } from "../models/action-contracts";
2021

2122
export async function createMyArticle(
2223
_input: z.infer<typeof ArticleRepositoryInput.createMyArticleInput>
@@ -166,8 +167,6 @@ export async function updateMyArticle(
166167
excerpt: input.excerpt,
167168
body: input.body,
168169
cover_image: input.cover_image,
169-
is_published: input.is_published,
170-
published_at: input.is_published ? new Date() : null,
171170
metadata: input.metadata,
172171
}),
173172
});
@@ -537,6 +536,12 @@ export async function setArticlePublished(
537536
published_at: is_published ? new Date() : null,
538537
},
539538
});
539+
if (articles?.rows?.[0] && is_published) {
540+
syncArticleById(article_id);
541+
}
542+
if (articles?.rows?.[0] && !is_published) {
543+
deleteArticleById(article_id);
544+
}
540545
return articles?.rows?.[0];
541546
} catch (error) {
542547
handleActionException(error);

src/backend/services/search.service.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { env } from "@/env";
1+
import { meilisearchClient } from "@/lib/meilisearch.admin.client";
22
import { and, eq, neq } from "sqlkit";
33
import { persistenceRepository } from "../persistence/persistence-repositories";
4-
import { meilisearchClient } from "@/lib/meilisearch.admin.client";
54

65
const index = meilisearchClient.index("articles");
76
meilisearchClient
@@ -15,6 +14,10 @@ meilisearchClient
1514
console.error("Error creating index 'articles':", error);
1615
});
1716

17+
/**
18+
* Sync all articles to the search index
19+
* @returns The number of articles synced
20+
*/
1821
export const syncAllArticles = async () => {
1922
try {
2023
const articles = await persistenceRepository.article.find({
@@ -49,6 +52,11 @@ export const syncAllArticles = async () => {
4952
}
5053
};
5154

55+
/**
56+
* Sync an article by its ID
57+
* @param articleId - The ID of the article to sync
58+
* @returns The article that was synced
59+
*/
5260
export const syncArticleById = async (articleId: string) => {
5361
try {
5462
const [article] = await persistenceRepository.article.find({
@@ -95,6 +103,7 @@ export const syncArticleById = async (articleId: string) => {
95103
export const deleteArticleById = async (articleId: string) => {
96104
try {
97105
const response = await index.deleteDocument(articleId);
106+
console.log(`Article ${articleId} deleted successfully`);
98107
return {
99108
message: `Article ${articleId} deleted successfully`,
100109
response,

src/components/Editor/ArticleEditor.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,20 @@ const ArticleEditor: React.FC<ArticleEditorProps> = ({ article, uuid }) => {
175175
},
176176
onConfirm: () => {
177177
if (uuid) {
178-
updateMyArticleMutation.mutate({
179-
article_id: uuid,
180-
is_published: !article?.is_published,
181-
});
178+
updateMyArticleMutation.mutate(
179+
{
180+
article_id: uuid,
181+
is_published: !article?.is_published,
182+
},
183+
{
184+
onSuccess: () => {
185+
articleActions.setArticlePublished(
186+
uuid,
187+
!article?.is_published
188+
);
189+
},
190+
}
191+
);
182192
}
183193
},
184194
});

0 commit comments

Comments
 (0)