- Introduced a new input for capturing the surname of the user during contact addition. - Updated the contact parsing logic to include surname alongside name and phone number. - Modified the customer creation and update processes to accommodate surname, ensuring full name is used in confirmation messages. - Adjusted localization files to reflect the new surname input prompt and updated confirmation messages. - Refactored components to utilize a unified method for retrieving full customer names, improving consistency across the application.
74 lines
2.1 KiB
TypeScript
74 lines
2.1 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';
|
|
import { isCustomerBanned } from '@repo/utils/customer';
|
|
|
|
const DEFAULT_CUSTOMER_ROLE = GQL.Enum_Customer_Role.Client;
|
|
|
|
export class RegistrationService {
|
|
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 getCustomer(variables: VariablesOf<typeof GQL.GetCustomerDocument>) {
|
|
const { query } = await getClientWithToken();
|
|
|
|
const result = await query({
|
|
query: GQL.GetCustomerDocument,
|
|
variables,
|
|
});
|
|
|
|
const customer = result.data.customers.at(0);
|
|
|
|
return { customer };
|
|
}
|
|
|
|
async updateCustomer(variables: VariablesOf<typeof GQL.UpdateCustomerDocument>) {
|
|
// Проверяем бан для существующего пользователя
|
|
if (variables.documentId) {
|
|
const { query } = await getClientWithToken();
|
|
const result = await query({
|
|
query: GQL.GetCustomerDocument,
|
|
variables: { documentId: variables.documentId },
|
|
});
|
|
const customer = result.data.customers.at(0);
|
|
if (customer && isCustomerBanned(customer)) {
|
|
throw new Error(ERRORS.NO_PERMISSION);
|
|
}
|
|
}
|
|
|
|
if (variables.data.bannedUntil) {
|
|
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;
|
|
}
|
|
}
|