mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-11-12 12:40:08 +08:00
32 lines
1 KiB
JavaScript
32 lines
1 KiB
JavaScript
const PubsubConnector = require('./pubsub-connector')
|
|
|
|
module.exports = (db, sequelize) => {
|
|
const parseHookData = ({dataValues, _changed, $modelOptions}) => {
|
|
return {
|
|
objectId: dataValues.id,
|
|
modelName: $modelOptions.name.singular,
|
|
changedFields: _changed,
|
|
}
|
|
}
|
|
|
|
const isTransaction = ({$modelOptions}) => {
|
|
return $modelOptions.name.singular === "Transaction"
|
|
}
|
|
|
|
const transactionLogger = (type) => {
|
|
return (sequelizeHookData) => {
|
|
if (isTransaction(sequelizeHookData)) return;
|
|
const transactionData = Object.assign({type: type},
|
|
parseHookData(sequelizeHookData)
|
|
);
|
|
db.Transaction.create(transactionData);
|
|
transactionData.object = sequelizeHookData.dataValues;
|
|
|
|
PubsubConnector.notifyAccountDeltas(db.accountId, transactionData);
|
|
}
|
|
}
|
|
|
|
sequelize.addHook("afterCreate", transactionLogger("create"))
|
|
sequelize.addHook("afterUpdate", transactionLogger("update"))
|
|
sequelize.addHook("afterDelete", transactionLogger("delete"))
|
|
}
|