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) {
return new Promise((resolve, reject) => {
let publishResults = {};
// 1. make sure the name is (still) available
// 1. make sure the name is available
publishHelpers.checkClaimNameAvailability(publishParams.name)
// 2. publish the file
.then(result => {
if (result === true) {
return lbryApi.publishClaim(publishParams);
} 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)
@ -40,10 +40,15 @@ module.exports = {
name : publishParams.name,
claimId: publishResults.claim_id,
};
// create the records
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');
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;
})
.catch(error => {

View file

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

View file

@ -14,11 +14,21 @@ module.exports = new PassportLocalStrategy(
(req, username, password, done) => {
logger.debug('new channel signup request');
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
return lbryApi.createChannel(username)
.then(channelTx => {
// create user record
const userData = {
channelName : username,
channelClaimId: channelTx.claim_id,
password : password,
address,
};
logger.debug('userData >', userData);
// create certificate record
const certificateData = {
address,
@ -26,24 +36,19 @@ module.exports = new PassportLocalStrategy(
name : username,
};
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 => {
logger.debug('certificate result >', certificate.dataValues);
logger.debug('Certificate record was created successfully');
// define an object that contains all the user data
const userData = {
channelName : username,
channelClaimId: certificate.claimId,
password : password,
address,
CertificateId : certificate.id,
};
logger.debug('userData >', userData);
return db.User.create(userData);
}).then(user => {
.then(result => {
user = result[0];
certificate = result[1];
logger.debug('user and certificate successfully created');
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);
})
.catch(error => {

View file

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