fixed associations between User and Certificate

This commit is contained in:
bill bittner 2017-09-21 11:00:06 -07:00
parent b1f4bbeaf3
commit ba7e19a498
4 changed files with 37 additions and 25 deletions

View file

@ -7,14 +7,14 @@ module.exports = {
publish (publishParams, fileName, fileType) { publish (publishParams, fileName, fileType) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let publishResults = {}; let publishResults = {};
// 1. make sure the name is (still) available // 1. make sure the name is available
publishHelpers.checkClaimNameAvailability(publishParams.name) publishHelpers.checkClaimNameAvailability(publishParams.name)
// 2. publish the file // 2. publish the file
.then(result => { .then(result => {
if (result === true) { if (result === true) {
return lbryApi.publishClaim(publishParams); return lbryApi.publishClaim(publishParams);
} else { } else {
return new Error('That name has already been claimed on spee.ch.'); return new Error('That name has already been claimed by spee.ch.');
} }
}) })
// 3. upsert File record (update is in case the claim has been published before by this daemon) // 3. upsert File record (update is in case the claim has been published before by this daemon)
@ -40,10 +40,15 @@ module.exports = {
name : publishParams.name, name : publishParams.name,
claimId: publishResults.claim_id, claimId: publishResults.claim_id,
}; };
// create the records
return Promise.all([db.upsert(db.File, fileRecord, upsertCriteria, 'File'), db.upsert(db.Claim, fileRecord, upsertCriteria, 'Claim')]); return Promise.all([db.upsert(db.File, fileRecord, upsertCriteria, 'File'), db.upsert(db.Claim, fileRecord, upsertCriteria, 'Claim')]);
}) })
.then(() => { .then((file, claim) => {
logger.debug('File and Claim records successfully created'); logger.debug('File and Claim records successfully created');
return Promise.all([file.setClaims(claim), claim.setFiles(file)]);
})
.then(() => {
logger.debug('File and Claim records successfully associated');
resolve(publishResults); // resolve the promise with the result from lbryApi.publishClaim; resolve(publishResults); // resolve the promise with the result from lbryApi.publishClaim;
}) })
.catch(error => { .catch(error => {

View file

@ -18,12 +18,12 @@ module.exports = {
down: (queryInterface, Sequelize) => { down: (queryInterface, Sequelize) => {
// logic for reverting the changes // logic for reverting the changes
const p1 = queryInterface.removeColumn( const p1 = queryInterface.removeColumn(
'Certificate', 'User',
'UserId' 'CertificateId'
); );
const p2 = queryInterface.addColumn( const p2 = queryInterface.addColumn(
'User', 'Certificate',
'CertificateId', 'UserId',
{ {
type : Sequelize.STRING, type : Sequelize.STRING,
allowNull: true, allowNull: true,

View file

@ -14,11 +14,21 @@ module.exports = new PassportLocalStrategy(
(req, username, password, done) => { (req, username, password, done) => {
logger.debug('new channel signup request'); logger.debug('new channel signup request');
const address = config.get('WalletConfig.LbryClaimAddress'); const address = config.get('WalletConfig.LbryClaimAddress');
// server-side validaton of raw inputs (username, password) let user;
let certificate;
// server-side validaton of inputs (username, password)
// create the channel and retrieve the metadata // create the channel and retrieve the metadata
return lbryApi.createChannel(username) return lbryApi.createChannel(username)
.then(channelTx => { .then(channelTx => {
// create user record
const userData = {
channelName : username,
channelClaimId: channelTx.claim_id,
password : password,
address,
};
logger.debug('userData >', userData);
// create certificate record // create certificate record
const certificateData = { const certificateData = {
address, address,
@ -26,24 +36,19 @@ module.exports = new PassportLocalStrategy(
name : username, name : username,
}; };
logger.debug('certificateData >', certificateData); logger.debug('certificateData >', certificateData);
return db.Certificate.create(certificateData); // save user and certificate to db
return Promise.all([db.User.create(userData), db.Certificate.create(certificateData)]);
}) })
.then(certificate => { .then(result => {
logger.debug('certificate result >', certificate.dataValues); user = result[0];
logger.debug('Certificate record was created successfully'); certificate = result[1];
// define an object that contains all the user data logger.debug('user and certificate successfully created');
const userData = {
channelName : username,
channelClaimId: certificate.claimId,
password : password,
address,
CertificateId : certificate.id,
};
logger.debug('userData >', userData);
return db.User.create(userData);
}).then(user => {
logger.debug('user result >', user.dataValues); logger.debug('user result >', user.dataValues);
logger.debug('User record was created successfully'); logger.debug('certificate result >', certificate.dataValues);
// associate the instances
return Promise.all([certificate.setUser(user), user.setCertificate(certificate)]);
}).then(result => {
logger.debug('user and certificate successfully associated');
return done(null, user); return done(null, user);
}) })
.catch(error => { .catch(error => {

View file

@ -45,8 +45,10 @@ passport.serializeUser((user, done) => {
passport.deserializeUser((id, done) => { passport.deserializeUser((id, done) => {
db.User.findOne({ where: { id } }) db.User.findOne({ where: { id } })
.then(user => { .then(user => {
done(null, user); // user.dataValues? done(null, user);
return null;
}) })
.then()
.catch(error => { .catch(error => {
logger.error('sequelize error', error); logger.error('sequelize error', error);
done(error, null); done(error, null);