refactor: update article deletion and retrieval methods to use external ID

This commit is contained in:
2026-04-17 00:12:44 -03:00
parent 873e372bad
commit b9e34e590d
11 changed files with 134 additions and 115 deletions

View File

@@ -8,43 +8,56 @@ import {
} from '@/lib/feature/article/article.model';
import * as service from '@/lib/feature/article/article.service';
import { getSessionData } from '@/lib/session/session-storage';
import { TypedResult, wrap } from '@/utils/types/results';
import { TypedResult, wrap, wrapCached } from '@/utils/types/results';
import { UUIDv4 } from '@/utils/types/uuid';
import { revalidatePath } from 'next/cache';
import { revalidateTag } from 'next/cache';
export const getArticleByExternalId: (
externalId: UUIDv4
) => Promise<TypedResult<ArticleModel | null>> = wrap(
) => Promise<TypedResult<ArticleModel | null>> = wrapCached(
async (externalId: UUIDv4): Promise<ArticleModel | null> => {
const result = await service.getArticleByExternalId(externalId);
if (!result.ok) throw result.error;
return result.value;
},
{
key: (id) => [`article:${id}`],
tags: (id) => ['articles', `article:${id}`],
}
);
export const getArticleBySlug: (
slug: string
) => Promise<TypedResult<ArticleModel | null>> = wrap(
) => Promise<TypedResult<ArticleModel | null>> = wrapCached(
async (slug: string): Promise<ArticleModel | null> => {
const result = await service.getArticleBySlug(slug);
if (!result.ok) throw result.error;
return result.value;
},
{
key: (slug) => [`article:slug:${slug}`],
tags: (slug) => ['articles', `article:slug:${slug}`],
}
);
export const getArticlesPaginated: (
page?: number,
pageSize?: number
) => Promise<TypedResult<PaginatedArticlesResult>> = wrap(
) => Promise<TypedResult<PaginatedArticlesResult>> = wrapCached(
async (
page: number = 1,
pageSize: number = 10
): Promise<PaginatedArticlesResult> => {
// await new Promise((r) => setTimeout(r, 1000));
const result = await service.getArticlesPaginated(page, pageSize);
if (!result.ok) throw result.error;
return result.value;
},
{
key: (page, pageSize) => [`articles:page:${page}:${pageSize}`],
tags: (page, pageSize) => [
'articles',
`articles:page:${page}-${pageSize}`,
],
}
);
@@ -62,6 +75,8 @@ export const saveArticle: (
const result = await service.saveArticle(article);
if (!result.ok) throw result.error;
revalidateTag('articles', 'max');
return result.value;
}
);
@@ -83,13 +98,19 @@ export const updateArticle: (
const result = await service.updateArticle(articleId, article);
if (!result.ok) throw result.error;
revalidatePath('/admin');
revalidateTag('articles', 'max');
revalidateTag(`article:${articleId}`, 'max');
revalidateTag(`article:slug:${result.value.slug}`, 'max');
return result.value;
}
);
export const deleteArticle: (articleId: string) => Promise<TypedResult<void>> =
wrap(async (articleId: string): Promise<void> => {
export const deleteArticleByExternalId: (
externalId: UUIDv4
) => Promise<TypedResult<void>> = wrap(
async (externalId: UUIDv4): Promise<void> => {
const session = await getSessionData();
if (!session || !session?.user || session?.user.role !== 'admin') {
throw new Error(
@@ -97,7 +118,17 @@ export const deleteArticle: (articleId: string) => Promise<TypedResult<void>> =
);
}
const result = await service.deleteArticle(articleId);
const getResult = await service.getArticleByExternalId(externalId);
if (!getResult.ok) throw getResult.error;
const article = getResult.value;
if (!article) throw new Error('Article not found');
const result = await service.deleteArticleByExternalId(externalId);
if (!result.ok) throw result.error;
revalidatePath('/admin');
});
revalidateTag('articles', 'max');
revalidateTag(`article:${externalId}`, 'max');
revalidateTag(`article:slug:${article.slug}`, 'max');
}
);