updated db.User to db.Channel refs

This commit is contained in:
bill bittner 2017-09-25 21:03:43 -07:00
parent 6e60e0aa2f
commit a2b3ed1ab2
11 changed files with 24 additions and 23 deletions

View file

@ -21,7 +21,7 @@ module.exports = {
.then(tx => { .then(tx => {
logger.info(`Successfully published ${fileName}`, tx); logger.info(`Successfully published ${fileName}`, tx);
publishResults = tx; publishResults = tx;
return db.User.findOne({where: {channelName: publishParams.channel_name}}); return db.Channel.findOne({where: {channelName: publishParams.channel_name}});
}) })
.then(user => { .then(user => {
logger.debug('found user', user.datavalues); logger.debug('found user', user.datavalues);

View file

@ -140,7 +140,7 @@ module.exports = {
checkChannelAvailability (name) { checkChannelAvailability (name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// find any records where the name is used // find any records where the name is used
db.User.findAll({ where: { channelName: name } }) db.Channel.findAll({ where: { channelName: name } })
.then(result => { .then(result => {
if (result.length >= 1) { if (result.length >= 1) {
return resolve(false); return resolve(false);

View file

@ -85,12 +85,11 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
}, },
{ {
freezeTableName: true, freezeTableName: true,
underscored : true,
} }
); );
Certificate.associate = db => { Certificate.associate = db => {
Certificate.belongsTo(db.User, { Certificate.belongsTo(db.Channel, {
onDelete : 'cascade', onDelete : 'cascade',
foreignKey: { foreignKey: {
allowNull: true, allowNull: true,

View file

@ -13,7 +13,6 @@ module.exports = (sequelize, { STRING }) => {
}, },
{ {
freezeTableName: true, freezeTableName: true,
underscored : true,
} }
); );

View file

@ -137,7 +137,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
}, },
{ {
freezeTableName: true, freezeTableName: true,
underscored : true,
} }
); );

View file

@ -47,18 +47,11 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => {
}, },
{ {
freezeTableName: true, freezeTableName: true,
underscored : true,
} }
); );
File.associate = db => { File.associate = db => {
File.hasMany(db.Request); File.hasMany(db.Request);
File.belongsTo(db.User, {
onDelete : 'cascade',
foreignKey: {
allowNull: true,
},
});
File.hasOne(db.Claim); File.hasOne(db.Claim);
}; };

View file

@ -22,7 +22,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, TEXT }) => {
}, },
{ {
freezeTableName: true, freezeTableName: true,
underscored : true,
} }
); );

View file

@ -13,7 +13,6 @@ module.exports = (sequelize, { STRING }) => {
}, },
{ {
freezeTableName: true, freezeTableName: true,
underscored : true,
} }
); );

View file

@ -12,7 +12,7 @@ module.exports = new PassportLocalStrategy(
(req, username, password, done) => { (req, username, password, done) => {
logger.debug(`verifying loggin attempt ${username} ${password}`); logger.debug(`verifying loggin attempt ${username} ${password}`);
return db.User return db.User
.findOne({where: {channelName: username}}) .findOne({where: {userName: username}})
.then(user => { .then(user => {
if (!user) { if (!user) {
logger.debug('no user found'); logger.debug('no user found');
@ -23,7 +23,11 @@ module.exports = new PassportLocalStrategy(
return done(null, false, {message: 'Incorrect username or password.'}); return done(null, false, {message: 'Incorrect username or password.'});
} }
logger.debug('user found:', user.dataValues); logger.debug('user found:', user.dataValues);
return user.getChannel().then(channel => {
user['channelName'] = channel.channelClaimId;
user['channelClaimId'] = channel.channelClaimId;
return done(null, user); return done(null, user);
});
}) })
.catch(error => { .catch(error => {
return done(error); return done(error);

View file

@ -22,11 +22,16 @@ module.exports = new PassportLocalStrategy(
.then(tx => { .then(tx => {
// create user record // create user record
const userData = { const userData = {
channelName : username, userName: username,
channelClaimId: tx.claim_id,
password: password, password: password,
}; };
logger.debug('userData >', userData); logger.debug('userData >', userData);
// create user record
const channelData = {
channelName : `@${username}`,
channelClaimId: tx.claim_id,
};
logger.debug('channelData >', channelData);
// create certificate record // create certificate record
const certificateData = { const certificateData = {
claimId: tx.claim_id, claimId: tx.claim_id,
@ -35,15 +40,18 @@ module.exports = new PassportLocalStrategy(
}; };
logger.debug('certificateData >', certificateData); logger.debug('certificateData >', certificateData);
// save user and certificate to db // save user and certificate to db
return Promise.all([db.User.create(userData), db.Certificate.create(certificateData)]); return Promise.all([db.User.create(userData), db.Channel.create(channelData), db.Certificate.create(certificateData)]);
}) })
.then(([newUser, newCertificate]) => { .then(([newUser, newChannel, newCertificate]) => {
user = newUser; // save outside scope of this function user = newUser; // save outside scope of this function
user['channelName'] = newChannel.channelClaimId;
user['channelClaimId'] = newChannel.channelClaimId;
logger.debug('user and certificate successfully created'); logger.debug('user and certificate successfully created');
logger.debug('user result >', newUser.dataValues); logger.debug('user result >', newUser.dataValues);
logger.debug('user result >', newChannel.dataValues);
logger.debug('certificate result >', newCertificate.dataValues); logger.debug('certificate result >', newCertificate.dataValues);
// associate the instances // associate the instances
return Promise.all([newCertificate.setUser(newUser), newUser.setCertificate(newCertificate)]); return Promise.all([newCertificate.setChannel(newChannel), newChannel.setUser(newUser)]);
}).then(() => { }).then(() => {
logger.debug('user and certificate successfully associated'); logger.debug('user and certificate successfully associated');
logger.debug('user ===', user.dataValues); logger.debug('user ===', user.dataValues);

View file

@ -76,6 +76,7 @@ app.use((req, res, next) => {
if (req.user) { if (req.user) {
res.locals.user = { res.locals.user = {
id : req.user.id, id : req.user.id,
userName : req.user.userName,
channelName : req.user.channelName, channelName : req.user.channelName,
channelClaimId: req.user.channelClaimId, channelClaimId: req.user.channelClaimId,
}; };