narinder1231's picture
include user as actionBy in list audit log api
5256dce
import {
DataTypes,
Model,
InferAttributes,
InferCreationAttributes,
CreationOptional,
} from 'sequelize';
import { sequelize } from './index';
import { AuditLogInterface } from '../shared/interfaces/auditLog.interface';
import User from './users';
import Invoice from './invoice';
class AuditLog extends Model<InferAttributes<AuditLog>, InferCreationAttributes<AuditLog>> implements AuditLogInterface {
declare id?: CreationOptional<number>;
declare action_by: number;
declare invoice_id: number;
declare action: string;
declare details: string;
}
AuditLog.init(
{
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true,
unique: true,
},
invoice_id: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
references: {
model: Invoice,
key: 'id',
}
},
action_by: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
references: {
model: User,
key: 'id',
},
onDelete: 'CASCADE',
},
action: {
type: DataTypes.STRING,
allowNull: false,
},
details: {
type: DataTypes.TEXT,
allowNull: false,
},
},
{
sequelize,
tableName: 'audit_logs',
underscored: true,
freezeTableName: true,
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
}
);
AuditLog.belongsTo(User, {
foreignKey: 'action_by', as: 'actionBy',
})
AuditLog.belongsTo(Invoice, {
foreignKey: 'invoice_id',
})
export default AuditLog