From ec06282d56de9afe314e33a403ec87d19af0567c Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Fri, 18 Jul 2025 15:20:59 +0300 Subject: [PATCH] fix: enhance datetime validation in order and slot lifecycles to ensure accurate comparisons --- src/api/order/content-types/order/lifecycles.ts | 9 ++++++--- src/api/slot/content-types/slot/lifecycles.ts | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/api/order/content-types/order/lifecycles.ts b/src/api/order/content-types/order/lifecycles.ts index 7541e00..4fb52a4 100644 --- a/src/api/order/content-types/order/lifecycles.ts +++ b/src/api/order/content-types/order/lifecycles.ts @@ -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('Нельзя завершить запись до её наступления'); } diff --git a/src/api/slot/content-types/slot/lifecycles.ts b/src/api/slot/content-types/slot/lifecycles.ts index 39982a3..1604b65 100644 --- a/src/api/slot/content-types/slot/lifecycles.ts +++ b/src/api/slot/content-types/slot/lifecycles.ts @@ -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); + } } // Проверка валидности времени