{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Tern Client Services operating model v1",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "slug": {
      "type": "string"
    },
    "title": {
      "type": "string"
    },
    "industry": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "workflows": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "asOf": {
      "type": "string"
    },
    "source": {
      "type": "string"
    },
    "tables": {
      "type": "object",
      "properties": {
        "companies": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {}
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "company_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "name": {
                    "type": "string"
                  },
                  "domain": {
                    "type": "string"
                  }
                },
                "required": [
                  "company_id",
                  "name",
                  "domain"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "contacts": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "contact_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "company_id": {
                    "type": "string"
                  },
                  "first_name": {
                    "type": "string"
                  },
                  "last_name": {
                    "type": "string"
                  },
                  "email": {
                    "type": "string"
                  },
                  "job_title": {
                    "type": "string"
                  }
                },
                "required": [
                  "contact_id",
                  "company_id",
                  "first_name",
                  "last_name",
                  "email",
                  "job_title"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "deals": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "deal_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "company_id": {
                    "type": "string"
                  },
                  "contact_id": {
                    "type": "string"
                  },
                  "stage": {
                    "type": "string"
                  },
                  "amount": {
                    "type": "number"
                  },
                  "probability": {
                    "type": "number"
                  },
                  "weighted_amount": {
                    "type": "number"
                  }
                },
                "required": [
                  "deal_id",
                  "company_id",
                  "contact_id",
                  "stage",
                  "amount",
                  "probability",
                  "weighted_amount"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "activities": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "activity_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "deal_id": {
                    "type": "string"
                  },
                  "date": {
                    "type": "string"
                  },
                  "type": {
                    "type": "string"
                  },
                  "minutes": {
                    "type": "number"
                  }
                },
                "required": [
                  "activity_id",
                  "deal_id",
                  "date",
                  "type",
                  "minutes"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "leads": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "lead_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "contact_id": {
                    "type": "string"
                  },
                  "source": {
                    "type": "string"
                  },
                  "qualified": {
                    "type": "boolean"
                  }
                },
                "required": [
                  "lead_id",
                  "contact_id",
                  "source",
                  "qualified"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "quotes": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "quote_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "deal_id": {
                    "type": "string"
                  },
                  "amount": {
                    "type": "number"
                  },
                  "currency": {
                    "type": "string"
                  },
                  "valid_until": {
                    "type": "string"
                  },
                  "status": {
                    "type": "string"
                  }
                },
                "required": [
                  "quote_id",
                  "deal_id",
                  "amount",
                  "currency",
                  "valid_until",
                  "status"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "projects": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "project_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "deal_id": {
                    "type": "string"
                  },
                  "name": {
                    "type": "string"
                  },
                  "budget": {
                    "type": "number"
                  },
                  "status": {
                    "type": "string"
                  }
                },
                "required": [
                  "project_id",
                  "deal_id",
                  "name",
                  "budget",
                  "status"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "timesheets": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "time_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "project_id": {
                    "type": "string"
                  },
                  "date": {
                    "type": "string"
                  },
                  "worker": {
                    "type": "string"
                  },
                  "hours": {
                    "type": "number"
                  },
                  "hourly_rate": {
                    "type": "number"
                  },
                  "billable_amount": {
                    "type": "number"
                  }
                },
                "required": [
                  "time_id",
                  "project_id",
                  "date",
                  "worker",
                  "hours",
                  "hourly_rate",
                  "billable_amount"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        },
        "invoices": {
          "type": "object",
          "properties": {
            "primaryKey": {
              "type": "string"
            },
            "foreignKeys": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "column": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "targetColumn": {
                    "type": "string"
                  }
                },
                "required": [
                  "column",
                  "table",
                  "targetColumn"
                ],
                "additionalProperties": false
              }
            },
            "rows": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "invoice_id": {
                    "type": "string",
                    "minLength": 1
                  },
                  "project_id": {
                    "type": "string"
                  },
                  "issue_date": {
                    "type": "string"
                  },
                  "due_date": {
                    "type": "string"
                  },
                  "amount": {
                    "type": "number"
                  },
                  "paid": {
                    "type": "number"
                  },
                  "currency": {
                    "type": "string"
                  }
                },
                "required": [
                  "invoice_id",
                  "project_id",
                  "issue_date",
                  "due_date",
                  "amount",
                  "paid",
                  "currency"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "primaryKey",
            "foreignKeys",
            "rows"
          ],
          "additionalProperties": false
        }
      },
      "required": [
        "companies",
        "contacts",
        "deals",
        "activities",
        "leads",
        "quotes",
        "projects",
        "timesheets",
        "invoices"
      ],
      "additionalProperties": false
    },
    "metrics": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "label": {
            "type": "string"
          },
          "value": {
            "type": "number"
          },
          "unit": {
            "type": "string"
          },
          "calculation": {
            "type": "string"
          },
          "formula": {
            "type": "object",
            "properties": {
              "op": {
                "type": "string"
              },
              "table": {
                "type": "string"
              },
              "column": {
                "type": "string"
              }
            },
            "required": [
              "op",
              "table",
              "column"
            ],
            "additionalProperties": false
          }
        },
        "required": [
          "id",
          "label",
          "value",
          "unit",
          "calculation",
          "formula"
        ],
        "additionalProperties": false
      }
    },
    "chart": {
      "type": "object",
      "properties": {
        "title": {
          "type": "string"
        },
        "unit": {
          "type": "string"
        },
        "points": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "label": {
                "type": "string"
              },
              "value": {
                "type": "number"
              }
            },
            "required": [
              "label",
              "value"
            ],
            "additionalProperties": false
          }
        }
      },
      "required": [
        "title",
        "unit",
        "points"
      ],
      "additionalProperties": false
    },
    "notes": {
      "type": "array",
      "items": {}
    }
  },
  "required": [
    "id",
    "slug",
    "title",
    "industry",
    "description",
    "workflows",
    "asOf",
    "source",
    "tables",
    "metrics",
    "chart",
    "notes"
  ],
  "additionalProperties": false,
  "x-relationalRules": [
    {
      "table": "companies",
      "unique": "company_id",
      "foreignKeys": []
    },
    {
      "table": "contacts",
      "unique": "contact_id",
      "foreignKeys": [
        {
          "column": "company_id",
          "table": "companies",
          "targetColumn": "company_id"
        }
      ]
    },
    {
      "table": "deals",
      "unique": "deal_id",
      "foreignKeys": [
        {
          "column": "company_id",
          "table": "companies",
          "targetColumn": "company_id"
        },
        {
          "column": "contact_id",
          "table": "contacts",
          "targetColumn": "contact_id"
        }
      ]
    },
    {
      "table": "activities",
      "unique": "activity_id",
      "foreignKeys": [
        {
          "column": "deal_id",
          "table": "deals",
          "targetColumn": "deal_id"
        }
      ]
    },
    {
      "table": "leads",
      "unique": "lead_id",
      "foreignKeys": [
        {
          "column": "contact_id",
          "table": "contacts",
          "targetColumn": "contact_id"
        }
      ]
    },
    {
      "table": "quotes",
      "unique": "quote_id",
      "foreignKeys": [
        {
          "column": "deal_id",
          "table": "deals",
          "targetColumn": "deal_id"
        }
      ]
    },
    {
      "table": "projects",
      "unique": "project_id",
      "foreignKeys": [
        {
          "column": "deal_id",
          "table": "deals",
          "targetColumn": "deal_id"
        }
      ]
    },
    {
      "table": "timesheets",
      "unique": "time_id",
      "foreignKeys": [
        {
          "column": "project_id",
          "table": "projects",
          "targetColumn": "project_id"
        }
      ]
    },
    {
      "table": "invoices",
      "unique": "invoice_id",
      "foreignKeys": [
        {
          "column": "project_id",
          "table": "projects",
          "targetColumn": "project_id"
        }
      ]
    }
  ]
}
