diff --git a/apps/bot/src/index.ts b/apps/bot/src/index.ts index fffc8bf..26d5b4e 100644 --- a/apps/bot/src/index.ts +++ b/apps/bot/src/index.ts @@ -23,6 +23,7 @@ import { import { isCustomerMaster } from './utils/customer'; import { normalizePhoneNumber } from './utils/phone'; import { CustomersService } from '@repo/graphql/api/customers'; +import { RegistrationService } from '@repo/graphql/api/registration'; import { Enum_Customer_Role } from '@repo/graphql/types'; import { Scenes, session, Telegraf, type Context as TelegrafContext } from 'telegraf'; import { message } from 'telegraf/filters'; @@ -85,7 +86,8 @@ const addContactScene = new Scenes.WizardScene( let documentId = existingCustomer?.documentId; if (!documentId) { - const createCustomerResult = await customerService.createCustomer({ name, phone }); + const registrationService = new RegistrationService(); + const createCustomerResult = await registrationService.createCustomer({ name, phone }); documentId = createCustomerResult?.createCustomer?.documentId; if (!documentId) throw new Error('Customer not created'); } @@ -181,12 +183,14 @@ bot.on(message('contact'), async (context) => { const name = (contact.first_name || '') + ' ' + (contact.last_name || '').trim(); const phone = normalizePhoneNumber(contact.phone_number); - const customerService = new CustomersService({ telegramId }); - const { customer } = await customerService.getCustomer({ phone, telegramId }); + const registrationService = new RegistrationService(); + const { customer } = await registrationService.getCustomer({ phone }); - if (customer) { - await customerService.updateCustomer({ - data: { active: true, telegramId }, + if (customer && !customer.telegramId) { + // Значит пользователя добавили через отправку контакта + await registrationService.updateCustomer({ + data: { active: true, name, telegramId }, + documentId: customer.documentId, }); return context.reply(MSG_PHONE_SAVED + commandsList, { @@ -195,7 +199,7 @@ bot.on(message('contact'), async (context) => { }); } - const response = await customerService + const response = await registrationService .createCustomer({ name, phone, telegramId }) .catch((error) => { context.reply(MSG_ERROR(error), { parse_mode: 'HTML' }); diff --git a/packages/graphql/api/customers.ts b/packages/graphql/api/customers.ts index c5a3aed..b65cb42 100644 --- a/packages/graphql/api/customers.ts +++ b/packages/graphql/api/customers.ts @@ -33,20 +33,6 @@ export class CustomersService extends BaseService { return mutationResult.data; } - async createCustomer(variables: VariablesOf) { - const { mutate } = await getClientWithToken(); - - const mutationResult = await mutate({ - mutation: GQL.CreateCustomerDocument, - variables, - }); - - const error = mutationResult.errors?.at(0); - if (error) throw new Error(error.message); - - return mutationResult.data; - } - async getClients(variables?: VariablesOf) { const { query } = await getClientWithToken(); diff --git a/packages/graphql/api/registration.ts b/packages/graphql/api/registration.ts new file mode 100644 index 0000000..cf401d6 --- /dev/null +++ b/packages/graphql/api/registration.ts @@ -0,0 +1,46 @@ +import { getClientWithToken } from '../apollo/client'; +import * as GQL from '../types'; +import { type VariablesOf } from '@graphql-typed-document-node/core'; + +export class RegistrationService { + async createCustomer(variables: VariablesOf) { + const { mutate } = await getClientWithToken(); + + const 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) { + 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) { + 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; + } +}