/ src / models / SplitsReceiverModel.ts
SplitsReceiverModel.ts
  1  import type {
  2    CreationOptional,
  3    InferAttributes,
  4    InferCreationAttributes,
  5    Sequelize,
  6  } from 'sequelize';
  7  import { DataTypes, Model } from 'sequelize';
  8  import getSchema from '../utils/getSchema';
  9  import type { AccountId } from '../core/types';
 10  import {
 11    type RelationshipType,
 12    type AccountType,
 13    ACCOUNT_TYPES,
 14    RELATIONSHIP_TYPES,
 15  } from '../core/splitRules';
 16  
 17  export default class SplitsReceiverModel extends Model<
 18    InferAttributes<SplitsReceiverModel>,
 19    InferCreationAttributes<SplitsReceiverModel>
 20  > {
 21    declare public id: CreationOptional<number>;
 22    declare public receiverAccountId: AccountId;
 23    declare public receiverAccountType: AccountType;
 24    declare public senderAccountId: AccountId;
 25    declare public senderAccountType: AccountType;
 26    declare public relationshipType: RelationshipType;
 27    declare public weight: number;
 28    declare public blockTimestamp: Date;
 29    declare public splitsToRepoDriverSubAccount: boolean | undefined;
 30    declare public createdAt: CreationOptional<Date>;
 31    declare public updatedAt: CreationOptional<Date>;
 32  
 33    public static initialize(sequelize: Sequelize): void {
 34      this.init(
 35        {
 36          id: {
 37            primaryKey: true,
 38            autoIncrement: true,
 39            type: DataTypes.INTEGER,
 40          },
 41          receiverAccountId: {
 42            allowNull: false,
 43            type: DataTypes.STRING,
 44          },
 45          receiverAccountType: {
 46            allowNull: false,
 47            type: DataTypes.ENUM(...ACCOUNT_TYPES),
 48          },
 49          senderAccountId: {
 50            allowNull: false,
 51            type: DataTypes.STRING,
 52          },
 53          senderAccountType: {
 54            allowNull: false,
 55            type: DataTypes.ENUM(...ACCOUNT_TYPES),
 56          },
 57          relationshipType: {
 58            allowNull: false,
 59            type: DataTypes.ENUM(...RELATIONSHIP_TYPES),
 60          },
 61          splitsToRepoDriverSubAccount: {
 62            type: DataTypes.BOOLEAN,
 63            allowNull: true,
 64          },
 65          weight: {
 66            type: DataTypes.INTEGER,
 67            allowNull: false,
 68          },
 69          blockTimestamp: {
 70            type: DataTypes.DATE,
 71            allowNull: false,
 72          },
 73          createdAt: {
 74            type: DataTypes.DATE,
 75            allowNull: false,
 76          },
 77          updatedAt: {
 78            type: DataTypes.DATE,
 79            allowNull: false,
 80          },
 81        },
 82        {
 83          sequelize,
 84          schema: getSchema(),
 85          tableName: 'splits_receivers',
 86          underscored: true,
 87          timestamps: true,
 88          indexes: [
 89            {
 90              fields: ['receiverAccountId', 'senderAccountId'],
 91              name: 'idx_splits_receivers_receiver_sender',
 92            },
 93            {
 94              fields: ['senderAccountId', 'receiverAccountId'],
 95              name: 'idx_splits_receivers_sender_receiver',
 96            },
 97          ],
 98        },
 99      );
100    }
101  }