feat: wrap article and user service functions with TypedResult for improved error handling

This commit is contained in:
2026-04-11 01:48:09 -03:00
parent 94e8058880
commit af17b6dc5a
10 changed files with 363 additions and 240 deletions

View File

@@ -8,28 +8,35 @@ 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 { UUIDv4 } from '@/utils/types/uuid';
export const getArticleByExternalId = async (
const _getArticleByExternalId = async (
externalId: UUIDv4
): Promise<ArticleModel | null> => {
return await service.getArticleByExternalId(externalId);
const result = await service.getArticleByExternalId(externalId);
if (!result.ok) throw result.error;
return result.value;
};
export const getArticleBySlug = async (
const _getArticleBySlug = async (
slug: string
): Promise<ArticleModel | null> => {
return await service.getArticleBySlug(slug);
const result = await service.getArticleBySlug(slug);
if (!result.ok) throw result.error;
return result.value;
};
export const getArticlesPaginated = async (
const _getArticlesPaginated = async (
page: number = 1,
pageSize: number = 10
): Promise<PaginatedArticlesResult> => {
return await service.getArticlesPaginated(page, pageSize);
const result = await service.getArticlesPaginated(page, pageSize);
if (!result.ok) throw result.error;
return result.value;
};
export const saveArticle = async (
const _saveArticle = async (
article: CreateArticleModel
): Promise<ArticleModel> => {
const session = await getSessionData();
@@ -38,10 +45,12 @@ export const saveArticle = async (
}
article.authorId = session.user.id;
return await service.saveArticle(article);
const result = await service.saveArticle(article);
if (!result.ok) throw result.error;
return result.value;
};
export const updateArticle = async (
const _updateArticle = async (
articleId: string,
article: UpdateArticleModel
): Promise<ArticleModel> => {
@@ -49,13 +58,45 @@ export const updateArticle = async (
if (!session || !session?.user || session?.user.role !== 'admin') {
throw new Error('Unauthorized: Only admin users can save articles.');
}
return await service.updateArticle(articleId, article);
const result = await service.updateArticle(articleId, article);
if (!result.ok) throw result.error;
return result.value;
};
export const deleteArticle = async (articleId: string): Promise<void> => {
const _deleteArticle = async (articleId: string): Promise<void> => {
const session = await getSessionData();
if (!session || !session?.user || session?.user.role !== 'admin') {
throw new Error('Unauthorized: Only admin users can delete articles.');
}
await service.deleteArticle(articleId);
const result = await service.deleteArticle(articleId);
if (!result.ok) throw result.error;
};
export const getArticleByExternalId: (
externalId: UUIDv4
) => Promise<TypedResult<ArticleModel | null>> = wrap(_getArticleByExternalId);
export const getArticleBySlug: (
slug: string
) => Promise<TypedResult<ArticleModel | null>> = wrap(_getArticleBySlug);
export const getArticlesPaginated: (
page?: number,
pageSize?: number
) => Promise<TypedResult<PaginatedArticlesResult>> = wrap(
_getArticlesPaginated
);
export const saveArticle: (
article: CreateArticleModel
) => Promise<TypedResult<ArticleModel>> = wrap(_saveArticle);
export const updateArticle: (
articleId: string,
article: UpdateArticleModel
) => Promise<TypedResult<ArticleModel>> = wrap(_updateArticle);
export const deleteArticle: (articleId: string) => Promise<TypedResult<void>> =
wrap(_deleteArticle);