order: fix lifecycle
This commit is contained in:
parent
9ca79ed336
commit
caa9fc54a7
@ -5,10 +5,28 @@ function timeToDate(time: string) {
|
||||
return new Date(`1970-01-01T${time}Z`);
|
||||
}
|
||||
|
||||
function extractSlotId(slotField: any): number | null {
|
||||
if (typeof slotField === 'number') {
|
||||
return slotField;
|
||||
}
|
||||
|
||||
if (slotField?.id) {
|
||||
return slotField.id;
|
||||
}
|
||||
|
||||
if (slotField?.set?.[0]?.id) {
|
||||
return slotField.set[0].id;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export default {
|
||||
async beforeCreate(event) {
|
||||
const { data } = event.params;
|
||||
const { time_start, time_end, date } = data;
|
||||
const { time_start, time_end } = data;
|
||||
|
||||
const slotId = extractSlotId(data.slot);
|
||||
|
||||
if (!time_start || !time_end) {
|
||||
throw new Error(ERR_INVALID_TIME);
|
||||
@ -22,17 +40,21 @@ export default {
|
||||
.query('api::order.order')
|
||||
.findMany({
|
||||
where: {
|
||||
date,
|
||||
documentId: { $ne: data.documentId },
|
||||
time_start: { $lt: time_end },
|
||||
time_end: { $gt: time_start },
|
||||
slot: {
|
||||
id: { $ne: slotId },
|
||||
},
|
||||
},
|
||||
populate: ['slot'],
|
||||
});
|
||||
|
||||
if (overlappingEntities.length > 0) {
|
||||
throw new Error(ERR_OVERLAPPING_TIME);
|
||||
}
|
||||
},
|
||||
|
||||
async beforeUpdate(event) {
|
||||
const { data, where } = event.params;
|
||||
const { id: entityId } = where;
|
||||
@ -46,27 +68,30 @@ export default {
|
||||
throw new Error(ERR_INVALID_TIME);
|
||||
}
|
||||
|
||||
const existingEntity = await strapi.db.query('api::order.order').findOne({
|
||||
const existingOrder = await strapi.db.query('api::order.order').findOne({
|
||||
where: { id: entityId },
|
||||
select: ['date', 'documentId'],
|
||||
select: ['documentId'],
|
||||
populate: ['slot'],
|
||||
});
|
||||
|
||||
if (!existingEntity) {
|
||||
throw new Error('Запись не найдена');
|
||||
if (!existingOrder || !existingOrder.slot) {
|
||||
throw new Error('Существующий заказ или слот не найден');
|
||||
}
|
||||
|
||||
const { date, documentId } = existingEntity;
|
||||
const slotId = existingOrder.slot.documentId;
|
||||
|
||||
const overlappingEntities = await strapi.db
|
||||
.query('api::order.order')
|
||||
.findMany({
|
||||
where: {
|
||||
date,
|
||||
id: { $ne: entityId },
|
||||
documentId: { $ne: documentId },
|
||||
time_start: { $lt: time_end },
|
||||
time_end: { $gt: time_start },
|
||||
slot: {
|
||||
documentId: { $eq: slotId },
|
||||
},
|
||||
},
|
||||
populate: ['slot'],
|
||||
});
|
||||
|
||||
if (overlappingEntities.length > 0) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user