File size: 1,588 Bytes
634ea47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5256dce
634ea47
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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