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

@ -52,6 +52,7 @@ NotificationSetting.associate = models => {
NotificationSetting.belongsTo(models.User, { NotificationSetting.belongsTo(models.User, {
as: 'user', as: 'user',
foreignKey: 'userId', foreignKey: 'userId',
onDelete: 'cascade',
}); });
NotificationSetting.belongsTo(models.Team, { NotificationSetting.belongsTo(models.Team, {
as: 'team', as: 'team',

View File

@ -9,29 +9,13 @@ const Revision = sequelize.define('revision', {
}, },
title: DataTypes.STRING, title: DataTypes.STRING,
text: DataTypes.TEXT, text: DataTypes.TEXT,
userId: {
type: 'UUID',
allowNull: false,
references: {
model: 'users',
},
},
documentId: {
type: 'UUID',
allowNull: false,
references: {
model: 'documents',
onDelete: 'CASCADE',
},
},
}); });
Revision.associate = models => { Revision.associate = models => {
Revision.belongsTo(models.Document, { Revision.belongsTo(models.Document, {
as: 'document', as: 'document',
foreignKey: 'documentId', foreignKey: 'documentId',
onDelete: 'cascade',
}); });
Revision.belongsTo(models.User, { Revision.belongsTo(models.User, {
as: 'user', as: 'user',

View File

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