openapi: 3.1.0 info: title: Api version: 0.1.0 description: Tanim Agricultural Supply Chain API servers: - url: /api description: Base API path tags: - name: health description: Health operations - name: auth description: Authentication operations - name: users description: User management - name: products description: Product catalog - name: categories description: Product categories - name: cart description: Shopping cart - name: orders description: Order management - name: reviews description: Product reviews and ratings - name: notifications description: Notifications - name: dashboard description: Admin dashboard analytics paths: /healthz: get: operationId: healthCheck tags: [health] summary: Health check responses: "200": description: Healthy content: application/json: schema: $ref: "#/components/schemas/HealthStatus" /auth/register: post: operationId: register tags: [auth] summary: Register a new user requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/RegisterRequest" responses: "201": description: User registered content: application/json: schema: $ref: "#/components/schemas/AuthResponse" "400": description: Validation error content: application/json: schema: $ref: "#/components/schemas/ErrorResponse" /auth/login: post: operationId: login tags: [auth] summary: Login a user requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/LoginRequest" responses: "200": description: Login successful content: application/json: schema: $ref: "#/components/schemas/AuthResponse" "401": description: Invalid credentials content: application/json: schema: $ref: "#/components/schemas/ErrorResponse" /auth/logout: post: operationId: logout tags: [auth] summary: Logout current user responses: "200": description: Logged out successfully content: application/json: schema: $ref: "#/components/schemas/MessageResponse" /auth/me: get: operationId: getMe tags: [auth] summary: Get current authenticated user responses: "200": description: Current user info content: application/json: schema: $ref: "#/components/schemas/User" "401": description: Not authenticated content: application/json: schema: $ref: "#/components/schemas/ErrorResponse" /users: get: operationId: listUsers tags: [users] summary: List all users (admin only) parameters: - name: role in: query schema: type: string enum: [buyer, farmer, admin] - name: page in: query schema: type: integer default: 1 - name: limit in: query schema: type: integer default: 20 responses: "200": description: List of users content: application/json: schema: $ref: "#/components/schemas/UserListResponse" /users/{id}: get: operationId: getUser tags: [users] summary: Get user by ID parameters: - name: id in: path required: true schema: type: integer responses: "200": description: User details content: application/json: schema: $ref: "#/components/schemas/User" put: operationId: updateUser tags: [users] summary: Update user profile parameters: - name: id in: path required: true schema: type: integer requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/UpdateUserRequest" responses: "200": description: User updated content: application/json: schema: $ref: "#/components/schemas/User" delete: operationId: deleteUser tags: [users] summary: Delete user (admin only) parameters: - name: id in: path required: true schema: type: integer responses: "200": description: User deleted content: application/json: schema: $ref: "#/components/schemas/MessageResponse" /categories: get: operationId: listCategories tags: [categories] summary: List all product categories responses: "200": description: List of categories content: application/json: schema: type: array items: $ref: "#/components/schemas/Category" post: operationId: createCategory tags: [categories] summary: Create a new category (admin only) requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/CreateCategoryRequest" responses: "201": description: Category created content: application/json: schema: $ref: "#/components/schemas/Category" /categories/{id}: put: operationId: updateCategory tags: [categories] summary: Update a category (admin only) parameters: - name: id in: path required: true schema: type: integer requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/CreateCategoryRequest" responses: "200": description: Category updated content: application/json: schema: $ref: "#/components/schemas/Category" delete: operationId: deleteCategory tags: [categories] summary: Delete a category (admin only) parameters: - name: id in: path required: true schema: type: integer responses: "200": description: Category deleted content: application/json: schema: $ref: "#/components/schemas/MessageResponse" /products: get: operationId: listProducts tags: [products] summary: List products with search and filters parameters: - name: search in: query schema: type: string - name: category_id in: query schema: type: integer - name: farmer_id in: query schema: type: integer - name: min_price in: query schema: type: number - name: max_price in: query schema: type: number - name: in_stock in: query schema: type: boolean - name: sort_by in: query schema: type: string enum: [name, price_asc, price_desc, newest, rating] - name: page in: query schema: type: integer default: 1 - name: limit in: query schema: type: integer default: 12 responses: "200": description: List of products content: application/json: schema: $ref: "#/components/schemas/ProductListResponse" post: operationId: createProduct tags: [products] summary: Create a new product (farmer or admin) requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/CreateProductRequest" responses: "201": description: Product created content: application/json: schema: $ref: "#/components/schemas/Product" /products/{id}: get: operationId: getProduct tags: [products] summary: Get product details parameters: - name: id in: path required: true schema: type: integer responses: "200": description: Product details content: application/json: schema: $ref: "#/components/schemas/ProductDetail" put: operationId: updateProduct tags: [products] summary: Update product (farmer who owns it or admin) parameters: - name: id in: path required: true schema: type: integer requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/UpdateProductRequest" responses: "200": description: Product updated content: application/json: schema: $ref: "#/components/schemas/Product" delete: operationId: deleteProduct tags: [products] summary: Delete product (farmer who owns it or admin) parameters: - name: id in: path required: true schema: type: integer responses: "200": description: Product deleted content: application/json: schema: $ref: "#/components/schemas/MessageResponse" /cart: get: operationId: getCart tags: [cart] summary: Get current user's cart responses: "200": description: Cart items content: application/json: schema: $ref: "#/components/schemas/Cart" delete: operationId: clearCart tags: [cart] summary: Clear cart responses: "200": description: Cart cleared content: application/json: schema: $ref: "#/components/schemas/MessageResponse" /cart/items: post: operationId: addToCart tags: [cart] summary: Add item to cart requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/AddToCartRequest" responses: "200": description: Item added content: application/json: schema: $ref: "#/components/schemas/Cart" /cart/items/{productId}: put: operationId: updateCartItem tags: [cart] summary: Update cart item quantity parameters: - name: productId in: path required: true schema: type: integer requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/UpdateCartItemRequest" responses: "200": description: Item updated content: application/json: schema: $ref: "#/components/schemas/Cart" delete: operationId: removeFromCart tags: [cart] summary: Remove item from cart parameters: - name: productId in: path required: true schema: type: integer responses: "200": description: Item removed content: application/json: schema: $ref: "#/components/schemas/Cart" /orders: get: operationId: listOrders tags: [orders] summary: List orders (admin sees all, buyer sees own, farmer sees their product orders) parameters: - name: status in: query schema: type: string enum: [pending, confirmed, processing, shipped, delivered, cancelled] - name: page in: query schema: type: integer default: 1 - name: limit in: query schema: type: integer default: 20 responses: "200": description: List of orders content: application/json: schema: $ref: "#/components/schemas/OrderListResponse" post: operationId: createOrder tags: [orders] summary: Create order from cart (checkout) requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/CreateOrderRequest" responses: "201": description: Order created content: application/json: schema: $ref: "#/components/schemas/Order" /orders/{id}: get: operationId: getOrder tags: [orders] summary: Get order details parameters: - name: id in: path required: true schema: type: integer responses: "200": description: Order details content: application/json: schema: $ref: "#/components/schemas/OrderDetail" /orders/{id}/status: put: operationId: updateOrderStatus tags: [orders] summary: Update order status (admin only) parameters: - name: id in: path required: true schema: type: integer requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/UpdateOrderStatusRequest" responses: "200": description: Order status updated content: application/json: schema: $ref: "#/components/schemas/Order" /orders/{id}/receipt: get: operationId: getOrderReceipt tags: [orders] summary: Get PDF receipt URL for an order parameters: - name: id in: path required: true schema: type: integer responses: "200": description: Receipt info content: application/json: schema: $ref: "#/components/schemas/ReceiptResponse" /reviews: get: operationId: listReviews tags: [reviews] summary: List reviews for a product parameters: - name: product_id in: query required: true schema: type: integer - name: page in: query schema: type: integer default: 1 - name: limit in: query schema: type: integer default: 10 responses: "200": description: List of reviews content: application/json: schema: $ref: "#/components/schemas/ReviewListResponse" post: operationId: createReview tags: [reviews] summary: Create a review for a purchased product requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/CreateReviewRequest" responses: "201": description: Review created content: application/json: schema: $ref: "#/components/schemas/Review" /reviews/{id}: put: operationId: updateReview tags: [reviews] summary: Update a review parameters: - name: id in: path required: true schema: type: integer requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/UpdateReviewRequest" responses: "200": description: Review updated content: application/json: schema: $ref: "#/components/schemas/Review" delete: operationId: deleteReview tags: [reviews] summary: Delete a review (admin or review owner) parameters: - name: id in: path required: true schema: type: integer responses: "200": description: Review deleted content: application/json: schema: $ref: "#/components/schemas/MessageResponse" /notifications: get: operationId: listNotifications tags: [notifications] summary: Get notifications for current user parameters: - name: unread_only in: query schema: type: boolean responses: "200": description: List of notifications content: application/json: schema: type: array items: $ref: "#/components/schemas/Notification" /notifications/{id}/read: put: operationId: markNotificationRead tags: [notifications] summary: Mark notification as read parameters: - name: id in: path required: true schema: type: integer responses: "200": description: Notification marked as read content: application/json: schema: $ref: "#/components/schemas/MessageResponse" /notifications/read-all: put: operationId: markAllNotificationsRead tags: [notifications] summary: Mark all notifications as read responses: "200": description: All notifications marked as read content: application/json: schema: $ref: "#/components/schemas/MessageResponse" /dashboard/stats: get: operationId: getDashboardStats tags: [dashboard] summary: Get admin dashboard statistics responses: "200": description: Dashboard stats content: application/json: schema: $ref: "#/components/schemas/DashboardStats" /dashboard/sales-by-year: get: operationId: getSalesByYear tags: [dashboard] summary: Get yearly sales data for bar chart parameters: - name: year in: query schema: type: integer responses: "200": description: Monthly sales data for year content: application/json: schema: $ref: "#/components/schemas/SalesByYearResponse" /dashboard/sales-by-product: get: operationId: getSalesByProduct tags: [dashboard] summary: Get sales by product for pie chart parameters: - name: year in: query schema: type: integer responses: "200": description: Sales by product content: application/json: schema: $ref: "#/components/schemas/SalesByProductResponse" /dashboard/low-stock: get: operationId: getLowStockProducts tags: [dashboard] summary: Get products with low stock responses: "200": description: Low stock products content: application/json: schema: type: array items: $ref: "#/components/schemas/LowStockProduct" /farmer/products: get: operationId: getFarmerProducts tags: [products] summary: Get products for current farmer parameters: - name: page in: query schema: type: integer default: 1 - name: limit in: query schema: type: integer default: 20 responses: "200": description: Farmer's products content: application/json: schema: $ref: "#/components/schemas/ProductListResponse" /farmer/stats: get: operationId: getFarmerStats tags: [dashboard] summary: Get farmer stats (total sales, revenue, low stock) responses: "200": description: Farmer stats content: application/json: schema: $ref: "#/components/schemas/FarmerStats" components: schemas: HealthStatus: type: object properties: status: type: string required: [status] ErrorResponse: type: object properties: error: type: string message: type: string required: [error] MessageResponse: type: object properties: message: type: string required: [message] RegisterRequest: type: object properties: name: type: string email: type: string format: email password: type: string minLength: 6 role: type: string enum: [buyer, farmer] phone: type: string address: type: string farm_name: type: string farm_location: type: string required: [name, email, password, role] LoginRequest: type: object properties: email: type: string format: email password: type: string required: [email, password] AuthResponse: type: object properties: user: $ref: "#/components/schemas/User" token: type: string message: type: string required: [user, token] User: type: object properties: id: type: integer name: type: string email: type: string role: type: string enum: [buyer, farmer, admin] phone: type: string nullable: true address: type: string nullable: true farm_name: type: string nullable: true farm_location: type: string nullable: true is_active: type: boolean created_at: type: string format: date-time required: [id, name, email, role, is_active, created_at] UpdateUserRequest: type: object properties: name: type: string phone: type: string address: type: string farm_name: type: string farm_location: type: string is_active: type: boolean low_stock_threshold: type: integer UserListResponse: type: object properties: users: type: array items: $ref: "#/components/schemas/User" total: type: integer page: type: integer limit: type: integer required: [users, total, page, limit] Category: type: object properties: id: type: integer name: type: string description: type: string nullable: true product_count: type: integer required: [id, name, product_count] CreateCategoryRequest: type: object properties: name: type: string description: type: string required: [name] Product: type: object properties: id: type: integer name: type: string description: type: string nullable: true price: type: number stock_quantity: type: integer unit: type: string image_url: type: string nullable: true category_id: type: integer nullable: true category_name: type: string nullable: true farmer_id: type: integer farmer_name: type: string farm_name: type: string nullable: true average_rating: type: number nullable: true review_count: type: integer low_stock_threshold: type: integer is_active: type: boolean created_at: type: string format: date-time required: [id, name, price, stock_quantity, unit, farmer_id, farmer_name, review_count, low_stock_threshold, is_active, created_at] ProductDetail: allOf: - $ref: "#/components/schemas/Product" - type: object properties: reviews: type: array items: $ref: "#/components/schemas/Review" ProductListResponse: type: object properties: products: type: array items: $ref: "#/components/schemas/Product" total: type: integer page: type: integer limit: type: integer required: [products, total, page, limit] CreateProductRequest: type: object properties: name: type: string description: type: string price: type: number stock_quantity: type: integer unit: type: string image_url: type: string category_id: type: integer low_stock_threshold: type: integer default: 10 required: [name, price, stock_quantity, unit] UpdateProductRequest: type: object properties: name: type: string description: type: string price: type: number stock_quantity: type: integer unit: type: string image_url: type: string category_id: type: integer low_stock_threshold: type: integer is_active: type: boolean CartItem: type: object properties: id: type: integer product_id: type: integer product_name: type: string product_image: type: string nullable: true price: type: number quantity: type: integer stock_quantity: type: integer unit: type: string farmer_name: type: string subtotal: type: number required: [id, product_id, product_name, price, quantity, stock_quantity, unit, farmer_name, subtotal] Cart: type: object properties: items: type: array items: $ref: "#/components/schemas/CartItem" total: type: number item_count: type: integer required: [items, total, item_count] AddToCartRequest: type: object properties: product_id: type: integer quantity: type: integer minimum: 1 required: [product_id, quantity] UpdateCartItemRequest: type: object properties: quantity: type: integer minimum: 1 required: [quantity] OrderItem: type: object properties: id: type: integer product_id: type: integer product_name: type: string quantity: type: integer unit_price: type: number subtotal: type: number unit: type: string required: [id, product_id, product_name, quantity, unit_price, subtotal, unit] Order: type: object properties: id: type: integer user_id: type: integer buyer_name: type: string status: type: string enum: [pending, confirmed, processing, shipped, delivered, cancelled] payment_method: type: string enum: [cod, online] payment_status: type: string enum: [pending, paid, failed, refunded] total_amount: type: number shipping_address: type: string notes: type: string nullable: true created_at: type: string format: date-time updated_at: type: string format: date-time required: [id, user_id, buyer_name, status, payment_method, payment_status, total_amount, shipping_address, created_at, updated_at] OrderDetail: allOf: - $ref: "#/components/schemas/Order" - type: object properties: items: type: array items: $ref: "#/components/schemas/OrderItem" OrderListResponse: type: object properties: orders: type: array items: $ref: "#/components/schemas/Order" total: type: integer page: type: integer limit: type: integer required: [orders, total, page, limit] CreateOrderRequest: type: object properties: shipping_address: type: string payment_method: type: string enum: [cod, online] notes: type: string required: [shipping_address, payment_method] UpdateOrderStatusRequest: type: object properties: status: type: string enum: [pending, confirmed, processing, shipped, delivered, cancelled] notes: type: string required: [status] ReceiptResponse: type: object properties: receipt_url: type: string order_id: type: integer message: type: string required: [receipt_url, order_id, message] Review: type: object properties: id: type: integer product_id: type: integer user_id: type: integer reviewer_name: type: string rating: type: integer minimum: 1 maximum: 5 comment: type: string nullable: true created_at: type: string format: date-time updated_at: type: string format: date-time required: [id, product_id, user_id, reviewer_name, rating, created_at, updated_at] ReviewListResponse: type: object properties: reviews: type: array items: $ref: "#/components/schemas/Review" total: type: integer average_rating: type: number page: type: integer limit: type: integer required: [reviews, total, average_rating, page, limit] CreateReviewRequest: type: object properties: product_id: type: integer rating: type: integer minimum: 1 maximum: 5 comment: type: string required: [product_id, rating] UpdateReviewRequest: type: object properties: rating: type: integer minimum: 1 maximum: 5 comment: type: string Notification: type: object properties: id: type: integer user_id: type: integer type: type: string enum: [low_stock, order_status, new_order, payment, system] title: type: string message: type: string is_read: type: boolean related_id: type: integer nullable: true created_at: type: string format: date-time required: [id, user_id, type, title, message, is_read, created_at] DashboardStats: type: object properties: total_revenue: type: number total_orders: type: integer total_products: type: integer total_users: type: integer total_farmers: type: integer total_buyers: type: integer pending_orders: type: integer low_stock_count: type: integer revenue_this_month: type: number orders_this_month: type: integer required: [total_revenue, total_orders, total_products, total_users, total_farmers, total_buyers, pending_orders, low_stock_count, revenue_this_month, orders_this_month] MonthlySales: type: object properties: month: type: string month_num: type: integer revenue: type: number orders: type: integer required: [month, month_num, revenue, orders] SalesByYearResponse: type: object properties: year: type: integer data: type: array items: $ref: "#/components/schemas/MonthlySales" required: [year, data] ProductSales: type: object properties: product_id: type: integer product_name: type: string total_revenue: type: number total_sold: type: integer percentage: type: number required: [product_id, product_name, total_revenue, total_sold, percentage] SalesByProductResponse: type: object properties: year: type: integer products: type: array items: $ref: "#/components/schemas/ProductSales" required: [year, products] LowStockProduct: type: object properties: id: type: integer name: type: string stock_quantity: type: integer low_stock_threshold: type: integer farmer_name: type: string unit: type: string required: [id, name, stock_quantity, low_stock_threshold, farmer_name, unit] FarmerStats: type: object properties: total_products: type: integer total_revenue: type: number total_orders: type: integer low_stock_products: type: integer unread_notifications: type: integer required: [total_products, total_revenue, total_orders, low_stock_products, unread_notifications]