fix(bot): streamline customer creation logic by checking for existing customer before creating a new one

This commit is contained in:
vchikalkin 2025-07-03 19:02:25 +03:00
parent 28e88e02aa
commit d41194f177

View File

@ -177,25 +177,34 @@ bot.command('sharebot', async (context) => {
bot.on(message('contact'), async (context) => {
const telegramId = context.from.id;
const { contact } = context.message;
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({ telegramId });
const { customer } = await customerService.getCustomer({ phone, telegramId });
if (!customer) {
const { contact } = context.message;
const name = (contact.first_name || '') + ' ' + (contact.last_name || '').trim();
const phone = normalizePhoneNumber(contact.phone_number);
if (customer) {
await customerService.updateCustomer({
data: { active: true, telegramId },
});
const response = await customerService
.createCustomer({ name, phone, telegramId: context.from.id })
.catch((error) => {
context.reply(MSG_ERROR(error), { parse_mode: 'HTML' });
});
if (response) {
return context.reply(MSG_PHONE_SAVED + commandsList, {
...KEYBOARD_REMOVE,
parse_mode: 'HTML',
});
}
return context.reply(MSG_PHONE_SAVED + commandsList, {
...KEYBOARD_REMOVE,
parse_mode: 'HTML',
});
}
const response = await customerService
.createCustomer({ name, phone, telegramId })
.catch((error) => {
context.reply(MSG_ERROR(error), { parse_mode: 'HTML' });
});
if (response) {
return context.reply(MSG_PHONE_SAVED + commandsList, {
...KEYBOARD_REMOVE,
parse_mode: 'HTML',
});
}
});