Some changes
This commit is contained in:
30
backend/models/Activity.js
Normal file
30
backend/models/Activity.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import { DataTypes } from 'sequelize';
|
||||
import sequelize from '../database.js';
|
||||
import DiaryDate from './DiaryDates.js';
|
||||
|
||||
const Activity = sequelize.define('Activity', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true,
|
||||
allowNull: false
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.TEXT('long'),
|
||||
allowNull: false
|
||||
},
|
||||
diaryDateId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: DiaryDate,
|
||||
key: 'id'
|
||||
}
|
||||
}
|
||||
}, {
|
||||
underscored: true,
|
||||
tableName: 'activities',
|
||||
timestamps: true
|
||||
});
|
||||
|
||||
export default Activity;
|
||||
39
backend/models/Participant.js
Normal file
39
backend/models/Participant.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { DataTypes } from 'sequelize';
|
||||
import sequelize from '../database.js';
|
||||
import Member from './Member.js';
|
||||
import DiaryDate from './DiaryDates.js';
|
||||
|
||||
const Participant = sequelize.define('Participant', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true,
|
||||
allowNull: false
|
||||
},
|
||||
diaryDateId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: DiaryDate,
|
||||
key: 'id'
|
||||
}
|
||||
},
|
||||
memberId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: Member,
|
||||
key: 'id'
|
||||
}
|
||||
},
|
||||
notes: {
|
||||
type: DataTypes.STRING(4096),
|
||||
allowNull: true,
|
||||
}
|
||||
}, {
|
||||
underscored: true,
|
||||
tableName: 'participants',
|
||||
timestamps: true
|
||||
});
|
||||
|
||||
export default Participant;
|
||||
@@ -46,7 +46,6 @@ const User = sequelize.define('User', {
|
||||
beforeCreate: async (user) => {
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
user.salt = salt;
|
||||
console.log(user);
|
||||
user.password = await bcrypt.hash(user.password, salt);
|
||||
},
|
||||
beforeUpdate: async (user) => {
|
||||
@@ -58,8 +57,7 @@ const User = sequelize.define('User', {
|
||||
}
|
||||
},
|
||||
afterCreate: async (user) => {
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
user.hashedId = await bcrypt.hash(user.id.toString(), salt);
|
||||
user.hashedId = await bcrypt.hash(user.id.toString(), user.salt);
|
||||
await user.save();
|
||||
}
|
||||
},
|
||||
|
||||
@@ -3,6 +3,9 @@ import Log from './Log.js';
|
||||
import Club from './Club.js';
|
||||
import UserClub from './UserClub.js';
|
||||
import DiaryDate from './DiaryDates.js';
|
||||
import Participant from './Participant.js';
|
||||
import Member from './Member.js';
|
||||
import Activity from './Activity.js';
|
||||
|
||||
User.hasMany(Log, { foreignKey: 'userId' });
|
||||
Log.belongsTo(User, { foreignKey: 'userId' });
|
||||
@@ -13,4 +16,11 @@ Club.belongsToMany(User, { through: UserClub, foreignKey: 'clubId' });
|
||||
DiaryDate.belongsTo(Club, { foreignKey: 'clubId' });
|
||||
Club.hasMany(DiaryDate, { foreignKey: 'clubId' });
|
||||
|
||||
DiaryDate.belongsToMany(Member, { through: Participant, as: 'participants' });
|
||||
Member.belongsToMany(DiaryDate, { through: Participant, as: 'diaryDates' });
|
||||
|
||||
DiaryDate.hasMany(Activity, { as: 'activities', foreignKey: 'diaryDateId' });
|
||||
Activity.belongsTo(DiaryDate, { as: 'diaryDate', foreignKey: 'diaryDateId' });
|
||||
|
||||
|
||||
export { User, Log, Club, UserClub };
|
||||
|
||||
Reference in New Issue
Block a user