From 85264f08fd7835eb581c68aa247f771154daa468 Mon Sep 17 00:00:00 2001 From: Conrad S Date: Thu, 9 Feb 2017 12:39:38 -0500 Subject: [PATCH] Update buildings to use new utilityservice endpoints --- src/components/Utilities/index.js | 76 +++++++++++++++++++------------ src/utils/rest_services.js | 3 +- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/src/components/Utilities/index.js b/src/components/Utilities/index.js index e2e895e4..393b8133 100644 --- a/src/components/Utilities/index.js +++ b/src/components/Utilities/index.js @@ -2,7 +2,7 @@ import React, { Component, PropTypes } from 'react'; import buildingDetailPropTypes from '../../containers/Building/propTypes'; import request from '../../utils/request'; -import { getHeaders, utilityURL } from '../../utils/rest_services'; +import { getHeaders, billsURL, accountURL } from '../../utils/rest_services'; import UtilityAccount from '../UtilityAccount'; @@ -23,37 +23,57 @@ class Utilities extends Component { } getUtilityAccounts = (address) => { - request(`${utilityURL}${this.props.buildingId}?address=${address}`, { + // First reqeust gets the utility account information + request(`${accountURL}${this.props.buildingId}`, { method: 'GET', headers: getHeaders(), - }).then((res) => { - if (!res.err && res.data.utilities) { - const allAccounts = res.data.utilities.map(account => ( - this.createUtilityComponent({ - building_address: address, - utility: account.type, - account_number: account.account_number, - username: account.login, - password: account.pass, - access_code: account.access_code, - }, - account.url_download, - ) - )); - const blankAccount = this.createUtilityComponent(); - this.setState({ - accountLines: this.state.accountLines.concat(allAccounts, blankAccount), - }); - this.resetErrorMessage(); - } else if (res.err) { - this.setState({ error: `Failed to retrieve utility accounts. | ${res.err.message}` }); - } + }).then((accountRes) => { + // Second request gets the box files. Need to link them + request(`${billsURL}${this.props.buildingId}?address=${address}`, { + method: 'GET', + headers: getHeaders(), + }).then((billsRes) => { + if ((!accountRes.err && accountRes.data.utilities) + && (!billsRes.err)) { + const boxBuildingList = billsRes.data.box_building_list; + const allAccounts = accountRes.data.utilities.map((account) => { + let urlDownload = ''; + // Loop through and connect a box URL with this account + for (let i = 0; i < boxBuildingList.length; i += 1) { + const boxFile = boxBuildingList[i]; + if (boxFile.tags === account.account_number) { + urlDownload = boxFile.url_download; + break; + } + } + return this.createUtilityComponent({ + building_address: address, + utility: account.type, + account_number: account.account_number, + username: account.login, + password: account.pass, + access_code: account.access_code, + }, + urlDownload, + ); + }); + const blankAccount = this.createUtilityComponent(); + this.setState({ + accountLines: this.state.accountLines.concat(allAccounts, blankAccount), + }); + this.resetErrorMessage(); + } else if (accountRes.err) { + this.setState({ error: `Failed to retrieve utility accounts. | ${accountRes.err.message}` }); + } else if (billsRes.err) { + this.setState({ error: `Failed to retrieve utility bills. | ${billsRes.err.message}` }); + } + }); }); } updateUtilityBill = (form, updateLoadingState, setDownloadURL) => { - request(utilityURL, { - method: 'PUT', + request(billsURL, { + method: 'POST', headers: getHeaders(), body: JSON.stringify({ ...form, @@ -72,7 +92,7 @@ class Utilities extends Component { } createAccount = (form) => { - request(utilityURL, { + request(accountURL, { method: 'POST', headers: getHeaders(), body: JSON.stringify({ @@ -102,7 +122,7 @@ class Utilities extends Component { const accounts = this.state.accountLines; const accountNumberToRemove = accountLineToRemove.state.form.account_number; - request(`${utilityURL}${this.props.buildingId}?account_number=${accountNumberToRemove}`, { + request(`${accountURL}${this.props.buildingId}?account_number=${accountNumberToRemove}`, { method: 'DELETE', headers: getHeaders(), }).then((res) => { diff --git a/src/utils/rest_services.js b/src/utils/rest_services.js index 4f2a2c16..a3353078 100644 --- a/src/utils/rest_services.js +++ b/src/utils/rest_services.js @@ -10,4 +10,5 @@ const utilityService = process.env.REACT_APP_UTILITY_SERVICE; export const buildingsURL = `${buildingService}/building/`; export const turkURL = `${buildingService}/turkhit/`; -export const utilityURL = `${utilityService}/scraper/`; +export const accountURL = `${utilityService}/account/`; +export const billsURL = `${utilityService}/bills/`; -- GitLab