{
  "openapi": "3.1.0",
  "info": {
    "title": "Crewzy Enterprise API",
    "version": "1",
    "description": "Crewzy issues one key to each ERP provider once it has accepted the API Terms. It works for each enterprise that has signed with Crewzy and named that ERP, while its agreement is live. Every call names the enterprise and the link; a link that is not that enterprise's answers 404, even one the key serves."
  },
  "servers": [
    {
      "url": "{address}/api/v1/enterprise",
      "variables": {
        "address": {
          "default": "https://app.getcrewzy.com",
          "description": "The API address Crewzy gives you."
        }
      }
    }
  ],
  "components": {
    "securitySchemes": {
      "key": {
        "type": "http",
        "scheme": "bearer",
        "description": "The key, starting crewzy_ent_, as a bearer token."
      }
    }
  },
  "security": [
    {
      "key": []
    }
  ],
  "paths": {
    "/enterprises": {
      "get": {
        "summary": "The live enterprises this key may act for.",
        "responses": {
          "200": {
            "description": "Their ids and names."
          }
        }
      }
    },
    "/{enterpriseId}/invitations": {
      "post": {
        "summary": "Asks Crewzy to invite a contractor. Crewzy emails them in its own name, with one reminder after 7 days; the invitation lapses after 30. The answer is the same whether or not the business is on Crewzy.",
        "parameters": [
          {
            "name": "enterpriseId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "contractorRef": {
                    "type": "string",
                    "pattern": "^[A-Za-z0-9._:-]{1,100}$"
                  },
                  "businessName": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 200
                  },
                  "abn": {
                    "type": "string",
                    "pattern": "^[0-9 ]{9,17}$"
                  },
                  "contactName": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 120
                  },
                  "contactEmail": {
                    "type": "string",
                    "maxLength": 254,
                    "format": "email",
                    "pattern": "^(?:[A-Za-z0-9_'+\\-]+\\.)*[A-Za-z0-9_'+\\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$"
                  },
                  "consent": {
                    "type": "boolean",
                    "const": true
                  }
                },
                "required": [
                  "contractorRef",
                  "businessName",
                  "contactName",
                  "contactEmail",
                  "consent"
                ]
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "The invitation, pending."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          },
          "429": {
            "description": "The enterprise has sent 50 invitations in the last day."
          }
        }
      }
    },
    "/{enterpriseId}/invitations/{id}": {
      "get": {
        "summary": "Where an invitation stands: pending, accepted (with the link id), declined or expired.",
        "parameters": [
          {
            "name": "enterpriseId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The invitation."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          }
        }
      }
    },
    "/{enterpriseId}/links": {
      "get": {
        "summary": "The enterprise's links, live and ended.",
        "parameters": [
          {
            "name": "enterpriseId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The links."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          }
        }
      }
    },
    "/{enterpriseId}/links/{linkId}": {
      "get": {
        "summary": "One link, with the contractor's details and the licences and insurance it shares.",
        "parameters": [
          {
            "name": "enterpriseId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "linkId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The link."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          }
        }
      },
      "delete": {
        "summary": "Ends the link: the enterprise no longer uses the contractor. New jobs stop; the jobs already sent stay.",
        "parameters": [
          {
            "name": "enterpriseId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "linkId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Ended."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          }
        }
      }
    },
    "/{enterpriseId}/links/{linkId}/work-orders/{ref}": {
      "put": {
        "summary": "Sends a new job, or changes one, under your own reference for it. References are unique within a link. Naming an order puts the job under that purchase order.",
        "parameters": [
          {
            "name": "enterpriseId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "linkId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "ref",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "title": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 200
                  },
                  "description": {
                    "type": "string",
                    "maxLength": 5000
                  },
                  "priority": {
                    "type": "string",
                    "enum": [
                      "low",
                      "normal",
                      "high",
                      "urgent"
                    ]
                  },
                  "reference": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 100
                  },
                  "requestedBy": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 120
                  },
                  "dueAt": {
                    "type": "string",
                    "format": "date-time",
                    "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
                  },
                  "site": {
                    "type": "object",
                    "properties": {
                      "address": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 200
                      },
                      "suburb": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 100
                      },
                      "postcode": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 10
                      },
                      "name": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 120
                      }
                    },
                    "required": [
                      "address"
                    ]
                  },
                  "updatedAt": {
                    "type": "string",
                    "format": "date-time",
                    "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
                  },
                  "cancelled": {
                    "type": "boolean"
                  },
                  "status": {
                    "type": "string",
                    "enum": [
                      "approved",
                      "rejected",
                      "closed"
                    ]
                  },
                  "order": {
                    "type": "string",
                    "pattern": "^[A-Za-z0-9._:-]{1,100}$"
                  }
                },
                "required": [
                  "title"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The job was there and is updated; the answer is the job as it stands."
          },
          "201": {
            "description": "The job is new."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          },
          "409": {
            "description": "The link has ended."
          }
        }
      },
      "get": {
        "summary": "The whole job as it stands: status, booked dates, approved time, parts, sign-off, its invoices, and the invoices against a purchase order that bill it, with this job's lines. Rebuild from this after losing changes.",
        "parameters": [
          {
            "name": "enterpriseId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "linkId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "ref",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The job."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          }
        }
      }
    },
    "/{enterpriseId}/links/{linkId}/work-orders/{ref}/signature": {
      "get": {
        "summary": "The sign-off signature, as an image, when the job's sign-off says there is one.",
        "parameters": [
          {
            "name": "enterpriseId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "linkId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "ref",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The image."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          }
        }
      }
    },
    "/{enterpriseId}/links/{linkId}/files/{id}": {
      "get": {
        "summary": "A photo or form on one of the link's jobs, or a licence or insurance the contractor shares.",
        "parameters": [
          {
            "name": "enterpriseId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "linkId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The file."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          }
        }
      }
    },
    "/{enterpriseId}/links/{linkId}/quotes/{number}/decision": {
      "put": {
        "summary": "Accepts a quote with your purchase order (its number, and what it authorises before tax, the quote's total when left out), declines it, or asks a question about it.",
        "parameters": [
          {
            "name": "enterpriseId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "linkId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "number",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "decision": {
                    "type": "string",
                    "enum": [
                      "accepted",
                      "declined",
                      "question"
                    ]
                  },
                  "revision": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 9007199254740991
                  },
                  "by": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 120
                  },
                  "note": {
                    "type": "string",
                    "maxLength": 2000
                  },
                  "order": {
                    "type": "object",
                    "properties": {
                      "ref": {
                        "type": "string",
                        "pattern": "^[A-Za-z0-9._:-]{1,100}$"
                      },
                      "amount": {
                        "type": "number",
                        "minimum": 0,
                        "maximum": 100000000
                      }
                    },
                    "required": [
                      "ref"
                    ]
                  }
                },
                "required": [
                  "decision",
                  "by"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The quote as it now stands."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          },
          "409": {
            "description": "The quote has already been answered, or has lapsed."
          }
        }
      }
    },
    "/{enterpriseId}/links/{linkId}/client-orders/{ref}": {
      "put": {
        "summary": "Sends a purchase order on its own: one with no quote, a raised amount, or closing or cancelling it. Jobs join it by naming it.",
        "parameters": [
          {
            "name": "enterpriseId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "linkId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "ref",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "amount": {
                    "anyOf": [
                      {
                        "type": "number",
                        "minimum": 0,
                        "maximum": 100000000
                      },
                      {
                        "type": "null"
                      }
                    ]
                  },
                  "status": {
                    "type": "string",
                    "enum": [
                      "open",
                      "closed",
                      "cancelled"
                    ]
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The order as it now stands."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          },
          "409": {
            "description": "The link has ended."
          }
        }
      }
    },
    "/{enterpriseId}/links/{linkId}/invoices/{number}/status": {
      "put": {
        "summary": "Says what you did with an invoice: approved, disputed with the reason, or paid with the date, amount and reference. Paid is advice: the contractor records the payment.",
        "parameters": [
          {
            "name": "enterpriseId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "linkId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "number",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "status": {
                    "type": "string",
                    "enum": [
                      "approved",
                      "disputed",
                      "paid"
                    ]
                  },
                  "note": {
                    "type": "string",
                    "maxLength": 2000
                  },
                  "paidOn": {
                    "type": "string",
                    "format": "date",
                    "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))$"
                  },
                  "amount": {
                    "type": "number",
                    "minimum": 0,
                    "maximum": 100000000
                  },
                  "reference": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 100
                  }
                },
                "required": [
                  "status"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Recorded."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          }
        }
      }
    },
    "/changes": {
      "get": {
        "summary": "The change feed, oldest first, for every enterprise the key serves or one of them. Pass the answer's next as after in the following read. Changes are kept 90 days. Read about once a minute.",
        "parameters": [
          {
            "name": "after",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "pattern": "^\\d{1,18}$"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "default": 200,
              "type": "integer",
              "minimum": 1,
              "maximum": 200
            }
          },
          {
            "name": "enterprise",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "format": "uuid",
              "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The changes and the next cursor."
          },
          "404": {
            "description": "Nothing with that id this key may see. Another business or enterprise answers the same."
          },
          "410": {
            "description": "The cursor is older than the changes kept. Read each job again, then start from the newest."
          }
        }
      }
    }
  }
}