updateAccountPolicyLayer method

  1. @override
Future<BusinessAccountPolicyLayerUpdateResult> updateAccountPolicyLayer(
  1. String businessAccountId,
  2. BusinessAccountPolicyLayerEdit request, [
  3. WaasPolicyLayerResponse? layer,
  4. String? proposalId,
  5. bool? autoExecute,
  6. SignedBusinessAccountIntent? signedIntent,
])
override

Updates a business-account policy layer, signing an updatePolicyLayer intent when the layer carries a digest and RBAC version, and returning actionRequired when approvals are needed.

Implementation

@override
Future<BusinessAccountPolicyLayerUpdateResult> updateAccountPolicyLayer(String businessAccountId, BusinessAccountPolicyLayerEdit request, [WaasPolicyLayerResponse? layer, String? proposalId, bool? autoExecute, SignedBusinessAccountIntent? signedIntent]) async {
  Future<BusinessAccountPolicyLayerUpdateResult> instrumentationOperation() async {
    final operationScope = _state.flowScope.capture(replace: false);
    await operationScope.ready();
    if (!_state.initialized) {
      throw DynamicStateException("call initialize() before using the client");
    }
    if (_state.token == null) {
      throw DynamicStateException("not authenticated — sign in first");
    }
    var toSend = signedIntent;
    Map<String, Object?>? operationParsed;
    try {
      final decoded = jsonDecode(jsonEncode(request.toJson()));
      operationParsed = decoded is Map<String, dynamic> ? decoded : null;
    } catch (_) {
      operationParsed = null;
    }
    final operation = operationParsed;
    if (operation == null) {
      throw DynamicParseException("unexpected policy-layer response shape");
    }
    if ((toSend == null && (layer?.layerDigest != null)) && (layer?.rbacVersion != null)) {
      final Map<String, Object?> intentToSign = <String, Object?>{"actionType": jsonDecode(jsonEncode("updatePolicyLayer")), "autoExecute": jsonDecode(jsonEncode((autoExecute ?? true))), "businessAccountId": jsonDecode(jsonEncode(businessAccountId)), "createdAt": jsonDecode(jsonEncode(DateTime.now().toUtc().toIso8601String())), "intentId": jsonDecode(jsonEncode(dynRandomUuid())), "initiatorUserId": jsonDecode(jsonEncode((_state.user?.id ?? ""))), "payload": jsonDecode(jsonEncode(<String, Object?>{"layerUpdate": jsonDecode(jsonEncode(<String, Object?>{"currentLayerDigest": jsonDecode(jsonEncode(layer?.layerDigest)), "environmentId": jsonDecode(jsonEncode(_config.environmentId)), "layerId": jsonDecode(jsonEncode(layer?.layerId)), "operation": jsonDecode(jsonEncode(operation)), "rbacVersion": jsonDecode(jsonEncode(layer?.rbacVersion))}))}))};
      operationScope.check();
      final intentSignature = await _sessionSigner.sign(dynCanonicalJson(intentToSign));
      operationScope.check();
      toSend = SignedBusinessAccountIntent(intent: intentToSign, intentSignature: intentSignature);
    }
    operationScope.commit(() {
        _state.elevatedAccessTokens = _state.elevatedAccessTokens?.map((validElevation) => (validElevation.expiresAtMs > DateTime.now().millisecondsSinceEpoch ? validElevation : null)).where((v) => v != null).map((v) => v!).toList() ?? const [];
        _state.elevatedAccessTokensChanged.emit(_state.elevatedAccessTokens);
    });
    final elevationGrant = firstOrNull(_state.elevatedAccessTokens?.map((matchingElevation) => ((matchingElevation.scopes.split(' ').where((word) => word.isNotEmpty).toList()).contains("waas:policy:update") ? matchingElevation : null)).where((v) => v != null).map((v) => v!).toList() ?? const []);
    final elevation = elevationGrant?.token;
    operationScope.commit(() {
        _state.elevatedAccessTokens = _state.elevatedAccessTokens?.map((retainedElevation) => (!retainedElevation.singleUse || retainedElevation.token != elevation ? retainedElevation : null)).where((v) => v != null).map((v) => v!).toList() ?? const [];
        _state.elevatedAccessTokensChanged.emit(_state.elevatedAccessTokens);
    });
    operationScope.check();
    final updateGovernedBusinessAccountPolicyLayerBody = <String, dynamic>{"op": operation["op"]};
    if (request.rule != null) updateGovernedBusinessAccountPolicyLayerBody["rule"] = request.rule;
    if (request.ruleId != null) updateGovernedBusinessAccountPolicyLayerBody["ruleId"] = request.ruleId;
    if (request.operations != null) updateGovernedBusinessAccountPolicyLayerBody["operations"] = request.operations;
    if (request.rules != null) updateGovernedBusinessAccountPolicyLayerBody["rules"] = request.rules;
    if (proposalId != null) updateGovernedBusinessAccountPolicyLayerBody["proposalId"] = proposalId;
    if (autoExecute != null) updateGovernedBusinessAccountPolicyLayerBody["autoExecute"] = autoExecute;
    if (toSend?.intent != null) updateGovernedBusinessAccountPolicyLayerBody["intent"] = toSend?.intent;
    if (toSend?.intentSignature != null) updateGovernedBusinessAccountPolicyLayerBody["intentSignature"] = toSend?.intentSignature;
    final response = await _api.requestRaw(method: "PATCH", path: "/businessAccounts/${Uri.encodeComponent(businessAccountId)}/policy-layer", query: const {}, body: updateGovernedBusinessAccountPolicyLayerBody, extraHeaders: <(String, String?)>[("x-dyn-elevated-access-token", elevation)], token: _state.token, sessionPublicKey: _state.sessionPublicKey);
    operationScope.check();
    if (response.status == 202) {
      BusinessAccountPolicyLayerActionRequired? pendingParsed;
      try {
        final decoded = jsonDecode(response.body);
        pendingParsed = decoded is Map<String, dynamic> ? BusinessAccountPolicyLayerActionRequired.fromJson(decoded) : null;
      } catch (_) {
        pendingParsed = null;
      }
      final pending = pendingParsed;
      if (pending == null) {
        throw DynamicParseException("unexpected policy-layer response shape");
      }
      if (pending.reason != "APPROVALS_REQUIRED") {
        throw DynamicParseException("unexpected policy-layer response shape");
      }
      return BusinessAccountPolicyLayerUpdateResultActionRequired(pending);
    }
    WaasPolicyLayerResponse? updatedLayerParsed;
    try {
      final decoded = jsonDecode(response.body);
      updatedLayerParsed = decoded is Map<String, dynamic> ? WaasPolicyLayerResponse.fromJson(decoded) : null;
    } catch (_) {
      updatedLayerParsed = null;
    }
    final updatedLayer = updatedLayerParsed;
    if (updatedLayer == null) {
      throw DynamicParseException("unexpected policy-layer response shape");
    }
    return BusinessAccountPolicyLayerUpdateResultLayer(updatedLayer);
  }
  return await _api.instrumentation.runAsync("policyEngine.updateAccountPolicyLayer", instrumentationOperation);
}