openapi: 3.1.0 info: # Do not change the title, if the title changes, the import paths will be broken title: Api version: 0.1.0 description: API specification servers: - url: /api description: Base API path tags: - name: health description: Health operations - name: auth description: Phone + PIN authentication - name: me description: Current user identity/role - name: borrowers description: Borrower directory - name: loans description: Loan records, backed directly by the user's Heat Map spreadsheet - name: loan-requests description: Borrower-submitted requests for new loans - name: dashboard description: Aggregate dashboard views paths: /auth/login: post: operationId: login tags: [auth] summary: Log in with phone number + 6-digit PIN description: > Sets a signed session cookie. The configured admin phone number always logs in as staff; any other phone must match a Borrower record with a PIN set by staff. There is no self-service PIN reset — borrowers must contact staff to have their PIN reset. requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/LoginBody" responses: "200": description: Logged in content: application/json: schema: $ref: "#/components/schemas/MeInfo" "401": description: Invalid phone number or PIN /auth/logout: post: operationId: logout tags: [auth] summary: Log out description: Clears the session cookie. responses: "204": description: Logged out /healthz: get: operationId: healthCheck tags: [health] summary: Health check description: Returns server health status responses: "200": description: Healthy content: application/json: schema: $ref: "#/components/schemas/HealthStatus" /me: get: operationId: getMe tags: [me] summary: Get current user's role and identity description: Returns whether the signed-in user is staff or a linked borrower. responses: "200": description: Current user info content: application/json: schema: $ref: "#/components/schemas/MeInfo" /borrowers: get: operationId: listBorrowers tags: [borrowers] summary: List borrowers responses: "200": description: List of borrowers content: application/json: schema: type: array items: $ref: "#/components/schemas/Borrower" post: operationId: createBorrower tags: [borrowers] summary: Create a borrower requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/BorrowerInput" responses: "201": description: Created content: application/json: schema: $ref: "#/components/schemas/Borrower" /borrowers/{id}: get: operationId: getBorrower tags: [borrowers] summary: Get a borrower parameters: - $ref: "#/components/parameters/BorrowerId" responses: "200": description: Borrower content: application/json: schema: $ref: "#/components/schemas/Borrower" patch: operationId: updateBorrower tags: [borrowers] summary: Update a borrower parameters: - $ref: "#/components/parameters/BorrowerId" requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/BorrowerUpdate" responses: "200": description: Updated content: application/json: schema: $ref: "#/components/schemas/Borrower" delete: operationId: deleteBorrower tags: [borrowers] summary: Delete a borrower parameters: - $ref: "#/components/parameters/BorrowerId" responses: "204": description: Deleted /loans: get: operationId: listLoans tags: [loans] summary: List loans description: > Staff see all loans; borrowers see only loans whose Name matches their linked borrower record. Backed live by the Heat Map sheet. parameters: - name: status in: query required: false schema: $ref: "#/components/schemas/LoanStatus" responses: "200": description: List of loans content: application/json: schema: type: array items: $ref: "#/components/schemas/Loan" post: operationId: createLoan tags: [loans] summary: Create a loan description: > Appends a new row to the Heat Map sheet with only the input fields. Computed fields (fees, interest, final amount) are populated by the sheet's own formulas and returned on read-back. requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/LoanInput" responses: "201": description: Created content: application/json: schema: $ref: "#/components/schemas/Loan" /loans/{id}: get: operationId: getLoan tags: [loans] summary: Get a loan parameters: - $ref: "#/components/parameters/LoanId" responses: "200": description: Loan content: application/json: schema: $ref: "#/components/schemas/Loan" patch: operationId: updateLoan tags: [loans] summary: Update a loan description: > Writes only the changed input cells (e.g. status, part payment, discount). Computed columns are never written; re-read the loan to see the sheet's recalculated values. parameters: - $ref: "#/components/parameters/LoanId" requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/LoanUpdate" responses: "200": description: Updated content: application/json: schema: $ref: "#/components/schemas/Loan" delete: operationId: deleteLoan tags: [loans] summary: Delete a loan parameters: - $ref: "#/components/parameters/LoanId" responses: "204": description: Deleted /loan-requests: get: operationId: listLoanRequests tags: [loan-requests] summary: List loan requests description: > Staff see all requests; borrowers see only their own submitted requests. responses: "200": description: List of loan requests content: application/json: schema: type: array items: $ref: "#/components/schemas/LoanRequest" post: operationId: createLoanRequest tags: [loan-requests] summary: Submit a new loan request description: > Borrowers submit a request for staff to review; this does not create a Heat Map row directly. requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/LoanRequestInput" responses: "201": description: Created content: application/json: schema: $ref: "#/components/schemas/LoanRequest" /loan-requests/{id}: patch: operationId: updateLoanRequest tags: [loan-requests] summary: Update a loan request's status parameters: - $ref: "#/components/parameters/LoanRequestId" requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/LoanRequestUpdate" responses: "200": description: Updated content: application/json: schema: $ref: "#/components/schemas/LoanRequest" delete: operationId: deleteLoanRequest tags: [loan-requests] summary: Delete a loan request parameters: - $ref: "#/components/parameters/LoanRequestId" responses: "204": description: Deleted /dashboard/summary: get: operationId: getDashboardSummary tags: [dashboard] summary: Aggregate portfolio summary responses: "200": description: Dashboard summary content: application/json: schema: $ref: "#/components/schemas/DashboardSummary" /dashboard/activity: get: operationId: getRecentActivity tags: [dashboard] summary: Recent loan and repayment activity feed responses: "200": description: Recent activity content: application/json: schema: $ref: "#/components/schemas/RecentActivity" components: parameters: BorrowerId: name: id in: path required: true schema: type: string LoanId: name: id in: path required: true schema: type: string LoanRequestId: name: id in: path required: true schema: type: string schemas: HealthStatus: type: object properties: status: type: string required: - status LoanStatus: type: string enum: [Pending, Clear, Temp, Archived] LoginBody: type: object required: [phone, pin] properties: phone: type: string minLength: 1 pin: type: string description: Exactly 6 digits. pattern: "^[0-9]{6}$" minLength: 6 maxLength: 6 MeInfo: type: object required: [role] properties: role: type: string enum: [staff, borrower] borrowerId: type: ["string", "null"] name: type: ["string", "null"] phone: type: ["string", "null"] creditLimit: type: ["number", "null"] description: Maximum total principal the borrower may have outstanding; null means no limit set. Always null for staff. Borrower: type: object required: [id, name, createdAt] properties: id: type: string name: type: string phone: type: ["string", "null"] hasPin: type: boolean description: Whether staff has set a login PIN for this borrower. creditLimit: type: ["number", "null"] description: Maximum total borrowing allowed for this borrower; null means no limit set. createdAt: type: string BorrowerInput: type: object required: [name] properties: name: type: string minLength: 1 phone: type: ["string", "null"] pin: type: ["string", "null"] description: > Write-only. Sets the borrower's login PIN. Exactly 6 digits. Stored in plain text in the spreadsheet (never returned by the API) — staff sets it and shares it with the borrower directly; there is no self-service reset. pattern: "^[0-9]{6}$" BorrowerUpdate: type: object properties: name: type: string minLength: 1 phone: type: ["string", "null"] pin: type: ["string", "null"] description: > Write-only. Set to update the borrower's login PIN. Exactly 6 digits. pattern: "^[0-9]{6}$" Loan: type: object required: - id - loanId - name - borrowerId - principal - tenureDays - transactionDate - status - whatsapp - discountOrCharges - notes properties: id: type: string description: Internal UUID used as the row's primary key. loanId: type: string description: Human-readable loan identifier (e.g. "L-0001"). Assigned once on creation and persisted in column A of the Heat Map sheet alongside the UUID. name: type: string description: Borrower name as recorded on the Heat Map sheet. borrowerId: type: ["string", "null"] description: Linked Borrower id, resolved by matching name; null if unlinked. returnDate: type: ["string", "null"] description: Computed by the sheet from transaction date + tenure. timelyReturn: type: ["number", "null"] description: Legacy manually-entered figure, kept for reference. transactionDate: type: string principal: type: number tenureDays: type: integer whatsapp: type: string status: $ref: "#/components/schemas/LoanStatus" flatFee: type: ["number", "null"] description: Computed (tiered % of principal by tenure). interestPct: type: ["number", "null"] description: Computed tiered rate. interest: type: ["number", "null"] description: Computed interest amount. discountOrCharges: type: number description: Manually entered; negative is a discount, positive a charge. lateDays: type: ["number", "null"] description: Computed. lateFees: type: ["number", "null"] description: Computed. finalAmount: type: ["number", "null"] description: Computed authoritative amount to collect. partPayment: type: ["number", "null"] dateOfPartPayment: type: ["string", "null"] paid: type: ["number", "null"] description: Total amount actually collected so far. profit: type: ["number", "null"] description: Computed as paid minus principal. notes: type: string createdAt: type: ["string", "null"] description: ISO datetime when this loan row was created. Null for legacy rows predating this field. partPaymentTimestamps: type: array items: type: string description: ISO datetimes for each entry in dateOfPartPayment (same order). Empty for legacy rows. LoanInput: type: object required: [name, transactionDate, principal, tenureDays] properties: name: type: string minLength: 1 transactionDate: type: string principal: type: number minimum: 0 tenureDays: type: integer minimum: 0 whatsapp: type: ["string", "null"] status: $ref: "#/components/schemas/LoanStatus" discountOrCharges: type: ["number", "null"] notes: type: ["string", "null"] LoanUpdate: type: object properties: name: type: string minLength: 1 transactionDate: type: string principal: type: number tenureDays: type: integer whatsapp: type: ["string", "null"] status: $ref: "#/components/schemas/LoanStatus" discountOrCharges: type: ["number", "null"] partPayment: type: ["number", "null"] dateOfPartPayment: type: ["string", "null"] paid: type: ["number", "null"] notes: type: ["string", "null"] LoanRequestStatus: type: string enum: [Pending, Approved, Rejected] LoanRequest: type: object required: - id - name - phone - borrowerId - amount - tenureDays - purpose - status - createdAt properties: id: type: string name: type: string phone: type: string borrowerId: type: ["string", "null"] amount: type: number tenureDays: type: integer purpose: type: ["string", "null"] upiId: type: ["string", "null"] description: Optional UPI ID provided by the borrower for payment reference. tenureMonths: type: ["integer", "null"] description: Tenure in months (for EMI-type requests). Null for regular day-based loans. adminNote: type: ["string", "null"] description: Optional note left by the admin when approving or declining. discount: type: number description: Discount applied by the admin at approval (≥ 0). Zero if none. status: $ref: "#/components/schemas/LoanRequestStatus" createdAt: type: string approvedAt: type: ["string", "null"] description: ISO datetime when the request was approved or rejected. Null if still Pending or pre-dates this field. loanId: type: ["string", "null"] description: UUID of the loan row created when this request was approved. LoanRequestInput: type: object required: [amount, tenureDays] properties: amount: type: number minimum: 0 tenureDays: type: integer minimum: 0 purpose: type: ["string", "null"] upiId: type: ["string", "null"] description: Optional UPI ID for payment reference. LoanRequestUpdate: type: object properties: status: $ref: "#/components/schemas/LoanRequestStatus" DashboardSummary: type: object required: - totalBorrowers - activeLoansCount - totalOutstanding - totalCollected - overdueLoansCount - overdueAmount - dueSoonCount properties: totalBorrowers: type: integer activeLoansCount: type: integer totalOutstanding: type: number totalCollected: type: number overdueLoansCount: type: integer overdueAmount: type: number dueSoonCount: type: integer ActivityItem: type: object required: [type, description, occurredAt] properties: type: type: string enum: [loan_created, loan_settled] description: type: string amount: type: ["number", "null"] occurredAt: type: string borrowerName: type: ["string", "null"] RecentActivity: type: object required: [items] properties: items: type: array items: $ref: "#/components/schemas/ActivityItem"