-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(register): added campaigns and mail confirmations
- Loading branch information
Sergey Peshkov
committed
Jan 30, 2020
1 parent
6173a0c
commit 7187d83
Showing
7 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |