2020-02-06 15:24:04 +01:00
|
|
|
'use strict'
|
2020-02-08 19:47:21 +01:00
|
|
|
|
2020-02-14 15:13:11 +01:00
|
|
|
function isValidName(value, minLength = 4) {
|
|
|
|
const regExpression = new RegExp(`^[0-9a-zA-Z\\x20]{${minLength},35}$`)
|
|
|
|
return regExpression.test(value)
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:47:21 +01:00
|
|
|
const validate = (contribution, options) => {
|
|
|
|
const { name, company, socialType } = contribution.dataValues
|
2020-02-14 15:13:11 +01:00
|
|
|
if (socialType !== 'anonymous' && !isValidName(name)) {
|
2020-04-29 21:26:36 +02:00
|
|
|
throw new Error('Invalid user name')
|
2020-02-08 19:47:21 +01:00
|
|
|
}
|
2020-02-14 15:13:11 +01:00
|
|
|
if (company && !isValidName(company, 0)) {
|
2020-04-29 21:26:36 +02:00
|
|
|
throw new Error('Invalid company name')
|
2020-02-08 19:47:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:24:04 +01:00
|
|
|
module.exports = (sequelize, DataTypes) => {
|
|
|
|
const Contribution = sequelize.define(
|
|
|
|
'Contribution',
|
|
|
|
{
|
|
|
|
token: DataTypes.STRING,
|
|
|
|
name: DataTypes.STRING,
|
|
|
|
company: DataTypes.STRING,
|
|
|
|
handle: DataTypes.STRING,
|
|
|
|
socialType: DataTypes.STRING,
|
2020-02-25 15:13:09 +01:00
|
|
|
attestation: DataTypes.STRING
|
2020-02-06 15:24:04 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
hooks: {
|
2020-02-08 19:47:21 +01:00
|
|
|
beforeCreate: validate,
|
|
|
|
beforeUpdate: validate
|
2020-02-06 15:24:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2020-04-29 21:26:36 +02:00
|
|
|
Contribution.nextContributionIndex = async function () {
|
2020-02-06 15:24:04 +01:00
|
|
|
const rowsCount = await this.count()
|
|
|
|
return rowsCount + 1
|
|
|
|
}
|
|
|
|
|
2020-04-29 21:26:36 +02:00
|
|
|
Contribution.associate = function (models) {
|
2020-02-06 15:24:04 +01:00
|
|
|
// associations can be defined here
|
|
|
|
}
|
|
|
|
return Contribution
|
|
|
|
}
|