import type { servicesSchema } from '@/content.config' import type { z } from 'astro/zod' import { YCLIENTS_TOKEN, YCLIENTS_API_URL } from 'astro:env/server' type HTML = string const currencyFormatter = Intl.NumberFormat('ru', { currency: 'rub' }) const RENT_GYM_SERVICE: z.infer = { title: 'Аренда зала', charachteristics: [ { title: 'Разовое посещение', description: 'Разовое посещение студии для самостоятельной тренировки', price: `${currencyFormatter.format(1500)} ₽`, link: { title: 'Записаться', href: 'https://n1381542.yclients.com/company/1254211/activity/select?o=act', }, }, ], } export type YclientsBookinRawResponse = { category: { id: number title: string }[] services: { id: number title: string price_min: number price_max: number discount: number comment: string weight: number active: number sex: number image: string category_id: number }[] } export type YClientsAbonementRawItem = { id: number image: string online_sale_title: string online_sale_description: HTML online_sale_price: number online_sale_image: string cost: number price: number } export type YclientsAbonementsRawResponse = { data: { loyalty_abonement_types: YClientsAbonementRawItem[] } } const fetcher = (...[path, options = {}]: Parameters): Promise => fetch(path, { ...options, headers: { Authorization: `Bearer ${YCLIENTS_TOKEN}`, ...(options.headers || {}), }, }).then((res) => res.json() as Promise) export const yclientsClient = { async getServices(): Promise[]> { const booking = await fetcher( `${YCLIENTS_API_URL}/book_services/1254211`, { method: 'GET', } ) const services = booking.category.map((categoryItem) => ({ title: categoryItem.title, charachteristics: booking.services .filter((service) => service.category_id === categoryItem.id) .map((service) => ({ title: service.title, price: `${currencyFormatter.format(service.price_min)} ₽`, description: service.comment, link: { title: 'Записаться', href: `https://n1381542.yclients.com/company/1254211/personal/select-services?o=s${service.id}`, }, })), })) return [services[0], RENT_GYM_SERVICE, ...services.slice(1)] }, }