diff --git a/src/api/slot/content-types/slot/lifecycles.ts b/src/api/slot/content-types/slot/lifecycles.ts index 6443e1d..02ab373 100644 --- a/src/api/slot/content-types/slot/lifecycles.ts +++ b/src/api/slot/content-types/slot/lifecycles.ts @@ -1,5 +1,14 @@ const ERR_INVALID_TIME = 'Некорректное время'; const ERR_OVERLAPPING_TIME = 'Время пересекается с другими слотами'; +const ERR_FORBIDDEN_SLOT_STATUS = + 'Нельзя менять время слота, если есть связанные заказы'; + +const FORBIDDEN_ORDER_STATES = [ + 'scheduled', + 'approved', + 'completed', + 'cancelling', +]; function timeToDate(time: string) { return new Date(`1970-01-01T${time}Z`); @@ -50,8 +59,18 @@ export default { const existingEntity = await strapi.db.query('api::slot.slot').findOne({ where: { id: entityId }, select: ['date', 'documentId'], + populate: ['orders'], }); + const orders = existingEntity?.orders; + + if ( + orders?.length > 0 && + orders?.some(order => FORBIDDEN_ORDER_STATES.includes(order.state)) + ) { + throw new Error(ERR_FORBIDDEN_SLOT_STATUS); + } + if (!existingEntity) { throw new Error('Запись не найдена'); } @@ -76,4 +95,3 @@ export default { } }, }; -