From e5bada04d35e0f1d5cefbc5d4b2d5c44b7d005ff Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Fri, 15 Jul 2016 10:23:01 -0700 Subject: [PATCH] Do contact insertion in a transaction --- .../processors/contact.js | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/nylas-message-processor/processors/contact.js b/packages/nylas-message-processor/processors/contact.js index dc07a9f8b..c5d93e6e0 100644 --- a/packages/nylas-message-processor/processors/contact.js +++ b/packages/nylas-message-processor/processors/contact.js @@ -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) } }