mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-01-13 03:29:46 +08:00
2f3ca2a906
# Conflicts: # packages/nylas-core/hook-transaction-log.js # packages/nylas-core/models/account/transaction.js
35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
const _ = require('underscore')
|
|
const PubsubConnector = require('./pubsub-connector')
|
|
|
|
module.exports = (db, sequelize) => {
|
|
const isTransaction = ($modelOptions) => {
|
|
return $modelOptions.name.singular === "transaction"
|
|
}
|
|
|
|
const allIgnoredFields = (changedFields) => {
|
|
const IGNORED_FIELDS = ["syncState", "version"];
|
|
return _.difference(Object.keys(changedFields), IGNORED_FIELDS).length === 0
|
|
}
|
|
|
|
const transactionLogger = (event) => {
|
|
return ({dataValues, _changed, $modelOptions}) => {
|
|
if ((isTransaction($modelOptions) || allIgnoredFields(_changed))) {
|
|
return;
|
|
}
|
|
|
|
const transactionData = Object.assign({event}, {
|
|
object: $modelOptions.name.singular,
|
|
objectId: dataValues.id,
|
|
changedFields: Object.keys(_changed),
|
|
});
|
|
|
|
db.Transaction.create(transactionData).then((transaction) => {
|
|
PubsubConnector.notifyDelta(db.accountId, transaction.toJSON());
|
|
})
|
|
}
|
|
}
|
|
|
|
sequelize.addHook("afterCreate", transactionLogger("create"))
|
|
sequelize.addHook("afterUpdate", transactionLogger("modify"))
|
|
sequelize.addHook("afterDelete", transactionLogger("delete"))
|
|
}
|