/* eslint-disable complexity */ import * as GQL from '../types'; import { notifyByTelegramId } from '../utils/notify'; import { BaseService } from './base'; import { CustomersService } from './customers'; import { OrdersService } from './orders'; import { ServicesService } from './services'; import { SlotsService } from './slots'; import { type VariablesOf } from '@graphql-typed-document-node/core'; import { formatDate, formatTime, getMinutes, sumTime } from '@repo/utils/datetime-format'; const STATE_MAP = { approved: 'Одобрено', cancelled: 'Отменено', cancelling: 'Отменяется', completed: 'Завершено', created: 'Создано', scheduled: 'Запланировано', unknown: 'Неизвестно', }; export class NotifyService extends BaseService { async orderCreated( variables: VariablesOf, createdByMaster: boolean, ) { const customersService = new CustomersService(this.customer); const slotsService = new SlotsService(this.customer); const servicesService = new ServicesService(this.customer); const slotId = String(variables.input.slot ?? ''); const serviceId = String(variables.input.services?.[0] ?? ''); const clientId = String(variables.input.client ?? ''); if (!serviceId || !clientId || !slotId) return; let emoji = '🆕'; let confirmText = ''; if (createdByMaster) { emoji = '✅'; confirmText = ' и подтверждена'; } const { slot } = await slotsService.getSlot({ documentId: slotId }); const { service } = await servicesService.getService({ documentId: serviceId }); const { customer: master } = await customersService.getCustomer({ documentId: slot?.master?.documentId ?? '', }); const { customer: client } = await customersService.getCustomer({ documentId: clientId }); if (!slot?.datetime_start || !variables.input.datetime_start || !service?.duration) return; const slotDate = formatDate(slot?.datetime_start).user(); const timeStartString = formatTime(variables.input.datetime_start).user(); const timeEndString = formatTime( sumTime(variables.input.datetime_start, getMinutes(service?.duration)), ).user(); // Мастеру if (master?.telegramId) { const message = `${emoji} Запись создана${confirmText}!\nДата: ${slotDate}\nВремя: ${timeStartString} - ${timeEndString}\nКлиент: ${client?.name ?? '-'}\nУслуга: ${service?.name ?? '-'}`; await notifyByTelegramId(String(master.telegramId), message); } // Клиенту if (client?.telegramId) { const message = `${emoji} Запись создана${confirmText}!\nДата: ${slotDate}\nВремя: ${timeStartString} - ${timeEndString}\nМастер: ${master?.name ?? '-'}\nУслуга: ${service?.name ?? '-'}`; await notifyByTelegramId(String(client.telegramId), message); } } async orderUpdated(variables: VariablesOf) { // Получаем order через OrdersService const ordersService = new OrdersService(this.customer); const { order } = await ordersService.getOrder({ documentId: variables.documentId }); if (!order) return; const slot = order.slot; if (!slot) return; const service = order.services[0]; const master = slot?.master; const client = order.client; const orderStateString = STATE_MAP[order.state || 'unknown']; const slotDate = formatDate(slot?.datetime_start).user(); const timeStartString = formatTime(order.datetime_start ?? '').user(); const timeEndString = formatTime(order.datetime_end ?? '').user(); let emoji = '✏️'; if (order.state === GQL.Enum_Order_State.Cancelled) { emoji = '❌'; } else if (order.state === GQL.Enum_Order_State.Approved) { emoji = '✅'; } // Мастеру if (master?.telegramId) { const message = `${emoji} Запись изменена!\nДата: ${slotDate}\nВремя: ${timeStartString} - ${timeEndString}\nКлиент: ${client?.name ?? '-'}\nУслуга: ${service?.name ?? '-'}\nСтатус: ${orderStateString}`; await notifyByTelegramId(String(master.telegramId), message); } // Клиенту if (client?.telegramId) { const message = `${emoji} Запись изменена!\nДата: ${slotDate}\nВремя: ${timeStartString} - ${timeEndString}\nМастер: ${master?.name ?? '-'}\nУслуга: ${service?.name ?? '-'}\nСтатус: ${orderStateString}`; await notifyByTelegramId(String(client.telegramId), message); } } }