aboutsummaryrefslogtreecommitdiffstats
path: root/db/normalized/referrals.js
blob: 1726622563acee3cce8eccecc4f2301cb13fd9f0 (plain) (blame)
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
const { DataTypes } = require('sequelize');

const attributes = {
  id: {
    type: DataTypes.INTEGER(11),
    primaryKey: true,
    autoIncrement: true,
    allowNull: false
  },
  sponsor_id: {
    type: DataTypes.INTEGER(11),
    references: { model: db.User, key: 'id' },
    allowNull: false,
    comment: "Referral made by this user"
  },
  token: {
    type: DataTypes.STRING(255),
    allowNull: false,
    comment: "Unique referral token"
  },
  user_id: {
    type: DataTypes.INTEGER(11),
    references: { model: db.User, key: 'id' },
    allowNull: true,
    comment: "Referral granted to this user (updated when token used)"
  },
  expiry: {
    type: DataTypes.DATE
  },
  status: {
    type: DataTypes.ENUM,
    values: ['pending', 'active', 'expired', 'cancelled'],
    defaultValue: 'pending'
  }
}

const options = {
  tableName: "referrals",
  comment: "Referrals - unique record for each sponsor / user_id",
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    // are FK fields automatically indexed ?  ... if not add sponsor_id
    {
      unique: true,
      fields: ['user_id']
    }
  ]
};

db["Referral"] = db.define("referrals_model", attributes, options);

// const config = require('./../config')
// const dbc = require('knex')(config.knex)

// dbc.schema.createTable('referrals', function (table) {
//   table.increments();

//   table.string('token')
//   table.date('expiry')
//   table.enu('status', ['active', 'expired', 'closed'])

//   table.dateTime('created_at').notNullable().defaultTo(knex.raw('CURRENT_TIMESTAMP'))
//   table.dateTime('updated_at').notNullable().defaultTo(knex.raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
// })

// dbc.schema.createTable('waiting_list', function (table) {
//   table.increments();

//   table.string('email')
//   table.enu('status', ['pending', 'activated', 'cancelled']).defaultTo('pending')

//   table.dateTime('created_at').notNullable().defaultTo(knex.raw('CURRENT_TIMESTAMP'))
//   table.dateTime('updated_at').notNullable().defaultTo(knex.raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
// })