Added accidents to diary

This commit is contained in:
Torsten Schulz
2025-03-10 16:46:43 +01:00
parent 9442e3683b
commit c294dd7b2a
10 changed files with 2176 additions and 1369 deletions

View File

@@ -0,0 +1,43 @@
import { DataTypes } from 'sequelize';
import sequelize from '../database.js';
import bcrypt from 'bcrypt';
import { encryptData, decryptData } from '../utils/encrypt.js';
const Accident = sequelize.define('Accident', {
memberId: {
type: DataTypes.INTEGER,
allowNull: false,
},
diaryDateId: {
type: DataTypes.INTEGER,
allowNull: false,
},
accident: {
type: DataTypes.STRING,
allowNull: false,
set(value) {
const encryptedValue = encryptData(value);
this.setDataValue('accident', encryptedValue);
},
get() {
const encryptedValue = this.getDataValue('accident');
return decryptData(encryptedValue);
}
},
salt: {
type: DataTypes.STRING,
allowNull: true,
},
}, {
tableName: 'accident',
underscored: true,
timestamps: true,
hooks: {
beforeCreate: async (user) => {
const salt = await bcrypt.genSalt(10);
user.salt = salt;
},
}
});
export default Accident;

View File

@@ -4,7 +4,7 @@ import sequelize from '../database.js';
import Club from './Club.js';
import { encryptData, decryptData } from '../utils/encrypt.js';
const Member = sequelize.define('User', {
const Member = sequelize.define('Member', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,

View File

@@ -21,6 +21,7 @@ import Season from './Season.js';
import Location from './Location.js';
import Group from './Group.js';
import GroupActivity from './GroupActivity.js';
import Accident from './Accident.js';
User.hasMany(Log, { foreignKey: 'userId' });
Log.belongsTo(User, { foreignKey: 'userId' });
@@ -114,6 +115,12 @@ DiaryDateTag.belongsTo(DiaryTag, { foreignKey: 'tagId', as: 'tag' });
DiaryMemberTag.belongsTo(DiaryDate, { foreignKey: 'diaryDateId', as: 'diaryDates' });
DiaryDate.hasMany(DiaryMemberTag, { foreignKey: 'diaryDateId', as: 'diaryMemberTags' });
Accident.belongsTo(Member, { foreignKey: 'memberId', as: 'members' });
Member.hasMany(Accident, { foreignKey: 'memberId', as: 'accidents' });
Accident.belongsTo(DiaryDate, { foreignKey: 'diaryDateId', as: 'diaryDates' });
DiaryDate.hasMany(Accident, { foreignKey: 'diaryDateId', as: 'accidents' });
export {
User,
Log,
@@ -137,4 +144,5 @@ export {
Team,
Group,
GroupActivity,
Accident,
};