45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
const { REGIONS } = require('../config/constants');
|
|
|
|
module.exports = (sequelize, DataTypes) => {
|
|
const Region = sequelize.define('Region', {
|
|
id: {
|
|
type: DataTypes.UUID,
|
|
defaultValue: DataTypes.UUIDV4,
|
|
primaryKey: true
|
|
},
|
|
name: {
|
|
type: DataTypes.ENUM(Object.values(REGIONS)),
|
|
unique: true,
|
|
allowNull: false
|
|
},
|
|
description: {
|
|
type: DataTypes.TEXT,
|
|
allowNull: true
|
|
},
|
|
regionalManagerId: {
|
|
type: DataTypes.UUID,
|
|
allowNull: true,
|
|
references: {
|
|
model: 'users',
|
|
key: 'id'
|
|
}
|
|
}
|
|
}, {
|
|
tableName: 'regions',
|
|
timestamps: true
|
|
});
|
|
|
|
Region.associate = (models) => {
|
|
Region.belongsTo(models.User, {
|
|
foreignKey: 'regionalManagerId',
|
|
as: 'regionalManager'
|
|
});
|
|
Region.hasMany(models.Zone, {
|
|
foreignKey: 'regionId',
|
|
as: 'zones'
|
|
});
|
|
};
|
|
|
|
return Region;
|
|
};
|