Dealer_Onboarding_Backend/src/database/models/Outlet.ts

126 lines
3.3 KiB
TypeScript

import { Model, DataTypes, Sequelize } from 'sequelize';
import { OUTLET_TYPES, OUTLET_STATUS, REGIONS } from '../../common/config/constants.js';
export interface OutletAttributes {
id: string;
code: string;
name: string;
type: typeof OUTLET_TYPES[keyof typeof OUTLET_TYPES];
address: string;
city: string;
state: string;
pincode: string;
latitude: number | null;
longitude: number | null;
status: typeof OUTLET_STATUS[keyof typeof OUTLET_STATUS];
establishedDate: string;
dealerId: string;
region: typeof REGIONS[keyof typeof REGIONS];
zone: string;
}
export interface OutletInstance extends Model<OutletAttributes>, OutletAttributes { }
export default (sequelize: Sequelize) => {
const Outlet = sequelize.define<OutletInstance>('Outlet', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
code: {
type: DataTypes.STRING,
unique: true,
allowNull: false
},
name: {
type: DataTypes.STRING,
allowNull: false
},
type: {
type: DataTypes.ENUM(...Object.values(OUTLET_TYPES)),
allowNull: false
},
address: {
type: DataTypes.TEXT,
allowNull: false
},
city: {
type: DataTypes.STRING,
allowNull: false
},
state: {
type: DataTypes.STRING,
allowNull: false
},
pincode: {
type: DataTypes.STRING,
allowNull: false
},
latitude: {
type: DataTypes.DECIMAL(10, 8),
allowNull: true
},
longitude: {
type: DataTypes.DECIMAL(11, 8),
allowNull: true
},
status: {
type: DataTypes.ENUM(...Object.values(OUTLET_STATUS)),
defaultValue: OUTLET_STATUS.ACTIVE
},
establishedDate: {
type: DataTypes.DATEONLY,
allowNull: false
},
dealerId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'users',
key: 'id'
}
},
region: {
type: DataTypes.ENUM(...Object.values(REGIONS)),
allowNull: false
},
zone: {
type: DataTypes.STRING,
allowNull: false
}
}, {
tableName: 'outlets',
timestamps: true,
indexes: [
{ fields: ['code'] },
{ fields: ['dealerId'] },
{ fields: ['type'] },
{ fields: ['status'] },
{ fields: ['region'] },
{ fields: ['zone'] }
]
});
(Outlet as any).associate = (models: any) => {
Outlet.belongsTo(models.User, {
foreignKey: 'dealerId',
as: 'dealer'
});
Outlet.hasMany(models.Resignation, {
foreignKey: 'outletId',
as: 'resignations'
});
Outlet.hasMany(models.ConstitutionalChange, {
foreignKey: 'outletId',
as: 'constitutionalChanges'
});
Outlet.hasMany(models.RelocationRequest, {
foreignKey: 'outletId',
as: 'relocationRequests'
});
};
return Outlet;
};