78 lines
3.7 KiB
TypeScript
78 lines
3.7 KiB
TypeScript
import type * 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';
|
|
|
|
export class NotifyService extends BaseService {
|
|
async orderCreated(variables: {
|
|
input: Omit<VariablesOf<typeof GQL.CreateOrderDocument>['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 { 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 = `✅ <b>Запись создана!</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 = `✅ <b>Запись создана!</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;
|
|
const service = order.services[0];
|
|
const master = slot?.master;
|
|
const client = order.client;
|
|
|
|
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 = `✏️ <b>Запись изменена!</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 = `✏️ <b>Запись изменена!</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);
|
|
}
|
|
}
|
|
}
|