cubash-archive/models/item.js

153 lines
4.4 KiB
JavaScript

let bcrypt = require('bcryptjs')
let randomColor = require('randomcolor')
var passportLocalSequelize = require('passport-local-sequelize');
let pagination = require('../lib/pagination.js')
const Errors = require('../lib/errors.js')
var crypto = require("crypto");
var cryptoRandomString = require("crypto-random-string");
module.exports = (sequelize, DataTypes) => {
let Item = sequelize.define('Item', {
name: {
type: DataTypes.STRING(191),
validate: {
len: {
args: [3, 30],
msg: 'Item name must be between 3 and 30 characters'
},
isString(val) {
if (typeof val !== 'string') {
throw new sequelize.ValidationError('Item name must be a string')
}
}
}
},
type: {
type: DataTypes.STRING,
defaultValue: "shirt",
default: "shirt",
allowNull: false
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
},
UserId: {
type: DataTypes.BIGINT,
},
sourceFile: {
type: DataTypes.TEXT,
},
previewFile: {
type: DataTypes.TEXT
},
limited: {
type: DataTypes.BOOLEAN,
defaultValue: 0,
default: 0,
validate: {
isBoolean: {
args: true,
msg: 'You can only set this as a true or false value.'
}
}
},
approved: {
type: DataTypes.BOOLEAN,
defaultValue: 0,
default: 0,
validate: {
isBoolean: {
args: true,
msg: 'You can only set this as a true or false value.'
}
}
},
salePrice: {
type: DataTypes.BIGINT,
validate: {
isNumeric: {
args: true,
msg: 'The on-sale price of your Marketplace Item needs to be a numeric value, numbers only.'
}
}
},
saleEnabled: {
type: DataTypes.BOOLEAN,
defaultValue: 0,
default: 0,
validate: {
isBoolean: {
args: true,
msg: 'You can only set this as a true or false value.'
}
}
},
price: {
type: DataTypes.BIGINT,
validate: {
isNumeric: {
args: true,
msg: 'The price of your Marketplace Item needs to be a numeric value, numbers only.'
}
}
},
quantityAllowed: {
type: DataTypes.BIGINT,
default: 0,
defaultValue: 0,
validate: {
isNumeric: {
args: true,
msg: 'The quantity of your Marketplace Item needs to be a numeric value, numbers only.'
}
}
},
offSale: {
type: DataTypes.BOOLEAN,
default: 0,
defaultValue: 0,
validate: {
isBoolean: {
args: true,
msg: 'You can only set this as a true or false value.'
}
}
},
deleted: {
type: DataTypes.BOOLEAN,
default: false,
defaultValue: false
},
description: {
type: DataTypes.TEXT,
validate: {
len: {
args: [0, 150],
msg: 'Marketplace description must be under 150 characters.'
}
},
default: "No Marketplace item description provided"
}
}, {
hooks: {
async afterValidate(user, options) {
if(user.changed('hash') && user.hash.length <= 50) {
user.hash = await bcrypt.hash(user.hash, 12)
}
options.hooks = false
return options
}
}
})
Item.associate = function (models) {
Item.belongsTo(models.ItemCategory)
Item.belongsTo(models.User, {through: 'User'})
}
return Item
}