Do contact insertion in a transaction

This commit is contained in:
Ben Gotow 2016-07-15 10:23:01 -07:00
parent 979401883d
commit e5bada04d3

View file

@ -17,24 +17,24 @@ class ContactProcessor {
processMessage({db, message}) {
const {Contact} = db;
let allContacts = []
const fields = ['to', 'from', 'bcc', 'cc']
fields.forEach((field) => {
let allContacts = [];
['to', 'from', 'bcc', 'cc'].forEach((field) => {
allContacts = allContacts.concat(message[field])
})
const upserts = allContacts.filter(this.verified).map((contact) =>
Contact.upsert({
name: contact.name,
email: contact.email,
accountId: message.accountId,
})
)
const verifiedContacts = allContacts.filter(this.verified);
return Promise.all(upserts)
.then(() => {
return message
})
return db.sequelize.transaction((transaction) => {
return Promise.all(verifiedContacts.map((contact) =>
Contact.upsert({
name: contact.name,
email: contact.email,
accountId: message.accountId,
}, {
transaction,
})
))
}).thenReturn(message)
}
}