2019-06-20 00:20:09 +02:00
|
|
|
import { DDO } from '../ddo/DDO'
|
2019-11-15 00:00:10 +01:00
|
|
|
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 {
|
2020-05-19 16:15:56 +02:00
|
|
|
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':
|
2019-09-09 12:18:54 +02:00
|
|
|
return String(ddo.findServiceByType('metadata').attributes.main.price)
|
2019-06-20 00:20:09 +02:00
|
|
|
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[] {
|
2020-05-19 16:15:56 +02:00
|
|
|
return conditions.map((condition) => ({
|
2019-06-20 00:20:09 +02:00
|
|
|
...condition,
|
2020-05-19 16:15:56 +02:00
|
|
|
parameters: condition.parameters.map((parameter) => ({
|
2019-06-20 00:20:09 +02:00
|
|
|
...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
|
|
|
}
|