Skip to content

Commit

Permalink
Merge pull request #360 from csmartinsfct/hotfix/airtable_error_handling
Browse files Browse the repository at this point in the history
Airtable and list asset error handling
  • Loading branch information
csmartinsfct committed Jan 16, 2019
2 parents f1baea7 + af352b7 commit 84f6331
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 71 deletions.
152 changes: 101 additions & 51 deletions src/apis/brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ export const createAsset = async params =>
collateralMyb,
collateralPercentage,
amountToBeRaisedInUSD,
userAddress,
managerPercentage,
} = params;
const collateral = window.web3js.utils.toWei(collateralMyb.toString(), 'ether');
debug(collateral)
Expand All @@ -377,8 +379,8 @@ export const createAsset = async params =>

const futureAssetId = generateAssetId(
window.web3js,
params.userAddress,
params.managerPercentage,
userAddress,
managerPercentage,
amountToBeRaisedInUSD,
installerId,
assetType,
Expand All @@ -395,7 +397,7 @@ export const createAsset = async params =>
randomBlockNumber.toString(),
ipfsHash,
)
.send({ from: params.userAddress })
.send({ from: userAddress })
.on('transactionHash', (transactionHash) => {
updateNotification(id, {
listAssetProps: {
Expand All @@ -414,73 +416,121 @@ export const createAsset = async params =>
resolve(false);
})
.then( async(receipt) => {
//TODO add error handling
if(receipt.status){
const response = await axios.post(UPDATE_ASSETS_URL, {
assetId: futureAssetId,
assetName: assetName,
country: country,
city: city,
collateral: collateralMyb,
collateralPercentage,
});

if(fileList.length > 0){
let data = new FormData();
data.append('assetId', futureAssetId);
for(const file of fileList){
data.append('file', file.originFileObj);
const numberOfInternalActions = 2;
const numberOfInternalActionsWithFileUpload = numberOfInternalActions + 1;
const filesUploaded = fileList.length > 0;
const requiredCallsToInternalActions = filesUploaded ? numberOfInternalActionsWithFileUpload : numberOfInternalActions;
let counterCallsToInternalActions = 0;

// we need to perform a few actions before declaring the listing of the asset as successful
const performInternalAction = () => {
counterCallsToInternalActions++;
if(counterCallsToInternalActions === requiredCallsToInternalActions){
onSuccess(() => {
updateNotification(id, {
listAssetProps: {
assetName: assetName,
assetId: futureAssetId,
},
status: 'success',
})
}, futureAssetId)

resolve(receipt.status);
}
axios.post(S3_UPLOAD_URL,
data, {
headers: {
'Content-Type': 'multipart/form-data'
}
}
).then((response) => {
debug('success');
})
.catch((err) => {
debug('fail');
});
}

onSuccess(() => {
axios.post(MYBIT_API_COLLATERAL, {
address: params.userAddress,
escrow: collateral,
assetId: futureAssetId,
}).catch((error) => {
debug(error);
})

updateNotification(id, {
listAssetProps: {
assetName: assetName,
assetId: futureAssetId,
},
status: 'success',
})
}, futureAssetId)
updateAirTableWithNewAsset(futureAssetId, assetName, country, city, collateralMyb, collateralPercentage, performInternalAction)
createEntryForNewCollateral(userAddress, collateral, futureAssetId, performInternalAction);
filesUploaded && uploadFilesToAWS(futureAssetId, fileList, performInternalAction);

} else {
updateNotification(id, {
listAssetProps: {
assetName: assetName,
},
status: 'error',
});
resolve(assetCreationResponse);
}

debug(receipt)
resolve(receipt.status);
});

resolve(assetCreationResponse);
} catch (err) {
reject(err);
}
});

const uploadFilesToAWS = async (
assetId,
fileList,
performInternalAction,
) => {
try{
let data = new FormData();
data.append('assetId', assetId);
for(const file of fileList){
data.append('file', file.originFileObj);
}
await axios.post(S3_UPLOAD_URL,
data, {
headers: {
'Content-Type': 'multipart/form-data'
}
}
)

performInternalAction();
} catch(err){
setTimeout(() => uploadFilesToAWS(assetId, fileList, performInternalAction), 5000);
debug(err);
}
}

const createEntryForNewCollateral = async (
address,
escrow,
assetId,
performInternalAction,
) => {
try{
await axios.post(MYBIT_API_COLLATERAL, {
address,
escrow,
assetId,
})
performInternalAction();
} catch(err){
setTimeout(() => createEntryForNewCollateral(address, escrow, assetId, performInternalAction), 5000);
debug(err);
}
}

const updateAirTableWithNewAsset = async (
assetId,
assetName,
country,
city,
collateral,
collateralPercentage,
performInternalAction,
) => {
try{
await axios.post(UPDATE_ASSETS_URL, {
assetId,
assetName,
country,
city,
collateral,
collateralPercentage,
});
performInternalAction();
} catch(err){
setTimeout(() => updateAirTableWithNewAsset(assetId, assetName, country, city, collateral, collateralPercentage, performInternalAction), 5000);
debug(err);
}
}

const checkTransactionConfirmation = async (
transactionHash,
resolve,
Expand Down
53 changes: 33 additions & 20 deletions src/components/BlockchainInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,28 +417,35 @@ class BlockchainInfo extends React.Component {
}

async getAssetsFromAirTable(){
const request = await fetch(AIRTABLE_ASSETS_URL);
if(request.status !== 200){
//TODO AIRTABLE DOWN - handle loader
try{
const request = await fetch(AIRTABLE_ASSETS_URL);
const json = await request.json();
const { records } = json;
const assets = records.filter(({ fields }) => Object.keys(fields).length >= AIRTABLE_ASSETS_NUMBER_OF_FIELDS).map(this.processAssetsFromAirTable)
const assetsById = this.processAssetsByIdFromAirTable(records.filter(({ fields }) => Object.keys(fields).length >= AIRTABLE_ASSETS_NUMBER_OF_FIELDS), assets);
this.setState({
assetsAirTable: assets,
assetsAirTableById: assetsById,
});
}catch(err){
setTimeout(this.getAssetsFromAirTable, 5000);
debug(err);
}
const json = await request.json();
const { records } = json;
const assets = records.filter(({ fields }) => Object.keys(fields).length >= AIRTABLE_ASSETS_NUMBER_OF_FIELDS).map(this.processAssetsFromAirTable)
const assetsById = this.processAssetsByIdFromAirTable(records.filter(({ fields }) => Object.keys(fields).length >= AIRTABLE_ASSETS_NUMBER_OF_FIELDS), assets);
this.setState({
assetsAirTable: assets,
assetsAirTableById: assetsById,
});
}

async getCategoriesFromAirTable(){
const request = await fetch(AIRTABLE_CATEGORIES_URL);
const json = await request.json();
const { records } = json;
const categories = this.processCategoriesFromAirTable(records.filter(({ fields }) => Object.keys(fields).length === AIRTABLE_CATEGORIES_NUMBER_OF_FIELDS));
this.setState({
categoriesAirTable: categories,
})
try{
const request = await fetch(AIRTABLE_CATEGORIES_URL);
const json = await request.json();
const { records } = json;
const categories = this.processCategoriesFromAirTable(records.filter(({ fields }) => Object.keys(fields).length === AIRTABLE_CATEGORIES_NUMBER_OF_FIELDS));
this.setState({
categoriesAirTable: categories,
})
}catch(err){
setTimeout(this.getCategoriesFromAirTable, 5000);
debug(err);
}
}

getCategoriesForAssets(country, city){
Expand Down Expand Up @@ -705,14 +712,20 @@ class BlockchainInfo extends React.Component {
}

async fetchAssets(updateNotification, updateUserListingAsset, assetId) {
if (!this.state.prices.ether) {
const {
assetsAirTableById,
prices,
categoriesAirTable,
user,
} = this.state;
if (!prices.ether || !assetsAirTableById || !categoriesAirTable) {
setTimeout(this.fetchAssets, 10000);
return;
}
this.setState({
usingServer: false,
});
await Brain.fetchAssets(this.state.user, this.state.prices.ether.price, this.state.assetsAirTableById, this.state.categoriesAirTable)
await Brain.fetchAssets(user, prices.ether.price, assetsAirTableById, categoriesAirTable)
.then( async (response) => {
const updatedAssets = await this.pullFileInfoForAssets(response);
this.setState({
Expand Down

0 comments on commit 84f6331

Please sign in to comment.