Home > Software design >  JOLT apply different transformations based on success of the call
JOLT apply different transformations based on success of the call

Time:01-05

I'm calling a rest web service and i receive the following responses depending if the call was successful or not :

{
  "success": true,
  "data": {
    "series": "XX/32/V32/LM",
    "number": 242,
    "end_date": "31/12/2023",
    "premium": "2309.68",
    "premium_net": "2286.58",
    "total_premium": 2494.46,
    "commission": 1,
    "installments": [
      {
        "number": 1,
        "due_date": "30/12/2022",
        "value": "2494.46",
        "currency": "RON"
      }
    ],
    "reference_premium": 1061,
    "direct_settlement_cover": 1,
    "direct_settlement": 184.78,
    "direct_settlement_net": 182.93,
    "bm": "B0",
    "exclusion_countries": [
      "BY",
      "IL",
      "IR",
      "MA",
      "RUS",
      "TN",
      "UA"
    ]
  },
  "message": "Polița a fost emisă cu succes"
}

{
  "success": false,
  "message": "Eroare validare date",
  "data": {
    "pay_document": [
      "Trebuie să menționați modalitatea de plată"
    ]
  }
}

And i wrote the following operations that work individually

[
  {
    "operation": "shift",
    "spec": {
      "message": "body.info_message",
      "data": {
        "#1": "body.good_id",
        "series": "body.policy_series",
        "number": "body.policy_number",
        "end_date": "body.policy_end_date",
        "direct_settlement_cover": {
          "1": {
            "#Decontare directa": "clauses[0].clause_name",
            "#1": "clauses[0].clause_id"
          }
        },
        "direct_settlement": "clauses[0].premium",
        "installments": {
          "*": {
            "number": "body.installments[&1].number",
            "value": "body.installments[&1].amount",
            "currency": "body.installments[&1].currency",
            "due_date": "body.installments[&1].due_date"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "body": {
        "good_id": "=toInteger"
      }
    }
  }
]


[
  {
    "operation": "shift",
    "spec": {
      "message": "body.info_message",
      "data": {
        "*": {
          "*": "body.error_message"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "error_message": "=join(' | ',@(1,&))"
      }
    }
  }
]

Now i'm trying to combine those two and display the error messages if success is false or return the model to be saved. i tried to place success inside my transformation but it keeps returning null. what is the best practice in this regard...

CodePudding user response:

You can use this spec:

I'm just adding the first shift operator and changing the data key with true and false in your code. So the final code is like the below:

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": "@(1,success)"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "message": "body.info_&",
      "true": {
        "#1": "body.good_id",
        "series": "body.policy_&",
        "number": "body.policy_&",
        "end_date": "body.policy_&",
        "direct_settlement_cover": {
          "1": {
            "#Decontare directa": "clauses[0].clause_name",
            "#1": "clauses[0].clause_id"
          }
        },
        "direct_settlement": "clauses[0].premium",
        "installments": {
          "*": {
            "*": "body.&2[&1].&",
            "value": "body.&2[&1].amount"
          }
        }
      },
      "false": {
        "*": {
          "*": "body.error_message"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "good_id": "=toInteger",
        "error_message": "=join(' | ',@(1,&))"
      }
    }
  }
]
  • Related