2015-09-10 20:20:42 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-06 16:47:59 +02:00
|
|
|
import { alt } from '../alt';
|
2015-09-10 20:20:42 +02:00
|
|
|
import Q from 'q';
|
|
|
|
|
|
|
|
import OwnershipFetcher from '../fetchers/ownership_fetcher';
|
2015-09-11 18:23:31 +02:00
|
|
|
import ContractListActions from './contract_list_actions';
|
2015-09-10 20:20:42 +02:00
|
|
|
|
|
|
|
class ContractAgreementListActions {
|
|
|
|
constructor() {
|
|
|
|
this.generateActions(
|
|
|
|
'updateContractAgreementList',
|
|
|
|
'flushContractAgreementList'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchContractAgreementList(issuer, accepted, pending) {
|
2015-09-21 10:57:10 +02:00
|
|
|
this.actions.updateContractAgreementList(null);
|
2015-09-10 20:20:42 +02:00
|
|
|
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);
|
2015-09-10 20:20:42 +02:00
|
|
|
}
|
2015-09-11 18:23:31 +02:00
|
|
|
else{
|
|
|
|
resolve(null);
|
2015-09-10 20:20:42 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.logGlobal(err);
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-22 16:45:24 +02:00
|
|
|
fetchAvailableContractAgreementList(issuer, createContractAgreement) {
|
2015-09-14 17:53:50 +02:00
|
|
|
return Q.Promise((resolve, reject) => {
|
2015-09-22 16:45:24 +02:00
|
|
|
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 there was neither a pending nor an active contractAgreement
|
|
|
|
// found and createContractAgreement is set to true, we create a
|
|
|
|
// new contract agreement
|
|
|
|
if(createContractAgreement) {
|
|
|
|
this.actions.createContractAgreementFromPublicContract(issuer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.logGlobal(err);
|
|
|
|
reject(err);
|
|
|
|
});
|
2015-09-14 17:53:50 +02:00
|
|
|
}
|
2015-09-22 16:45:24 +02:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2015-09-14 17:53:50 +02:00
|
|
|
console.logGlobal(err);
|
|
|
|
reject(err);
|
|
|
|
});
|
2015-09-22 16:45:24 +02:00
|
|
|
}
|
|
|
|
);
|
2015-09-14 17:53:50 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 16:45:24 +02:00
|
|
|
createContractAgreementFromPublicContract(issuer) {
|
2015-09-14 17:53:50 +02:00
|
|
|
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 {
|
|
|
|
/*
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-09-10 20:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default alt.createActions(ContractAgreementListActions);
|