diff --git a/apps/bot/src/index.ts b/apps/bot/src/index.ts index 65f28a8..fffc8bf 100644 --- a/apps/bot/src/index.ts +++ b/apps/bot/src/index.ts @@ -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', + }); } });