diff --git a/src/api/slot/content-types/slot/lifecycles.ts b/src/api/slot/content-types/slot/lifecycles.ts index bbf1ec3..6443e1d 100644 --- a/src/api/slot/content-types/slot/lifecycles.ts +++ b/src/api/slot/content-types/slot/lifecycles.ts @@ -38,39 +38,42 @@ export default { const { id: entityId } = where; const { time_start, time_end } = data; - if (!time_start || !time_end) { - throw new Error(ERR_INVALID_TIME); - } + if (time_start && time_end) { + if (!time_start || !time_end) { + throw new Error(ERR_INVALID_TIME); + } - if (timeToDate(time_start) >= timeToDate(time_end)) { - throw new Error(ERR_INVALID_TIME); - } + if (timeToDate(time_start) >= timeToDate(time_end)) { + throw new Error(ERR_INVALID_TIME); + } - const existingEntity = await strapi.db.query('api::slot.slot').findOne({ - where: { id: entityId }, - select: ['date', 'documentId'], - }); - - if (!existingEntity) { - throw new Error('Запись не найдена'); - } - - const { date, documentId } = existingEntity; - - const overlappingEntities = await strapi.db - .query('api::slot.slot') - .findMany({ - where: { - date, - id: { $ne: entityId }, - documentId: { $ne: documentId }, - time_start: { $lt: time_end }, - time_end: { $gt: time_start }, - }, + const existingEntity = await strapi.db.query('api::slot.slot').findOne({ + where: { id: entityId }, + select: ['date', 'documentId'], }); - if (overlappingEntities.length > 0) { - throw new Error(ERR_OVERLAPPING_TIME); + if (!existingEntity) { + throw new Error('Запись не найдена'); + } + + const { date, documentId } = existingEntity; + + const overlappingEntities = await strapi.db + .query('api::slot.slot') + .findMany({ + where: { + date, + id: { $ne: entityId }, + documentId: { $ne: documentId }, + time_start: { $lt: time_end }, + time_end: { $gt: time_start }, + }, + }); + + if (overlappingEntities.length > 0) { + throw new Error(ERR_OVERLAPPING_TIME); + } } }, }; +