aboutsummaryrefslogtreecommitdiffstats
path: root/db/normalized/referrals.js
blob: 1193b613d00846346597fa87fc4263faff65d6e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888 } /* Comment */
.highlight .err { color: #A61717; background-color: #E3D2D2 } /* Error */
.highlight .k { color: #080; font-weight: bold } /* Keyword */
.highlight .ch { color: #888 } /* Comment.Hashbang */
.highlight .cm { color: #888 } /* Comment.Multiline */
.highlight .cp { color: #C00; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888 } /* Comment.Single */
.highlight .cs { color: #C00; font-weight: bold; background-color: #FFF0F0 } /* Comment.Special */
.highlight .gd { color: #000; background-color: #FDD } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #A00 } /* Generic.Error */
.highlight .gh { color: #333 } /* Generic.Heading */
.highlight .gi { color: #000; background-color: #DFD } /* Generic.Inserted */
.highlight .go { color: #888 } /* Generic.Output */
.highlight .gp { color: #555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666 } /* Generic.Subheading */
.highlight .gt { color: #A00 } /* Generic
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: ['available', 'used', 'expired', 'cancelled'],
    defaultValue: 'available'
  }
}

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'))
// })