Implemented personal settings
This commit is contained in:
24
backend/models/associations.js
Normal file
24
backend/models/associations.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import User from './community/user.js';
|
||||
import UserParam from './community/user_param.js';
|
||||
import UserParamType from './type/user_param.js';
|
||||
import UserRightType from './type/user_right.js';
|
||||
import UserRight from './community/user_right.js';
|
||||
import SettingsType from './type/settings.js';
|
||||
import UserParamValue from './type/user_param_value.js';
|
||||
|
||||
export default function setupAssociations() {
|
||||
SettingsType.hasMany(UserParamType, { foreignKey: 'settingsId', as: 'user_param_types' });
|
||||
UserParamType.belongsTo(SettingsType, { foreignKey: 'settingsId', as: 'settings_type' });
|
||||
|
||||
UserParamType.hasMany(UserParam, { foreignKey: 'paramTypeId', as: 'user_params' });
|
||||
UserParam.belongsTo(UserParamType, { foreignKey: 'paramTypeId', as: 'paramType' });
|
||||
|
||||
UserParam.belongsTo(SettingsType, { foreignKey: 'settingsId', as: 'settings' });
|
||||
UserParam.belongsTo(User, { foreignKey: 'userId', as: 'user' });
|
||||
|
||||
UserRight.belongsTo(User, { foreignKey: 'userId' });
|
||||
UserRight.belongsTo(UserRightType, { foreignKey: 'rightTypeId', as: 'rightType' });
|
||||
|
||||
UserParamType.hasMany(UserParamValue, { foreignKey: 'userParamTypeId', as: 'user_param_values' });
|
||||
UserParamValue.belongsTo(UserParamType, { foreignKey: 'userParamTypeId', as: 'user_param_type' });
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { sequelize } from '../../utils/sequelize.js';
|
||||
import { DataTypes } from 'sequelize';
|
||||
import bcrypt from 'bcrypt';
|
||||
import { encrypt, generateIv } from '../../utils/encryption.js';
|
||||
import crypto from 'crypto';
|
||||
|
||||
const User = sequelize.define('user', {
|
||||
email: {
|
||||
@@ -38,7 +39,7 @@ const User = sequelize.define('user', {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false
|
||||
},
|
||||
resetToken: {
|
||||
resetToken: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: true
|
||||
},
|
||||
@@ -49,7 +50,14 @@ const User = sequelize.define('user', {
|
||||
}, {
|
||||
tableName: 'user',
|
||||
schema: 'community',
|
||||
underscored: true
|
||||
underscored: true,
|
||||
hooks: {
|
||||
afterCreate: async (user, options) => {
|
||||
const hashedId = crypto.createHash('sha256').update(user.id.toString()).digest('hex');
|
||||
user.hashedId = hashedId;
|
||||
await user.save();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export default User;
|
||||
|
||||
@@ -26,9 +26,25 @@ const UserParam = sequelize.define('user_param', {
|
||||
allowNull: false,
|
||||
set(value) {
|
||||
if (value) {
|
||||
const iv = generateIv();
|
||||
this.setDataValue('iv', iv.toString('hex'));
|
||||
this.setDataValue('value', encrypt(value, iv));
|
||||
try {
|
||||
const iv = generateIv();
|
||||
console.log(value);
|
||||
this.setDataValue('iv', iv.toString('hex'));
|
||||
this.setDataValue('value', encrypt(value, iv));
|
||||
} catch (error) {
|
||||
console.log('Error setting value:', error);
|
||||
this.setDataValue('value', '');
|
||||
}
|
||||
}
|
||||
},
|
||||
get() {
|
||||
try {
|
||||
const value = this.getDataValue('value');
|
||||
const iv = Buffer.from(this.getDataValue('iv'), 'hex');
|
||||
return decrypt(value, iv);
|
||||
} catch (error) {
|
||||
console.log('Error getting value:', error);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -40,31 +56,29 @@ const UserParam = sequelize.define('user_param', {
|
||||
tableName: 'user_param',
|
||||
schema: 'community',
|
||||
underscored: true,
|
||||
hooks: {
|
||||
beforeSave: (userParam) => {
|
||||
if (userParam.value && !userParam.iv) {
|
||||
const iv = generateIv();
|
||||
userParam.iv = iv.toString('hex');
|
||||
userParam.value = encrypt(userParam.value, iv);
|
||||
}
|
||||
},
|
||||
afterFind: (userParams) => {
|
||||
if (userParams) {
|
||||
if (Array.isArray(userParams)) {
|
||||
userParams.forEach((userParam) => {
|
||||
const iv = Buffer.from(userParam.iv, 'hex');
|
||||
userParam.value = decrypt(userParam.value, iv);
|
||||
});
|
||||
} else {
|
||||
const iv = Buffer.from(userParams.iv, 'hex');
|
||||
userParams.value = decrypt(userParams.value, iv);
|
||||
}
|
||||
}
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['user_id', 'param_type_id']
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
UserParam.belongsTo(User, { foreignKey: 'userId' });
|
||||
UserParam.belongsTo(UserParamType, { foreignKey: 'paramTypeId' });
|
||||
UserParam.upsertParam = async function (userId, paramTypeId, value) {
|
||||
try {
|
||||
const [userParam, created] = await UserParam.findOrCreate({
|
||||
where: { userId, paramTypeId },
|
||||
defaults: { value }
|
||||
});
|
||||
|
||||
if (!created) {
|
||||
userParam.value = value;
|
||||
await userParam.save();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error in upsertParam:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export default UserParam;
|
||||
|
||||
@@ -26,7 +26,4 @@ const UserRight = sequelize.define('user_right', {
|
||||
underscored: true,
|
||||
});
|
||||
|
||||
UserRight.belongsTo(User, { foreignKey: 'userId' });
|
||||
UserRight.belongsTo(UserRightType, { foreignKey: 'rightTypeId', as: 'rightType' });
|
||||
|
||||
export default UserRight;
|
||||
|
||||
@@ -4,7 +4,8 @@ import UserParamType from './type/user_param.js';
|
||||
import Login from './logs/login.js';
|
||||
import UserRightType from './type/user_right.js';
|
||||
import UserRight from './community/user_right.js';
|
||||
import SettingsType from './type/settings_type.js';
|
||||
import SettingsType from './type/settings.js';
|
||||
import UserParamValue from './type/user_param_value.js';
|
||||
|
||||
const models = {
|
||||
User,
|
||||
@@ -14,6 +15,7 @@ const models = {
|
||||
UserRightType,
|
||||
UserRight,
|
||||
SettingsType,
|
||||
UserParamValue,
|
||||
};
|
||||
|
||||
export default models;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { sequelize } from '../../utils/sequelize.js';
|
||||
import { DataTypes } from 'sequelize';
|
||||
|
||||
const SettingsType = sequelize.define('settings_type', {
|
||||
const Settings = sequelize.define('settings_type', {
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
@@ -12,4 +12,4 @@ const SettingsType = sequelize.define('settings_type', {
|
||||
underscored: true
|
||||
});
|
||||
|
||||
export default SettingsType;
|
||||
export default Settings;
|
||||
@@ -1,13 +1,12 @@
|
||||
import { sequelize } from '../../utils/sequelize.js';
|
||||
import { DataTypes } from 'sequelize';
|
||||
import SettingsType from './settings_type.js';
|
||||
|
||||
const UserParamType = sequelize.define('user_param_type', {
|
||||
description: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
minAge: {
|
||||
minAge: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: true
|
||||
},
|
||||
@@ -18,22 +17,20 @@ const UserParamType = sequelize.define('user_param_type', {
|
||||
datatype: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
defaultValue: 'string'
|
||||
defaultValue: 'string'
|
||||
},
|
||||
settingsTypeId: {
|
||||
settingsId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: SettingsType,
|
||||
model: 'settings',
|
||||
key: 'id'
|
||||
}
|
||||
},
|
||||
}
|
||||
}, {
|
||||
tableName: 'user_param',
|
||||
schema: 'type',
|
||||
underscored: true
|
||||
});
|
||||
|
||||
UserParamType.belongsTo(SettingsType, { foreignKey: 'settingsTypeId' });
|
||||
|
||||
export default UserParamType;
|
||||
|
||||
22
backend/models/type/user_param_value.js
Normal file
22
backend/models/type/user_param_value.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import { sequelize } from '../../utils/sequelize.js';
|
||||
import { DataTypes } from 'sequelize';
|
||||
import UserParam from './user_param.js';
|
||||
|
||||
const UserParamValue = sequelize.define('user_param_value', {
|
||||
userParamTypeId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
},
|
||||
value: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
}
|
||||
},
|
||||
{
|
||||
tableName: 'user_param_value',
|
||||
schema: 'type',
|
||||
underscored: true
|
||||
}
|
||||
);
|
||||
|
||||
export default UserParamValue;
|
||||
@@ -7,7 +7,7 @@ const UserRightType = sequelize.define('user_right_type', {
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
tableName: 'user_right',
|
||||
tableName: 'user_right',
|
||||
schema: 'type',
|
||||
underscored: true
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user