1import { useQuery } from "@tanstack/react-query";
2import { fetch } from "@marzneshin/common/utils";
3import { InboundType } from "@marzneshin/modules/inbounds";
4import {
5 FetchEntityReturn,
6 SelectableEntityQueryKeyType
7} from "@marzneshin/libs/entity-table";
8
9interface FetchServiceInboundsType {
10 queryKey: [string, number, number, number]
11}
12
13interface UseServiceInboundsQueryProps {
14 serviceId: number;
15 page?: number;
16 size?: number;
17}
18
19export async function fetchServiceInbounds({
20 queryKey
21}: FetchServiceInboundsType): Promise<InboundType[]> {
22 return fetch(`/services/${queryKey[1]}/inbounds`, {
23 query: {
24 page: queryKey[2],
25 size: queryKey[3]
26 }
27 }).then((result) => {
28 return result.items;
29 });
30}
31
32
33export async function fetchSelectableServiceInbounds({ queryKey }: SelectableEntityQueryKeyType): FetchEntityReturn<InboundType> {
34 const pagination = queryKey[3];
35 const primaryFilter = queryKey[4];
36 const filters = queryKey[6].filters;
37 return fetch(`/inbounds`, {
38 query: {
39 ...pagination,
40 ...filters,
41 tag: primaryFilter,
42 descending: queryKey[5].desc,
43 order_by: queryKey[5].sortBy,
44 }
45 }).then((result) => {
46 return {
47 entities: result.items,
48 pageCount: result.pages
49 };
50 })
51}
52
53const ServicesQueryFetchKey = "services";
54
55export const useInboundsServiceQuery = ({
56 serviceId, page = 1, size = 50
57}: UseServiceInboundsQueryProps) => {
58 return useQuery({
59 queryKey: [ServicesQueryFetchKey, serviceId, page, size],
60 queryFn: fetchServiceInbounds,
61 initialData: []
62 })
63}