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 => {
logger.info(`Successfully published ${fileName}`, tx);
publishResults = tx;
return db.User.findOne({where: {channelName: publishParams.channel_name}});
return db.Channel.findOne({where: {channelName: publishParams.channel_name}});
})
.then(user => {
logger.debug('found user', user.datavalues);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -22,11 +22,16 @@ module.exports = new PassportLocalStrategy(
.then(tx => {
// create user record
const userData = {
channelName : username,
channelClaimId: tx.claim_id,
password : password,
userName: username,
password: password,
};
logger.debug('userData >', userData);
// create user record
const channelData = {
channelName : `@${username}`,
channelClaimId: tx.claim_id,
};
logger.debug('channelData >', channelData);
// create certificate record
const certificateData = {
claimId: tx.claim_id,
@ -35,15 +40,18 @@ module.exports = new PassportLocalStrategy(
};
logger.debug('certificateData >', certificateData);
// 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['channelName'] = newChannel.channelClaimId;
user['channelClaimId'] = newChannel.channelClaimId;
logger.debug('user and certificate successfully created');
logger.debug('user result >', newUser.dataValues);
logger.debug('user result >', newChannel.dataValues);
logger.debug('certificate result >', newCertificate.dataValues);
// associate the instances
return Promise.all([newCertificate.setUser(newUser), newUser.setCertificate(newCertificate)]);
return Promise.all([newCertificate.setChannel(newChannel), newChannel.setUser(newUser)]);
}).then(() => {
logger.debug('user and certificate successfully associated');
logger.debug('user ===', user.dataValues);

View file

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