All files / fabric-contract-api/lib/annotations transaction.js

100% Statements 71/71
100% Branches 31/31
100% Functions 9/9
100% Lines 70/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145            2x 2x 2x 2x 2x   2x 6x 5x   5x   5x   5x 5x   5x   5x 5x   5x   5x   5x 5x 12x   12x 7x 7x     12x   5x 5x         5x 5x 1x   4x   4x     4x 1x 1x 2x 1x           4x 4x 3x 3x   1x     4x         4x 4x       2x 2x 2x   2x   2x   2x       2x 2x       2x 6x 5x   5x   5x   5x   5x 5x   5x 2x 2x   2x 1x 1x   1x             3x                 5x 5x      
/*
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const getParams = require('get-params');
const utils = require('./utils');
const Logger = require('../logger');
const logger = Logger.getLogger('./lib/annotations/transaction.js');
require('reflect-metadata');
 
module.exports.Transaction = function Transaction (commit = true) {
    return (target, propertyKey) => {
        logger.info('@Transaction args:', `Property Key -> ${propertyKey}, Commit -> ${commit},`, 'Target ->', target.constructor.name);
 
        const transactions = Reflect.getMetadata('fabric:transactions', target) || [];
 
        logger.debug('Existing fabric:transactions', transactions);
 
        const transaction = utils.findByValue(transactions, 'name', propertyKey);
        const paramNames = getParams(target[propertyKey]);
 
        logger.debug('@Transaction params:', `Property Key -> ${propertyKey}, Param Names ->  ${JSON.stringify(paramNames)},`, 'Target ->', target.constructor.name);
 
        const description = '';
        const contextType = target.createContext().constructor;
 
        logger.debug(`Transaction ${target} -> ${propertyKey} params`, paramNames);
 
        const paramTypes = Reflect.getMetadata('design:paramtypes', target, propertyKey) || [];
 
        let numRemoved = 0;
        const parameters = paramTypes.filter((paramType, paramIdx) => {
            const filter = paramType === contextType;
 
            if (filter) {
                logger.debug('@Transaction ignoring param as matched context type', `Property Key -> ${propertyKey}, Param Name ->, ${paramNames[paramIdx]},`, 'Target ->', target.constructor.name);
                paramNames.splice(paramIdx - numRemoved++, 1);
            }
 
            return !filter;
        }).map((paramType, paramIdx) => {
            const paramName = paramNames[paramIdx];
            const obj = {
                name: paramName,
                description
            };
 
            const type = typeof paramType === 'function' ? paramType.name : paramType.toString();
            if (type === 'Object') {
                throw new Error(`Type not properly specified for parameter ${paramName}, can not process pure Object types`);
            }
            obj.schema = utils.generateSchema(type);
 
            return obj;
        });
 
        if (transaction && transaction.parameters) {
            transaction.parameters.forEach((tParam) => {
                for (let i = 0; i < parameters.length; i++) {
                    if (parameters[i].name === tParam.name) {
                        parameters[i] = tParam;
                    }
                }
            });
        }
 
        const tag = [];
        if (commit) {
            tag.push('SUBMIT');
            tag.push('submitTx');
        } else {
            tag.push('EVALUATE');
        }
 
        utils.appendOrUpdate(transactions, 'name', propertyKey, {
            tag: tag,
            parameters: parameters
        });
 
        Reflect.defineMetadata('fabric:transactions', transactions, target);
        logger.debug('Updated fabric:transactions', transactions);
    };
};
 
module.exports.Returns = function Returns (returnType) {
    return (target, propertyKey) => {
        logger.info('@Returns args:', `, Property Key -> ${propertyKey}, Return Type -> ${returnType},`, 'Target ->', target.constructor.name);
 
        const transactions = Reflect.getMetadata('fabric:transactions', target) || [];
 
        logger.debug('Existing fabric:transactions', transactions);
 
        utils.appendOrUpdate(transactions, 'name', propertyKey, {
            returns: utils.generateSchema(returnType)
        });
 
        Reflect.defineMetadata('fabric:transactions', transactions, target);
        logger.debug('Updated fabric:transactions', transactions);
    };
};
 
module.exports.Param = function Param (paramName, paramType, description) {
    return (target, propertyKey) => {
        logger.info('@Param args:', `Property Key -> ${propertyKey}, Param Name -> ${paramName}, Param Type -> ${paramType}, Description -> ${description},`, 'Target ->', target.constructor.name);
 
        const transactions = Reflect.getMetadata('fabric:transactions', target) || [];
 
        logger.debug('Existing fabric:transactions', transactions);
 
        const transaction = utils.findByValue(transactions, 'name', propertyKey);
 
        const paramSchema = utils.generateSchema(paramType);
        const paramDesc = description || '';
 
        if (transaction && transaction.parameters) {
            logger.debug('Updating existing transaction parameters', transaction.parameters);
            const param = utils.findByValue(transaction.parameters, 'name', paramName);
 
            if (param) {
                param.schema = paramSchema;
                param.description = paramDesc;
            } else {
                transaction.parameters.push({
                    name: paramName,
                    description: paramDesc,
                    schema: paramSchema
                });
            }
        } else {
            utils.appendOrUpdate(transactions, 'name', propertyKey, {
                parameters: [{
                    name: paramName,
                    description: paramDesc,
                    schema: paramSchema
                }]
            });
        }
 
        Reflect.defineMetadata('fabric:transactions', transactions, target);
        logger.debug('Updated fabric:transactions', transactions);
    };
};