vchikalkin 8e61fbbb40 Fix: Refactor customer retrieval logic to use checkCustomerExists method
- Updated the addContact and registration features to replace getCustomer calls with checkCustomerExists, improving consistency in customer existence checks.
- Modified GraphQL operations to remove deprecated getCustomer queries and introduced a new checkCustomerExists query for better clarity and efficiency.
- Adjusted queryTTL configuration in cache-proxy to reflect the removal of unused endpoints.
2025-10-09 00:20:37 +03:00

60 lines
1.6 KiB
TypeScript

import { getClientWithToken } from '../apollo/client';
import { ERRORS } from '../constants/errors';
import * as GQL from '../types';
import { type VariablesOf } from '@graphql-typed-document-node/core';
const DEFAULT_CUSTOMER_ROLE = GQL.Enum_Customer_Role.Client;
export class RegistrationService {
async checkCustomerExists(variables: VariablesOf<typeof GQL.CheckCustomerExistsDocument>) {
const { query } = await getClientWithToken();
const result = await query({
query: GQL.CheckCustomerExistsDocument,
variables,
});
const customer = result.data.customers.at(0);
return { customer };
}
async createCustomer(variables: VariablesOf<typeof GQL.CreateCustomerDocument>) {
const { mutate } = await getClientWithToken();
const mutationResult = await mutate({
mutation: GQL.CreateCustomerDocument,
variables: {
...variables,
data: {
...variables.data,
role: DEFAULT_CUSTOMER_ROLE,
},
},
});
const error = mutationResult.errors?.at(0);
if (error) throw new Error(error.message);
return mutationResult.data;
}
async updateCustomer(variables: VariablesOf<typeof GQL.UpdateCustomerDocument>) {
if (variables.data.bannedUntil || variables.data.phone) {
throw new Error(ERRORS.NO_PERMISSION);
}
const { mutate } = await getClientWithToken();
const mutationResult = await mutate({
mutation: GQL.UpdateCustomerDocument,
variables,
});
const error = mutationResult.errors?.at(0);
if (error) throw new Error(error.message);
return mutationResult.data;
}
}