Compare commits
4 Commits
94e8058880
...
9d2ce50b26
| Author | SHA1 | Date | |
|---|---|---|---|
| 9d2ce50b26 | |||
|
3addd38bba
|
|||
|
3ea1112369
|
|||
|
af17b6dc5a
|
@@ -70,7 +70,9 @@ const ArticleContentSkeleton = () => (
|
||||
|
||||
const ArticleContent = async ({ params }: ArticleContentProps) => {
|
||||
const { slug } = await params;
|
||||
const article = await getArticleBySlug(slug);
|
||||
const articleResult = await getArticleBySlug(slug);
|
||||
if (!articleResult.ok) throw articleResult.error;
|
||||
const article = articleResult.value;
|
||||
|
||||
if (!article) notFound();
|
||||
|
||||
|
||||
@@ -45,11 +45,9 @@ const ArticleList = async ({ searchParams }: ArticleListProps) => {
|
||||
const page = Math.max(1, Number(pageParam) || 1);
|
||||
const pageSize = Number(pageSizeParam) || PAGE_SIZE;
|
||||
|
||||
const {
|
||||
data: articles,
|
||||
totalPages,
|
||||
total,
|
||||
} = await getArticlesPaginated(page, pageSize);
|
||||
const paginationResult = await getArticlesPaginated(page, pageSize);
|
||||
if (!paginationResult.ok) throw paginationResult.error;
|
||||
const { data: articles, totalPages, total } = paginationResult.value;
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
@@ -15,8 +15,12 @@ export async function GET() {
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
const syncedUser = await syncUser(parsedClaims);
|
||||
const syncResult = await syncUser(parsedClaims);
|
||||
if (!syncResult.ok) {
|
||||
console.error(syncResult.error);
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
await setSessionData('user', syncedUser);
|
||||
await setSessionData('user', syncResult.value);
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
@@ -8,54 +8,91 @@ 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 (
|
||||
export const getArticleByExternalId: (
|
||||
externalId: UUIDv4
|
||||
): Promise<ArticleModel | null> => {
|
||||
return await service.getArticleByExternalId(externalId);
|
||||
};
|
||||
) => 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 = async (
|
||||
export const getArticleBySlug: (
|
||||
slug: string
|
||||
): Promise<ArticleModel | null> => {
|
||||
return await service.getArticleBySlug(slug);
|
||||
};
|
||||
) => 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 = async (
|
||||
export const getArticlesPaginated: (
|
||||
page?: number,
|
||||
pageSize?: number
|
||||
) => Promise<TypedResult<PaginatedArticlesResult>> = wrap(
|
||||
async (
|
||||
page: number = 1,
|
||||
pageSize: number = 10
|
||||
): Promise<PaginatedArticlesResult> => {
|
||||
return await service.getArticlesPaginated(page, pageSize);
|
||||
};
|
||||
): Promise<PaginatedArticlesResult> => {
|
||||
const result = await service.getArticlesPaginated(page, pageSize);
|
||||
if (!result.ok) throw result.error;
|
||||
return result.value;
|
||||
}
|
||||
);
|
||||
|
||||
export const saveArticle = async (
|
||||
export const saveArticle: (
|
||||
article: CreateArticleModel
|
||||
): Promise<ArticleModel> => {
|
||||
) => 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.');
|
||||
throw new Error(
|
||||
'Unauthorized: Only admin users can save articles.'
|
||||
);
|
||||
}
|
||||
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 (
|
||||
export const updateArticle: (
|
||||
articleId: string,
|
||||
article: UpdateArticleModel
|
||||
): Promise<ArticleModel> => {
|
||||
) => Promise<TypedResult<ArticleModel>> = wrap(
|
||||
async (
|
||||
articleId: string,
|
||||
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.');
|
||||
throw new Error(
|
||||
'Unauthorized: Only admin users can save articles.'
|
||||
);
|
||||
}
|
||||
return await service.updateArticle(articleId, article);
|
||||
};
|
||||
|
||||
export const deleteArticle = async (articleId: string): Promise<void> => {
|
||||
const result = await service.updateArticle(articleId, article);
|
||||
if (!result.ok) throw result.error;
|
||||
return result.value;
|
||||
}
|
||||
);
|
||||
|
||||
export const deleteArticle: (articleId: string) => Promise<TypedResult<void>> =
|
||||
wrap(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.');
|
||||
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;
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
PaginatedArticlesResult,
|
||||
UpdateArticleModel,
|
||||
} from '@/lib/feature/article/article.model';
|
||||
import { TypedResult, wrap } from '@/utils/types/results';
|
||||
import { UUIDv4 } from '@/utils/types/uuid';
|
||||
|
||||
export const articleEntityToModel = (
|
||||
@@ -25,13 +26,11 @@ export const articleEntityToModel = (
|
||||
};
|
||||
};
|
||||
|
||||
/** Retrieves an artible by its external ID.
|
||||
* @param externalId - The external ID of the article to retrieve.
|
||||
* @returns {Promise<ArticleModel | null>} The article model if found, otherwise null.
|
||||
* */
|
||||
export const getArticleByExternalId = async (
|
||||
/** Retrieves an article by its external ID. */
|
||||
export const getArticleByExternalId: (
|
||||
externalId: UUIDv4
|
||||
): Promise<ArticleModel | null> => {
|
||||
) => Promise<TypedResult<ArticleModel | null>> = wrap(
|
||||
async (externalId: UUIDv4): Promise<ArticleModel | null> => {
|
||||
const articleRepository = await getRepository(ArticleEntity);
|
||||
|
||||
const articleEntity = await articleRepository.findOneBy({
|
||||
@@ -43,16 +42,14 @@ export const getArticleByExternalId = async (
|
||||
}
|
||||
|
||||
return articleEntityToModel(articleEntity);
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Retrieves an article by its slug.
|
||||
* @param slug - The slug of the article to retrieve.
|
||||
* @returns {Promise<ArticleModel | null>} The article model if found, otherwise null.
|
||||
*/
|
||||
export const getArticleBySlug = async (
|
||||
/** Retrieves an article by its slug. */
|
||||
export const getArticleBySlug: (
|
||||
slug: string
|
||||
): Promise<ArticleModel | null> => {
|
||||
) => Promise<TypedResult<ArticleModel | null>> = wrap(
|
||||
async (slug: string): Promise<ArticleModel | null> => {
|
||||
const articleRepository = await getRepository(ArticleEntity);
|
||||
|
||||
const articleEntity = await articleRepository.findOneBy({ slug });
|
||||
@@ -62,33 +59,31 @@ export const getArticleBySlug = async (
|
||||
}
|
||||
|
||||
return articleEntityToModel(articleEntity);
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Retrieves all articles by a given author ID.
|
||||
* @param authorId - The ID of the author.
|
||||
* @returns {Promise<ArticleModel[]>} A list of article models.
|
||||
*/
|
||||
export const getArticlesByAuthorId = async (
|
||||
/** Retrieves all articles by a given author ID. */
|
||||
export const getArticlesByAuthorId: (
|
||||
authorId: string
|
||||
): Promise<ArticleModel[]> => {
|
||||
) => Promise<TypedResult<ArticleModel[]>> = wrap(
|
||||
async (authorId: string): Promise<ArticleModel[]> => {
|
||||
const articleRepository = await getRepository(ArticleEntity);
|
||||
|
||||
const articleEntities = await articleRepository.findBy({ authorId });
|
||||
|
||||
return articleEntities.map(articleEntityToModel);
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Retrieves a paginated list of articles ordered by creation date descending.
|
||||
* @param page - The page number (1-based).
|
||||
* @param pageSize - The number of articles per page.
|
||||
* @returns {Promise<PaginatedArticlesResult>} The paginated result.
|
||||
*/
|
||||
export const getArticlesPaginated = async (
|
||||
/** Retrieves a paginated list of articles ordered by creation date descending. */
|
||||
export const getArticlesPaginated: (
|
||||
page?: number,
|
||||
pageSize?: number
|
||||
) => Promise<TypedResult<PaginatedArticlesResult>> = wrap(
|
||||
async (
|
||||
page: number = 1,
|
||||
pageSize: number = 10
|
||||
): Promise<PaginatedArticlesResult> => {
|
||||
): Promise<PaginatedArticlesResult> => {
|
||||
const articleRepository = await getRepository(ArticleEntity);
|
||||
|
||||
const [articleEntities, total] = await articleRepository.findAndCount({
|
||||
@@ -104,17 +99,14 @@ export const getArticlesPaginated = async (
|
||||
pageSize,
|
||||
totalPages: Math.ceil(total / pageSize),
|
||||
};
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Saves a new article to the database.
|
||||
* @param article - The article data to save.
|
||||
* @returns {Promise<ArticleModel>} The saved article model.
|
||||
* @throws {Error} If an article with the same slug already exists.
|
||||
*/
|
||||
export const saveArticle = async (
|
||||
/** Saves a new article to the database. */
|
||||
export const saveArticle: (
|
||||
article: CreateArticleModel
|
||||
): Promise<ArticleModel> => {
|
||||
) => Promise<TypedResult<ArticleModel>> = wrap(
|
||||
async (article: CreateArticleModel): Promise<ArticleModel> => {
|
||||
const articleRepository = await getRepository(ArticleEntity);
|
||||
|
||||
if (!article.authorId) {
|
||||
@@ -127,19 +119,18 @@ export const saveArticle = async (
|
||||
|
||||
const newArticle = articleRepository.create(article);
|
||||
return articleEntityToModel(await articleRepository.save(newArticle));
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Updates an existing article in the database.
|
||||
* @param articleId - The ID of the article to update.
|
||||
* @param article - The new article data.
|
||||
* @returns {Promise<ArticleModel>} The updated article model.
|
||||
* @throws {Error} If the article with the given ID does not exist.
|
||||
*/
|
||||
export const updateArticle = async (
|
||||
/** Updates an existing article in the database. */
|
||||
export const updateArticle: (
|
||||
articleId: string,
|
||||
article: UpdateArticleModel
|
||||
): Promise<ArticleModel> => {
|
||||
) => Promise<TypedResult<ArticleModel>> = wrap(
|
||||
async (
|
||||
articleId: string,
|
||||
article: UpdateArticleModel
|
||||
): Promise<ArticleModel> => {
|
||||
const articleRepository = await getRepository(ArticleEntity);
|
||||
|
||||
const existingArticle = await articleRepository.findOneBy({
|
||||
@@ -157,15 +148,15 @@ export const updateArticle = async (
|
||||
existingArticle.coverImageUrl = article.coverImageUrl;
|
||||
if (!!article.content) existingArticle.content = article.content;
|
||||
|
||||
return articleEntityToModel(await articleRepository.save(existingArticle));
|
||||
};
|
||||
return articleEntityToModel(
|
||||
await articleRepository.save(existingArticle)
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Deletes an article from the database.
|
||||
* @param articleId - The ID of the article to delete.
|
||||
* @throws {Error} If the article with the given ID does not exist.
|
||||
*/
|
||||
export const deleteArticle = async (articleId: string): Promise<void> => {
|
||||
/** Deletes an article from the database. */
|
||||
export const deleteArticle: (articleId: string) => Promise<TypedResult<void>> =
|
||||
wrap(async (articleId: string): Promise<void> => {
|
||||
const articleRepository = await getRepository(ArticleEntity);
|
||||
|
||||
const existingArticle = await articleRepository.findOneBy({
|
||||
@@ -176,4 +167,4 @@ export const deleteArticle = async (articleId: string): Promise<void> => {
|
||||
}
|
||||
|
||||
await articleRepository.remove(existingArticle);
|
||||
};
|
||||
});
|
||||
|
||||
@@ -2,11 +2,16 @@
|
||||
|
||||
import { getRepository } from '@/lib/db/client';
|
||||
import { UserEntity } from '@/lib/db/entities';
|
||||
import { UserModel } from '@/lib/feature/user/user.model';
|
||||
import { userEntityToModel } from '@/lib/feature/user/user.service';
|
||||
import { getSessionData } from '@/lib/session/session-storage';
|
||||
import { TypedResult, wrap } from '@/utils/types/results';
|
||||
import { UUIDv4 } from '@/utils/types/uuid';
|
||||
|
||||
export const getUserByExternalId = async (externalId: UUIDv4) => {
|
||||
export const getUserByExternalId: (
|
||||
externalId: UUIDv4
|
||||
) => Promise<TypedResult<UserModel | null>> = wrap(
|
||||
async (externalId: UUIDv4): Promise<UserModel | null> => {
|
||||
const sessionData = await getSessionData();
|
||||
if (
|
||||
!sessionData ||
|
||||
@@ -27,4 +32,5 @@ export const getUserByExternalId = async (externalId: UUIDv4) => {
|
||||
}
|
||||
|
||||
return userEntityToModel(userEntity);
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
UpdateUserModel,
|
||||
UserModel,
|
||||
} from '@/lib/feature/user/user.model';
|
||||
import { TypedResult, wrap } from '@/utils/types/results';
|
||||
|
||||
export const userEntityToModel = (userEntity: UserEntity): UserModel => {
|
||||
return {
|
||||
@@ -20,11 +21,10 @@ export const userEntityToModel = (userEntity: UserEntity): UserModel => {
|
||||
/**
|
||||
* Retrieves a user by their email address.
|
||||
* @param email - The email address of the user to retrieve.
|
||||
* @returns {Promise<UserModel | null>} The user model if found, otherwise null.
|
||||
* @returns {Promise<TypedResult<UserModel | null>>} The user model if found, otherwise null.
|
||||
*/
|
||||
export const getUserByEmail = async (
|
||||
email: string
|
||||
): Promise<UserModel | null> => {
|
||||
export const getUserByEmail = wrap(
|
||||
async (email: string): Promise<UserModel | null> => {
|
||||
const userRepository = await getRepository(UserEntity);
|
||||
|
||||
const userEntity = await userRepository.findOneBy({ email });
|
||||
@@ -34,15 +34,16 @@ export const getUserByEmail = async (
|
||||
}
|
||||
|
||||
return userEntityToModel(userEntity);
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Saves a new user to the database.
|
||||
* @param user - The user data to save.
|
||||
* @returns {Promise<UserModel>} The saved user model.
|
||||
* @throws {Error} If a user with the same email already exists.
|
||||
* @returns {Promise<TypedResult<UserModel>>} The saved user model.
|
||||
*/
|
||||
export const saveUser = async (user: CreateUserModel): Promise<UserModel> => {
|
||||
export const saveUser = wrap(
|
||||
async (user: CreateUserModel): Promise<UserModel> => {
|
||||
const userRepository = await getRepository(UserEntity);
|
||||
|
||||
if (!!(await userRepository.findOneBy({ email: user.email }))) {
|
||||
@@ -51,19 +52,17 @@ export const saveUser = async (user: CreateUserModel): Promise<UserModel> => {
|
||||
|
||||
const newUser = userRepository.create(user);
|
||||
return userEntityToModel(await userRepository.save(newUser));
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Updates an existing user in the database.
|
||||
* @param userId - The ID of the user to update.
|
||||
* @param user - The new user data.
|
||||
* @returns {Promise<UserModel>} The updated user model.
|
||||
* @throws {Error} If the user with the given ID does not exist.
|
||||
* @returns {Promise<TypedResult<UserModel>>} The updated user model.
|
||||
*/
|
||||
export const updateUser = async (
|
||||
userId: string,
|
||||
user: UpdateUserModel
|
||||
): Promise<UserModel> => {
|
||||
export const updateUser = wrap(
|
||||
async (userId: string, user: UpdateUserModel): Promise<UserModel> => {
|
||||
const userRepository = await getRepository(UserEntity);
|
||||
|
||||
const existingUser = await userRepository.findOneBy({ id: userId });
|
||||
@@ -76,35 +75,47 @@ export const updateUser = async (
|
||||
if (!!user.role) existingUser.role = user.role;
|
||||
|
||||
return userEntityToModel(await userRepository.save(existingUser));
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Synchronizes a user with the database.
|
||||
* If the user already exists, it skips saving and returns the existing user.
|
||||
* If the user does not exist, it creates a new user record.
|
||||
* @returns {Promise<UserModel>} The synchronized user model.
|
||||
* @throws {Error} If the user email is not provided or if there is an issue
|
||||
* saving the user.
|
||||
* If the user already exists, updates it; if not, creates a new record.
|
||||
* @param sessionClaims Session Claims from the Auth Provider
|
||||
* @returns {Promise<TypedResult<UserModel>>} The synchronized user model.
|
||||
*/
|
||||
export const syncUser = async (
|
||||
sessionClaims: SessionClaims
|
||||
): Promise<UserModel> => {
|
||||
export const syncUser = wrap(
|
||||
async (sessionClaims: SessionClaims): Promise<UserModel> => {
|
||||
const { full_name, email } = sessionClaims.user;
|
||||
const role = sessionClaims.user.public_metadata.role;
|
||||
|
||||
const existingUser = await getUserByEmail(email);
|
||||
if (!existingUser) {
|
||||
return await saveUser({
|
||||
const existingUserResult = await getUserByEmail(email);
|
||||
if (!existingUserResult.ok || !existingUserResult.value) {
|
||||
const saveResult = await saveUser({
|
||||
name: full_name,
|
||||
email: email,
|
||||
role: role,
|
||||
});
|
||||
if (!saveResult.ok) {
|
||||
throw new Error(`User with email ${email} already exists`);
|
||||
}
|
||||
return saveResult.value;
|
||||
}
|
||||
const existingUser = existingUserResult.value;
|
||||
|
||||
return await updateUser(existingUser.id, {
|
||||
const updateResult = await updateUser(existingUser.id, {
|
||||
name: full_name,
|
||||
email: existingUser.email,
|
||||
role: role,
|
||||
});
|
||||
};
|
||||
if (!updateResult.ok) {
|
||||
throw new Error(
|
||||
`Failed to update user with email ${email}: ${updateResult.error}`
|
||||
);
|
||||
}
|
||||
return updateResult.value;
|
||||
}
|
||||
);
|
||||
|
||||
// Explicit re-export for TypeScript consumers who need the result type
|
||||
export type { TypedResult };
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
'use server';
|
||||
|
||||
import { getSessionData } from '@/lib/session/session-storage';
|
||||
import { createStorageProvider } from '@/lib/storage/storage.factory';
|
||||
import { StorageProvider } from '@/lib/storage/storage.interface';
|
||||
import { TypedResult } from '@/utils/types/results';
|
||||
@@ -13,6 +14,10 @@ export const getSignedUrl = async (
|
||||
if (!storageProvider) {
|
||||
storageProvider = storage;
|
||||
}
|
||||
const session = await getSessionData();
|
||||
if (!session || !session?.user || session?.user.role !== 'admin') {
|
||||
throw new Error('Unauthorized: Only admin users can delete articles.');
|
||||
}
|
||||
return await storageProvider.get(key);
|
||||
};
|
||||
|
||||
@@ -23,6 +28,10 @@ export const checkExists = async (
|
||||
if (!storageProvider) {
|
||||
storageProvider = storage;
|
||||
}
|
||||
const session = await getSessionData();
|
||||
if (!session || !session?.user || session?.user.role !== 'admin') {
|
||||
throw new Error('Unauthorized: Only admin users can delete articles.');
|
||||
}
|
||||
return await storageProvider.exists(key);
|
||||
};
|
||||
|
||||
@@ -34,6 +43,10 @@ export const getPutUrl = async (
|
||||
if (!storageProvider) {
|
||||
storageProvider = storage;
|
||||
}
|
||||
const session = await getSessionData();
|
||||
if (!session || !session?.user || session?.user.role !== 'admin') {
|
||||
throw new Error('Unauthorized: Only admin users can delete articles.');
|
||||
}
|
||||
return await storageProvider.put(key, contentType);
|
||||
};
|
||||
|
||||
@@ -44,5 +57,9 @@ export const deleteByKey = async (
|
||||
if (!storageProvider) {
|
||||
storageProvider = storage;
|
||||
}
|
||||
const session = await getSessionData();
|
||||
if (!session || !session?.user || session?.user.role !== 'admin') {
|
||||
throw new Error('Unauthorized: Only admin users can delete articles.');
|
||||
}
|
||||
return await storageProvider.delete(key);
|
||||
};
|
||||
|
||||
@@ -101,23 +101,20 @@ export const CreateArticleForm = () => {
|
||||
|
||||
const handleFormSubmit = useCallback(
|
||||
async (data: z.infer<typeof formSchema>) => {
|
||||
try {
|
||||
const result = await saveArticle({ ...data });
|
||||
if (!result.ok) {
|
||||
toast.error('Failed to create article', {
|
||||
description: result.error.message,
|
||||
position: 'bottom-right',
|
||||
});
|
||||
return;
|
||||
}
|
||||
toast.success('Article created successfully!', {
|
||||
description: `Article "${result.title}" has been created.`,
|
||||
description: `Article "${result.value.title}" has been created.`,
|
||||
position: 'bottom-right',
|
||||
});
|
||||
form.reset();
|
||||
resetFiles();
|
||||
} catch (error) {
|
||||
toast.error('Failed to create article', {
|
||||
description:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: 'An error occurred',
|
||||
position: 'bottom-right',
|
||||
});
|
||||
}
|
||||
},
|
||||
[form, resetFiles]
|
||||
);
|
||||
|
||||
@@ -34,7 +34,8 @@ describe('ArticleService', () => {
|
||||
role: 'admin',
|
||||
};
|
||||
const savedAuthor = await saveUser(author);
|
||||
authorId = savedAuthor.id;
|
||||
if (!savedAuthor.ok) throw savedAuthor.error;
|
||||
authorId = savedAuthor.value.id;
|
||||
}, 1_000_000);
|
||||
|
||||
afterAll(async () => {
|
||||
@@ -51,16 +52,18 @@ describe('ArticleService', () => {
|
||||
authorId: authorId,
|
||||
};
|
||||
|
||||
const savedArticle = await saveArticle(articleToSave);
|
||||
const result = await saveArticle(articleToSave);
|
||||
|
||||
expect(savedArticle.id).toBeDefined();
|
||||
expect(savedArticle.title).toBe(articleToSave.title);
|
||||
expect(savedArticle.slug).toBe(articleToSave.slug);
|
||||
expect(savedArticle.description).toBe(articleToSave.description);
|
||||
expect(savedArticle.coverImageUrl).toBe(articleToSave.coverImageUrl);
|
||||
expect(savedArticle.content).toBe(articleToSave.content);
|
||||
expect(savedArticle.authorId).toBe(authorId);
|
||||
expect(savedArticle.externalId).toBeDefined();
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.id).toBeDefined();
|
||||
expect(result.value.title).toBe(articleToSave.title);
|
||||
expect(result.value.slug).toBe(articleToSave.slug);
|
||||
expect(result.value.description).toBe(articleToSave.description);
|
||||
expect(result.value.coverImageUrl).toBe(articleToSave.coverImageUrl);
|
||||
expect(result.value.content).toBe(articleToSave.content);
|
||||
expect(result.value.authorId).toBe(authorId);
|
||||
expect(result.value.externalId).toBeDefined();
|
||||
});
|
||||
|
||||
test('cannot save article with existing slug', async () => {
|
||||
@@ -72,77 +75,99 @@ describe('ArticleService', () => {
|
||||
content: 'Duplicate content.',
|
||||
authorId: authorId,
|
||||
};
|
||||
await expect(saveArticle(articleToSave)).rejects.toThrow(
|
||||
`Article with slug ${articleToSave.slug} already exists`
|
||||
);
|
||||
|
||||
const result = await saveArticle(articleToSave);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.message).toContain(articleToSave.slug);
|
||||
});
|
||||
|
||||
test('can getArticleBySlug', async () => {
|
||||
const article = await getArticleBySlug('test-article');
|
||||
const result = await getArticleBySlug('test-article');
|
||||
|
||||
expect(article).toBeDefined();
|
||||
expect(article?.slug).toBe('test-article');
|
||||
expect(article?.title).toBe('Test Article');
|
||||
expect(article?.authorId).toBe(authorId);
|
||||
expect(article?.externalId).toBeDefined();
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value).toBeDefined();
|
||||
expect(result.value?.slug).toBe('test-article');
|
||||
expect(result.value?.title).toBe('Test Article');
|
||||
expect(result.value?.authorId).toBe(authorId);
|
||||
expect(result.value?.externalId).toBeDefined();
|
||||
});
|
||||
|
||||
test('cannot getArticleBySlug with non-existing slug', async () => {
|
||||
await expect(getArticleBySlug('non-existing-slug')).resolves.toBeNull();
|
||||
const result = await getArticleBySlug('non-existing-slug');
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value).toBeNull();
|
||||
});
|
||||
|
||||
test('can getArticlesByAuthorId', async () => {
|
||||
const articles = await getArticlesByAuthorId(authorId);
|
||||
const result = await getArticlesByAuthorId(authorId);
|
||||
|
||||
expect(articles).toBeDefined();
|
||||
expect(articles.length).toBeGreaterThanOrEqual(1);
|
||||
expect(articles[0].authorId).toBe(authorId);
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value).toBeDefined();
|
||||
expect(result.value.length).toBeGreaterThanOrEqual(1);
|
||||
expect(result.value[0].authorId).toBe(authorId);
|
||||
});
|
||||
|
||||
test('getArticlesByAuthorId returns empty for non-existing author', async () => {
|
||||
const articles = await getArticlesByAuthorId('9999');
|
||||
const result = await getArticlesByAuthorId('9999');
|
||||
|
||||
expect(articles).toBeDefined();
|
||||
expect(articles.length).toBe(0);
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value).toBeDefined();
|
||||
expect(result.value.length).toBe(0);
|
||||
});
|
||||
|
||||
test('can getArticleByExternalId', async () => {
|
||||
const article = await getArticleBySlug('test-article');
|
||||
const slugResult = await getArticleBySlug('test-article');
|
||||
expect(slugResult.ok).toBe(true);
|
||||
if (!slugResult.ok) return;
|
||||
const article = slugResult.value;
|
||||
expect(article).toBeDefined();
|
||||
|
||||
const foundArticle = await getArticleByExternalId(
|
||||
const result = await getArticleByExternalId(
|
||||
article!.externalId as UUIDv4
|
||||
);
|
||||
|
||||
expect(foundArticle).toBeDefined();
|
||||
expect(foundArticle?.id).toBe(article!.id);
|
||||
expect(foundArticle?.title).toBe(article!.title);
|
||||
expect(foundArticle?.slug).toBe(article!.slug);
|
||||
expect(foundArticle?.externalId).toBe(article!.externalId);
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value).toBeDefined();
|
||||
expect(result.value?.id).toBe(article!.id);
|
||||
expect(result.value?.title).toBe(article!.title);
|
||||
expect(result.value?.slug).toBe(article!.slug);
|
||||
expect(result.value?.externalId).toBe(article!.externalId);
|
||||
});
|
||||
|
||||
test('getArticleByExternalId returns null for non-existing id', async () => {
|
||||
const result = await getArticleByExternalId(
|
||||
'00000000-0000-4000-a000-000000000000' as UUIDv4
|
||||
);
|
||||
expect(result).toBeNull();
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value).toBeNull();
|
||||
});
|
||||
|
||||
test('can getArticlesPaginated with defaults', async () => {
|
||||
const result = await getArticlesPaginated();
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result.data.length).toBeGreaterThanOrEqual(1);
|
||||
expect(result.page).toBe(1);
|
||||
expect(result.pageSize).toBe(10);
|
||||
expect(result.total).toBeGreaterThanOrEqual(1);
|
||||
expect(result.totalPages).toBeGreaterThanOrEqual(1);
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.data.length).toBeGreaterThanOrEqual(1);
|
||||
expect(result.value.page).toBe(1);
|
||||
expect(result.value.pageSize).toBe(10);
|
||||
expect(result.value.total).toBeGreaterThanOrEqual(1);
|
||||
expect(result.value.totalPages).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
|
||||
test('can getArticlesPaginated with custom page size', async () => {
|
||||
// Save extra articles to test pagination
|
||||
for (let i = 0; i < 3; i++) {
|
||||
await saveArticle({
|
||||
const r = await saveArticle({
|
||||
title: `Paginated Article ${i}`,
|
||||
slug: `paginated-article-${i}`,
|
||||
description: `Description ${i}`,
|
||||
@@ -150,29 +175,36 @@ describe('ArticleService', () => {
|
||||
content: `Content ${i}`,
|
||||
authorId: authorId,
|
||||
});
|
||||
if (!r.ok) throw r.error;
|
||||
}
|
||||
|
||||
const firstPage = await getArticlesPaginated(1, 2);
|
||||
const firstPageResult = await getArticlesPaginated(1, 2);
|
||||
|
||||
expect(firstPage.data.length).toBe(2);
|
||||
expect(firstPage.page).toBe(1);
|
||||
expect(firstPage.pageSize).toBe(2);
|
||||
expect(firstPage.total).toBeGreaterThanOrEqual(4);
|
||||
expect(firstPage.totalPages).toBeGreaterThanOrEqual(2);
|
||||
expect(firstPageResult.ok).toBe(true);
|
||||
if (!firstPageResult.ok) return;
|
||||
expect(firstPageResult.value.data.length).toBe(2);
|
||||
expect(firstPageResult.value.page).toBe(1);
|
||||
expect(firstPageResult.value.pageSize).toBe(2);
|
||||
expect(firstPageResult.value.total).toBeGreaterThanOrEqual(4);
|
||||
expect(firstPageResult.value.totalPages).toBeGreaterThanOrEqual(2);
|
||||
|
||||
const secondPage = await getArticlesPaginated(2, 2);
|
||||
const secondPageResult = await getArticlesPaginated(2, 2);
|
||||
|
||||
expect(secondPage.data.length).toBe(2);
|
||||
expect(secondPage.page).toBe(2);
|
||||
expect(secondPage.pageSize).toBe(2);
|
||||
expect(secondPageResult.ok).toBe(true);
|
||||
if (!secondPageResult.ok) return;
|
||||
expect(secondPageResult.value.data.length).toBe(2);
|
||||
expect(secondPageResult.value.page).toBe(2);
|
||||
expect(secondPageResult.value.pageSize).toBe(2);
|
||||
});
|
||||
|
||||
test('can getArticlesPaginated returns empty on out-of-range page', async () => {
|
||||
const result = await getArticlesPaginated(999, 10);
|
||||
|
||||
expect(result.data.length).toBe(0);
|
||||
expect(result.page).toBe(999);
|
||||
expect(result.total).toBeGreaterThanOrEqual(1);
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.data.length).toBe(0);
|
||||
expect(result.value.page).toBe(999);
|
||||
expect(result.value.total).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
|
||||
test('can update article', async () => {
|
||||
@@ -181,18 +213,23 @@ describe('ArticleService', () => {
|
||||
description: 'Updated description',
|
||||
};
|
||||
|
||||
const article = await getArticleBySlug('test-article');
|
||||
const slugResult = await getArticleBySlug('test-article');
|
||||
expect(slugResult.ok).toBe(true);
|
||||
if (!slugResult.ok) return;
|
||||
const article = slugResult.value;
|
||||
expect(article).toBeDefined();
|
||||
|
||||
const updatedArticle = await updateArticle(article!.id, dataToUpdate);
|
||||
const result = await updateArticle(article!.id, dataToUpdate);
|
||||
|
||||
expect(updatedArticle).toBeDefined();
|
||||
expect(updatedArticle.id).toBe(article!.id);
|
||||
expect(updatedArticle.title).toBe(dataToUpdate.title);
|
||||
expect(updatedArticle.description).toBe(dataToUpdate.description);
|
||||
expect(updatedArticle.slug).toBe(article!.slug);
|
||||
expect(updatedArticle.content).toBe(article!.content);
|
||||
expect(updatedArticle.externalId).toBeDefined();
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value).toBeDefined();
|
||||
expect(result.value.id).toBe(article!.id);
|
||||
expect(result.value.title).toBe(dataToUpdate.title);
|
||||
expect(result.value.description).toBe(dataToUpdate.description);
|
||||
expect(result.value.slug).toBe(article!.slug);
|
||||
expect(result.value.content).toBe(article!.content);
|
||||
expect(result.value.externalId).toBeDefined();
|
||||
});
|
||||
|
||||
test('cannot update non-existing article', async () => {
|
||||
@@ -200,9 +237,11 @@ describe('ArticleService', () => {
|
||||
title: 'Updated Article Title',
|
||||
};
|
||||
|
||||
await expect(updateArticle('9999', dataToUpdate)).rejects.toThrow(
|
||||
`Article with ID 9999 not found`
|
||||
);
|
||||
const result = await updateArticle('9999', dataToUpdate);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.message).toContain('9999');
|
||||
});
|
||||
|
||||
test('can delete article', async () => {
|
||||
@@ -215,18 +254,25 @@ describe('ArticleService', () => {
|
||||
authorId: authorId,
|
||||
};
|
||||
|
||||
const savedArticle = await saveArticle(articleToSave);
|
||||
expect(savedArticle.id).toBeDefined();
|
||||
const saveResult = await saveArticle(articleToSave);
|
||||
expect(saveResult.ok).toBe(true);
|
||||
if (!saveResult.ok) return;
|
||||
expect(saveResult.value.id).toBeDefined();
|
||||
|
||||
await deleteArticle(savedArticle.id);
|
||||
const deleteResult = await deleteArticle(saveResult.value.id);
|
||||
expect(deleteResult.ok).toBe(true);
|
||||
|
||||
const deletedArticle = await getArticleBySlug('article-to-delete');
|
||||
expect(deletedArticle).toBeNull();
|
||||
const getResult = await getArticleBySlug('article-to-delete');
|
||||
expect(getResult.ok).toBe(true);
|
||||
if (!getResult.ok) return;
|
||||
expect(getResult.value).toBeNull();
|
||||
});
|
||||
|
||||
test('cannot delete non-existing article', async () => {
|
||||
await expect(deleteArticle('9999')).rejects.toThrow(
|
||||
`Article with ID 9999 not found`
|
||||
);
|
||||
const result = await deleteArticle('9999');
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.message).toContain('9999');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -35,14 +35,15 @@ describe('UserService', () => {
|
||||
role: 'user',
|
||||
};
|
||||
|
||||
const savedUser = await saveUser(userToSave);
|
||||
const result = await saveUser(userToSave);
|
||||
|
||||
expect(savedUser.id).toBeDefined();
|
||||
expect(savedUser.name).toBe(userToSave.name);
|
||||
expect(savedUser.email).toBe(userToSave.email);
|
||||
expect(savedUser.role).toBe(userToSave.role);
|
||||
expect(savedUser.role).toBe(userToSave.role);
|
||||
expect(savedUser.externalId).toBeDefined(); // Default to true if not set
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.id).toBeDefined();
|
||||
expect(result.value.name).toBe(userToSave.name);
|
||||
expect(result.value.email).toBe(userToSave.email);
|
||||
expect(result.value.role).toBe(userToSave.role);
|
||||
expect(result.value.externalId).toBeDefined();
|
||||
});
|
||||
|
||||
test('cannot save user with existing email', async () => {
|
||||
@@ -51,23 +52,31 @@ describe('UserService', () => {
|
||||
email: 'test@email.com',
|
||||
role: 'user',
|
||||
};
|
||||
await expect(saveUser(userToSave)).rejects.toThrow(
|
||||
`User with email ${userToSave.email} already exists`
|
||||
);
|
||||
|
||||
const result = await saveUser(userToSave);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.message).toContain(userToSave.email);
|
||||
});
|
||||
|
||||
test('can getUserByEmail', async () => {
|
||||
const user = await getUserByEmail('test@email.com');
|
||||
const result = await getUserByEmail('test@email.com');
|
||||
|
||||
expect(user).toBeDefined();
|
||||
expect(user?.email).toBe('test@email.com');
|
||||
expect(user?.name).toBe('Test User');
|
||||
expect(user?.role).toBe('user');
|
||||
expect(user?.externalId).toBeDefined(); // Default to true if not set
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value?.email).toBe('test@email.com');
|
||||
expect(result.value?.name).toBe('Test User');
|
||||
expect(result.value?.role).toBe('user');
|
||||
expect(result.value?.externalId).toBeDefined();
|
||||
});
|
||||
|
||||
test('cannot getUserByEmail with non-existing email', async () => {
|
||||
await expect(getUserByEmail('missing@email.com')).resolves.toBeNull();
|
||||
const result = await getUserByEmail('missing@email.com');
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value).toBeNull();
|
||||
});
|
||||
|
||||
test('can update user', async () => {
|
||||
@@ -76,17 +85,21 @@ describe('UserService', () => {
|
||||
role: 'admin',
|
||||
};
|
||||
|
||||
const user = await getUserByEmail('test@email.com');
|
||||
const userResult = await getUserByEmail('test@email.com');
|
||||
expect(userResult.ok).toBe(true);
|
||||
if (!userResult.ok) return;
|
||||
const user = userResult.value;
|
||||
expect(user).toBeDefined();
|
||||
|
||||
const updatedUser = await updateUser(user!.id, dataToUpdate);
|
||||
const result = await updateUser(user!.id, dataToUpdate);
|
||||
|
||||
expect(updatedUser).toBeDefined();
|
||||
expect(updatedUser.id).toBe(user!.id);
|
||||
expect(updatedUser.name).toBe(dataToUpdate.name);
|
||||
expect(updatedUser.role).toBe(dataToUpdate.role);
|
||||
expect(updatedUser.email).toBe(user!.email);
|
||||
expect(updatedUser.externalId).toBeDefined(); // Default to true if not set
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.id).toBe(user!.id);
|
||||
expect(result.value.name).toBe(dataToUpdate.name);
|
||||
expect(result.value.role).toBe(dataToUpdate.role);
|
||||
expect(result.value.email).toBe(user!.email);
|
||||
expect(result.value.externalId).toBeDefined();
|
||||
});
|
||||
|
||||
test('cannot update non-existing user', async () => {
|
||||
@@ -95,9 +108,11 @@ describe('UserService', () => {
|
||||
role: 'admin',
|
||||
};
|
||||
|
||||
await expect(updateUser('9999', dataToUpdate)).rejects.toThrow(
|
||||
`User with ID 9999 not found`
|
||||
);
|
||||
const result = await updateUser('9999', dataToUpdate);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.message).toContain('9999');
|
||||
});
|
||||
|
||||
test('can sync admin user', async () => {
|
||||
@@ -131,12 +146,14 @@ describe('UserService', () => {
|
||||
},
|
||||
},
|
||||
} as SessionClaims;
|
||||
const syncedUser = await syncUser(sessionClaims);
|
||||
|
||||
expect(syncedUser).toBeDefined();
|
||||
expect(syncedUser.name).toBe('Updated Name');
|
||||
expect(syncedUser.email).toBe('test@email.com');
|
||||
expect(syncedUser.role).toBe('admin');
|
||||
const result = await syncUser(sessionClaims);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.name).toBe('Updated Name');
|
||||
expect(result.value.email).toBe('test@email.com');
|
||||
expect(result.value.role).toBe('admin');
|
||||
});
|
||||
|
||||
test('can sync internal user', async () => {
|
||||
@@ -170,12 +187,14 @@ describe('UserService', () => {
|
||||
},
|
||||
},
|
||||
} as SessionClaims;
|
||||
const syncedUser = await syncUser(sessionClaims);
|
||||
|
||||
expect(syncedUser).toBeDefined();
|
||||
expect(syncedUser.name).toBe('Updated Name');
|
||||
expect(syncedUser.email).toBe('test@email.com');
|
||||
expect(syncedUser.role).toBe('internal');
|
||||
const result = await syncUser(sessionClaims);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.name).toBe('Updated Name');
|
||||
expect(result.value.email).toBe('test@email.com');
|
||||
expect(result.value.role).toBe('internal');
|
||||
});
|
||||
|
||||
test('can sync user', async () => {
|
||||
@@ -202,12 +221,14 @@ describe('UserService', () => {
|
||||
},
|
||||
},
|
||||
} as SessionClaims;
|
||||
const syncedUser = await syncUser(sessionClaims);
|
||||
|
||||
expect(syncedUser).toBeDefined();
|
||||
expect(syncedUser.name).toBe('Updated Name');
|
||||
expect(syncedUser.email).toBe('test@email.com');
|
||||
expect(syncedUser.role).toBe('user');
|
||||
const result = await syncUser(sessionClaims);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.name).toBe('Updated Name');
|
||||
expect(result.value.email).toBe('test@email.com');
|
||||
expect(result.value.role).toBe('user');
|
||||
});
|
||||
|
||||
test('can sync saving new user', async () => {
|
||||
@@ -234,11 +255,13 @@ describe('UserService', () => {
|
||||
},
|
||||
},
|
||||
} as SessionClaims;
|
||||
const syncedUser = await syncUser(sessionClaims);
|
||||
|
||||
expect(syncedUser).toBeDefined();
|
||||
expect(syncedUser.name).toBe('Updated Name');
|
||||
expect(syncedUser.email).toBe('new@email.com');
|
||||
expect(syncedUser.role).toBe('user');
|
||||
const result = await syncUser(sessionClaims);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.name).toBe('Updated Name');
|
||||
expect(result.value.email).toBe('new@email.com');
|
||||
expect(result.value.role).toBe('user');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user