- Added `isCustomerBanned` function to determine if a customer is banned based on the `bannedUntil` field. - Updated the `BaseService` to throw an error if a banned customer attempts to access certain functionalities. - Enhanced the GraphQL operations to include the `bannedUntil` field in customer queries and mutations, improving data integrity and user experience. - Integrated the `CheckBanned` component in the layout to manage banned customer states effectively.
53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
'use client';
|
|
|
|
import { getCustomer, updateCustomer } from '@/actions/api/customers';
|
|
import { isCustomerBanned, isCustomerMaster } from '@repo/utils/customer';
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { useSession } from 'next-auth/react';
|
|
|
|
export const useCustomerQuery = (variables?: Parameters<typeof getCustomer>[0]) => {
|
|
const { data: session } = useSession();
|
|
const telegramId = variables?.telegramId || session?.user?.telegramId;
|
|
|
|
return useQuery({
|
|
enabled: Boolean(telegramId),
|
|
queryFn: () => getCustomer({ telegramId }),
|
|
queryKey: ['customer', telegramId],
|
|
});
|
|
};
|
|
|
|
export const useIsMaster = () => {
|
|
const { data: { customer } = {} } = useCustomerQuery();
|
|
|
|
if (!customer) return false;
|
|
|
|
return isCustomerMaster(customer);
|
|
};
|
|
|
|
export const useIsBanned = () => {
|
|
const { data: { customer } = {} } = useCustomerQuery();
|
|
|
|
if (!customer) return false;
|
|
|
|
return isCustomerBanned(customer);
|
|
};
|
|
|
|
export const useCustomerMutation = () => {
|
|
const { data: session } = useSession();
|
|
const telegramId = session?.user?.telegramId;
|
|
const queryClient = useQueryClient();
|
|
|
|
const handleOnSuccess = () => {
|
|
if (!telegramId) return;
|
|
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['customer', telegramId],
|
|
});
|
|
};
|
|
|
|
return useMutation({
|
|
mutationFn: updateCustomer,
|
|
onSuccess: handleOnSuccess,
|
|
});
|
|
};
|