refactor customer api

This commit is contained in:
vchikalkin 2025-05-16 12:55:50 +03:00
parent fda1a0a531
commit fc94f373e9
31 changed files with 361 additions and 269 deletions

View File

@ -0,0 +1,50 @@
'use server';
import { useService } from './lib/service';
import { CustomersService } from '@repo/graphql/api/customers';
const getService = useService(CustomersService);
export async function createOrUpdateCustomer(
...variables: Parameters<CustomersService['createOrUpdateCustomer']>
) {
const service = await getService();
return service.createOrUpdateCustomer(...variables);
}
export async function getCustomer(...variables: Parameters<CustomersService['getCustomer']>) {
const service = await getService();
return service.getCustomer(...variables);
}
export async function getCustomerClients(
...variables: Parameters<CustomersService['getCustomerClients']>
) {
const service = await getService();
return service.getCustomerClients(...variables);
}
export async function getCustomerMasters(
...variables: Parameters<CustomersService['getCustomerMasters']>
) {
const service = await getService();
return service.getCustomerMasters(...variables);
}
export async function updateCustomer(...variables: Parameters<CustomersService['updateCustomer']>) {
const service = await getService();
return service.updateCustomer(...variables);
}
export async function updateCustomerMaster(
...variables: Parameters<CustomersService['updateCustomerMaster']>
) {
const service = await getService();
return service.updateCustomerMaster(...variables);
}

View File

@ -0,0 +1,22 @@
/* eslint-disable canonical/id-match */
import { authOptions } from '@/config/auth';
import { type BaseService } from '@repo/graphql/api/base';
import { getServerSession } from 'next-auth';
export async function _temporaryGetCustomer() {
const session = await getServerSession(authOptions);
if (!session?.user?.telegramId) throw new Error('Unauthorized');
return { telegramId: session.user.telegramId };
}
export function useService<T extends typeof BaseService>(service: T) {
return async function () {
const session = await getServerSession(authOptions);
if (!session?.user?.telegramId) throw new Error('Unauthorized');
const customer = { telegramId: session.user.telegramId };
return new service(customer) as InstanceType<T>;
};
}

View File

@ -1,26 +0,0 @@
'use server';
import { authOptions } from '@/config/auth';
import { getCustomerClients, getCustomerMasters } from '@repo/graphql/api';
import { getServerSession } from 'next-auth/next';
export async function getClients() {
const session = await getServerSession(authOptions);
if (!session) throw new Error('Missing session');
const { user } = session;
const response = await getCustomerClients({ telegramId: user?.telegramId });
return response.data?.customers?.at(0);
}
export async function getMasters() {
const session = await getServerSession(authOptions);
if (!session) throw new Error('Missing session');
const { user } = session;
const response = await getCustomerMasters({ telegramId: user?.telegramId });
return response.data?.customers?.at(0);
}

View File

