I mean this should work now? (Like the archiving feat.

This commit is contained in:
吳元皓 2025-06-18 20:22:26 +08:00
parent 804694f74c
commit a032abbc9b

View File

@ -38,20 +38,32 @@ function cleanUpSlug(orgslug: string) {
} }
// Archive articles. For future use? // Archive articles. For future use?
async function storeArticlesIfItDoesNotExists(data, RequestId) { function storeArticlesIfItDoesNotExists(data, RequestId) {
try {
setImmediate(async () => {
try {
const checkDataIsInDatabase = await sql` const checkDataIsInDatabase = await sql`
SELECT * FROM news_articles SELECT uuid FROM news_articles
WHERE jsondata = ${data} WHERE article_id = ${RequestId}
LIMIT 1
`; `;
if (checkDataIsInDatabase.length > 0) { if (checkDataIsInDatabase.length > 0) {
console.log(`Article ${RequestId} already exists in database`);
return; return;
} }
const storeData = await sql` const jsonData = JSON.stringify(data);
await sql`
INSERT INTO news_articles (uuid, article_id, jsondata) INSERT INTO news_articles (uuid, article_id, jsondata)
VALUES (${uuidv4()}, ${RequestId}, ${data}::JSON) VALUES (${uuidv4()}, ${RequestId}, ${jsonData}::JSON)
`; `;
console.log(storeData); console.log(`Successfully archived article ${RequestId} in background`);
return; } catch (error) {
console.error("Failed to archive article in background:", error);
}
});
} catch (error) {
console.error("Failed to initiate background archiving:", error);
}
} }
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {