получаю сервисы из yclients

This commit is contained in:
Yuu Ottosoka
2025-06-10 14:32:39 +03:00
parent 8359ab14ea
commit fadb188200
10 changed files with 157 additions and 124 deletions

113
src/libs/api/yclients.ts Normal file
View File

@@ -0,0 +1,113 @@
import type { servicesSchema } from '@/content.config'
import type { z } from 'astro/zod'
import {
YCLIENTS_TOKEN,
YCLIENTS_API_URL,
YCLIENTS_ABONEMENTS_API_URL,
YCLIENTS_ABONEMENTS_TOKEN,
} from 'astro:env/server'
type HTML = string
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 = <R>(...[path, options = {}]: Parameters<typeof fetch>): Promise<R> =>
fetch(path, {
...options,
headers: {
Authorization: `Bearer ${YCLIENTS_TOKEN}`,
...(options.headers || {}),
},
}).then((res) => res.json() as Promise<R>)
export const yclientsClient = {
getAbonements: (): Promise<z.infer<typeof servicesSchema.shape.charachteristics>> =>
fetcher<YclientsAbonementsRawResponse>(
`${YCLIENTS_ABONEMENTS_API_URL}/chain/1259425/online_sale/loyalty`,
{
method: 'GET',
headers: {
Authorization: `Bearer ${YCLIENTS_ABONEMENTS_TOKEN}`,
},
}
).then((res) =>
res.data.loyalty_abonement_types.map((rawItem) => ({
title: rawItem.online_sale_title,
description: rawItem.online_sale_description,
price: `${rawItem.online_sale_price} ₽`,
link: {
title: 'Купить абонемент',
href: `https://o9998.yclients.com/subscriptions/${rawItem.id}`,
},
}))
),
async getServices(): Promise<z.infer<typeof servicesSchema>[]> {
const [booking, abonements] = await Promise.all([
fetcher<YclientsBookinRawResponse>(`${YCLIENTS_API_URL}/book_services/1254211`, {
method: 'GET',
}),
this.getAbonements(),
])
const personalServicesCatergory = booking.category.find((category) => category.id === 18760529)
const abonementsCategory = booking.category.find((category) => category.id === 19562190)
const personalServiceCharachteristics = booking.services
.filter((service) => service.category_id === 18760529)
.map((service) => ({
title: service.title,
price: `${service.price_min} ₽`,
description: service.comment,
link: {
title: 'Записаться',
href: `https://n1381542.yclients.com/company/1254211/personal/select-services?o=s${service.id}`,
},
}))
return [
{
title: personalServicesCatergory?.title || '',
charachteristics: personalServiceCharachteristics,
},
{
title: abonementsCategory?.title || '',
charachteristics: abonements,
},
]
},
}