mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-01-10 10:11:25 +08:00
33 lines
1 KiB
JavaScript
33 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"))
|
||
|
}
|