function()

in src/governance/ccf-app/js/constitution/actions.js [189:303]


    function (args) {
      const cleanRoomPolicyMapName =
        "public:ccf.gov.policies.cleanroom-" + args.contractId;
      // Function to add  policy claims
      const add = (claims) => {
        let items = [];
        console.log(
          `Add claims to clean room policy: ${JSON.stringify(claims)}`
        );
        Object.keys(claims).forEach((key) => {
          let item = claims[key];
          // Make sure item is always an array
          if (!Array.isArray(item)) {
            item = [item];
          }

          let keyBuf = ccf.strToBuf(key);
          if (ccf.kv[cleanRoomPolicyMapName].has(keyBuf)) {
            // Key is already available
            const itemsBuf = ccf.kv[cleanRoomPolicyMapName].get(keyBuf);
            items = ccf.bufToStr(itemsBuf);
            console.log(`key: ${key} already exist: ${items}`);
            items = JSON.parse(items);
            if (typeof item[0] === "boolean") {
              //booleans are single value arrays
              items = item;
            } else {
              // loop through the input and add it to the existing set
              item.forEach((i) => {
                items.push(i);
              });
            }
          } else {
            // set single value
            items = item;
          }

          // prepare and store items
          let jsonItems = JSON.stringify(items);
          let jsonItemsBuf = ccf.strToBuf(jsonItems);
          console.log(
            `Voted clean room policy item. Key: ${key}, value: ${jsonItems}`
          );
          ccf.kv[cleanRoomPolicyMapName].set(keyBuf, jsonItemsBuf);
        });
      };

      // Function to remove clean room policy claims
      const remove = (claims) => {
        let items = [];
        console.log(
          `Remove claims to clean room policy: ${JSON.stringify(claims)}`
        );
        Object.keys(claims).forEach((key) => {
          let item = claims[key];
          // Make sure item is always an array
          if (!Array.isArray(item)) {
            item = [item];
          }

          let keyBuf = ccf.strToBuf(key);
          if (ccf.kv[cleanRoomPolicyMapName].has(keyBuf)) {
            // Key must be available
            const itemsBuf = ccf.kv[cleanRoomPolicyMapName].get(keyBuf);
            items = ccf.bufToStr(itemsBuf);
            console.log(`key: ${key} exist: ${items}`);
            items = JSON.parse(items);
            if (typeof item[0] === "boolean") {
              //booleans are single value arrays, removing will remove the whole key
              ccf.kv[cleanRoomPolicyMapName].delete(keyBuf);
            } else {
              // loop through the input and delete it from the existing set
              item.forEach((i) => {
                if (items.filter((ii) => ii === i).length === 0) {
                  throw new Error(
                    `Trying to remove value '${i}' from ${items} and it does not exist`
                  );
                }
                // Remove value from list
                const index = items.indexOf(i);
                if (index > -1) {
                  items.splice(index, 1);
                }
              });
              // update items
              if (items.length === 0) {
                ccf.kv[cleanRoomPolicyMapName].delete(keyBuf);
              } else {
                let jsonItems = JSON.stringify(items);
                let jsonItemsBuf = ccf.strToBuf(jsonItems);
                ccf.kv[cleanRoomPolicyMapName].set(keyBuf, jsonItemsBuf);
              }
            }
          } else {
            throw new Error(
              `Cannot remove values of ${key} because the key does not exist in the clean room policy claims`
            );
          }
        });
      };

      const type = args.type;
      switch (type) {
        case "add":
          add(args.claims);
          break;
        case "remove":
          remove(args.claims);
          break;
        default:
          throw new Error(
            `Clean Room Policy with type '${type}' is not supported`
          );
      }
    }