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, sumTime } from '@repo/utils/datetime-format'; const STATE_MAP = { approved: 'Одобрено', cancelled: 'Отменено', cancelling: 'Отменяется', completed: 'Завершено', created: 'Создано', scheduled: 'Запланировано', unknown: 'Неизвестно', }; export class NotifyService extends BaseService { async orderCreated(variables: { input: Omit['input'], 'time_end'>; }) { 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 timeStart = String(variables.input.time_start ?? ''); const clientId = String(variables.input.client ?? ''); const state = String(variables.input.state ?? ''); const isApproved = state === GQL.Enum_Order_State.Approved; 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 }); const slotDate = formatDate(slot?.date).user(); const timeStartString = formatTime(timeStart).user(); const timeEndString = formatTime( service?.duration ? sumTime(timeStart, service.duration) : timeStart, ).user(); // Мастеру if (master?.telegramId) { const message = `✅ Запись создана${isApproved ? ' и подтверждена' : ''}!\nДата: ${slotDate}\nВремя: ${timeStartString} - ${timeEndString}\nКлиент: ${client?.name ?? '-'}\nУслуга: ${service?.name ?? '-'}`; await notifyByTelegramId(String(master.telegramId), message); } // Клиенту if (client?.telegramId) { const message = `✅ Запись создана${isApproved ? ' и подтверждена' : ''}!\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; const service = order.services[0]; const master = slot?.master; const client = order.client; const orderStateString = STATE_MAP[order.state || 'unknown']; const slotDate = formatDate(slot?.date).user(); const timeStartString = formatTime(order.time_start ?? '').user(); const timeEndString = formatTime(order.time_end ?? '').user(); // Мастеру if (master?.telegramId) { const message = `✏️ Запись изменена!\nДата: ${slotDate}\nВремя: ${timeStartString} - ${timeEndString}\nКлиент: ${client?.name ?? '-'}\nУслуга: ${service?.name ?? '-'}\nСтатус: ${orderStateString}`; await notifyByTelegramId(String(master.telegramId), message); } // Клиенту if (client?.telegramId) { const message = `✏️ Запись изменена!\nДата: ${slotDate}\nВремя: ${timeStartString} - ${timeEndString}\nМастер: ${master?.name ?? '-'}\nУслуга: ${service?.name ?? '-'}\nСтатус: ${orderStateString}`; await notifyByTelegramId(String(client.telegramId), message); } } }