1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-15 17:45:10 +01:00
onion/js/actions/contract_agreement_list_actions.js

106 lines
4.4 KiB
JavaScript
Raw Normal View History

'use strict';
import { alt } from '../alt';
import Q from 'q';
import OwnershipFetcher from '../fetchers/ownership_fetcher';
2015-09-11 18:23:31 +02:00
import ContractListActions from './contract_list_actions';
class ContractAgreementListActions {
constructor() {
this.generateActions(
'updateContractAgreementList',
'flushContractAgreementList'
);
}
fetchContractAgreementList(issuer, accepted, pending) {
this.actions.updateContractAgreementList(null);
return Q.Promise((resolve, reject) => {
OwnershipFetcher.fetchContractAgreementList(issuer, accepted, pending)
.then((contractAgreementList) => {
if (contractAgreementList.count > 0) {
this.actions.updateContractAgreementList(contractAgreementList.results);
2015-09-11 18:23:31 +02:00
resolve(contractAgreementList.results);
} else {
2015-09-11 18:23:31 +02:00
resolve(null);
}
})
.catch((err) => {
console.logGlobal(err);
reject(err);
});
}
);
}
fetchAvailableContractAgreementList(issuer, createPublicContractAgreement) {
return Q.Promise((resolve, reject) => {
OwnershipFetcher.fetchContractAgreementList(issuer, true, null)
.then((acceptedContractAgreementList) => {
// if there is at least an accepted contract agreement, we're going to
// use it
if (acceptedContractAgreementList.count > 0) {
this.actions.updateContractAgreementList(acceptedContractAgreementList.results);
} else {
// otherwise, we're looking for contract agreements that are still pending
//
// Normally nesting promises, but for this conditional one, it makes sense to not
// overcomplicate the method
OwnershipFetcher.fetchContractAgreementList(issuer, null, true)
.then((pendingContractAgreementList) => {
if (pendingContractAgreementList.count > 0) {
this.actions.updateContractAgreementList(pendingContractAgreementList.results);
} else if (createPublicContractAgreement) {
// if there was neither a pending nor an active contractAgreement
// found and createPublicContractAgreement is set to true, we create a
// new public contract agreement
this.actions.createContractAgreementFromPublicContract(issuer);
}
})
.catch((err) => {
console.logGlobal(err);
reject(err);
});
}
})
.catch((err) => {
console.logGlobal(err);
reject(err);
});
}
);
}
createContractAgreementFromPublicContract(issuer) {
ContractListActions.fetchContractList(null, null, issuer)
.then((publicContract) => {
2015-09-11 18:23:31 +02:00
// create an agreement with the public contract if there is one
if (publicContract && publicContract.length > 0) {
return this.actions.createContractAgreement(null, publicContract[0]);
} else {
2015-09-11 18:23:31 +02:00
/*
contractAgreementList in the store is already set to null;
*/
}
}).then((publicContracAgreement) => {
if (publicContracAgreement) {
this.actions.updateContractAgreementList([publicContracAgreement]);
}
}).catch(console.logGlobal);
2015-09-11 18:23:31 +02:00
}
createContractAgreement(issuer, contract){
return Q.Promise((resolve, reject) => {
OwnershipFetcher
.createContractAgreement(issuer, contract).then(resolve)
.catch((err) => {
console.logGlobal(err);
reject(err);
});
2015-09-11 18:23:31 +02:00
});
}
}
export default alt.createActions(ContractAgreementListActions);