refactor(bot): replace CustomersService with RegistrationService for customer management
This commit is contained in:
parent
d41194f177
commit
33a20b9f2e
@ -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<BotContext>(
|
||||
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' });
|
||||
|
||||
@ -33,20 +33,6 @@ export class CustomersService extends BaseService {
|
||||
return mutationResult.data;
|
||||
}
|
||||
|
||||
async createCustomer(variables: VariablesOf<typeof GQL.CreateCustomerDocument>) {
|
||||
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<typeof GQL.GetClientsDocument>) {
|
||||
const { query } = await getClientWithToken();
|
||||
|
||||
|
||||
46
packages/graphql/api/registration.ts
Normal file
46
packages/graphql/api/registration.ts
Normal file
@ -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<typeof GQL.CreateCustomerDocument>) {
|
||||
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<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>) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user