1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00

create public agreement

This commit is contained in:
diminator 2015-09-11 18:23:31 +02:00
parent 23eb3a7fd9
commit ef22707680
6 changed files with 78 additions and 98 deletions

View File

@ -1,47 +0,0 @@
'use strict';
import alt from '../alt';
import OwnershipFetcher from '../fetchers/ownership_fetcher';
class ContractActions {
constructor() {
this.generateActions(
'updateContract',
'flushContract'
);
}
fetchContract(email) {
if(email.match(/.+\@.+\..+/)) {
OwnershipFetcher.fetchContract(email)
.then((contracts) => {
if (contracts && contracts.length > 0) {
this.actions.updateContract({
contractKey: contracts[0].s3Key,
contractUrl: contracts[0].s3Url,
contractEmail: email
});
}
else {
this.actions.updateContract({
contractKey: null,
contractUrl: null,
contractEmail: null
});
}
})
.catch((err) => {
console.logGlobal(err);
this.actions.updateContract({
contractKey: null,
contractUrl: null,
contractEmail: null
});
});
}
}
}
export default alt.createActions(ContractActions);

View File

@ -4,7 +4,7 @@ import alt from '../alt';
import Q from 'q'; import Q from 'q';
import OwnershipFetcher from '../fetchers/ownership_fetcher'; import OwnershipFetcher from '../fetchers/ownership_fetcher';
import ContractListActions from './contract_list_actions';
class ContractAgreementListActions { class ContractAgreementListActions {
constructor() { constructor() {
@ -16,25 +16,69 @@ class ContractAgreementListActions {
fetchContractAgreementList(issuer, accepted, pending) { fetchContractAgreementList(issuer, accepted, pending) {
return Q.Promise((resolve, reject) => { return Q.Promise((resolve, reject) => {
this.actions.updateContractAgreementList(null);
OwnershipFetcher.fetchContractAgreementList(issuer, accepted, pending) OwnershipFetcher.fetchContractAgreementList(issuer, accepted, pending)
.then((contractAgreementList) => { .then((contractAgreementList) => {
if (contractAgreementList.count > 0) { if (contractAgreementList.count > 0) {
this.actions.updateContractAgreementList(contractAgreementList.results); this.actions.updateContractAgreementList(contractAgreementList.results);
resolve(contractAgreementList.results);
} }
else { else{
this.actions.updateContractAgreementList(null); resolve(null);
} }
resolve();
}) })
.catch((err) => { .catch((err) => {
console.logGlobal(err); console.logGlobal(err);
this.actions.updateContractAgreementList(null);
reject(err); reject(err);
}); });
} }
); );
} }
fetchAvailableContractAgreementList(issuer){
this.actions.fetchContractAgreementList(issuer, 'True', null)
.then((contractAgreementListAccepted) => {
if (!contractAgreementListAccepted) {
// fetch pending agreements if no accepted ones
return this.actions.fetchContractAgreementList(issuer, null, 'True');
}
}).then((contractAgreementListPending) => {
// fetch public contract if no accepted nor pending agreements
if (!contractAgreementListPending) {
return ContractListActions.fetchContractList(null, null, issuer);
}
}).then((publicContract) => {
// create an agreement with the public contract if there is one
if (publicContract && publicContract.length > 0) {
return this.actions.createContractAgreement(null, publicContract[0]);
}
else {
/*
contractAgreementList in the store is already set to null;
*/
}
}).then((publicContracAgreement) => {
if (publicContracAgreement) {
this.actions.updateContractAgreementList([publicContracAgreement]);
}
}).catch((err) => {
console.logGlobal(err);
});
}
createContractAgreement(issuer, contract){
return Q.Promise((resolve, reject) => {
OwnershipFetcher.createContractAgreement(issuer, contract).then(
(contractAgreement) => {
resolve(contractAgreement);
}
).catch((err) => {
console.logGlobal(err);
reject(err);
});
});
}
} }
export default alt.createActions(ContractAgreementListActions); export default alt.createActions(ContractAgreementListActions);

View File

@ -12,14 +12,18 @@ class ContractListActions {
); );
} }
fetchContractList(isActive) { fetchContractList(isActive, isPublic, issuer) {
OwnershipFetcher.fetchContractList(isActive) return Q.Promise((resolve, reject) => {
OwnershipFetcher.fetchContractList(isActive, isPublic, issuer)
.then((contracts) => { .then((contracts) => {
this.actions.updateContractList(contracts.results); this.actions.updateContractList(contracts.results);
resolve(contracts.results);
}) })
.catch((err) => { .catch((err) => {
console.logGlobal(err); console.logGlobal(err);
this.actions.updateContractList([]); this.actions.updateContractList([]);
reject(err);
});
}); });
} }

View File

@ -54,14 +54,8 @@ let LoanForm = React.createClass({
componentDidMount() { componentDidMount() {
ContractAgreementListStore.listen(this.onChange); ContractAgreementListStore.listen(this.onChange);
if (this.props.email){ if (this.props.email){
ContractAgreementListActions.fetchContractAgreementList( ContractAgreementListActions.fetchAvailableContractAgreementList.defer(this.props.email);
this.props.email, 'True', null);
} }
/* @Tim:
throws Uncaught TypeError: Cannot read property 'defer' of undefined
We might not need this
ContractAgreementListActions.flushContractAgreementList().defer();*/
}, },
componentWillUnmount() { componentWillUnmount() {

View File

@ -15,8 +15,21 @@ let OwnershipFetcher = {
/** /**
* Fetch the contracts of the logged-in user from the API. * Fetch the contracts of the logged-in user from the API.
*/ */
fetchContractList(isActive){ fetchContractList(isActive, isPublic, issuer){
return requests.get(ApiUrls.ownership_contract_list, isActive); let queryParams = {
isActive,
isPublic,
issuer
};
return requests.get(ApiUrls.ownership_contract_list, queryParams);
},
/**
* Create a contractagreement between the logged-in user and the email from the API with contract.
*/
createContractAgreement(signee, contractObj){
return requests.post(ApiUrls.ownership_contract_agreements, { body: {signee: signee, contract: contractObj.id }});
}, },
/** /**

View File

@ -1,28 +0,0 @@
'use strict';
import alt from '../alt';
import ContractActions from '../actions/contract_actions';
class ContractStore {
constructor() {
this.contractKey = null;
this.contractUrl = null;
this.contractEmail = null;
this.bindActions(ContractActions);
}
onUpdateContract({contractKey, contractUrl, contractEmail}) {
this.contractKey = contractKey;
this.contractUrl = contractUrl;
this.contractEmail = contractEmail;
}
onFlushContract() {
this.contractKey = null;
this.contractUrl = null;
this.contractEmail = null;
}
}
export default alt.createStore(ContractStore, 'ContractStore');