Last active
February 1, 2019 12:43
-
-
Save GeorgDangl/145f90164fdea9baa3db372bbd468c90 to your computer and use it in GitHub Desktop.
AVACloud Node Examples - www.dangl-it.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// App main entry point | |
(async () => { | |
await getOAuth2AccessToken(); | |
await executeAvaCloudExample(); | |
})(); | |
async function executeAvaCloudExample() { | |
if (!accessToken) { | |
console.log('No access token, exiting app.'); | |
return; | |
} | |
await transformGaebToExcel(); | |
await printProjectTotalPriceAndPositionCount(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getProjectTotalPrice(project: ProjectDto): number { | |
return project | |
.serviceSpecifications[0] | |
.totalPrice; | |
} | |
function getProjectPositionCount(project: ProjectDto): number { | |
const servSpec = project | |
.serviceSpecifications[0]; | |
const positionsCount = getPositionsInElementList(servSpec.elements); | |
return positionsCount; | |
} | |
function getPositionsInElementList(elements: IElementDto[]): number { | |
let positionsCount = 0; | |
elements.forEach(element => { | |
if (element instanceof PositionDto) { | |
positionsCount++; | |
} else if (element instanceof ServiceSpecificationGroupDto) { | |
if (element.elements) { | |
positionsCount += getPositionsInElementList(element.elements); | |
} | |
} | |
}); | |
return positionsCount; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function printProjectTotalPriceAndPositionCount() { | |
console.log('Transforming GAEB file to AVA Project...'); | |
const gaebConversionClient = new GaebConversionApi(); | |
gaebConversionClient.accessToken = accessToken; | |
const fileParam = getGaebFile(); | |
// The avaProject variable is of type ProjectDto and contains the unified project model | |
const avaProject = (await gaebConversionClient.gaebConversionConvertToAva(fileParam)).body; | |
const totalPrice = getProjectTotalPrice(avaProject); | |
console.log('Project total price (net): ' + totalPrice); | |
const countOfPositions = getProjectPositionCount(avaProject); | |
console.log('Count of positions: ' + countOfPositions); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function createNewGaebFile() { | |
const avaConversionApi = new AvaConversionApi(); | |
avaConversionApi.accessToken = accessToken; | |
const avaProject = { | |
serviceSpecifications: [ | |
{ | |
elements: [ | |
{ | |
elementTypeDiscriminator: 'PositionDto', | |
shortText: 'Concrete Wall', | |
unitTag: 'm²', | |
quantityComponents: [ | |
{ | |
formula: '10' | |
} | |
], | |
priceComponents: [ | |
{ | |
values: [ | |
{ | |
formula: '80' | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
] | |
}; | |
const gaebCreationResponse = await avaConversionApi.avaConversionConvertToGaeb(<ProjectDto><any>avaProject); | |
writeFileSync('CreatedGaebFile.X86', gaebCreationResponse.body); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getOAuth2AccessToken(): Promise<void> { | |
if (!clientId || !clientSecret) { | |
console.log('Please provide values for clientId and clientSecret. You can find more info in the tutorial at www.dangl-it.com or the AVACloud documenation.'); | |
return; | |
} | |
const clientCredentialsRequest = new Promise(function (resolve, reject) { | |
post('https://identity.dangl-it.com/connect/token', { | |
auth: { | |
username: clientId, | |
password: clientSecret | |
}, | |
body: 'grant_type=client_credentials&scope=avacloud', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
}, function (err, resp, body) { | |
if (err) { | |
console.log('Error'); | |
reject(err); | |
} else { | |
resolve(body); | |
} | |
}); | |
}); | |
try { | |
const clientCredentialsResult = await clientCredentialsRequest; | |
accessToken = JSON.parse(<string>clientCredentialsResult)['access_token']; | |
if (!accessToken) { | |
console.log('Failed to obtain an access token. Have you read the documentation and set up your OAuth2 client?'); | |
} | |
} catch { | |
console.log('Failed to obtain an access token. Have you read the documentation and set up your OAuth2 client?'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function transformGaebToExcel() { | |
console.log('Transforming GAEB file to Excel...'); | |
const gaebConversionClient = new GaebConversionApi(); | |
gaebConversionClient.accessToken = accessToken; | |
const fileParam = getGaebFile(); | |
const conversionResult = await gaebConversionClient.gaebConversionConvertToExcel(fileParam, true, true, 'de'); | |
console.log('Saving Excel conversion result to:'); | |
console.log(gaebInputFile + '.xlsx'); | |
writeFileSync(gaebInputFile + '.xlsx', conversionResult.body); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getGaebFile(): FileParameter { | |
const gaebFileBuffer = readFileSync(gaebInputFile); | |
const fileParam: FileParameter = { | |
value: gaebFileBuffer, | |
options: { | |
filename: 'GAEBXML_EN.X86', | |
contentType: 'application/octet-stream' | |
} | |
}; | |
return fileParam; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "avacloud-demo-node", | |
"version": "0.0.0", | |
"private": true, | |
"scripts": { | |
"build": "tsc", | |
"start": "tsc && node app.js" | |
}, | |
"dependencies": { | |
"@dangl/avacloud-client-node": "1.0.10" | |
}, | |
"devDependencies": { | |
"typescript": "^2.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment