Cascade notification setting deletion on user account deletion

This commit is contained in:
Tom Moor
2018-12-06 19:14:43 -08:00
parent 9ca0038d39
commit 06e7ab84cd
3 changed files with 21 additions and 27 deletions

View File

@ -39,17 +39,16 @@ const User = sequelize.define(
return !!this.suspendedAt;
},
},
indexes: [
{
fields: ['email'],
},
],
}
);
// Class methods
User.associate = models => {
User.hasMany(models.ApiKey, { as: 'apiKeys' });
User.hasMany(models.ApiKey, { as: 'apiKeys', onDelete: 'cascade' });
User.hasMany(models.NotificationSetting, {
as: 'notificationSettings',
onDelete: 'cascade',
});
User.hasMany(models.Document, { as: 'documents' });
User.hasMany(models.View, { as: 'views' });
};
@ -93,9 +92,19 @@ const setRandomJwtSecret = model => {
model.jwtSecret = crypto.randomBytes(64).toString('hex');
};
const removeIdentifyingInfo = async model => {
await ApiKey.destroy({ where: { userId: model.id } });
await Star.destroy({ where: { userId: model.id } });
const removeIdentifyingInfo = async (model, options) => {
await NotificationSetting.destroy({
where: { userId: model.id },
transaction: options.transaction,
});
await ApiKey.destroy({
where: { userId: model.id },
transaction: options.transaction,
});
await Star.destroy({
where: { userId: model.id },
transaction: options.transaction,
});
model.email = '';
model.name = 'Unknown';
@ -108,7 +117,7 @@ const removeIdentifyingInfo = async model => {
// this shouldn't be needed once this issue is resolved:
// https://github.com/sequelize/sequelize/issues/9318
await model.save({ hooks: false });
await model.save({ hooks: false, transaction: options.transaction });
};
const checkLastAdmin = async model => {