UNPKG

synt_backend

Version:

Synt light-weight node backend service

92 lines (90 loc) 2.62 kB
/* eslint-disable no-undef */ "use strict"; const { Model } = require("sequelize"); module.exports = (sequelize, DataTypes) => { class User 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) { // Define association here User.hasMany(models.Device, { foreignKey: "UserId", as: "Devices", }); User.belongsToMany(models.VME, { through: "UserVME", foreignKey: "UserId", as: "VMEs", }); User.belongsToMany(models.LotPhase, { through: "LotPhaseUser", }); User.hasMany(models.Meeting, { foreignKey: { name: "UserId", }, }); User.hasMany(models.Incident); User.belongsToMany(models.Meeting, { through: "MeetingUser", foreignKey: "UserId", as: "MeetingPresences", }); User.belongsTo(models.Company); User.belongsToMany(models.NotificationSetting, { through: "NotificationSettingUser", }); User.hasMany(models.Yea); } } User.init( { character: DataTypes.ENUM("natural", "legal"), first_name: DataTypes.STRING, last_name: DataTypes.STRING, full_name: { type: DataTypes.VIRTUAL, get() { return `${this.getDataValue("first_name") || ""} ${ this.getDataValue("last_name") || "" }`; }, }, email: DataTypes.STRING, phone: DataTypes.STRING, email_verified_at: DataTypes.DATE, phone_verified_at: DataTypes.DATE, address: DataTypes.STRING, terms_accepted_at: DataTypes.DATE, is_admin: DataTypes.BOOLEAN, }, { hooks: { afterBulkCreate: (Users) => { // add notification settings sequelize.models.NotificationSetting.findAll() .then((NotificationSettings) => { for (const User of Users) { User.setNotificationSettings(NotificationSettings); } }) .catch((err) => console.error(err)); }, afterCreate: (User) => { // add notification settings sequelize.models.NotificationSetting.findAll() .then((NotificationSettings) => { User.setNotificationSettings(NotificationSettings); }) .catch((err) => console.error(err)); }, }, sequelize, modelName: "User", } ); return User; };