Profiles: Add user profile model and command

This commit is contained in:
2022-09-05 23:22:36 +02:00
parent bba51cea2e
commit 1206251ff6
4 changed files with 134 additions and 0 deletions

32
models/profile.js Normal file
View File

@@ -0,0 +1,32 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Profile extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
Profile.belongsTo(models.User, { foreignKey: 'userId' });
Profile.belongsTo(models.Card, { foreignKey: 'slotOne' });
Profile.belongsTo(models.Card, { foreignKey: 'slotTwo' });
Profile.belongsTo(models.Card, { foreignKey: 'slotThree' });
Profile.belongsTo(models.Card, { foreignKey: 'slotFour' });
}
}
Profile.init({
userId: DataTypes.INTEGER,
customStatus: DataTypes.STRING,
slotOne: DataTypes.INTEGER,
slotTwo: DataTypes.INTEGER,
slotThree: DataTypes.INTEGER,
slotFour: DataTypes.INTEGER
}, {
sequelize,
modelName: 'Profile',
});
return Profile;
};

View File

@@ -13,6 +13,7 @@ module.exports = (sequelize, DataTypes) => {
static associate(models) {
// define association here
User.hasMany(models.Card);
User.hasOne(models.Profile);
}
//instance methods
async getCardsWithCharactersCounted() {
@@ -26,6 +27,13 @@ module.exports = (sequelize, DataTypes) => {
});
return cards;
}
async getProfile() {
return await sequelize.models.Profile.findOne({
where: {
userId: this.id
}
});
}
}
User.init({
discordId: DataTypes.BIGINT,
@@ -35,6 +43,14 @@ module.exports = (sequelize, DataTypes) => {
nextPull: DataTypes.DATE,
nextDaily: DataTypes.DATE
}, {
hooks: {
afterCreate: async (user, options) => {
//Create new user profile
await sequelize.models.Profile.create({
userId: user.id
});
}
},
sequelize,
modelName: 'User',
});