refactor(graphql): remove NotifyService and related notification logic from orders and API, clean up unused dependencies

This commit is contained in:
vchikalkin 2025-07-22 15:46:58 +03:00
parent 60002a7169
commit 57cab6209d
7 changed files with 0 additions and 138 deletions

View File

@ -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<typeof GQL.CreateOrderDocument>,
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} <b>Запись создана${confirmText}!</b>\n<b>Дата:</b> ${slotDate}\n<b>Время:</b> ${timeStartString} - ${timeEndString}\n<b>Клиент:</b> ${client?.name ?? '-'}\n<b>Услуга:</b> ${service?.name ?? '-'}`;
await notifyByTelegramId(String(master.telegramId), message);
}
// Клиенту
if (client?.telegramId) {
const message = `${emoji} <b>Запись создана${confirmText}!</b>\n<b>Дата:</b> ${slotDate}\n<b>Время:</b> ${timeStartString} - ${timeEndString}\n<b>Мастер:</b> ${master?.name ?? '-'}\n<b>Услуга:</b> ${service?.name ?? '-'}`;
await notifyByTelegramId(String(client.telegramId), message);
}
}
async orderUpdated(variables: VariablesOf<typeof GQL.UpdateOrderDocument>) {
// Получаем 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} <b>Запись изменена!</b>\n<b>Дата:</b> ${slotDate}\n<b>Время:</b> ${timeStartString} - ${timeEndString}\n<b>Клиент:</b> ${client?.name ?? '-'}\n<b>Услуга:</b> ${service?.name ?? '-'}\n<b>Статус:</b> ${orderStateString}`;
await notifyByTelegramId(String(master.telegramId), message);
}
// Клиенту
if (client?.telegramId) {
const message = `${emoji} <b>Запись изменена!</b>\n<b>Дата:</b> ${slotDate}\n<b>Время:</b> ${timeStartString} - ${timeEndString}\n<b>Мастер:</b> ${master?.name ?? '-'}\n<b>Услуга:</b> ${service?.name ?? '-'}\n<b>Статус:</b> ${orderStateString}`;
await notifyByTelegramId(String(client.telegramId), message);
}
}
}

View File

@ -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;
}
}

View File

@ -16,7 +16,6 @@
"dayjs": "catalog:",
"jsonwebtoken": "catalog:",
"radashi": "catalog:",
"telegraf": "catalog:",
"vite-tsconfig-paths": "catalog:",
"vitest": "catalog:",
"zod": "catalog:"

View File

@ -317,9 +317,7 @@ export type OrderFiltersInput = {
not?: InputMaybe<OrderFiltersInput>;
or?: InputMaybe<Array<InputMaybe<OrderFiltersInput>>>;
order_number?: InputMaybe<IntFilterInput>;
price?: InputMaybe<IntFilterInput>;
publishedAt?: InputMaybe<DateTimeFilterInput>;
service_description?: InputMaybe<StringFilterInput>;
services?: InputMaybe<ServiceFiltersInput>;
slot?: InputMaybe<SlotFiltersInput>;
state?: InputMaybe<StringFilterInput>;
@ -332,9 +330,7 @@ export type OrderInput = {
datetime_end?: InputMaybe<Scalars['DateTime']['input']>;
datetime_start?: InputMaybe<Scalars['DateTime']['input']>;
order_number?: InputMaybe<Scalars['Int']['input']>;
price?: InputMaybe<Scalars['Int']['input']>;
publishedAt?: InputMaybe<Scalars['DateTime']['input']>;
service_description?: InputMaybe<Scalars['String']['input']>;
services?: InputMaybe<Array<InputMaybe<Scalars['ID']['input']>>>;
slot?: InputMaybe<Scalars['ID']['input']>;
state?: InputMaybe<Enum_Order_State>;

View File

@ -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',
});
}

View File

@ -1,4 +0,0 @@
import { env as environment } from '../config/env';
import { Telegraf } from 'telegraf';
export const bot = new Telegraf(environment.BOT_TOKEN);

3
pnpm-lock.yaml generated
View File

@ -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))