fix: enhance datetime validation in order and slot lifecycles to ensure accurate comparisons

This commit is contained in:
vchikalkin 2025-07-18 15:20:59 +03:00
parent f0e4a99032
commit ec06282d56
2 changed files with 20 additions and 5 deletions

View File

@ -15,8 +15,11 @@ const ERR_MISSING_SERVICE = 'Не указан сервис';
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');
dayjs.extend(utc);
dayjs.extend(timezone);
if (!dayjs.prototype.tz) {
dayjs.extend(utc);
dayjs.extend(timezone);
}
export default {
async beforeCreate(event) {
@ -157,7 +160,7 @@ export default {
const now = dayjs().tz('Europe/Moscow');
const orderStart = dayjs(existingOrder.datetime_start).tz('Europe/Moscow');
if (state === 'completed' && now.isBefore(orderStart)) {
if (state === 'completed' && now.isBefore(orderStart, 'minute')) {
throw new Error('Нельзя завершить запись до её наступления');
}

View File

@ -1,4 +1,12 @@
import { extractId } from '../../../../utils';
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');
if (!dayjs.prototype.tz) {
dayjs.extend(utc);
dayjs.extend(timezone);
}
const ERR_INVALID_TIME = 'Некорректное время';
const ERR_OVERLAPPING_TIME = 'Время пересекается с другими слотами';
@ -35,8 +43,12 @@ export default {
}
// Проверка, что слот не создаётся в прошлом
if (datetime_start && new Date(datetime_start) < new Date()) {
throw new Error(ERR_PAST_SLOT);
if (datetime_start) {
const now = dayjs().tz('Europe/Moscow');
const slotStart = dayjs(datetime_start).tz('Europe/Moscow');
if (slotStart.isBefore(now, 'minute')) {
throw new Error(ERR_PAST_SLOT);
}
}
// Проверка валидности времени