Hi !
I’ve been working with Meteor only for the last 2 weeks & I directly liked it !!!
But now I’m facing a problem and I don’t know how to address it !
I’m using simple-schema / collection 2 / autoform. I use doc validator feature this way:
PricingsSchema.addDocValidator((pricing) => {
let data;
if (_.has(pricing, '$set')) {
data = pricing.$set;
} else {
data = pricing;
}
if (_.has(data, 'customer') && _.has(data, 'startValidity') && _.has(data, 'endValidity')
&& _.has(data, 'startDay') && _.has(data, 'startHour') && _.has(data, 'endDay')
&& _.has(data, 'endHour') && _.has(data, 'formula')) {
const overlaps = !!Pricings.findOne({
$and: [
{ customer: data.customer },
{ startValidity: { $lte: data.endValidity } },
{ endValidity: { $gte: data.startValidity } },
{ $or: [
{ startDay: { $lt: data.endDay } },
{ $and: [
{ startDay: data.endDay },
{ startHour: { $lte: data.endHour } },
] },
] },
{ $or: [
{ endDay: { $gt: data.startDay } },
{ $and: [
{ endDay: data.startDay },
{ endHour: { $gte: data.startHour } },
] },
] },
],
});
if (overlaps) {
return [
{ name: 'startValidity', type: 'periodOverlap' },
{ name: 'endValidity', type: 'periodOverlap' },
{ name: 'startDay', type: 'periodOverlap' },
{ name: 'startHour', type: 'periodOverlap' },
{ name: 'endDay', type: 'periodOverlap' },
{ name: 'endHour', type: 'periodOverlap' },
];
}
if (data.startValidity.getTime() > data.endValidity.getTime()) {
return [
{ name: 'startValidity', type: 'validityIntervalNotValid' },
{ name: 'endValidity', type: 'validityIntervalNotValid' },
];
} else if (data.startDay > data.endDay) {
return [
{ name: 'startDay', type: 'applicationIntervalNotValid' },
{ name: 'endDay', type: 'applicationIntervalNotValid' },
];
} else if (data.startDay === data.endDay
&& parseInt(data.startHour.replace(':', ''), 10) >= parseInt(data.endHour.replace(':', ''), 10)) {
return [
{ name: 'startHour', type: 'applicationIntervalNotValid' },
{ name: 'endHour', type: 'applicationIntervalNotValid' },
];
}
}
return [];
});
I use it to check that there’s no overlap in my database for pricings.
I also use autoform to manage create & update forms for these pricings.
Insert form is OK for me :). But in update form, since my customer’s field has denyUpdate: true
, I can’t use it in my validator when updating a doc (this field isn’t in $set
). Moreover, I would like to know which _id
is updated in order to check that entry potentially returned by findOne
isn’t the same …
How can I complete that ? I was thinking about a autoform hook, but i’m quite confused by :
// Pass an array of form IDs for multiple forms
AutoForm.addHooks(['form1', 'form2', 'form3', 'form4'], hooksObject);
in documentation … Because since we are talking about update forms, their id
is dynamic, depending on pricings ids … I’ve created a helper for that :
formId() {
return `updatePricingForm-${this.pricing._id}`;
},
If it is the best solution to provide _id & customer to DocValidator, how could I use autoform hooks ?
Thanks for your help guys, and sorry if my english isn’t as good as i wish …
Mathias