{
  "$schema": "./.schemas/page.schema.json",
  "title": "Net Worth",
  "icon": "chart.line.uptrend.xyaxis",
  "sources": {
    "money": {
      "url": "https://api.lunchmoney.dev",
      "secret": "lunch-money",
      "every": "1h",
      "requests": {
        "banks": "/v2/plaid_accounts",
        "manual": "/v2/manual_accounts",
        "me": "/v2/me"
      },
      "derive": [
        {
          "name": "cash",
          "value": {
            "add": [
              {
                "sum": "to_base",
                "of": "banks.plaid_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "cash"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              },
              {
                "sum": "to_base",
                "of": "manual.manual_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "cash"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "name": "investments",
          "value": {
            "add": [
              {
                "sum": "to_base",
                "of": "banks.plaid_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "investment"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              },
              {
                "sum": "to_base",
                "of": "manual.manual_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "investment"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "name": "other_assets",
          "value": {
            "add": [
              {
                "sum": "to_base",
                "of": "banks.plaid_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "other asset"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              },
              {
                "sum": "to_base",
                "of": "manual.manual_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "other asset"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              },
              {
                "sum": "to_base",
                "of": "manual.manual_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "real estate"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              },
              {
                "sum": "to_base",
                "of": "manual.manual_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "vehicle"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "name": "cards",
          "value": {
            "add": [
              {
                "sum": "to_base",
                "of": "banks.plaid_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "credit"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              },
              {
                "sum": "to_base",
                "of": "manual.manual_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "credit"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "name": "loans",
          "value": {
            "add": [
              {
                "sum": "to_base",
                "of": "banks.plaid_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "loan"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              },
              {
                "sum": "to_base",
                "of": "manual.manual_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "loan"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              },
              {
                "sum": "to_base",
                "of": "manual.manual_accounts",
                "where": {
                  "all": [
                    {
                      "path": "type",
                      "equals": "other liability"
                    },
                    {
                      "path": "status",
                      "not": "closed"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "name": "assets",
          "value": {
            "add": [
              "cash",
              "investments",
              "other_assets"
            ]
          }
        },
        {
          "name": "debts",
          "value": {
            "add": [
              "cards",
              "loans"
            ]
          }
        },
        {
          "name": "net",
          "value": {
            "subtract": [
              "assets",
              "debts"
            ]
          }
        },
        {
          "name": "linked",
          "value": {
            "count": "banks.plaid_accounts",
            "where": {
              "path": "status",
              "not_in": [
                "inactive",
                "closed",
                "deactivated"
              ]
            }
          }
        },
        {
          "name": "attention",
          "value": {
            "count": "banks.plaid_accounts",
            "where": {
              "path": "status",
              "not_in": [
                "active",
                "syncing",
                "inactive",
                "closed",
                "deactivated"
              ]
            }
          }
        },
        {
          "name": "syncing",
          "value": {
            "count": "banks.plaid_accounts",
            "where": {
              "path": "status",
              "equals": "syncing"
            }
          }
        },
        {
          "name": "asset_parts",
          "value": {
            "each": [
              "banks.plaid_accounts",
              "manual.manual_accounts"
            ],
            "where": {
              "all": [
                {
                  "path": "type",
                  "in": [
                    "cash",
                    "investment",
                    "other asset",
                    "real estate",
                    "vehicle"
                  ]
                },
                {
                  "path": "status",
                  "not": "closed"
                }
              ]
            },
            "fields": {
              "part": {
                "if": {
                  "path": "type",
                  "in": [
                    "other asset",
                    "real estate",
                    "vehicle"
                  ]
                },
                "then": {
                  "path": "name"
                },
                "else": {
                  "if": {
                    "path": "type",
                    "equals": "investment"
                  },
                  "then": {
                    "template": "Investments"
                  },
                  "else": {
                    "template": "Cash"
                  }
                }
              },
              "value": "to_base"
            }
          }
        },
        {
          "name": "asset_mix",
          "value": {
            "each": "asset_parts",
            "group": "part",
            "fields": {
              "label": "key",
              "value": {
                "sum": "value",
                "of": "rows"
              }
            }
          }
        }
      ]
    },
    "flow": {
      "url": "https://api.lunchmoney.dev",
      "secret": "lunch-money",
      "every": "1h",
      "requests": {
        "summary": "/v2/summary?start_date={date.year_start}&end_date={date.month_end}&include_occurrences=true",
        "cats": "/v2/categories?format=flattened",
        "me": "/v2/me"
      }
    }
  },
  "cards": [
    {
      "id": "overview",
      "type": "tabs",
      "same_height": true,
      "tabs": [
        {
          "label": "Net Worth",
          "cards": [
            {
              "id": "trend",
              "type": "sparkline",
              "unit": "USD",
              "source": {
                "ref": "money",
                "map": {
                  "points": {
                    "history": "net",
                    "every": "1d",
                    "keep": 60
                  },
                  "unit": "me.primary_currency"
                }
              }
            }
          ]
        },
        {
          "label": "Income vs Spending",
          "cards": [
            {
              "id": "cashflow",
              "type": "columns",
              "unit": "USD",
              "series": [
                "Income",
                "Spending"
              ],
              "items": [
                {
                  "label": "Jan",
                  "values": [
                    0,
                    0
                  ]
                }
              ],
              "source": {
                "ref": "flow",
                "map": {
                  "items": {
                    "each": "summary.categories",
                    "lookup": {
                      "from": "cats.categories",
                      "by": "id",
                      "on": "category_id",
                      "as": "category"
                    },
                    "where": {
                      "all": [
                        {
                          "path": "category.is_group",
                          "not": true
                        },
                        {
                          "path": "category.exclude_from_totals",
                          "not": true
                        }
                      ]
                    },
                    "flatten": "occurrences",
                    "group": "start_date",
                    "limit": 12,
                    "fields": {
                      "label": {
                        "template": "{key|month}"
                      },
                      "values[0]": {
                        "add": [
                          {
                            "sum": "other_activity",
                            "of": "rows",
                            "where": {
                              "path": "parent.category.is_income",
                              "equals": true
                            }
                          },
                          {
                            "sum": "recurring_activity",
                            "of": "rows",
                            "where": {
                              "path": "parent.category.is_income",
                              "equals": true
                            }
                          }
                        ],
                        "round": 0,
                        "scale": -1
                      },
                      "values[1]": {
                        "add": [
                          {
                            "sum": "other_activity",
                            "of": "rows",
                            "where": {
                              "path": "parent.category.is_income",
                              "not": true
                            }
                          },
                          {
                            "sum": "recurring_activity",
                            "of": "rows",
                            "where": {
                              "path": "parent.category.is_income",
                              "not": true
                            }
                          }
                        ],
                        "round": 0
                      }
                    }
                  },
                  "unit": "me.primary_currency"
                }
              }
            }
          ]
        }
      ]
    },
    {
      "id": "assets",
      "type": "stat",
      "label": "Assets",
      "value": 0,
      "unit": "USD",
      "source": {
        "ref": "money",
        "map": {
          "value": {
            "path": "assets",
            "round": 0
          },
          "unit": "me.primary_currency"
        }
      }
    },
    {
      "id": "liabilities",
      "type": "stat",
      "label": "Liabilities",
      "value": 0,
      "unit": "USD",
      "source": {
        "ref": "money",
        "map": {
          "value": {
            "path": "debts",
            "round": 0
          },
          "unit": "me.primary_currency"
        }
      },
      "url": "legato://page/lunch-money-net-worth#cards"
    },
    {
      "id": "worth",
      "type": "breakdown",
      "label": "Asset mix",
      "unit": "USD",
      "items": [
        {
          "label": "Assets",
          "value": 0
        },
        {
          "label": "Assets",
          "value": 0
        }
      ],
      "source": {
        "ref": "money",
        "map": {
          "items": {
            "each": "asset_mix",
            "where": {
              "value": {
                "path": "value",
                "round": 0
              },
              "gt": 0
            },
            "sort": {
              "by": "value",
              "order": "desc"
            },
            "limit": 6,
            "fields": {
              "label": "label",
              "value": {
                "path": "value",
                "round": 0
              },
              "color": {
                "path": "label",
                "values": {
                  "Investments": "blue",
                  "Cash": "teal"
                }
              }
            }
          },
          "total": {
            "path": "assets",
            "round": 0
          },
          "unit": "me.primary_currency"
        }
      }
    },
    {
      "id": "accounts",
      "type": "tabs",
      "tabs": [
        {
          "label": "Cash",
          "cards": [
            {
              "id": "cash-linked",
              "type": "bars",
              "label": "Bank accounts",
              "unit": "USD",
              "items": [
                {
                  "label": "Bank accounts",
                  "value": 0
                }
              ],
              "source": {
                "ref": "money",
                "map": {
                  "items": {
                    "each": [
                      "banks.plaid_accounts",
                      "manual.manual_accounts"
                    ],
                    "where": {
                      "all": [
                        {
                          "all": [
                            {
                              "path": "type",
                              "equals": "cash"
                            },
                            {
                              "value": {
                                "path": "to_base",
                                "round": 0
                              },
                              "not": 0
                            }
                          ]
                        },
                        {
                          "path": "status",
                          "not": "closed"
                        }
                      ]
                    },
                    "sort": {
                      "by": "to_base",
                      "order": "desc"
                    },
                    "limit": 20,
                    "fields": {
                      "label": {
                        "path": "display_name",
                        "default": {
                          "template": "{name}"
                        }
                      },
                      "value": {
                        "path": "to_base",
                        "round": 0
                      },
                      "icon": {
                        "path": "institution_name",
                        "values": {
                          "American Express": "site:americanexpress.com",
                          "Bank of America": "site:bankofamerica.com",
                          "Capital One": "site:capitalone.com",
                          "Chase": "site:chase.com",
                          "Citibank Online": "site:citi.com",
                          "PayPal": "si:paypal",
                          "Robinhood": "site:robinhood.com",
                          "Wells Fargo": "site:wellsfargo.com",
                          "Ally Bank": "site:ally.com",
                          "Charles Schwab": "site:schwab.com"
                        },
                        "default": "building.columns"
                      }
                    }
                  },
                  "unit": "me.primary_currency"
                }
              }
            }
          ]
        },
        {
          "label": "Investments",
          "cards": [
            {
              "id": "investments-linked",
              "type": "bars",
              "label": "Investment accounts",
              "unit": "USD",
              "items": [
                {
                  "label": "Investment accounts",
                  "value": 0
                }
              ],
              "source": {
                "ref": "money",
                "map": {
                  "items": {
                    "each": [
                      "banks.plaid_accounts",
                      "manual.manual_accounts"
                    ],
                    "where": {
                      "all": [
                        {
                          "all": [
                            {
                              "path": "type",
                              "equals": "investment"
                            },
                            {
                              "value": {
                                "path": "to_base",
                                "round": 0
                              },
                              "not": 0
                            }
                          ]
                        },
                        {
                          "path": "status",
                          "not": "closed"
                        }
                      ]
                    },
                    "sort": {
                      "by": "to_base",
                      "order": "desc"
                    },
                    "limit": 20,
                    "fields": {
                      "label": {
                        "path": "display_name",
                        "default": {
                          "template": "{name}"
                        }
                      },
                      "value": {
                        "path": "to_base",
                        "round": 0
                      },
                      "icon": {
                        "path": "institution_name",
                        "values": {
                          "American Express": "site:americanexpress.com",
                          "Bank of America": "site:bankofamerica.com",
                          "Capital One": "site:capitalone.com",
                          "Chase": "site:chase.com",
                          "Citibank Online": "site:citi.com",
                          "PayPal": "si:paypal",
                          "Robinhood": "site:robinhood.com",
                          "Wells Fargo": "site:wellsfargo.com",
                          "Ally Bank": "site:ally.com",
                          "Charles Schwab": "site:schwab.com"
                        },
                        "default": "chart.line.uptrend.xyaxis"
                      }
                    }
                  },
                  "unit": "me.primary_currency"
                }
              }
            }
          ]
        },
        {
          "label": "Liabilities",
          "cards": [
            {
              "id": "cards",
              "type": "bars",
              "label": "Card balances",
              "unit": "USD",
              "items": [
                {
                  "label": "Card balances",
                  "value": 0
                }
              ],
              "source": {
                "ref": "money",
                "map": {
                  "items": {
                    "each": [
                      "banks.plaid_accounts",
                      "manual.manual_accounts"
                    ],
                    "where": {
                      "all": [
                        {
                          "all": [
                            {
                              "path": "type",
                              "equals": "credit"
                            },
                            {
                              "value": {
                                "path": "to_base",
                                "round": 0
                              },
                              "not": 0
                            }
                          ]
                        },
                        {
                          "path": "status",
                          "not": "closed"
                        }
                      ]
                    },
                    "sort": {
                      "by": "to_base",
                      "order": "desc"
                    },
                    "limit": 20,
                    "fields": {
                      "label": {
                        "path": "display_name",
                        "default": {
                          "template": "{name}"
                        }
                      },
                      "value": {
                        "path": "to_base",
                        "round": 0
                      },
                      "icon": {
                        "path": "institution_name",
                        "values": {
                          "American Express": "site:americanexpress.com",
                          "Bank of America": "site:bankofamerica.com",
                          "Capital One": "site:capitalone.com",
                          "Chase": "site:chase.com",
                          "Citibank Online": "site:citi.com",
                          "PayPal": "si:paypal",
                          "Robinhood": "site:robinhood.com",
                          "Wells Fargo": "site:wellsfargo.com",
                          "Ally Bank": "site:ally.com",
                          "Charles Schwab": "site:schwab.com"
                        },
                        "default": "creditcard"
                      }
                    }
                  },
                  "unit": "me.primary_currency"
                }
              }
            },
            {
              "id": "loans",
              "type": "bars",
              "label": "Loans and other debt",
              "unit": "USD",
              "items": [
                {
                  "label": "Loans",
                  "value": 0
                }
              ],
              "source": {
                "ref": "money",
                "map": {
                  "items": {
                    "each": [
                      "banks.plaid_accounts",
                      "manual.manual_accounts"
                    ],
                    "where": {
                      "all": [
                        {
                          "path": "type",
                          "in": [
                            "loan",
                            "other liability"
                          ]
                        },
                        {
                          "path": "status",
                          "not": "closed"
                        }
                      ]
                    },
                    "sort": {
                      "by": "to_base",
                      "order": "desc"
                    },
                    "limit": 20,
                    "fields": {
                      "label": {
                        "path": "display_name",
                        "default": {
                          "template": "{name}"
                        }
                      },
                      "value": {
                        "path": "to_base",
                        "round": 0
                      },
                      "icon": {
                        "path": "institution_name",
                        "values": {
                          "American Express": "site:americanexpress.com",
                          "Bank of America": "site:bankofamerica.com",
                          "Capital One": "site:capitalone.com",
                          "Chase": "site:chase.com",
                          "Citibank Online": "site:citi.com",
                          "PayPal": "si:paypal",
                          "Robinhood": "site:robinhood.com",
                          "Wells Fargo": "site:wellsfargo.com",
                          "Ally Bank": "site:ally.com",
                          "Charles Schwab": "site:schwab.com"
                        },
                        "default": "creditcard"
                      }
                    }
                  },
                  "unit": "me.primary_currency"
                },
                "when": {
                  "path": "loans",
                  "not": 0
                }
              }
            }
          ]
        },
        {
          "label": "Other",
          "cards": [
            {
              "id": "other-assets",
              "type": "bars",
              "label": "Other assets",
              "unit": "USD",
              "items": [
                {
                  "label": "Bank accounts",
                  "value": 0
                }
              ],
              "source": {
                "ref": "money",
                "map": {
                  "items": {
                    "each": [
                      "banks.plaid_accounts",
                      "manual.manual_accounts"
                    ],
                    "where": {
                      "all": [
                        {
                          "path": "type",
                          "in": [
                            "other asset",
                            "real estate",
                            "vehicle"
                          ]
                        },
                        {
                          "path": "status",
                          "not": "closed"
                        }
                      ]
                    },
                    "sort": {
                      "by": "to_base",
                      "order": "desc"
                    },
                    "limit": 20,
                    "fields": {
                      "label": {
                        "path": "display_name",
                        "default": {
                          "template": "{name}"
                        }
                      },
                      "value": {
                        "path": "to_base",
                        "round": 0
                      },
                      "icon": {
                        "path": "institution_name",
                        "values": {
                          "American Express": "site:americanexpress.com",
                          "Bank of America": "site:bankofamerica.com",
                          "Capital One": "site:capitalone.com",
                          "Chase": "site:chase.com",
                          "Citibank Online": "site:citi.com",
                          "PayPal": "si:paypal",
                          "Robinhood": "site:robinhood.com",
                          "Wells Fargo": "site:wellsfargo.com",
                          "Ally Bank": "site:ally.com",
                          "Charles Schwab": "site:schwab.com"
                        },
                        "default": "building.columns"
                      }
                    }
                  },
                  "unit": "me.primary_currency"
                }
              }
            }
          ]
        }
      ]
    },
    {
      "id": "balance-heading",
      "type": "heading",
      "text": "Bank connections",
      "collapsed": true
    },
    {
      "id": "sync",
      "type": "status",
      "placement": "page_info",
      "label": "Bank connections",
      "state": "unknown",
      "source": {
        "ref": "money",
        "map": {
          "state": {
            "if": {
              "path": "linked",
              "equals": 0
            },
            "then": {
              "template": "off"
            },
            "else": {
              "if": {
                "path": "attention",
                "gt": 0
              },
              "then": {
                "template": "warning"
              },
              "else": {
                "template": "ok"
              }
            }
          },
          "detail": {
            "if": {
              "path": "linked",
              "equals": 0
            },
            "then": {
              "template": "No active linked bank accounts."
            },
            "else": {
              "if": {
                "path": "attention",
                "gt": 0
              },
              "then": {
                "template": "{attention} connections need attention. Open Accounts in Lunch Money."
              },
              "else": {
                "if": {
                  "path": "syncing",
                  "gt": 0
                },
                "then": {
                  "template": "{syncing} accounts awaiting their first import."
                },
                "else": {
                  "template": "{linked} linked accounts connected."
                }
              }
            }
          }
        }
      },
      "url": "https://my.lunchmoney.app/accounts"
    },
    {
      "id": "balance-dates",
      "type": "list",
      "label": "Linked accounts · oldest first",
      "items": [],
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Request bank sync",
          "icon": "arrow.triangle.2.circlepath",
          "method": "POST",
          "path": "/v2/plaid_accounts/fetch",
          "error": "errors[0].errMsg"
        }
      ],
      "source": {
        "ref": "money",
        "map": {
          "items": {
            "each": "banks.plaid_accounts",
            "where": {
              "path": "status",
              "not": "closed"
            },
            "sort": "balance_last_update",
            "limit": 20,
            "fields": {
              "text": {
                "path": "display_name",
                "default": {
                  "template": "{name}"
                }
              },
              "detail": {
                "template": "{balance_last_update|ago} · {status}"
              },
              "url": {
                "template": "https://my.lunchmoney.app/accounts"
              }
            }
          }
        }
      }
    }
  ]
}
