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-12-03 15:22:17 +01:00
|
|
|
} else {
|
2015-09-11 18:23:31 +02:00
|
|
|
resolve(null);
|
2015-09-10 20:20:42 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.logGlobal(err);
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-03 15:22:17 +01:00
|
|
|
fetchAvailableContractAgreementList(issuer, createPublicContractAgreement) {
|
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
|
2015-12-03 15:22:17 +01:00
|
|
|
if (acceptedContractAgreementList.count > 0) {
|
2015-09-22 16:45:24 +02:00
|
|
|
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) => {
|
2015-12-03 15:22:17 +01:00
|
|
|
if (pendingContractAgreementList.count > 0) {
|
2015-09-22 16:45:24 +02:00
|
|
|
this.actions.updateContractAgreementList(pendingContractAgreementList.results);
|
2015-12-03 15:22:17 +01:00
|
|
|
} else if (createPublicContractAgreement) {
|
2015-09-22 16:45:24 +02:00
|
|
|
// if there was neither a pending nor an active contractAgreement
|
2015-12-03 15:22:17 +01:00
|
|
|
// found and createPublicContractAgreement is set to true, we create a
|
|
|
|
// new public contract agreement
|
|
|
|
this.actions.createContractAgreementFromPublicContract(issuer);
|
2015-09-22 16:45:24 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.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]);
|
2015-12-03 15:22:17 +01:00
|
|
|
} else {
|
2015-09-11 18:23:31 +02:00
|
|
|
/*
|
|
|
|
contractAgreementList in the store is already set to null;
|
|
|
|
*/
|
|
|
|
}
|
2015-12-17 11:01:49 +01:00
|
|
|
}).then((publicContractAgreement) => {
|
|
|
|
if (publicContractAgreement) {
|
|
|
|
this.actions.updateContractAgreementList([publicContractAgreement]);
|
2015-09-11 18:23:31 +02:00
|
|
|
}
|
2015-12-03 15:22:17 +01:00
|
|
|
}).catch(console.logGlobal);
|
2015-09-11 18:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
createContractAgreement(issuer, contract){
|
|
|
|
return Q.Promise((resolve, reject) => {
|
2015-12-03 15:22:17 +01:00
|
|
|
OwnershipFetcher
|
2015-12-17 11:01:49 +01:00
|
|
|
.createContractAgreement(issuer, contract)
|
|
|
|
.then((res) => {
|
|
|
|
resolve(res && res.contractagreement)
|
|
|
|
})
|
2015-12-03 15:22:17 +01:00
|
|
|
.catch((err) => {
|
|
|
|
console.logGlobal(err);
|
|
|
|
reject(err);
|
|
|
|
});
|
2015-09-11 18:23:31 +02:00
|
|
|
});
|
|
|
|
}
|
2015-09-10 20:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default alt.createActions(ContractAgreementListActions);
|