{
  "name": "Register_Transaction",
  "nodes": [
    {
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "operation"
            },
            {
              "name": "value",
              "type": "number"
            },
            {
              "name": "category"
            },
            {
              "name": "payment_method"
            },
            {
              "name": "registered_at"
            },
            {
              "name": "description"
            },
            {
              "name": "tenant_id"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        -2040,
        40
      ],
      "id": "1e52c656-9a06-43f7-adb5-65222559ba5c",
      "name": "Input"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.operation }}",
                    "rightValue": "input",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "6df1e742-1def-49d0-96ab-40bac09b4ea3"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Input"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "497e952d-1163-4624-be8e-dd12e8b43d55",
                    "leftValue": "={{ $json.operation }}",
                    "rightValue": "output",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Output"
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        -1860,
        40
      ],
      "id": "be9466e5-ccc9-4bc6-b05a-7722523d377e",
      "name": "Switch"
    },
    {
      "parameters": {
        "tableId": "transactions",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "value",
              "fieldValue": "={{ $json.value }}"
            },
            {
              "fieldId": "category",
              "fieldValue": "={{ $json.category }}"
            },
            {
              "fieldId": "description",
              "fieldValue": "={{ $json.description }}"
            },
            {
              "fieldId": "tenant_id",
              "fieldValue": "={{ $json.tenant_id }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        -1640,
        -120
      ],
      "id": "c92fd6af-7ef0-4077-988b-e95045d7384e",
      "name": "Register input",
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "tableId": "transactions",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "value",
              "fieldValue": "={{ $json.value*-1 }}"
            },
            {
              "fieldId": "category",
              "fieldValue": "={{ $json.category }}"
            },
            {
              "fieldId": "description",
              "fieldValue": "={{ $json.description }}"
            },
            {
              "fieldId": "tenant_id",
              "fieldValue": "={{ $json.tenant_id }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        -1640,
        40
      ],
      "id": "3dfea227-9ae5-4543-b8eb-31df9758cb9f",
      "name": "Register output",
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "errorMessage": "Error on register this operation, please provide input or output values correctly."
      },
      "type": "n8n-nodes-base.stopAndError",
      "typeVersion": 1,
      "position": [
        -1640,
        200
      ],
      "id": "b85a1de5-e1df-4161-a772-f726d85a7f0d",
      "name": "Stop and Error"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.1,
      "position": [
        -1380,
        -40
      ],
      "id": "001cacad-6a3c-4947-89cd-3ec5e4e1b64f",
      "name": "Merge"
    },
    {
      "parameters": {
        "operation": "get",
        "tableId": "balances",
        "filters": {
          "conditions": [
            {
              "keyName": "created_at",
              "keyValue": "={{ DateTime.fromISO($json.registered_at).format('yyyy-MM-dd') }}"
            },
            {
              "keyName": "tenant_id",
              "keyValue": "={{ $json.tenant_id }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        -1200,
        -40
      ],
      "id": "46811aeb-8425-4d76-b367-5161e6f9b011",
      "name": "Get balance",
      "alwaysOutputData": true,
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "d7111546-6ec5-4246-95cd-33c9ef2a25e3",
              "leftValue": "={{ $json.keys().length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -1020,
        -40
      ],
      "id": "50984e8d-2d1e-42bd-9767-fd81ea036e03",
      "name": "New Balance?"
    },
    {
      "parameters": {
        "tableId": "balances",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "value",
              "fieldValue": "={{ $('Merge').item.json.value }}"
            },
            {
              "fieldId": "created_at",
              "fieldValue": "={{ DateTime.fromISO($('Merge').item.json.registered_at).format('yyyy-MM-dd') }}"
            },
            {
              "fieldId": "tenant_id",
              "fieldValue": "={{ $('Merge').item.json.tenant_id }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        -800,
        -140
      ],
      "id": "8d48c282-c11f-4fe2-aeb5-c581ca802278",
      "name": "Create new balance",
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "tableId": "balances",
        "filters": {
          "conditions": [
            {
              "keyName": "id",
              "condition": "eq",
              "keyValue": "={{ $json.id }}"
            }
          ]
        },
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "value",
              "fieldValue": "={{ $json.value + $('Merge').item.json.value }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        -800,
        60
      ],
      "id": "36d4b26a-452d-4314-9060-137bc943a280",
      "name": "Update balance",
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Input": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Register input",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Register output",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Stop and Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Register input": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Register output": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Get balance",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get balance": {
      "main": [
        [
          {
            "node": "New Balance?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "New Balance?": {
      "main": [
        [
          {
            "node": "Create new balance",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Update balance",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "de4d8c34-439a-4e2a-a930-c99be265c8d7",
  "id": "moD0QA4XxTBcMhxx",
  "tags": []
}