From 57cab6209d86cbe0787e5a89d3b9c1e726ae2cd5 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Tue, 22 Jul 2025 15:46:58 +0300 Subject: [PATCH] refactor(graphql): remove NotifyService and related notification logic from orders and API, clean up unused dependencies --- packages/graphql/api/notify.ts | 108 ------------------ packages/graphql/api/orders.ts | 9 -- packages/graphql/package.json | 1 - .../graphql/types/operations.generated.ts | 4 - packages/graphql/utils/notify.ts | 9 -- packages/graphql/utils/telegram.ts | 4 - pnpm-lock.yaml | 3 - 7 files changed, 138 deletions(-) delete mode 100644 packages/graphql/api/notify.ts delete mode 100644 packages/graphql/utils/notify.ts delete mode 100644 packages/graphql/utils/telegram.ts diff --git a/packages/graphql/api/notify.ts b/packages/graphql/api/notify.ts deleted file mode 100644 index e23ed93..0000000 --- a/packages/graphql/api/notify.ts +++ /dev/null @@ -1,108 +0,0 @@ -/* 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); - } - } -} diff --git a/packages/graphql/api/orders.ts b/packages/graphql/api/orders.ts index 696f8e5..044ec11 100644 --- a/packages/graphql/api/orders.ts +++ b/packages/graphql/api/orders.ts @@ -2,7 +2,6 @@ import { getClientWithToken } from '../apollo/client'; import * as GQL from '../types'; import { BaseService } from './base'; import { CustomersService } from './customers'; -import { NotifyService } from './notify'; import { ServicesService } from './services'; import { type VariablesOf } from '@graphql-typed-document-node/core'; import { isCustomerMaster } from '@repo/utils/customer'; @@ -64,10 +63,6 @@ export class OrdersService extends BaseService { const error = mutationResult.errors?.at(0); if (error) throw new Error(error.message); - // Уведомление об создании заказа - const notifyService = new NotifyService(this.customer); - notifyService.orderCreated(variables, isCustomerMaster(customer)); - return mutationResult.data; } @@ -136,10 +131,6 @@ export class OrdersService extends BaseService { const error = mutationResult.errors?.at(0); if (error) throw new Error(error.message); - // Уведомление об изменении заказа - const notifyService = new NotifyService(this.customer); - notifyService.orderUpdated(variables); - return mutationResult.data; } } diff --git a/packages/graphql/package.json b/packages/graphql/package.json index c3d8381..9715fe3 100644 --- a/packages/graphql/package.json +++ b/packages/graphql/package.json @@ -16,7 +16,6 @@ "dayjs": "catalog:", "jsonwebtoken": "catalog:", "radashi": "catalog:", - "telegraf": "catalog:", "vite-tsconfig-paths": "catalog:", "vitest": "catalog:", "zod": "catalog:" diff --git a/packages/graphql/types/operations.generated.ts b/packages/graphql/types/operations.generated.ts index dece7ba..f8b43a0 100644 --- a/packages/graphql/types/operations.generated.ts +++ b/packages/graphql/types/operations.generated.ts @@ -317,9 +317,7 @@ export type OrderFiltersInput = { not?: InputMaybe; or?: InputMaybe>>; order_number?: InputMaybe; - price?: InputMaybe; publishedAt?: InputMaybe; - service_description?: InputMaybe; services?: InputMaybe; slot?: InputMaybe; state?: InputMaybe; @@ -332,9 +330,7 @@ export type OrderInput = { datetime_end?: InputMaybe; datetime_start?: InputMaybe; order_number?: InputMaybe; - price?: InputMaybe; publishedAt?: InputMaybe; - service_description?: InputMaybe; services?: InputMaybe>>; slot?: InputMaybe; state?: InputMaybe; diff --git a/packages/graphql/utils/notify.ts b/packages/graphql/utils/notify.ts deleted file mode 100644 index c6394f4..0000000 --- a/packages/graphql/utils/notify.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { bot } from './telegram'; - -export async function notifyByTelegramId(telegramId: string | undefined, message: string) { - if (!telegramId) return; - - await bot.telegram.sendMessage(telegramId, message, { - parse_mode: 'HTML', - }); -} diff --git a/packages/graphql/utils/telegram.ts b/packages/graphql/utils/telegram.ts deleted file mode 100644 index 754d6c0..0000000 --- a/packages/graphql/utils/telegram.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { env as environment } from '../config/env'; -import { Telegraf } from 'telegraf'; - -export const bot = new Telegraf(environment.BOT_TOKEN); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fb1ebf6..a0a03f4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -299,9 +299,6 @@ importers: radashi: specifier: 'catalog:' version: 12.6.0 - telegraf: - specifier: 'catalog:' - version: 4.16.3 vite-tsconfig-paths: specifier: 'catalog:' version: 5.1.4(typescript@5.8.3)(vite@5.4.19(@types/node@24.0.10))