Cascade notification setting deletion on user account deletion
This commit is contained in:
@ -52,6 +52,7 @@ NotificationSetting.associate = models => {
|
||||
NotificationSetting.belongsTo(models.User, {
|
||||
as: 'user',
|
||||
foreignKey: 'userId',
|
||||
onDelete: 'cascade',
|
||||
});
|
||||
NotificationSetting.belongsTo(models.Team, {
|
||||
as: 'team',
|
||||
|
@ -9,29 +9,13 @@ const Revision = sequelize.define('revision', {
|
||||
},
|
||||
title: DataTypes.STRING,
|
||||
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.belongsTo(models.Document, {
|
||||
as: 'document',
|
||||
foreignKey: 'documentId',
|
||||
onDelete: 'cascade',
|
||||
});
|
||||
Revision.belongsTo(models.User, {
|
||||
as: 'user',
|
||||
|
@ -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 => {
|
||||
|
Reference in New Issue
Block a user