const Sequelize = require('sequelize')

module.exports = db => db.define('accounts', {
  name: {
    type: Sequelize.STRING,
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  previousNames: {
    type: Sequelize.ARRAY(Sequelize.STRING),
    defaultValue: []
  },
  url: {
    type: Sequelize.STRING,
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  degrees: {
    type: Sequelize.INTEGER,
    allowNull: false
  },
  allTime: {
    type: Sequelize.JSON,
    allowNull: false
  },
  rolling300: {
    type: Sequelize.JSON,
    allowNull: false
  },
  flairs: {
    type: Sequelize.ARRAY(Sequelize.JSON),
    allowNull: false
  },
  timeline: {
    type: Sequelize.ARRAY(Sequelize.JSON),
    defaultValue: []
  },
  data: {
    type: Sequelize.JSON
  },
  selectedFlair: {
    type: Sequelize.STRING,
    allowNull: false
  }
}, {
  hooks: {
    afterUpdate: function(account) {
      if (account.previousNames.indexOf(account.name) === -1) {
        const newNames = account.previousNames.concat([account.name])
        this.update({
          previousNames: newNames
        }, {
          where: {
            id: account.id
          }
        })
      }
    }
  }
})

@@ -1201,15 +1201,159 @@
 %7D%0A    %7D%0A
+  %7D,%0A  instanceMethods: %7B%0A    getWeeklyStats: function() %7B%0A      if (this.data) return Object.assign(%7B%7D, this.data%5B'All Time'%5D%5B'Weekly'%5D)%0A    %7D%0A
   %7D%0A%7D)%0A

fix weekly stats instance method