From cfa5a02ca95eb5ff3ae793c1135393ed555bc359 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Sat, 2 Aug 2025 11:19:21 +0300 Subject: [PATCH] feat: add validation to prevent order creation in the past - Introduced a new error message for attempts to create orders with a start time in the past. - Implemented a check in the order lifecycle to validate that the order start time is not before the current time. --- src/api/order/content-types/order/lifecycles.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/api/order/content-types/order/lifecycles.ts b/src/api/order/content-types/order/lifecycles.ts index 0b2c3e3..8c7a42c 100644 --- a/src/api/order/content-types/order/lifecycles.ts +++ b/src/api/order/content-types/order/lifecycles.ts @@ -17,6 +17,7 @@ const ERR_EXISTING_ORDER_OR_SLOT_NOT_FOUND = 'Существующий заказ или слот не найден'; const ERR_CANNOT_COMPLETE_BEFORE_START = 'Нельзя завершить запись до её наступления'; +const ERR_ORDER_IN_PAST = 'Нельзя создать запись на время в прошлом'; const STATE_MAP: Record = { approved: 'Подтверждено', @@ -189,6 +190,14 @@ export default { throw new Error(ERR_INVALID_TIME); } + // Проверка, что заказ не создается на время в прошлом + const now = dayjs().tz(DEFAULT_TZ); + const orderStart = dayjs(datetime_start).tz(DEFAULT_TZ); + + if (orderStart.isBefore(now, 'minute')) { + throw new Error(ERR_ORDER_IN_PAST); + } + // Получаем слот const slot = await strapi.db.query('api::slot.slot').findOne({ where: { id: slotId },