import type { ITestimonials } from '@/database/testimonials/testimonials.types'; import type { ICategory, IParamsDirectus } from '@/database/baseTypes'; import { POST_STATUS } from '@/utils/constants'; import { directus } from '@/plugins/axios'; const postPreviewFields = 'slug,title,description,case_study.*,info,youtube_time,youtube_url,category.*,client.*,date'; async function getPostsPreview({ start, limit, categorySlug, }: ITestimonials.GetPostsParams): Promise { const params: IParamsDirectus = { sort: '-date_created', fields: postPreviewFields, filter: { status: POST_STATUS.published, }, }; if (start) params.offset = start; if (limit) params.limit = limit; if (categorySlug) params.filter = { ...params.filter, ...{ category: { slug: categorySlug, }, }, }; const { data: posts } = await directus.get( 'testimonials_posts', { params, }, ); return posts.data; } async function getPost({ slug, categorySlug, }: ITestimonials.GetPostsParams): Promise { const params: IParamsDirectus = { fields: '*.*.*.*.*', filter: { status: POST_STATUS.published, }, }; if (slug) params.filter = { ...params.filter, slug, }; if (categorySlug) params.filter = { ...params.filter, ...{ category: { slug: categorySlug, }, }, }; const { data: posts } = await directus.get( 'testimonials_posts', { params, }, ); const [post] = posts.data; return post; } async function getCategories(): Promise { const params: IParamsDirectus = { sort: '-name', fields: '*.*', }; const { data: categories } = await directus.get( 'testimonials_categories', { params }, ); return categories.data; } async function getPostsCount({ categorySlug, }: ITestimonials.GetPostsParams): Promise { const params: IParamsDirectus = { meta: '*', limit: 0, filter: { status: POST_STATUS.published, }, }; if (categorySlug) params.filter = { ...params.filter, ...{ category: { slug: categorySlug, }, }, }; const { data: postsCount } = await directus.get( 'testimonials_posts', { params, }, ); return postsCount?.meta?.filter_count || 0; } export const TestimonialsGateway = { getPost, getPostsPreview, getCategories, getPostsCount, };