const bcrypt = require('bcryptjs'); const { DataTypes } = require('sequelize'); const sequelize = require('../config/sequelize'); const timestamps = { timestamps: true, createdAt: 'created', updatedAt: 'modified', }; const User = sequelize.define('User', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, login: { type: DataTypes.STRING(255), allowNull: false, unique: true }, email: { type: DataTypes.STRING(255), allowNull: false, unique: true, validate: { isEmail: true } }, password: { type: DataTypes.STRING(255), allowNull: false }, first_name: { type: DataTypes.STRING(100), allowNull: false }, last_name: { type: DataTypes.STRING(100), allowNull: false }, role: { type: DataTypes.STRING(20), allowNull: false, defaultValue: 'user', validate: { isIn: [['admin', 'user']] } }, active: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true }, last_login: { type: DataTypes.DATE, allowNull: true }, }, { ...timestamps, tableName: 'users', defaultScope: { attributes: { exclude: ['password'] } }, scopes: { withPassword: { attributes: { include: ['password'] } } }, hooks: { beforeSave: async (user) => { if (user.changed('password')) { user.password = await bcrypt.hash(user.password, 10); } }, }, }); const Customer = sequelize.define('Customer', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, type: { type: DataTypes.STRING(20), allowNull: false, defaultValue: 'individual', validate: { isIn: [['individual', 'company']] } }, first_name: { type: DataTypes.STRING(100), allowNull: false }, last_name: { type: DataTypes.STRING(100), allowNull: false }, email: { type: DataTypes.STRING(255), allowNull: false, unique: true, validate: { isEmail: true } }, phone: { type: DataTypes.STRING(30), allowNull: false }, company_name: { type: DataTypes.STRING(255), allowNull: true }, tax_id: { type: DataTypes.STRING(20), allowNull: true }, street: { type: DataTypes.STRING(255), allowNull: true }, city: { type: DataTypes.STRING(100), allowNull: true }, postal_code: { type: DataTypes.STRING(10), allowNull: true }, country: { type: DataTypes.STRING(2), allowNull: false, defaultValue: 'PL' }, birth_date: { type: DataTypes.DATEONLY, allowNull: true }, notes: { type: DataTypes.TEXT, allowNull: true }, created_by: { type: DataTypes.INTEGER, allowNull: true }, }, { ...timestamps, tableName: 'customers' }); const Car = sequelize.define('Car', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, customer_id: { type: DataTypes.INTEGER, allowNull: false }, make: { type: DataTypes.STRING(50), allowNull: false }, model: { type: DataTypes.STRING(50), allowNull: false }, production_year: { type: DataTypes.INTEGER, allowNull: false }, vin: { type: DataTypes.STRING(17), allowNull: false, unique: true }, registration_number: { type: DataTypes.STRING(10), allowNull: false, unique: true }, fuel_type: { type: DataTypes.STRING(20), allowNull: false, validate: { isIn: [['petrol', 'diesel', 'lpg', 'hybrid', 'electric']] } }, color: { type: DataTypes.STRING(30), allowNull: true }, engine_capacity: { type: DataTypes.INTEGER, allowNull: true }, power_hp: { type: DataTypes.INTEGER, allowNull: true }, transmission: { type: DataTypes.STRING(20), allowNull: true, validate: { isIn: [['manual', 'automatic']] } }, mileage: { type: DataTypes.INTEGER, allowNull: true }, notes: { type: DataTypes.TEXT, allowNull: true }, }, { ...timestamps, tableName: 'cars' }); User.hasMany(Customer, { foreignKey: 'created_by', as: 'createdCustomers' }); Customer.belongsTo(User, { foreignKey: 'created_by', as: 'creator' }); Customer.hasMany(Car, { foreignKey: 'customer_id', as: 'cars', onDelete: 'CASCADE' }); Car.belongsTo(Customer, { foreignKey: 'customer_id', as: 'customer' }); module.exports = { sequelize, User, Customer, Car };