Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.92 KB | None | 0 0
  1. import moment from 'moment';
  2. import { compose, mapTo, mapFrom } from 'functional-functions';
  3. import actions from './actions';
  4. import { del, putChanged, postChanged, getChanged, responseIsJson } from '../../../server-com';
  5. import { FormValue } from '../../dataTypes';
  6. import {
  7. validateEditPayoutBankItem,
  8. isEditPayoutBankFormValid,
  9. validateEditPayoutCustomerItem,
  10. isEditPayoutCustomerFormValid,
  11. validateEditPayoutBrokerItem,
  12. isEditPayoutBrokerFormValid,
  13. validateEditPayoutInternalItem,
  14. isEditPayoutInternalFormValid,
  15. isTotalPayoutSumValid
  16. } from './validation';
  17. import { filterPayouts, sum } from './payout-functions';
  18. import { showModalNewPayout } from '../modal';
  19. import { eMailFormOperations } from './';
  20.  
  21.  
  22. const validatePayoutMap = {
  23. name: 'bankName',
  24. accountNumber: 'accountNumber',
  25. amount: 'amount',
  26. loanContractNumber: 'loanContractNumber',
  27. kid: 'kidNumber',
  28. postalNumber: 'bankPostalNumber',
  29. email: 'bankEmail'
  30. };
  31.  
  32. const payoutMap = {
  33. id: 'id',
  34. idForProposal: 'idForProposal',
  35. payoutType: 'payoutType',
  36. type: 'type',
  37. paymentInfo: 'informationText',
  38. address: 'bankAddress',
  39. location: 'bankLocation',
  40. payoutDate: 'payoutDate',
  41. isProposal: 'isProposal',
  42. documentId: 'documentId',
  43. includePayout: 'includePayout',
  44. isCredit: 'isCredit',
  45. ...validatePayoutMap
  46. };
  47.  
  48. const validateFor = type => state => value => ({
  49. external: (v, s) => validateEditPayoutBankItem(v, s),
  50. customer: (v, s) => validateEditPayoutCustomerItem(v, s),
  51. broker: (v, s) => validateEditPayoutBrokerItem(v, s),
  52. internal: (v, s) => validateEditPayoutInternalItem(v, s)
  53. }[type](value, state));
  54.  
  55. const isValidFor = type => state => value => ({
  56. customer: (v, s) => isEditPayoutCustomerFormValid(v, s),
  57. external: (v, s) => isEditPayoutBankFormValid(v, s),
  58. broker: (v, s) => isEditPayoutBrokerFormValid(v, s),
  59. internal: (v, s) => isEditPayoutInternalFormValid(v, s)
  60. }[type](value, state));
  61.  
  62. const validateForm = () => (dispatch, getState) => {
  63. const { payouts } = getState();
  64. const { form } = payouts;
  65. const { type } = form;
  66. const isValid = isValidFor(type)(payouts);
  67.  
  68. dispatch(actions.setFormValidity(isValid(getState().payouts.form.data)));
  69. };
  70.  
  71. const validateItem = id => (dispatch, getState) => {
  72. const { payouts } = getState();
  73. const { form } = payouts;
  74. const { type } = form;
  75. const value = form.data[id];
  76. const validate = validateFor(type)(payouts);
  77.  
  78. if (value) {
  79. dispatch(actions.changePayout(id, validate(value)));
  80. }
  81. };
  82.  
  83. const matchWithRecipient = (payout, banks) =>
  84. ((payout.isProposal && payout.name)
  85. ? banks.find(b => b.name.trim().toLowerCase() === payout.name.trim().toLowerCase())
  86. : null);
  87.  
  88. const toPayouts = ({ payouts, payoutDate, payoutBufferAmount, productId }) => ({ payouts: payouts.map(mapFrom(payoutMap)), payoutDate, payoutBufferAmount, productId });
  89.  
  90. const newPayoutOnServer = () => (dispatch, getState) => {
  91. dispatch(actions.writeErrorMessage(''));
  92. const { newPayoutUrl, payouts } = getState().payouts;
  93. const errorMessage = 'Noe gikk galt ved opprettelse av ny utbetaling';
  94. const payout = payouts.find(p => p.id === null);
  95. const payoutToSend = mapTo(payoutMap)(payout);
  96.  
  97. const errorHandler = error => dispatch(actions.writeErrorMessage(`\n ${errorMessage}: ${error}`));
  98.  
  99. postChanged(newPayoutUrl, payoutToSend)
  100. .then((response) => {
  101. if (response.message) {
  102. dispatch(errorHandler(response.message));
  103. } else {
  104. Promise.all([
  105. dispatch(actions.serverUpdated(response.id))
  106. ])
  107. .then(() => dispatch(actions.updateViewModel()));
  108. }
  109. }, (error) => {
  110. if (responseIsJson(error)) {
  111. error.json().then((json) => {
  112. dispatch(errorHandler(json));
  113. });
  114. } else {
  115. error.text().then((txt) => {
  116. dispatch(errorHandler(txt));
  117. });
  118. }
  119. });
  120. };
  121.  
  122. const updatePayoutOnServer = id => (dispatch, getState) => {
  123. dispatch(actions.writeErrorMessage(''));
  124. const { updatePayoutUrl, payouts } = getState().payouts;
  125. const errorMessage = 'Noe gikk galt ved oppdatering av utbetalinger';
  126. const payout = payouts.find(p => p.id === id);
  127.  
  128. const errorHandler = error => dispatch(actions.writeErrorMessage(`\n ${errorMessage}: ${error}`));
  129.  
  130. putChanged(updatePayoutUrl, mapTo(payoutMap)(payout))
  131. .then((response) => {
  132. if (response.message) {
  133. dispatch(errorHandler(response.message));
  134. } else {
  135. Promise.all([
  136. dispatch(actions.serverUpdated(response.id))
  137. ])
  138. .then(() => dispatch(actions.updateViewModel()));
  139. }
  140. }, (error) => {
  141. if (responseIsJson(error)) {
  142. error.json().then((json) => {
  143. dispatch(errorHandler(json));
  144. });
  145. } else {
  146. error.text().then((txt) => {
  147. dispatch(errorHandler(txt));
  148. });
  149. }
  150. });
  151. };
  152.  
  153. const updateServer = id => (dispatch) => {
  154. dispatch(actions.updatingServer());
  155.  
  156. return (!id) ? dispatch(newPayoutOnServer()) : dispatch(updatePayoutOnServer(id));
  157. };
  158.  
  159. const deleteFromServer = id => (dispatch, getState) => {
  160. dispatch(actions.writeErrorMessage(''));
  161. dispatch(actions.deletingFromServer(id));
  162. const errorMessage = 'Noe gikk galt ved sletting av utbetalinger';
  163. const { deletePayoutUrl } = getState().payouts;
  164. const deletionUrl = `${deletePayoutUrl}/${id}`;
  165.  
  166. const successHandler = () => {
  167. Promise.all([
  168. dispatch(actions.payoutDeleted)
  169. ])
  170. .then(() => dispatch(actions.updateViewModel()));
  171. };
  172. const errorHandler = error => dispatch(actions.deletePayoutFailed(`\n ${errorMessage}: Feilkode av type ${error} feil vart returnert`));
  173.  
  174. return del(deletionUrl)
  175. .then((response) => {
  176. if (!response.ok) throw new Error(response.status);
  177. })
  178. .then(successHandler, errorHandler);
  179. };
  180.  
  181.  
  182. const getRecipients = () => (dispatch, getState) => {
  183. dispatch(actions.fetchingRecipients());
  184. const errorMessage = 'Noe gikk galt ved uthenting av mottaker for utbetalinger';
  185. const { getRecipientsUrl } = getState().payouts;
  186. const errorHandler = error => dispatch(actions.getRecipientsFailed(`\n ${errorMessage}: ${error}`));
  187.  
  188. getChanged(getRecipientsUrl)
  189. .then((response) => {
  190. if (response.message) {
  191. dispatch(errorHandler(response.message));
  192. } else {
  193. dispatch(actions.getRecipientsReceived(response));
  194. }
  195. }, (error) => {
  196. if (responseIsJson(error)) {
  197. error.json().then((json) => {
  198. dispatch(errorHandler(json));
  199. });
  200. } else {
  201. error.text().then((txt) => {
  202. dispatch(errorHandler(txt));
  203. });
  204. }
  205. });
  206. };
  207.  
  208. const updateCustomerPayout = () => (dispatch, getState) => {
  209. const { payouts, loanAmount, registrationFee, registrationFeeInLoanAmount } = getState().payouts;
  210.  
  211. const payoutsExceptCustomer = payouts.filter(p => p.type !== 'customer');
  212. const { actualPayouts, newPayouts } = filterPayouts(payoutsExceptCustomer);
  213.  
  214. const sumPayouts = [...actualPayouts, ...newPayouts].reduce(sum, 0.0);
  215. const rest = loanAmount - (registrationFeeInLoanAmount ? 0.0 : registrationFee) - sumPayouts;
  216. const toCustomer = rest < 0 ? 0.0 : rest;
  217.  
  218. // Possibly add support for multiple accounts
  219.  
  220. dispatch(actions.updateCustomerAmount(toCustomer));
  221. };
  222.  
  223. const updatePayoutAmount = () => (dispatch, getState) => {
  224. const { payouts, registrationFee, registrationFeeInLoanAmount } = getState().payouts;
  225.  
  226. const onlyActualPayouts = p => !p.isProposal;
  227.  
  228. const payoutAmount = payouts.filter(onlyActualPayouts).reduce(sum, registrationFeeInLoanAmount ? 0.0 : registrationFee);
  229.  
  230. dispatch(actions.updatePayoutAmount(payoutAmount));
  231. };
  232.  
  233. const maybeResetCustomerPayout = type => (dispatch, getState) => {
  234. if (type && type === 'customer') return Promise.resolve();
  235.  
  236. const { payouts } = getState().payouts;
  237. const customerPayout = payouts.find(p => p.type === 'customer' && !p.isProposal);
  238.  
  239. if (customerPayout) {
  240. dispatch(actions.deletePayout(customerPayout.id));
  241. return dispatch(deleteFromServer(customerPayout.id))
  242. .then(() => dispatch(updatePayoutAmount()));
  243. }
  244.  
  245. return Promise.resolve();
  246. };
  247.  
  248. const getAccountNumbers = () => (dispatch, getState) => {
  249. dispatch(actions.fetchingCustomerAccounts());
  250. const errorMessage = 'Noe gikk galt ved henting av kontonummer';
  251. const { getCustomerAccountsUrl } = getState().payouts;
  252. const successHandler = response => dispatch(actions.customerAccountsReceived(response));
  253. const errorHandler = error => dispatch(actions.getCustomerAccountsFailed(`\n ${errorMessage}: ${error}`));
  254.  
  255. getChanged(getCustomerAccountsUrl)
  256. .then((response) => {
  257. if (response.message) {
  258. dispatch(errorHandler(response.message));
  259. } else {
  260. dispatch(successHandler(response));
  261. }
  262. }, (error) => {
  263. if (responseIsJson(error)) {
  264. error.json().then((json) => {
  265. dispatch(errorHandler(json));
  266. });
  267. } else {
  268. error.text().then((txt) => {
  269. dispatch(errorHandler(txt));
  270. });
  271. }
  272. });
  273. };
  274.  
  275. const maybeGetAccountNumbers = type => (dispatch, getState) => {
  276. if (type !== 'customer') return Promise.resolve();
  277.  
  278. const { customerAccountNumbers } = getState().payouts;
  279. if (customerAccountNumbers && customerAccountNumbers.length > 0) return Promise.resolve();
  280.  
  281. return dispatch(getAccountNumbers());
  282. };
  283.  
  284. const canSendToPostControlCheck = () => (dispatch, getState) => {
  285. dispatch(actions.writeValidateMessage(''));
  286. const
  287. { payouts,
  288. loanAmount,
  289. registrationFeeInLoanAmount,
  290. registrationFee,
  291. payoutDate,
  292. payoutBufferAmount,
  293. productId,
  294. buttonStates,
  295. applicationCurrentState } = getState().payouts;
  296.  
  297. let canSend = false;
  298. const currentState = applicationCurrentState ? applicationCurrentState === 'S017' : false;
  299.  
  300. if (currentState) {
  301. const onlyActualPayouts = p => !p.isProposal;
  302. const actualPayouts = payouts.filter(onlyActualPayouts);
  303. const isActualPayouts = actualPayouts && actualPayouts.length > 0;
  304. const sumPayouts = actualPayouts.reduce(sum, registrationFeeInLoanAmount ? 0.0 : registrationFee);
  305. const payoutSumOk = isTotalPayoutSumValid(sumPayouts, loanAmount, payoutBufferAmount, productId);
  306. const dateOk = payoutDate ? moment(payoutDate) >= moment().startOf('day') : false;
  307. const datesAreEqual = (actualPayouts && payoutDate) ? actualPayouts.every(p => new Date(p.payoutDate).getTime() === payoutDate.getTime()) : false;
  308. const buttonStateTilEtterkontrollErrorMessage = buttonStates ? buttonStates.find(b => b.name === 'Til etterkontroll').message : '';
  309.  
  310. canSend = isActualPayouts && payoutSumOk && dateOk && datesAreEqual && buttonStateTilEtterkontrollErrorMessage === '';
  311.  
  312. if (isActualPayouts) {
  313. if (!canSend) {
  314. const errorMsg = `\n Kan ikke gå videre til etterkontroll: ${!payoutSumOk ? '\n Det er ikke korrekt utbetalingsum.' : ''} ${!dateOk ? '\n Det er ikke mulig å sette opp utbetalinger med dato tilbake i tid.' : ''} ${!datesAreEqual ? '\n Utbetalingsdatoen må være lik for alle utbetalinger.' : ''} ${buttonStateTilEtterkontrollErrorMessage !== '' ? `\n ${buttonStateTilEtterkontrollErrorMessage}` : ''}`;
  315. dispatch(actions.writeValidateMessage(errorMsg));
  316. }
  317. }
  318. }
  319. return dispatch(actions.canSendToPostControl(canSend));
  320. };
  321.  
  322.  
  323. const getBankHolidays = () => (dispatch, getState) => {
  324. dispatch(actions.fetchingBankHolidays());
  325. const errorMessage = 'Noe gikk galt ved henting av mulige utbetalingsdatoer';
  326. const { getBankHolidaysUrl } = getState().payouts;
  327. const errorHandler = error => dispatch(actions.getBankHolidaysFailed(`\n ${errorMessage}: ${error}`));
  328.  
  329. getChanged(getBankHolidaysUrl)
  330. .then((response) => {
  331. if (response.message) {
  332. dispatch(errorHandler(response.message));
  333. } else {
  334. dispatch(actions.bankHolidaysReceived(response));
  335. }
  336. }, (error) => {
  337. if (responseIsJson(error)) {
  338. error.json().then((json) => {
  339. dispatch(errorHandler(json));
  340. });
  341. } else {
  342. error.text().then((txt) => {
  343. dispatch(errorHandler(txt));
  344. });
  345. }
  346. });
  347. };
  348.  
  349. const getButtonStates = () => (dispatch, getState) => {
  350. dispatch(actions.fetchingButtonStates());
  351. const errorMessage = 'Noe gikk galt ved henting av status for mulig funksjonalitet for utbetalinger';
  352. const { getButtonStatesUrl } = getState().payouts;
  353. const successHandler = response => dispatch(actions.buttonStatesReceived(response));
  354. const errorHandler = error => dispatch(actions.getButtonStatesFailed(`\n ${errorMessage}: ${error}`));
  355.  
  356. getChanged(getButtonStatesUrl)
  357. .then((response) => {
  358. if (response.message) {
  359. dispatch(errorHandler(response.message));
  360. } else {
  361. dispatch(successHandler(response));
  362. }
  363. }, (error) => {
  364. if (responseIsJson(error)) {
  365. error.json().then((json) => {
  366. dispatch(errorHandler(json));
  367. });
  368. } else {
  369. error.text().then((txt) => {
  370. dispatch(errorHandler(txt));
  371. });
  372. }
  373. });
  374. };
  375.  
  376. const getLetterOfPaymentCustomer = () => (dispatch, getState) => {
  377. const { getLetterOfPaymentCustomerUrl } = getState().payouts;
  378. const successHandler = response => dispatch(actions.letterOfPaymentCustomerReceived(response));
  379. getChanged(getLetterOfPaymentCustomerUrl)
  380. .then(
  381. (response) => {
  382. successHandler(response);
  383. },
  384. () => {} // No error-handling required
  385. );
  386. };
  387.  
  388. const getPayouts = () => (dispatch, getState) => {
  389. dispatch(actions.fetchingPayouts());
  390. const errorMessage = 'Noe gikk galt ved henting av utbetalinger';
  391. const { getPayoutsUrl } = getState().payouts;
  392. const successHandler = response => dispatch(actions.payoutsReceived(toPayouts(response)));
  393. const errorHandler = error => dispatch(actions.getPayoutsFailed(`\n ${errorMessage}: ${error}`));
  394.  
  395. getChanged(getPayoutsUrl)
  396. .then((response) => {
  397. if (response.message) {
  398. dispatch(errorHandler(response.message));
  399. } else {
  400. Promise.all([
  401. dispatch(successHandler(response))
  402. ])
  403. .then(() => dispatch(updateCustomerPayout()))
  404. .then(() => dispatch(updatePayoutAmount()))
  405. .then(() => dispatch(canSendToPostControlCheck()))
  406. .then(() => dispatch(getBankHolidays()))
  407. .then(() => dispatch(actions.updateViewModel()));
  408. }
  409. }, (error) => {
  410. if (responseIsJson(error)) {
  411. error.json().then((json) => {
  412. dispatch(errorHandler(json));
  413. });
  414. } else {
  415. error.text().then((txt) => {
  416. dispatch(errorHandler(txt));
  417. });
  418. }
  419. });
  420. };
  421.  
  422. /*-------------------------------------------------------------------*/
  423.  
  424. const initPayouts = props => (dispatch) => {
  425. const { proxy, ...rest } = props; // Proxy comes from habitat
  426. dispatch(actions.initPayouts(rest));
  427.  
  428. return Promise.all([
  429. dispatch(getPayouts()),
  430. dispatch(getButtonStates()),
  431. dispatch(getLetterOfPaymentCustomer())
  432. ])
  433. .then(() => dispatch(actions.payoutsInitialized()));
  434. };
  435.  
  436. const newPayout = () => (dispatch, getState) => {
  437. dispatch(getRecipients());
  438. const { newPayoutType } = getState().payouts;
  439.  
  440. const selectType = (type) => {
  441. dispatch(actions.newPayout(type));
  442. dispatch(actions.showForm());
  443. return dispatch(maybeGetAccountNumbers(type));
  444. };
  445.  
  446. return showModalNewPayout(
  447. dispatch,
  448. 'Velg type',
  449. {
  450. external: 'Bank',
  451. internal: 'Intern',
  452. broker: 'Megler/Annen part',
  453. customer: 'Kunde'
  454. },
  455. newPayoutType,
  456. selectType
  457. );
  458. };
  459.  
  460. const editPayout = id => (dispatch, getState) => {
  461. dispatch(getRecipients());
  462. const { payouts, banks, brokers } = getState().payouts;
  463. const payout = payouts.find(p => p.id === id);
  464. const getRecipient = () => {
  465. const registers = {
  466. external: banks,
  467. broker: brokers
  468. };
  469. const register = registers[payout.type];
  470. return register ? matchWithRecipient(payout, register) : null;
  471. };
  472. const recipient = getRecipient();
  473.  
  474. dispatch(actions.editPayout(id));
  475. if (recipient) dispatch(actions.selectRecipient(recipient.id)); // Can do this by id to make more performant.. Would need a new action then.
  476. dispatch(actions.showForm());
  477. return Promise.all([
  478. dispatch(maybeGetAccountNumbers(payout.type))])
  479. .then(() => dispatch(validateForm()));
  480. };
  481.  
  482. const changePayout = (id, value) => (dispatch) => {
  483. // Update form state with value
  484. dispatch(actions.changePayout(id, new FormValue(id, value)));
  485. };
  486.  
  487. const validatePayout = id => (dispatch) => {
  488. dispatch(validateItem(id));
  489. dispatch(validateForm());
  490. };
  491.  
  492. const changePayoutDate = date => (dispatch, getState) => {
  493. const { payouts } = getState().payouts;
  494. const selectId = payout => payout.id;
  495. const dispatchUpdateServer = compose(dispatch, updateServer, selectId);
  496.  
  497. dispatch(actions.changePayoutDate(date));
  498. return Promise.all(payouts.filter(p => !p.isProposal).map(dispatchUpdateServer))
  499. .then(() => dispatch(canSendToPostControlCheck()));
  500. };
  501.  
  502. const savePayout = () => (dispatch, getState) => {
  503. const { type } = getState().payouts.form.data;
  504. const { payoutDate } = getState().payouts;
  505.  
  506. dispatch(validateForm());
  507. Object.values(validatePayoutMap).forEach((entry) => {
  508. dispatch(validateItem(entry));
  509. });
  510. const { valid } = getState().payouts.form;
  511. if (!valid) return;
  512.  
  513. Promise.all([
  514. dispatch(actions.savePayout()),
  515. dispatch(updateCustomerPayout()),
  516. dispatch(updatePayoutAmount()),
  517. dispatch(actions.hideForm()),
  518. dispatch(actions.clearForm())])
  519. .then(() => dispatch(changePayoutDate(payoutDate)))
  520. .then(() => dispatch(maybeResetCustomerPayout(type.value)))
  521. .then(() => dispatch(actions.updateViewModel()));
  522. };
  523.  
  524. const abort = () => (dispatch) => {
  525. dispatch(actions.clearForm());
  526. dispatch(actions.hideForm());
  527. };
  528.  
  529. const deletePayout = id => (dispatch) => {
  530. dispatch(actions.deletePayout(id));
  531. dispatch(updateCustomerPayout());
  532. dispatch(updatePayoutAmount());
  533. dispatch(deleteFromServer(id))
  534. .then(() => dispatch(maybeResetCustomerPayout()))
  535. .then(() => dispatch(canSendToPostControlCheck()))
  536. .then(() => dispatch(actions.updateViewModel()));
  537. };
  538.  
  539. const selectBankRecipient = id => (dispatch, getState) => {
  540. dispatch(actions.selectRecipient(id));
  541. dispatch(actions.setFormValidity(isEditPayoutBankFormValid(getState().payouts.form.data)));
  542. };
  543.  
  544. const selectBrokerRecipient = id => (dispatch, getState) => {
  545. dispatch(actions.selectRecipient(id));
  546. dispatch(actions.setFormValidity(isEditPayoutBrokerFormValid(getState().payouts.form.data)));
  547. };
  548.  
  549. const refreshAmountInternal = (contractNumber, accountNumber, amount) => (dispatch, getState) => {
  550. dispatch(actions.writeErrorMessage(''));
  551. dispatch(actions.fetchingContractSettlement());
  552. const errorMessage = 'Noe gikk galt ved uthenting av internt beløp';
  553. const { getContractSettlementUrl, payoutDate, customerId } = getState().payouts;
  554. const successHandler = response => dispatch(actions.contractSettlementReceived(response));
  555. const errorHandler = error => dispatch(actions.getContractSettlementFailed(`\n ${errorMessage}: ${error}`));
  556.  
  557. getChanged(getContractSettlementUrl, { customerId, settlementDate: moment(payoutDate).format('YYYY-MM-DD'), loanContractNumber: contractNumber, paymentAccountNumber: accountNumber, loanAmount: amount })
  558. .then((response) => {
  559. if (response.message) {
  560. dispatch(errorHandler(response.message));
  561. } else {
  562. dispatch(successHandler(response));
  563. }
  564. }, (error) => {
  565. if (responseIsJson(error)) {
  566. error.json().then((json) => {
  567. dispatch(errorHandler(json));
  568. });
  569. } else {
  570. error.text().then((txt) => {
  571. dispatch(errorHandler(txt));
  572. });
  573. }
  574. });
  575. };
  576.  
  577. const toPostControl = () => (dispatch, getState) => {
  578. dispatch(actions.writeErrorMessage(''));
  579. const { canSendToPostControl } = getState().payouts;
  580. if (!canSendToPostControl) return Promise.resolve('Not possible!');
  581. const errorMessage = 'Noe gikk galt ved sending til etterkontroll';
  582. dispatch(actions.sendingToPostControl());
  583.  
  584. const { sendToPostControlUrl } = getState().payouts;
  585. const successHandler = (result) => {
  586. Promise.all([
  587. dispatch(actions.sendToPostControlSuccess)
  588. ]).then(() => window.location.replace(result.url));
  589. };
  590.  
  591. const errorHandler = error => dispatch(actions.sendToPostControlFailed(`\n ${errorMessage}: ${error}`));
  592.  
  593. return postChanged(sendToPostControlUrl)
  594. .then((response) => {
  595. if (response.message) {
  596. dispatch(errorHandler(response.message));
  597. } else {
  598. dispatch(successHandler(response));
  599. }
  600. }, (error) => {
  601. if (responseIsJson(error)) {
  602. error.json().then((json) => {
  603. dispatch(errorHandler(json));
  604. });
  605. } else {
  606. error.text().then((txt) => {
  607. dispatch(errorHandler(txt));
  608. });
  609. }
  610. });
  611. };
  612.  
  613. const showEmailForm = (payoutId, documentId) => (dispatch, getState) => {
  614. const { payouts } = getState().payouts;
  615. let payout = payouts.find(p => p.id === payoutId);
  616.  
  617. if (!payout) {
  618. const { defaultEmailAddress } = getState().emailBox;
  619. payout = { id: 0, documentId, email: defaultEmailAddress };
  620. }
  621.  
  622. dispatch(eMailFormOperations.prepareEmailForm(payout, documentId)).then(() => dispatch(actions.showEmailForm()));
  623. };
  624.  
  625. const closeEmailForm = () => (dispatch) => {
  626. // dispatch(cleanEmailForm());
  627. dispatch(actions.hideEmailForm());
  628. };
  629.  
  630. export default {
  631. initPayouts,
  632. newPayout,
  633. editPayout,
  634. validatePayout,
  635. savePayout,
  636. changePayout,
  637. abort,
  638. deletePayout,
  639. selectBankRecipient,
  640. selectBrokerRecipient,
  641. changePayoutDate,
  642. refreshAmountInternal,
  643. toPostControl,
  644. showEmailForm,
  645. closeEmailForm,
  646. getRecipients
  647. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement