feat: add validation for slot time changes based on order status
This commit is contained in:
parent
066de8ced8
commit
c5d4ef3392
@ -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 {
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user