Files
hideyoshi-blog/src/lib/feature/article/article.external.ts
Vitor Hideyoshi e2960027f2
All checks were successful
Build and Test / run-test (20.x) (push) Successful in 2m5s
refactor: update article service methods to use external ID and improve caching
2026-04-17 01:43:31 -03:00

123 lines
3.9 KiB
TypeScript

'use server';
import {
ArticleModel,
CreateArticleModel,
PaginatedArticlesResult,
UpdateArticleModel,
} 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 { UUIDv4 } from '@/utils/types/uuid';
import { revalidateTag } from 'next/cache';
export const getArticleByExternalId: (
externalId: UUIDv4
) => Promise<TypedResult<ArticleModel | null>> = wrap(
async (externalId: UUIDv4): Promise<ArticleModel | null> => {
const result = await service.getArticleByExternalId(externalId);
if (!result.ok) throw result.error;
return result.value;
}
);
export const getArticleBySlug: (
slug: string
) => Promise<TypedResult<ArticleModel | null>> = wrap(
async (slug: string): Promise<ArticleModel | null> => {
const result = await service.getArticleBySlug(slug);
if (!result.ok) throw result.error;
return result.value;
}
);
export const getArticlesPaginated: (
page?: number,
pageSize?: number
) => Promise<TypedResult<PaginatedArticlesResult>> = wrap(
async (
page: number = 1,
pageSize: number = 10
): Promise<PaginatedArticlesResult> => {
const result = await service.getArticlesPaginated(page, pageSize);
if (!result.ok) throw result.error;
return result.value;
}
);
export const saveArticle: (
article: CreateArticleModel
) => Promise<TypedResult<ArticleModel>> = wrap(
async (article: CreateArticleModel): Promise<ArticleModel> => {
const session = await getSessionData();
if (!session || !session?.user || session?.user.role !== 'admin') {
throw new Error(
'Unauthorized: Only admin users can save articles.'
);
}
article.authorId = session.user.id;
const result = await service.saveArticle(article);
if (!result.ok) throw result.error;
revalidateTag('articles', 'max');
return result.value;
}
);
export const updateArticleByExternalId: (
externalId: UUIDv4,
article: UpdateArticleModel
) => Promise<TypedResult<ArticleModel>> = wrap(
async (
externalId: UUIDv4,
article: UpdateArticleModel
): Promise<ArticleModel> => {
const session = await getSessionData();
if (!session || !session?.user || session?.user.role !== 'admin') {
throw new Error(
'Unauthorized: Only admin users can save articles.'
);
}
const result = await service.updateArticleByExternalId(
externalId,
article
);
if (!result.ok) throw result.error;
revalidateTag('articles', 'max');
revalidateTag(`article:${externalId}`, 'max');
revalidateTag(`article:slug:${result.value.slug}`, 'max');
return result.value;
}
);
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(
'Unauthorized: Only admin users can delete articles.'
);
}
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;
revalidateTag('articles', 'max');
revalidateTag(`article:${externalId}`, 'max');
revalidateTag(`article:slug:${article.slug}`, 'max');
}
);