Auto add properties

This commit is contained in:
dbroqua 2018-10-10 21:10:43 +02:00
parent dcc78c725b
commit e93ad5d7b3

View File

@ -31,7 +31,41 @@ class Vegetables {
vegetables.create(newItem)
.then(item => {
callback(null, item)
models.properties
.findAndCountAll()
.then(properties => {
if (properties && properties.rows.length > 0) {
let saved = 0
const _callback = () => {
if (properties.count === saved) {
callback(null, item)
}
properties.rows.forEach(propperty => {
models.vegetableProp
.create({
vegetablesId: item.id,
propertyId: propperty.id
})
.then(() => {
saved += 1
_callback()
})
.catch((e) => {
console.log(e)
saved += 1
_callback()
})
})
}
} else {
callback(null, item)
}
})
.catch(e => {
item.destroy()
callback(e, 500)
})
})
.catch(e => {
callback(e, null)