@ -2,7 +2,8 @@
'use server';
// eslint-disable-next-line sonarjs/no-internal-api-use
import type * as ApolloTypes from '../../../packages/graphql/node_modules/@apollo/client/core';
import { getProfile } from './profile';
import { getCustomer, getCustomerMasters } from './api/customers';
import { _temporaryGetCustomer } from './api/lib/service';
import { formatDate, formatTime, sumTime } from '@/utils/date';
import * as api from '@repo/graphql/api';
import { Enum_Customer_Role, Enum_Slot_State } from '@repo/graphql/types';
@ -21,7 +22,12 @@ type OrderInput = {
};
export async function createOrder(input: OrderInput) {
const customer = await getProfile();
const variables = await _temporaryGetCustomer();
const { customer } = await getCustomer(variables);
if (!customer) {
throw new Error('Missing customer');
}
if (!input.slotId) {
throw new Error('Missing slot');
@ -40,9 +46,9 @@ export async function createOrder(input: OrderInput) {
const masterId = data.slot?.master?.documentId;
const masters = await api.getCustomerMasters(customer);
const masters = await getCustomerMasters(customer);
if (!masters.data.customers.some((master) => master?.documentId === masterId)) {
if (!masters.customers.some((master) => master?.documentId === masterId)) {
throw new Error('Invalid master');
}
}

View File

@ -1,37 +0,0 @@
'use server';
import { authOptions } from '@/config/auth';
import { getCustomer, updateCustomerProfile } from '@repo/graphql/api';
import { type CustomerInput, type GetCustomerQueryVariables } from '@repo/graphql/types';
import { getServerSession } from 'next-auth/next';
export async function getProfile(input?: GetCustomerQueryVariables) {
const session = await getServerSession(authOptions);
if (!session) throw new Error('Missing session');
const { user } = session;
const telegramId = input?.telegramId || user?.telegramId;
const { data } = await getCustomer({ telegramId });
const customer = data?.customers?.at(0);
if (!customer) throw new Error('Customer not found');
return customer;
}
export async function updateProfile(input: CustomerInput) {
const session = await getServerSession(authOptions);
if (!session) throw new Error('Missing session');
const { user } = session;
const { data } = await getCustomer({ telegramId: user?.telegramId });
const customer = data.customers.at(0);
if (!customer) throw new Error('Customer not found');
await updateCustomerProfile({
data: input,
documentId: customer?.documentId,
});
}

View File

@ -1,5 +1,6 @@
'use server';
import { getProfile } from './profile';
import { getCustomer } from './api/customers';
import { _temporaryGetCustomer } from './api/lib/service';
import * as api from '@repo/graphql/api/service';
// eslint-disable-next-line sonarjs/no-internal-api-use
import type * as ApolloTypes from '@repo/graphql/node_modules/@apollo/client/core';
@ -9,9 +10,10 @@ import { type GetServicesQueryVariables } from '@repo/graphql/types';
type FixTypescriptCringe = ApolloTypes.FetchResult;
export async function getServices(input?: GetServicesQueryVariables) {
const customer = await getProfile();
const variables = await _temporaryGetCustomer();
const { customer } = await getCustomer(variables);
const filters = input || { filters: { master: { documentId: { eq: customer.documentId } } } };
const filters = input || { filters: { master: { documentId: { eq: customer?.documentId } } } };
return api.getServices(filters);
}

View File

@ -0,0 +1,12 @@
'use server';
import { authOptions } from '@/config/auth';
import { getServerSession } from 'next-auth/next';
export async function getSessionUser() {
const session = await getServerSession(authOptions);
const user = session?.user;
if (!user?.telegramId) throw new Error('Missing session');
return user;
}

View File

@ -1,7 +1,8 @@
'use server';
// eslint-disable-next-line sonarjs/no-internal-api-use
import type * as ApolloTypes from '../../../packages/graphql/node_modules/@apollo/client/core';
import { getProfile } from './profile';
import { getCustomer } from './api/customers';
import { _temporaryGetCustomer } from './api/lib/service';
import { formatDate, formatTime } from '@/utils/date';
import * as api from '@repo/graphql/api';
import type * as GQL from '@repo/graphql/types';
@ -12,7 +13,8 @@ type AddSlotInput = Omit<GQL.CreateSlotMutationVariables['input'], 'master'>;
type FixTypescriptCringe = ApolloTypes.FetchResult;
export async function addSlot(input: AddSlotInput) {
const customer = await getProfile();
const variables = await _temporaryGetCustomer();
const { customer } = await getCustomer(variables);
return api.createSlot({
...input,
@ -24,13 +26,14 @@ export async function addSlot(input: AddSlotInput) {
}
export async function getSlots(input: GQL.GetSlotsQueryVariables) {
const customer = await getProfile();
const variables = await _temporaryGetCustomer();
const { customer } = await getCustomer(variables);
return api.getSlots({
filters: {
master: {
documentId: {
eq: customer.documentId,
eq: customer?.documentId,
},
},
...input.filters,
@ -39,8 +42,6 @@ export async function getSlots(input: GQL.GetSlotsQueryVariables) {
}
export async function updateSlot(input: GQL.UpdateSlotMutationVariables) {
await getProfile();
return api.updateSlot({
...input,
data: {

View File

@ -1,18 +1,20 @@
'use client';
import { useProfileMutation } from '@/hooks/profile';
import { useCustomerMutation } from '@/hooks/api/customers';
import { initData, useSignal } from '@telegram-apps/sdk-react';
import { useEffect, useState } from 'react';
export function UpdateProfile() {
const initDataUser = useSignal(initData.user);
const { mutate: updateProfile } = useProfileMutation();
const { mutate: updateProfile } = useCustomerMutation();
const [hasUpdated, setHasUpdated] = useState(false);
useEffect(() => {
if (!hasUpdated) {
updateProfile({
active: true,
photoUrl: initDataUser?.photoUrl || undefined,
data: {
active: true,
photoUrl: initDataUser?.photoUrl || undefined,
},
});
setHasUpdated(true);
}

View File

@ -1,6 +1,6 @@
'use client';
import { LoadingSpinner } from '../common/spinner';
import { useCustomerContacts } from '@/hooks/contacts';
import { useCustomerContacts } from '@/hooks/api/contacts';
import * as GQL from '@repo/graphql/types';
import { Avatar, AvatarFallback, AvatarImage } from '@repo/ui/components/ui/avatar';
import Link from 'next/link';

View File

@ -2,13 +2,13 @@
import { ContactsGridBase } from './components';
import { LoadingSpinner } from '@/components/common/spinner';
import { ContactsFilterProvider } from '@/context/contacts-filter';
import { useCustomerContacts } from '@/hooks/contacts';
import { useCustomerContacts } from '@/hooks/api/contacts';
import { useOrderStore } from '@/stores/order';
import { withContext } from '@/utils/context';
import { useEffect } from 'react';
export const MastersGrid = withContext(ContactsFilterProvider)(function () {
const { contacts, isLoading, setFilter } = useCustomerContacts({ includeSelf: true });
const { contacts, isLoading, setFilter } = useCustomerContacts();
const masterId = useOrderStore((store) => store.masterId);
const setMasterId = useOrderStore((store) => store.setMasterId);

View File

@ -2,14 +2,14 @@
import { CardSectionHeader } from '../ui';
import { CheckboxWithText, DataField } from './components';
import { type ProfileProps } from './types';
import { useProfileMutation, useProfileQuery } from '@/hooks/profile';
import { useCustomerMutation, useCustomerQuery } from '@/hooks/api/customers';
import { Enum_Customer_Role as Role } from '@repo/graphql/types';
import { Button } from '@repo/ui/components/ui/button';
import { Card } from '@repo/ui/components/ui/card';
import Link from 'next/link';
export function ContactDataCard({ telegramId }: Readonly<ProfileProps>) {
const { data: customer } = useProfileQuery({ telegramId });
const { data: { customer } = {} } = useCustomerQuery({ telegramId });
if (!customer) return null;
@ -30,8 +30,8 @@ export function ContactDataCard({ telegramId }: Readonly<ProfileProps>) {
}
export function ProfileDataCard() {
const { data: customer } = useProfileQuery();
const { mutate: updateProfile } = useProfileMutation();
const { data: { customer } = {} } = useCustomerQuery();
const { mutate: updateCustomer } = useCustomerMutation();
if (!customer) return null;
@ -43,14 +43,18 @@ export function ProfileDataCard() {
fieldName="name"
id="name"
label="Имя"
onChange={updateProfile}
onChange={({ name }) => updateCustomer({ data: { name } })}
value={customer?.name ?? ''}
/>
<DataField disabled id="phone" label="Телефон" readOnly value={customer?.phone ?? ''} />
<CheckboxWithText
checked={customer.role !== 'client'}
description="Разрешить другим пользователям записываться к вам"
onChange={(checked) => updateProfile({ role: checked ? Role.Master : Role.Client })}
onChange={(checked) =>
updateCustomer({
data: { role: checked ? Role.Master : Role.Client },
})
}
text="Быть мастером"
/>
</div>

View File

@ -2,11 +2,11 @@
'use client';
import { LinkButton } from './components';
import { type ProfileProps } from './types';
import { useProfileQuery } from '@/hooks/profile';
import { useCustomerQuery } from '@/hooks/api/customers';
import { Enum_Customer_Role } from '@repo/graphql/types';
export function LinksCard({ telegramId }: Readonly<ProfileProps>) {
const { data: customer } = useProfileQuery({ telegramId });
const { data: { customer } = {} } = useCustomerQuery({ telegramId });
const isMaster = customer?.role === Enum_Customer_Role.Master;

View File

@ -1,12 +1,12 @@
'use client';
import { LoadingSpinner } from '../common/spinner';
import { type ProfileProps } from './types';
import { useProfileQuery } from '@/hooks/profile';
import { useCustomerQuery } from '@/hooks/api/customers';
import { Avatar, AvatarFallback, AvatarImage } from '@repo/ui/components/ui/avatar';
import { Card } from '@repo/ui/components/ui/card';
export function PersonCard({ telegramId }: Readonly<ProfileProps>) {
const { data: customer, isLoading } = useProfileQuery({ telegramId });
const { data: { customer } = {}, isLoading } = useCustomerQuery({ telegramId });
if (isLoading || !customer)
return (

View File

@ -0,0 +1,25 @@
import { getCustomerClients, getCustomerMasters } from '@/actions/api/customers';
import { useQuery } from '@tanstack/react-query';
import { useSession } from 'next-auth/react';
export const useClientsQuery = (props?: Parameters<typeof getCustomerClients>[0]) => {
const { data: session } = useSession();
const telegramId = props?.telegramId || session?.user?.telegramId;
return useQuery({
enabled: false,
queryFn: () => getCustomerClients({ telegramId }),
queryKey: ['customer', 'telegramId', telegramId, 'clients', 'get'],
});
};
export const useMastersQuery = (props?: Parameters<typeof getCustomerMasters>[0]) => {
const { data: session } = useSession();
const telegramId = props?.telegramId || session?.user?.telegramId;
return useQuery({
enabled: false,
queryFn: () => getCustomerMasters({ telegramId }),
queryKey: ['customer', 'telegramId', telegramId, 'masters', 'get'],
});
};

View File

@ -1,19 +1,11 @@
/* eslint-disable canonical/id-match */
'use client';
import { useProfileQuery } from '../profile';
import { useClientsQuery, useMastersQuery } from './query';
import { ContactsFilterContext } from '@/context/contacts-filter';
import { Enum_Customer_Role } from '@repo/graphql/types';
import { sift } from 'radash';
import { use, useEffect, useMemo } from 'react';
type Parameters_ = {
includeSelf: boolean;
};
export function useCustomerContacts(parameters?: Parameters_) {
export function useCustomerContacts() {
const { filter, setFilter } = use(ContactsFilterContext);
const { data: customer, isLoading: isLoadingProfile } = useProfileQuery();
const {
data: clientsData,
@ -27,14 +19,10 @@ export function useCustomerContacts(parameters?: Parameters_) {
refetch: refetchMasters,
} = useMastersQuery();
const clients = clientsData?.clients || [];
let masters = mastersData?.masters || [];
const clients = clientsData?.customers?.at(0)?.clients || [];
const masters = mastersData?.customers?.at(0)?.masters || [];
if (parameters?.includeSelf && customer?.role === Enum_Customer_Role.Master) {
masters = [{ ...customer, name: 'Я' }, ...masters];
}
const isLoading = isLoadingClients || isLoadingMasters || isLoadingProfile;
const isLoading = isLoadingClients || isLoadingMasters;
useEffect(() => {
if (filter === 'clients') {

View File

@ -0,0 +1,24 @@
'use client';
import { getCustomer, updateCustomer } from '@/actions/api/customers';
import { useMutation, useQuery } from '@tanstack/react-query';
import { useSession } from 'next-auth/react';
export const useCustomerQuery = (props?: Parameters<typeof getCustomer>[0]) => {
const { data: session } = useSession();
const telegramId = props?.telegramId || session?.user?.telegramId;
return useQuery({
queryFn: () => getCustomer({ telegramId }),
queryKey: ['customer', 'telegramId', telegramId, 'get'],
});
};
export const useCustomerMutation = () => {
const { refetch } = useCustomerQuery();
return useMutation({
mutationFn: updateCustomer,
mutationKey: ['customer', 'update'],
onSuccess: () => refetch(),
});
};

View File

@ -1,8 +0,0 @@
import { getClients, getMasters } from '@/actions/contacts';
import { useQuery } from '@tanstack/react-query';
export const useClientsQuery = () =>
useQuery({ enabled: false, queryFn: getClients, queryKey: ['contacts', 'clients', 'get'] });
export const useMastersQuery = () =>
useQuery({ enabled: false, queryFn: getMasters, queryKey: ['contacts', 'masters', 'get'] });

View File

@ -1,23 +0,0 @@
'use client';
import { getProfile, updateProfile } from '@/actions/profile';
import { type ProfileProps } from '@/components/profile/types';
import { useMutation, useQuery } from '@tanstack/react-query';
export const useProfileQuery = (props?: ProfileProps) => {
const telegramId = props?.telegramId;
return useQuery({
queryFn: () => getProfile({ telegramId }),
queryKey: telegramId ? ['profile', 'telegramId', telegramId, 'get'] : ['profile', 'get'],
});
};
export const useProfileMutation = () => {
const { refetch } = useProfileQuery();
return useMutation({
mutationFn: updateProfile,
mutationKey: ['profile', 'update'],
onSuccess: () => refetch(),
});
};

View File

@ -2,7 +2,7 @@
'use client';
import { OrderStoreContext } from './context';
import { type OrderStore, type Steps } from './types';
import { useProfileQuery } from '@/hooks/profile';
import { useCustomerQuery } from '@/hooks/api/customers';
import { Enum_Customer_Role } from '@repo/graphql/types';
import { useContext, useEffect } from 'react';
import { useStore } from 'zustand';
@ -28,7 +28,7 @@ export const MASTER_STEPS: Steps[] = STEPS.filter((step) => step !== 'master-sel
export const CLIENT_STEPS: Steps[] = STEPS.filter((step) => step !== 'client-select');
export function useInitOrderStore() {
const { data } = useProfileQuery();
const { data: { customer } = {} } = useCustomerQuery();
const setMasterId = useOrderStore((store) => store.setMasterId);
const setClientId = useOrderStore((store) => store.setClientId);
@ -36,14 +36,14 @@ export function useInitOrderStore() {
const setStepsSequence = useOrderStore((store) => store._setStepSequence);
useEffect(() => {
const role = data?.role;
const role = customer?.role;
if (role === Enum_Customer_Role.Master && data) {
setMasterId(data?.documentId);
if (role === Enum_Customer_Role.Master && customer) {
setMasterId(customer?.documentId);
}
if (role === Enum_Customer_Role.Client && data) {
setClientId(data?.documentId);
if (role === Enum_Customer_Role.Client && customer) {
setClientId(customer?.documentId);
}
const steps = role === Enum_Customer_Role.Master ? MASTER_STEPS : CLIENT_STEPS;
@ -51,5 +51,5 @@ export function useInitOrderStore() {
setStepsSequence(steps);
setStep(initialStep);
}, [data, setClientId, setMasterId, setStep, setStepsSequence]);
}, [customer, setClientId, setMasterId, setStep, setStepsSequence]);
}

View File

@ -0,0 +1,15 @@
type CustomerProfile = {
telegramId: string;
};
export class BaseService {
protected customer: CustomerProfile;
constructor(customer: CustomerProfile) {
if (!customer?.telegramId) {
throw new Error('Invalid customer profile: telegramId required');
}
this.customer = customer;
}
}

View File

@ -1,108 +0,0 @@
'use server';
import { getClientWithToken } from '../apollo/client';
import * as GQL from '../types';
export async function createOrUpdateUser(input: GQL.CreateCustomerMutationVariables) {
if (!input.phone && !input.telegramId) throw new Error('Missing phone and telegramId');
const { query, mutate } = await getClientWithToken();
const response = await query({
query: GQL.GetCustomerDocument,
variables: input,
});
const customer = response?.data?.customers?.at(0);
if (customer && customer.phone === input.phone) {
return mutate({
mutation: GQL.UpdateCustomerProfileDocument,
variables: {
documentId: customer.documentId,
data: { ...input },
},
});
}
return mutate({
mutation: GQL.CreateCustomerDocument,
variables: input,
});
}
export async function getCustomer(input: GQL.GetCustomerQueryVariables) {
const { query } = await getClientWithToken();
return query({
query: GQL.GetCustomerDocument,
variables: input,
});
}
export async function getCustomerMasters(input: GQL.GetCustomerMastersQueryVariables) {
const { query } = await getClientWithToken();
return query({
query: GQL.GetCustomerMastersDocument,
variables: input,
});
}
export async function getCustomerClients(input: GQL.GetCustomerClientsQueryVariables) {
const { query } = await getClientWithToken();
return query({
query: GQL.GetCustomerClientsDocument,
variables: input,
});
}
type AddCustomerMasterInput = Pick<GQL.CreateCustomerMutationVariables, 'phone' | 'telegramId'> & {
masterId: GQL.Scalars['ID']['input'];
operation: 'add' | 'remove';
};
export async function updateCustomerMaster(input: AddCustomerMasterInput) {
if (!input.phone && !input.telegramId) throw new Error('Missing phone and telegramId');
const { query } = await getClientWithToken();
const response = await query({
query: GQL.GetCustomerMastersDocument,
variables: input,
});
const customer = response?.data?.customers?.at(0);
if (customer) {
let newMastersIds = customer.masters.map((x) => x?.documentId);
switch (input.operation) {
case 'add':
if (newMastersIds.includes(input.masterId)) return;
newMastersIds = [...newMastersIds, input.masterId];
break;
case 'remove':
newMastersIds = newMastersIds.filter((x) => x !== input.masterId);
break;
default:
break;
}
return updateCustomerProfile({
documentId: customer.documentId,
data: {
masters: newMastersIds,
},
});
}
}
export async function updateCustomerProfile(input: GQL.UpdateCustomerProfileMutationVariables) {
const { mutate } = await getClientWithToken();
return mutate({
mutation: GQL.UpdateCustomerProfileDocument,
variables: input,
});
}

View File

@ -0,0 +1,140 @@
import { getClientWithToken } from '../apollo/client';
import * as GQL from '../types';
import { BaseService } from './base';
import { type VariablesOf } from '@graphql-typed-document-node/core';
export class CustomersService extends BaseService {
async createOrUpdateCustomer(variables: VariablesOf<typeof GQL.CreateCustomerDocument>) {
if (!variables.phone && !variables.telegramId) throw new Error('Missing phone and telegramId');
const { customer } = await this.getCustomer(variables);
const { mutate } = await getClientWithToken();
let mutationResult;
if (customer && customer.phone === variables.phone) {
mutationResult = await mutate({
mutation: GQL.UpdateCustomerDocument,
variables: {
data: { ...variables },
documentId: customer.documentId,
},
});
} else {
mutationResult = await mutate({
mutation: GQL.CreateCustomerDocument,
variables,
});
}
const error = mutationResult.errors?.at(0);
if (error) throw new Error(error.message);
return mutationResult.data;
}
async getCustomer(variables: VariablesOf<typeof GQL.GetCustomerDocument>) {
const { query } = await getClientWithToken();
const result = await query({
query: GQL.GetCustomerDocument,
variables,
});
if (result.error) throw new Error(result.error.message);
const customer = result.data.customers.at(0);
return { customer };
}
async getCustomerClients(variables?: VariablesOf<typeof GQL.GetCustomerClientsDocument>) {
const { query } = await getClientWithToken();
const result = await query({
query: GQL.GetCustomerClientsDocument,
variables: {
telegramId: variables?.telegramId || this.customer.telegramId,
},
});
if (result.error) throw new Error(result.error.message);
return result.data;
}
async getCustomerMasters(variables?: VariablesOf<typeof GQL.GetCustomerMastersDocument>) {
const { query } = await getClientWithToken();
const result = await query({
query: GQL.GetCustomerMastersDocument,
variables: {
telegramId: variables?.telegramId || this.customer.telegramId,
},
});
if (result.error) throw new Error(result.error.message);
return result.data;
}
async updateCustomer(
variables: Omit<VariablesOf<typeof GQL.UpdateCustomerDocument>, 'documentId'>,
) {
const { customer } = await this.getCustomer(this.customer);
if (!customer) throw new Error('Customer not found');
const { mutate } = await getClientWithToken();
const mutationResult = await mutate({
mutation: GQL.UpdateCustomerDocument,
variables: {
data: variables.data,
documentId: customer.documentId,
},
});
const error = mutationResult.errors?.at(0);
if (error) throw new Error(error.message);
return mutationResult.data;
}
async updateCustomerMaster(
variables: VariablesOf<typeof GQL.CreateCustomerDocument> & {
masterId: GQL.Scalars['ID']['input'];
operation: 'add' | 'remove';
},
) {
if (!variables.phone && !variables.telegramId) throw new Error('Missing phone and telegramId');
const { query } = await getClientWithToken();
const response = await query({ query: GQL.GetCustomerMastersDocument, variables });
if (response.error) throw new Error(response.error.message);
const customer = response?.data?.customers?.at(0);
if (!customer) throw new Error('Customer not found');
let newMastersIds = customer.masters.map((x) => x?.documentId);
switch (variables.operation) {
case 'add':
if (newMastersIds.includes(variables.masterId)) break;
newMastersIds = [...newMastersIds, variables.masterId];
break;
case 'remove':
newMastersIds = newMastersIds.filter((x) => x !== variables.masterId);
break;
default:
break;
}
return this.updateCustomer({
data: {
masters: newMastersIds,
},
});
}
}

View File

@ -1,5 +1,5 @@
export * from './auth';
export * from './customer';
export * from './slot';
export * from './customers';
export * from './order';
export * from './service';
export * from './slot';

View File

@ -4,6 +4,6 @@ import { typescript } from '@repo/eslint-config/typescript';
export default [
...typescript,
{
ignores: ['**/graphql/types.ts', '**/schema.graphql'],
ignores: ['**/types/**'],
},
];

View File

@ -48,7 +48,7 @@ query GetCustomerClients($phone: String, $telegramId: Long) {
}
}
mutation UpdateCustomerProfile($documentId: ID!, $data: CustomerInput!) {
mutation UpdateCustomer($documentId: ID!, $data: CustomerInput!) {
updateCustomer(documentId: $documentId, data: $data) {
...CustomerFields
}

View File

@ -13,15 +13,15 @@
"zod": "catalog:"
},
"devDependencies": {
"graphql": "catalog:",
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/jsonwebtoken": "^9.0.7",
"@graphql-codegen/cli": "^5.0.3",
"@graphql-codegen/typed-document-node": "^5.0.12",
"@graphql-codegen/typescript": "^4.1.2",
"@graphql-codegen/typescript-operations": "^4.4.0",
"@graphql-typed-document-node/core": "^3.2.0",
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/jsonwebtoken": "^9.0.7",
"graphql": "catalog:",
"vite-tsconfig-paths": "catalog:",
"vitest": "catalog:"
}

View File

@ -686,13 +686,13 @@ export type GetCustomerClientsQueryVariables = Exact<{
export type GetCustomerClientsQuery = { __typename?: 'Query', customers: Array<{ __typename?: 'Customer', documentId: string, clients: Array<{ __typename?: 'Customer', active?: boolean | null | undefined, documentId: string, name: string, phone: string, photoUrl?: string | null | undefined, role: Enum_Customer_Role, telegramId?: any | null | undefined } | null | undefined> } | null | undefined> };
export type UpdateCustomerProfileMutationVariables = Exact<{
export type UpdateCustomerMutationVariables = Exact<{
documentId: Scalars['ID']['input'];
data: CustomerInput;
}>;
export type UpdateCustomerProfileMutation = { __typename?: 'Mutation', updateCustomer?: { __typename?: 'Customer', active?: boolean | null | undefined, documentId: string, name: string, phone: string, photoUrl?: string | null | undefined, role: Enum_Customer_Role, telegramId?: any | null | undefined } | null | undefined };
export type UpdateCustomerMutation = { __typename?: 'Mutation', updateCustomer?: { __typename?: 'Customer', active?: boolean | null | undefined, documentId: string, name: string, phone: string, photoUrl?: string | null | undefined, role: Enum_Customer_Role, telegramId?: any | null | undefined } | null | undefined };
export type OrderFieldsFragment = { __typename?: 'Order', documentId: string, time_start?: any | null | undefined, time_end?: any | null | undefined, state?: Enum_Order_State | null | undefined, order_number?: number | null | undefined, services: Array<{ __typename?: 'Service', documentId: string, name?: string | null | undefined } | null | undefined>, client?: { __typename?: 'Customer', name: string, documentId: string, photoUrl?: string | null | undefined } | null | undefined };
@ -774,7 +774,7 @@ export const CreateCustomerDocument = {"kind":"Document","definitions":[{"kind":
export const GetCustomerDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetCustomer"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"phone"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"telegramId"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"Long"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"customers"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filters"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"or"},"value":{"kind":"ListValue","values":[{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"phone"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"eq"},"value":{"kind":"Variable","name":{"kind":"Name","value":"phone"}}}]}}]},{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"telegramId"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"eq"},"value":{"kind":"Variable","name":{"kind":"Name","value":"telegramId"}}}]}}]}]}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"CustomerFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"CustomerFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Customer"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"active"}},{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"phone"}},{"kind":"Field","name":{"kind":"Name","value":"photoUrl"}},{"kind":"Field","name":{"kind":"Name","value":"role"}},{"kind":"Field","name":{"kind":"Name","value":"telegramId"}}]}}]} as unknown as DocumentNode<GetCustomerQuery, GetCustomerQueryVariables>;
export const GetCustomerMastersDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetCustomerMasters"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"phone"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"telegramId"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"Long"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"customers"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filters"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"or"},"value":{"kind":"ListValue","values":[{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"phone"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"eq"},"value":{"kind":"Variable","name":{"kind":"Name","value":"phone"}}}]}}]},{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"telegramId"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"eq"},"value":{"kind":"Variable","name":{"kind":"Name","value":"telegramId"}}}]}}]}]}},{"kind":"ObjectField","name":{"kind":"Name","value":"and"},"value":{"kind":"ListValue","values":[{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"active"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"eq"},"value":{"kind":"BooleanValue","value":true}}]}}]}]}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"masters"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"CustomerFields"}}]}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"CustomerFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Customer"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"active"}},{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"phone"}},{"kind":"Field","name":{"kind":"Name","value":"photoUrl"}},{"kind":"Field","name":{"kind":"Name","value":"role"}},{"kind":"Field","name":{"kind":"Name","value":"telegramId"}}]}}]} as unknown as DocumentNode<GetCustomerMastersQuery, GetCustomerMastersQueryVariables>;
export const GetCustomerClientsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetCustomerClients"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"phone"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"telegramId"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"Long"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"customers"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filters"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"or"},"value":{"kind":"ListValue","values":[{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"phone"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"eq"},"value":{"kind":"Variable","name":{"kind":"Name","value":"phone"}}}]}}]},{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"telegramId"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"eq"},"value":{"kind":"Variable","name":{"kind":"Name","value":"telegramId"}}}]}}]}]}},{"kind":"ObjectField","name":{"kind":"Name","value":"and"},"value":{"kind":"ListValue","values":[{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"active"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"eq"},"value":{"kind":"BooleanValue","value":true}}]}}]}]}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"clients"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"CustomerFields"}}]}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"CustomerFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Customer"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"active"}},{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"phone"}},{"kind":"Field","name":{"kind":"Name","value":"photoUrl"}},{"kind":"Field","name":{"kind":"Name","value":"role"}},{"kind":"Field","name":{"kind":"Name","value":"telegramId"}}]}}]} as unknown as DocumentNode<GetCustomerClientsQuery, GetCustomerClientsQueryVariables>;
export const UpdateCustomerProfileDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"UpdateCustomerProfile"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"documentId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"data"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"CustomerInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"updateCustomer"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"documentId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"documentId"}}},{"kind":"Argument","name":{"kind":"Name","value":"data"},"value":{"kind":"Variable","name":{"kind":"Name","value":"data"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"CustomerFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"CustomerFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Customer"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"active"}},{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"phone"}},{"kind":"Field","name":{"kind":"Name","value":"photoUrl"}},{"kind":"Field","name":{"kind":"Name","value":"role"}},{"kind":"Field","name":{"kind":"Name","value":"telegramId"}}]}}]} as unknown as DocumentNode<UpdateCustomerProfileMutation, UpdateCustomerProfileMutationVariables>;
export const UpdateCustomerDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"UpdateCustomer"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"documentId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"data"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"CustomerInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"updateCustomer"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"documentId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"documentId"}}},{"kind":"Argument","name":{"kind":"Name","value":"data"},"value":{"kind":"Variable","name":{"kind":"Name","value":"data"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"CustomerFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"CustomerFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Customer"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"active"}},{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"phone"}},{"kind":"Field","name":{"kind":"Name","value":"photoUrl"}},{"kind":"Field","name":{"kind":"Name","value":"role"}},{"kind":"Field","name":{"kind":"Name","value":"telegramId"}}]}}]} as unknown as DocumentNode<UpdateCustomerMutation, UpdateCustomerMutationVariables>;
export const GetOrderDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetOrder"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"documentId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"order"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"documentId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"documentId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"OrderFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"OrderFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Order"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"time_start"}},{"kind":"Field","name":{"kind":"Name","value":"time_end"}},{"kind":"Field","name":{"kind":"Name","value":"state"}},{"kind":"Field","name":{"kind":"Name","value":"order_number"}},{"kind":"Field","name":{"kind":"Name","value":"services"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}}]}},{"kind":"Field","name":{"kind":"Name","value":"client"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"photoUrl"}}]}}]}}]} as unknown as DocumentNode<GetOrderQuery, GetOrderQueryVariables>;
export const CreateOrderDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"CreateOrder"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"OrderInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"createOrder"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"data"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"OrderFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"OrderFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Order"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"time_start"}},{"kind":"Field","name":{"kind":"Name","value":"time_end"}},{"kind":"Field","name":{"kind":"Name","value":"state"}},{"kind":"Field","name":{"kind":"Name","value":"order_number"}},{"kind":"Field","name":{"kind":"Name","value":"services"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}}]}},{"kind":"Field","name":{"kind":"Name","value":"client"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"photoUrl"}}]}}]}}]} as unknown as DocumentNode<CreateOrderMutation, CreateOrderMutationVariables>;
export const GetServicesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetServices"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"filters"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"ServiceFiltersInput"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"services"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filters"},"value":{"kind":"Variable","name":{"kind":"Name","value":"filters"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"ServiceFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"ServiceFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Service"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"duration"}}]}}]} as unknown as DocumentNode<GetServicesQuery, GetServicesQueryVariables>;

View File

@ -1,8 +1,8 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"declaration": false,
"declarationMap": false,
"esModuleInterop": true,
"incremental": false,
"isolatedModules": true,

3
pnpm-lock.yaml generated
View File

@ -290,6 +290,9 @@ importers:
'@graphql-typed-document-node/core':
specifier: ^3.2.0
version: 3.2.0(graphql@16.9.0)
'@repo/eslint-config':
specifier: workspace:*
version: link:../eslint-config
'@repo/typescript-config':
specifier: workspace:*
version: link:../typescript-config