Initial commit

This commit is contained in:
Torsten Schulz (notebook)
2024-08-16 16:34:23 +02:00
commit 31ca0979ce
4216 changed files with 499206 additions and 0 deletions

12
backend/models/Club.js Normal file
View File

@@ -0,0 +1,12 @@
import { DataTypes } from 'sequelize';
import sequelize from '../database.js';
const Club = sequelize.define('Club', {
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
});
export default Club;

23
backend/models/Log.js Normal file
View File

@@ -0,0 +1,23 @@
import { DataTypes } from 'sequelize';
import sequelize from '../database.js';
import User from './User.js';
const Log = sequelize.define('Log', {
activity: {
type: DataTypes.STRING,
allowNull: false,
},
timestamp: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
},
userId: {
type: DataTypes.INTEGER,
references: {
model: User,
key: 'id',
},
},
});
export default Log;

104
backend/models/Member.js Normal file
View File

@@ -0,0 +1,104 @@
const { DataTypes, Model } = require('sequelize');
const crypto = require('crypto');
const sequelize = require('../database');
const Club = require('./Club');
const { encryptData, decryptData } = require('../utils/encrypt');
class Member extends Model {}
Member.init({
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
hashedId: {
type: DataTypes.STRING,
unique: true,
allowNull: false,
defaultValue() {
return crypto.randomBytes(16).toString('hex');
}
},
name: {
type: DataTypes.STRING,
allowNull: false,
set(value) {
const encryptedValue = encryptData(value);
this.setDataValue('name', encryptedValue);
},
get() {
const encryptedValue = this.getDataValue('name');
return decryptData(encryptedValue);
}
},
birthDate: {
type: DataTypes.STRING,
allowNull: false,
set(value) {
const encryptedValue = encryptData(value);
this.setDataValue('birthDate', encryptedValue);
},
get() {
const encryptedValue = this.getDataValue('birthDate');
return decryptData(encryptedValue);
}
},
phone: {
type: DataTypes.STRING,
allowNull: false,
set(value) {
const encryptedValue = encryptData(value);
this.setDataValue('phone', encryptedValue);
},
get() {
const encryptedValue = this.getDataValue('phone');
return decryptData(encryptedValue);
}
},
street: {
type: DataTypes.STRING,
allowNull: false,
set(value) {
const encryptedValue = encryptData(value);
this.setDataValue('street', encryptedValue);
},
get() {
const encryptedValue = this.getDataValue('street');
return decryptData(encryptedValue);
}
},
city: {
type: DataTypes.STRING,
allowNull: false,
set(value) {
const encryptedValue = encryptData(value);
this.setDataValue('city', encryptedValue);
},
get() {
const encryptedValue = this.getDataValue('city');
return decryptData(encryptedValue);
}
}
}, {
sequelize,
modelName: 'Member',
tableName: 'members',
timestamps: true,
hooks: {
beforeCreate: (member) => {
member.hashedId = crypto.createHash('sha256').update(String(member.id)).digest('hex');
},
beforeUpdate: (member) => {
if (member.changed('id')) {
member.hashedId = crypto.createHash('sha256').update(String(member.id)).digest('hex');
}
}
}
});
Member.belongsTo(Club);
Club.hasMany(Member);
module.exports = Member;

49
backend/models/User.js Normal file
View File

@@ -0,0 +1,49 @@
import { DataTypes } from 'sequelize';
import sequelize from '../database.js';
import bcrypt from 'bcrypt';
const User = sequelize.define('User', {
hashedId: {
type: DataTypes.STRING(1024),
allowNull: true,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
activationCode: {
type: DataTypes.STRING,
allowNull: true,
},
isActive: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
authCode: {
type: DataTypes.STRING,
allowNull: true
}
}, {
hooks: {
beforeCreate: async (user) => {
const salt = await bcrypt.genSalt(10);
user.password = await bcrypt.hash(user.password, salt);
},
beforeUpdate: async (user) => {
if (user.changed('password')) {
const salt = await bcrypt.genSalt(10);
user.password = await bcrypt.hash(user.password, salt);
}
},
afterCreate: async (user) => {
user.hashedId = bcrypt.hash(user.id.toString());
}
},
});
export default User;

View File

@@ -0,0 +1,30 @@
import { DataTypes } from 'sequelize';
import sequelize from '../database.js';
import User from './User.js';
import Club from './Club.js';
const UserClub = sequelize.define('UserClub', {
userId: {
type: DataTypes.INTEGER,
references: {
model: User,
key: 'id',
},
},
clubId: {
type: DataTypes.INTEGER,
references: {
model: Club,
key: 'id',
},
},
approved: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
});
User.belongsToMany(Club, { through: UserClub, foreignKey: 'userId' });
Club.belongsToMany(User, { through: UserClub, foreignKey: 'clubId' });
export default UserClub;

12
backend/models/index.js Normal file
View File

@@ -0,0 +1,12 @@
import User from './User.js';
import Log from './Log.js';
import Club from './Club.js';
import UserClub from './UserClub.js';
User.hasMany(Log, { foreignKey: 'userId' });
Log.belongsTo(User, { foreignKey: 'userId' });
User.belongsToMany(Club, { through: UserClub, foreignKey: 'userId' });
Club.belongsToMany(User, { through: UserClub, foreignKey: 'clubId' });
export { User, Log, Club, UserClub };