squid-js/src/utils/DDOHelpers.ts

50 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-06-20 00:20:09 +02:00
import { DDO } from '../ddo/DDO'
import {
ServiceAgreementTemplateCondition,
ServiceAgreementTemplateParameter
} from '../ddo/ServiceAgreementTemplate'
2019-06-20 00:20:09 +02:00
function fillParameterWithDDO(
parameter: ServiceAgreementTemplateParameter,
ddo: DDO
): ServiceAgreementTemplateParameter {
const getValue = name => {
switch (name) {
2019-06-20 00:20:09 +02:00
case 'amount':
case 'price':
return String(
ddo.findServiceByType('Metadata').metadata.base.price
)
case 'assetId':
case 'documentId':
case 'documentKeyId':
return ddo.shortId()
2019-06-20 00:20:09 +02:00
case 'rewardAddress':
return ddo.publicKey[0].owner
}
2019-06-20 00:20:09 +02:00
return ''
}
2019-06-20 00:20:09 +02:00
const value = getValue(parameter.name.replace(/^_/, ''))
2019-06-20 00:20:09 +02:00
return { ...parameter, value }
}
/**
* Fill some static parameters that depends on the metadata.
* @param {ServiceAgreementTemplateCondition[]} conditions Conditions to fill.
* @param {DDO} ddo DDO related to this conditions.
* @return {ServiceAgreementTemplateCondition[]} Filled conditions.
*/
2019-06-20 00:20:09 +02:00
export function fillConditionsWithDDO(
conditions: ServiceAgreementTemplateCondition[],
ddo: DDO
): ServiceAgreementTemplateCondition[] {
return conditions.map(condition => ({
...condition,
parameters: condition.parameters.map(parameter => ({
...fillParameterWithDDO(parameter, ddo)
}))
2019-06-20 00:20:09 +02:00
}))
}