Skip to content

Commit

Permalink
feat(register): added campaigns and mail confirmations
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Peshkov committed Jan 30, 2020
1 parent 6173a0c commit 7187d83
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 4 deletions.
5 changes: 4 additions & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const config = {
},
filter_fields: [
'token',
'password'
'password',
'postgres.password',
'access_token',
'refresh_token'
],
bugsnagKey: process.env.BUGSNAG_KEY_CORE || 'CHANGEME'
},
Expand Down
37 changes: 37 additions & 0 deletions migrations/20200129163706-create-mail-confirmation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('mail_confirmations', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
user_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'id'
}
},
value: {
type: Sequelize.TEXT,
allowNull: false
},
expires_at: {
allowNull: false,
type: Sequelize.DATE
},
created_at: {
allowNull: false,
type: Sequelize.DATE
},
updated_at: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: (queryInterface) => {
return queryInterface.dropTable('mail_confirmations');
}
};
15 changes: 15 additions & 0 deletions migrations/20200129223311-add-campaign-id-to-users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.addColumn(
'users',
'campaign_id',
{
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'campaigns',
key: 'id'
}
},
),
down: (queryInterface) => queryInterface.removeColumn('users', 'campaign_id')
};
1 change: 0 additions & 1 deletion models/Campaign.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { Sequelize, sequelize } = require('../lib/sequelize');
const config = require('../config');

const Campaign = sequelize.define('campaign', {
name: {
Expand Down
48 changes: 48 additions & 0 deletions models/MailConfirmation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const crypto = require('crypto');
const moment = require('moment');
const util = require('util');

const { Sequelize, sequelize } = require('../lib/sequelize');
const config = require('../config');

const randomBytes = util.promisify(crypto.randomBytes);

const MailConfirmation = sequelize.define('mail_confirmation', {
user_id: {
type: Sequelize.INTEGER,
allowNull: false
},
value: {
type: Sequelize.TEXT,
allowNull: false,
defaultValue: '',
validate: {
notEmpty: { msg: 'Value should be set.' },
}
},
expires_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: '',
validate: {
notEmpty: { msg: 'Expires should be set.' },
}
}
}, {
underscored: true,
tableName: 'mail_confirmations',
createdAt: 'created_at',
updatedAt: 'updated_at',
});

MailConfirmation.createForUser = async function createForUser(userId) {
const value = (await randomBytes(128)).toString('hex');
return MailConfirmation.create({
user_id: userId,
expires_at: moment().add(config.ttl.mail_confirmation, 'seconds').toDate(),
value
});
};


module.exports = MailConfirmation;
2 changes: 1 addition & 1 deletion models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const User = sequelize.define('user', {
attributes: { exclude: [] }
},
noExtraFields: {
attributes: { exclude: ['password', 'superadmin', 'active', 'mail_confirmed_at'] }
attributes: { exclude: ['id', 'superadmin', 'active', 'mail_confirmed_at'] }
}
}
});
Expand Down
10 changes: 9 additions & 1 deletion models/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const User = require('./User');
const Campaign = require('./Campaign');
const MailConfirmation = require('./MailConfirmation');

Campaign.hasMany(User, { foreignKey: 'campaign_id' });
User.belongsTo(Campaign, { foreignKey: 'campaign_id' });

User.hasMany(MailConfirmation, { foreignKey: 'user_id' });
MailConfirmation.belongsTo(User, { foreignKey: 'user_id' });

module.exports = {
User,
Campaign
Campaign,
MailConfirmation
};

0 comments on commit 7187d83

Please sign in to comment.