Skip to content

Commit 26bb11a

Browse files
committed
refactor: simplify article deletion logic and remove unused functions
1 parent 974ab00 commit 26bb11a

File tree

1 file changed

+13
-124
lines changed

1 file changed

+13
-124
lines changed
Lines changed: 13 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,31 @@
11
"use server";
2-
3-
import { eq, and, lt, isNotNull, sql } from "drizzle-orm";
4-
import { drizzleClient } from "../persistence/clients";
5-
import { articlesTable } from "../persistence/schemas";
2+
import { and, lt, neq } from "sqlkit";
3+
import { persistenceRepository } from "../persistence/persistence-repositories";
64
import { handleActionException } from "./RepositoryException";
75

8-
export interface CleanupResult {
9-
deletedCount: number;
10-
deletedArticles: Array<{
11-
id: string;
12-
title: string;
13-
handle: string;
14-
delete_scheduled_at: Date;
15-
}>;
16-
}
17-
186
/**
197
* Delete articles that have passed their scheduled deletion time
208
*/
21-
export async function deleteExpiredArticles(): Promise<CleanupResult> {
9+
export async function deleteExpiredArticles() {
2210
try {
23-
// First, get the articles that are scheduled for deletion and have passed their deletion time
2411
const currentTime = new Date();
25-
26-
const expiredArticles = await drizzleClient
27-
.select({
28-
id: articlesTable.id,
29-
title: articlesTable.title,
30-
handle: articlesTable.handle,
31-
delete_scheduled_at: articlesTable.delete_scheduled_at,
32-
})
33-
.from(articlesTable)
34-
.where(
35-
and(
36-
isNotNull(articlesTable.delete_scheduled_at),
37-
lt(articlesTable.delete_scheduled_at, currentTime)
38-
)
39-
);
40-
41-
console.log(`Found ${expiredArticles.length} articles scheduled for deletion`);
4212

43-
if (expiredArticles.length === 0) {
44-
return {
45-
deletedCount: 0,
46-
deletedArticles: [],
47-
};
48-
}
13+
const deleteResult = await persistenceRepository.article.delete({
14+
where: and(
15+
neq("delete_scheduled_at", null),
16+
lt("delete_scheduled_at", currentTime)
17+
),
18+
});
4919

50-
// Delete the expired articles
51-
const articleIds = expiredArticles.map(article => article.id);
52-
53-
const deleteResult = await drizzleClient
54-
.delete(articlesTable)
55-
.where(
56-
and(
57-
isNotNull(articlesTable.delete_scheduled_at),
58-
lt(articlesTable.delete_scheduled_at, currentTime)
59-
)
60-
);
61-
62-
console.log(`Successfully deleted ${expiredArticles.length} expired articles`);
20+
console.log(
21+
`Successfully deleted ${deleteResult?.rowCount} expired articles`
22+
);
6323

6424
return {
65-
deletedCount: expiredArticles.length,
66-
deletedArticles: expiredArticles.map(article => ({
67-
id: article.id,
68-
title: article.title || "Untitled",
69-
handle: article.handle || "",
70-
delete_scheduled_at: article.delete_scheduled_at!,
71-
})),
25+
deletedCount: deleteResult?.rowCount || 0,
7226
};
7327
} catch (error) {
7428
console.error("Error deleting expired articles:", error);
7529
throw handleActionException(error);
7630
}
7731
}
78-
79-
/**
80-
* Schedule an article for deletion at a specific time
81-
*/
82-
export async function scheduleArticleForDeletion(
83-
articleId: string,
84-
deleteAt: Date
85-
): Promise<void> {
86-
try {
87-
await drizzleClient
88-
.update(articlesTable)
89-
.set({
90-
delete_scheduled_at: deleteAt,
91-
updated_at: new Date(),
92-
})
93-
.where(eq(articlesTable.id, articleId));
94-
95-
console.log(`Article ${articleId} scheduled for deletion at ${deleteAt.toISOString()}`);
96-
} catch (error) {
97-
console.error("Error scheduling article for deletion:", error);
98-
throw handleActionException(error);
99-
}
100-
}
101-
102-
/**
103-
* Cancel scheduled deletion for an article
104-
*/
105-
export async function cancelScheduledDeletion(articleId: string): Promise<void> {
106-
try {
107-
await drizzleClient
108-
.update(articlesTable)
109-
.set({
110-
delete_scheduled_at: null,
111-
updated_at: new Date(),
112-
})
113-
.where(eq(articlesTable.id, articleId));
114-
115-
console.log(`Cancelled scheduled deletion for article ${articleId}`);
116-
} catch (error) {
117-
console.error("Error cancelling scheduled deletion:", error);
118-
throw handleActionException(error);
119-
}
120-
}
121-
122-
/**
123-
* Get all articles scheduled for deletion
124-
*/
125-
export async function getScheduledArticles() {
126-
try {
127-
return await drizzleClient
128-
.select({
129-
id: articlesTable.id,
130-
title: articlesTable.title,
131-
handle: articlesTable.handle,
132-
delete_scheduled_at: articlesTable.delete_scheduled_at,
133-
author_id: articlesTable.author_id,
134-
})
135-
.from(articlesTable)
136-
.where(isNotNull(articlesTable.delete_scheduled_at))
137-
.orderBy(articlesTable.delete_scheduled_at);
138-
} catch (error) {
139-
console.error("Error getting scheduled articles:", error);
140-
throw handleActionException(error);
141-
}
142-
}

0 commit comments

Comments
 (0)