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) { 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) { 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) { 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; } }