2019-06-20 00:20:09 +02:00
|
|
|
import { DDO } from '../ddo/DDO'
|
|
|
|
import {
|
|
|
|
ServiceAgreementTemplateCondition,
|
|
|
|
ServiceAgreementTemplateParameter
|
|
|
|
} from '../ddo/ServiceAgreementTemplate'
|
2019-03-11 22:48:17 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
function fillParameterWithDDO(
|
|
|
|
parameter: ServiceAgreementTemplateParameter,
|
|
|
|
ddo: DDO
|
|
|
|
): ServiceAgreementTemplateParameter {
|
|
|
|
const getValue = name => {
|
2019-03-11 22:48:17 +01:00
|
|
|
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':
|
2019-03-11 22:48:17 +01:00
|
|
|
return ddo.shortId()
|
2019-06-20 00:20:09 +02:00
|
|
|
case 'rewardAddress':
|
2019-04-29 13:31:03 +02:00
|
|
|
return ddo.publicKey[0].owner
|
2019-03-11 22:48:17 +01:00
|
|
|
}
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
return ''
|
2019-03-11 22:48:17 +01:00
|
|
|
}
|
2019-06-20 00:20:09 +02:00
|
|
|
const value = getValue(parameter.name.replace(/^_/, ''))
|
2019-03-11 22:48:17 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
return { ...parameter, value }
|
2019-03-11 22:48:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-03-11 22:48:17 +01:00
|
|
|
}))
|
2019-06-20 00:20:09 +02:00
|
|
|
}))
|
2019-03-11 22:48:17 +01:00
|
|
|
}
|