[fix] Fix transaction creation for contacts

Summary:
Fixes T7283. We weren't creating deltas for contacts because we were inserting contacts using `UPSERT`, which requires us to add another sequelize hook.  Unfortunately, support for this hook is only available on sequelize 4.x.

I didn't want to upgrade our sequelize version right now and cause of bunch of mysterious failures, so I've simply changed the `UPSERT` to be either an `INSERT`or `UPDATE`. We didn't really need it anyway.

Test Plan: Checked that N1 was receiving contact deltas.

Reviewers: bengotow

Reviewed By: bengotow

Maniphest Tasks: T7283

Differential Revision: https://phab.nylas.com/D3481
This commit is contained in:
Karim Hamidou 2016-12-05 15:37:17 -08:00
parent 269d61171a
commit 04b1763965
4 changed files with 25 additions and 16 deletions

4
.arcconfig Normal file
View file

@ -0,0 +1,4 @@
{
"project_id" : "K2",
"conduit_uri" : "https://phab.nylas.com/"
}

3
.gitignore vendored
View file

@ -1,6 +1,3 @@
.arcconfig
.arclint
arclib
*.swp
*~
.DS_Store

View file

@ -46,5 +46,9 @@ module.exports = (db, sequelize, {only, onCreatedTransaction} = {}) => {
sequelize.addHook("afterCreate", transactionLogger("create"))
sequelize.addHook("afterUpdate", transactionLogger("modify"))
// NOTE: Hooking UPSERT requires Sequelize 4.x. We're
// on version 3 right now, but leaving this here for when we upgrade.
sequelize.addHook("afterUpsert", transactionLogger("modify"))
sequelize.addHook("afterDelete", transactionLogger("delete"))
}

View file

@ -13,26 +13,30 @@ function isContactVerified(contact) {
return true
}
function extractContacts({db, message}) {
const {Contact} = db;
async function extractContacts({db, message}) {
let allContacts = [];
['to', 'from', 'bcc', 'cc'].forEach((field) => {
allContacts = allContacts.concat(message[field])
})
const verifiedContacts = allContacts.filter(c => isContactVerified(c));
return db.sequelize.transaction((transaction) => {
return Promise.all(verifiedContacts.map((contact) =>
Contact.upsert({
name: contact.name,
email: contact.email,
return db.sequelize.transaction(async (transaction) => {
for (const c of verifiedContacts) {
const id = cryptography.createHash('sha256').update(c.email, 'utf8').digest('hex');
let contact = await db.Contact.findById(id);
const cdata = {
name: c.name,
email: c.email,
accountId: message.accountId,
id: cryptography.createHash('sha256').update(contact.email, 'utf8').digest('hex'),
}, {
transaction,
})
))
id: id,
};
if (!contact) {
contact = await db.Contact.create(cdata)
} else {
await contact.update(cdata);
}
}
}).thenReturn(message)
}