From 2ea38ef091bf38413e0ba9d7b5ffe376fbd4cb80 Mon Sep 17 00:00:00 2001 From: Soham Chari Date: Fri, 28 Nov 2025 18:21:51 +0530 Subject: [PATCH] Initial commit: Added listing and location routes --- .gitignore | 3 + README.md | 2245 ++++++++++++++++++++++++++++++++++++++ package-lock.json | 903 +++++++++++++++ package.json | 25 + routes/listingRoutes.js | 222 ++++ routes/locationRoutes.js | 136 +++ server.js | 19 + 7 files changed, 3553 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 routes/listingRoutes.js create mode 100644 routes/locationRoutes.js create mode 100644 server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..69da196 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +node_modules +.env \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..92a3846 --- /dev/null +++ b/README.md @@ -0,0 +1,2245 @@ +# Livestock Marketplace – Listing Service Spec + +This document covers: + +1. All DB tables required for animal listings +2. API endpoints – purpose, request, and response formats + +--- + +## 1. Database Tables + +> Convention: Every table has `created_at` and `updated_at` (`TIMESTAMP`). + +### 1.1 `users` (reference) + +Minimal definition (assuming you already have auth elsewhere). + +| Column | Type | Constraints | Description | +| ---------- | --------- | ---------------- | ---------------------- | +| id | UUID | PK | User ID (seller/buyer) | +| name | VARCHAR | NOT NULL | Display name | +| phone | VARCHAR | UNIQUE, NOT NULL | Phone number | +| created_at | TIMESTAMP | NOT NULL | Row created time | +| updated_at | TIMESTAMP | NOT NULL | Row last updated time | + +**Relationships** + +- 1 `user` → N `listings` +- 1 `user` → N `locations` (saved addresses only) + +--- + +### 1.2 `species` + +| Column | Type | Constraints | Description | +| ---------- | --------- | ----------- | -------------------------- | +| id | INT | PK | Species ID | +| name | VARCHAR | UNIQUE | e.g. Cattle, Buffalo, Goat | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `species` → N `breeds` +- 1 `species` → N `animals` + +--- + +### 1.3 `breeds` + +| Column | Type | Constraints | Description | +| ----------- | --------- | --------------- | ---------------- | +| id | INT | PK | Breed ID | +| species_id | INT | FK → species.id | Parent species | +| name | VARCHAR | NOT NULL | e.g. Gir, Murrah | +| description | TEXT | NULL | Optional notes | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `species` → N `breeds` +- 1 `breed` → N `animals` + +--- + +### 1.4 `locations` + +Used both for: + +- **Captured locations** (no user, not saved; `user_id = NULL`, `is_saved_address = false`) +- **Saved addresses** for a user (e.g. farm, home; `user_id` set, `is_saved_address = true`) + +| Column | Type | Constraints | Description | +| ----------------- | --------- | --------------------------- | --------------------------------------------------------------------- | +| id | UUID | PK | Location ID | +| user_id | UUID | FK → users.id, NULLABLE | Owner if this is a saved address; NULL if just captured for a listing | +| is_saved_address | BOOLEAN | NOT NULL, default false | True if user chose to save it as an address | +| location_type | VARCHAR | NULL (enum suggestion) | e.g. `farm`, `home`, `office`, `other` | +| country | VARCHAR | NULL | Country | +| state | VARCHAR | NULL | State | +| district | VARCHAR | NULL | District | +| city_village | VARCHAR | NULL | City / village | +| pincode | VARCHAR | NULL | Postal code | +| lat | DECIMAL | NULL | Latitude | +| lng | DECIMAL | NULL | Longitude | +| source_type | VARCHAR | NOT NULL, default `unknown` | `device_gps`, `manual`, `unknown` | +| source_confidence | VARCHAR | NOT NULL, default `medium` | `high`, `medium`, `low` | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Interpretation** + +- **Captured only**: `user_id = NULL`, `is_saved_address = false` +- **Captured + saved as user’s farm/home**: set `user_id`, `is_saved_address = true`, `location_type = 'farm'` (or similar). + +**Relationships** + +- 1 `user` → N `locations` (saved addresses) +- 1 `location` → N `animals` (many animals can share same farm address) + +--- + +### 1.5 `animals` + +One animal per listing (enforced via unique constraint at `listings.animal_id`). + +| Column | Type | Constraints | Description | +| -------------------------- | --------- | ------------------------- | ----------------------------------------------- | +| id | UUID | PK | Animal ID | +| species_id | INT | FK → species.id, NOT NULL | Species | +| breed_id | INT | FK → breeds.id, NULL | Breed (optional) | +| sex | VARCHAR | NOT NULL | `M`, `F`, `Neutered` | +| age_months | INT | NULL | Age in months | +| weight_kg | DECIMAL | NULL | Approx weight | +| color_markings | VARCHAR | NULL | Color / markings | +| quantity | INT | NOT NULL, default 1 | Number of animals in this listing | +| purpose | VARCHAR | NOT NULL | `dairy`, `meat`, `breeding`, `pet`, `work`, etc | +| health_status | VARCHAR | NOT NULL | `healthy`, `minor_issues`, `serious_issues` | +| vaccinated | BOOLEAN | NOT NULL, default false | | +| dewormed | BOOLEAN | NOT NULL, default false | | +| previous_pregnancies_count | INT | NULL | For females, number of previous pregnancies | +| pregnancy_status | VARCHAR | NULL | `not_pregnant`, `pregnant`, `recently_calved` | +| milk_yield_litre_per_day | DECIMAL | NULL | Avg daily milk yield | +| ear_tag_no | VARCHAR | NULL | Tag / registration ID | +| description | TEXT | NULL | Detailed description | +| suggested_care | TEXT | NULL | Suggested food & accessories (free-text) | +| location_id | UUID | FK → locations.id, NULL | Location of animal (farm etc.). NULL if unknown | +| created_from | VARCHAR | NOT NULL | listing/custom_requirement | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `species` → N `animals` +- 1 `breed` → N `animals` +- 1 `location` → N `animals` +- 1 `animal` ↔ 1 `listing` (via `listings.animal_id` UNIQUE) + +--- + +### 1.6 `listings` + +One listing per animal (1–1). + +| Column | Type | Constraints | Description | +| ------------------------ | --------- | --------------------------------- | ---------------------------------------------- | +| id | UUID | PK | Listing ID | +| seller_id | UUID | FK → users.id, NOT NULL | Seller | +| animal_id | UUID | FK → animals.id, UNIQUE, NOT NULL | The animal this listing is for | +| title | VARCHAR | NOT NULL | Listing title | +| price | DECIMAL | NOT NULL | Asking price | +| currency | VARCHAR | NOT NULL, e.g. `INR` | Currency code | +| is_negotiable | BOOLEAN | NOT NULL, default true | Price negotiable | +| listing_type | VARCHAR | NOT NULL | `sale`, `stud_service`, `adoption` | +| status | VARCHAR | NOT NULL, default `active` | `active`, `sold`, `expired`, `hidden` | +| listing_score | INT | NOT NULL, default 0 | ML ranking score | +| views_count | INT | NOT NULL, default 0 | Total views | +| bookmarks_count | INT | NOT NULL, default 0 | Times bookmarked | +| enquiries_call_count | INT | NOT NULL, default 0 | Phone enquiries | +| enquiries_whatsapp_count | INT | NOT NULL, default 0 | WhatsApp enquiries | +| clicks_count | INT | NOT NULL, default 0 | Other CTA clicks (e.g. “View number”) | +| listing_score_status | VARCHAR | NOT NULL, default `pending` | `pending`, `scored`, `error`, `not_applicable` | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `user` → N `listings` +- 1 `animal` ↔ 1 `listing` +- 1 `listing` → N `listing_media` + +--- + +### 1.7 `listing_media` (images / videos) + +| Column | Type | Constraints | Description | +| ---------- | --------- | ----------------------- | -------------------------------- | +| id | UUID | PK | Media ID | +| listing_id | UUID | FK → listings.id | Parent listing | +| media_url | VARCHAR | NOT NULL | URL to image/video | +| media_type | VARCHAR | NOT NULL | `image`, `video` | +| is_primary | BOOLEAN | NOT NULL, default false | True if main display image/video | +| sort_order | INT | NOT NULL, default 0 | For ordering media in gallery | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `listing` → N `listing_media` + +--- + +### 1.8 `custom_requirements` + +| Column | Type | Constraints | Description | +| ---------------- | --------- | ------------------------ | -------------------------------- | +| id | UUID | PK | Requirement ID | +| user_id | UUID | FK → users.id | User requesting | +| requirement_text | TEXT | NOT NULL | Free-text requirement | +| animal_id | UUID | FK → animals.id, NULL | Animal inserted from requirement | +| status | VARCHAR | NOT NULL, default 'open' | open / matched / closed | +| created_at | TIMESTAMP | NOT NULL | Created time | +| updated_at | TIMESTAMP | NOT NULL | Updated time | + +**Relationships** + +- 1 `user` → N `custom_requirements` +- 1 `custom_requirement` → 1 `animal` (optional) + +--- + +### 1.9 Relationship Summary + +- **1–N** + + - `users` → `listings` + - `users` → `locations` (saved addresses) + - `species` → `breeds` + - `species` → `animals` + - `breeds` → `animals` + - `locations` → `animals` + - `listings` → `listing_media` + +- **1–1** + + - `animals` ↔ `listings` (enforced via `listings.animal_id` UNIQUE) + +- **N–M** + - None currently; all many-to-many are avoided in this MVP schema. + +--- + +## 2. API Endpoints + +### 2.1 Create Listing (with Animal + optional Location) + +#### `POST /listings` + +**Purpose** + +Create a new listing and its animal. Optionally: + +- Use an existing `location_id` or +- Create a new captured/saved location in the same call. + +**Request (JSON)** + +```json +{ + "seller_id": "UUID-of-seller", + "title": "High-yield Gir cow for sale", + "price": 55000, + "currency": "INR", + "is_negotiable": true, + "listing_type": "sale", + "animal": { + "species_id": 1, + "breed_id": 10, + "sex": "F", + "age_months": 36, + "weight_kg": 450, + "color_markings": "Brown with white patches", + "quantity": 1, + "purpose": "dairy", + "health_status": "healthy", + "vaccinated": true, + "dewormed": true, + "previous_pregnancies_count": 1, + "pregnancy_status": "pregnant", + "milk_yield_litre_per_day": 15, + "ear_tag_no": "TAG-12345", + "description": "Calm nature, easy to handle.", + "suggested_care": "Green fodder, mineral mix, clean shed.", + "location_id": "existing-location-uuid", + "new_location": { + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high", + "save_as_address": true, + "location_type": "farm" + } + }, + "media": [ + { + "media_url": "https://cdn.app.com/listings/abc1.jpg", + "media_type": "image", + "is_primary": true, + "sort_order": 1 + } + ] +} +``` + +Notes: + +- Client can either: + + - Provide `location_id`, **or** + - Provide `new_location` object. If `save_as_address = true`, backend should create a `locations` row with `user_id = seller_id`, `is_saved_address = true`. + +- Media is optional in this first call; can also be added later via media APIs. + +**Response (201 Created)** + +```json +{ + "listing": { + "id": "listing-uuid", + "seller_id": "UUID-of-seller", + "animal_id": "animal-uuid", + "title": "High-yield Gir cow for sale", + "price": 55000, + "currency": "INR", + "is_negotiable": true, + "listing_type": "sale", + "status": "active", + "views_count": 0, + "bookmarks_count": 0, + "enquiries_call_count": 0, + "enquiries_whatsapp_count": 0, + "clicks_count": 0, + "created_at": "2025-11-22T10:00:00Z", + "updated_at": "2025-11-22T10:00:00Z", + "animal": { + "id": "animal-uuid", + "species_id": 1, + "breed_id": 10, + "sex": "F", + "age_months": 36, + "weight_kg": 450, + "color_markings": "Brown with white patches", + "quantity": 1, + "purpose": "dairy", + "health_status": "healthy", + "vaccinated": true, + "dewormed": true, + "previous_pregnancies_count": 1, + "pregnancy_status": "pregnant", + "milk_yield_litre_per_day": 15, + "ear_tag_no": "TAG-12345", + "description": "Calm nature, easy to handle.", + "suggested_care": "Green fodder, mineral mix, clean shed.", + "location": { + "id": "location-uuid", + "user_id": "UUID-of-seller", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high" + } + }, + "media": [ + { + "id": "media-uuid", + "media_url": "https://cdn.app.com/listings/abc1.jpg", + "media_type": "image", + "is_primary": true, + "sort_order": 1 + } + ] + } +} +``` + +--- + +### 2.2 List / Search Listings + +#### `GET /listings` + +**Purpose** + +List active listings with optional filters (species, location, price, etc.). + +**Query parameters (examples)** + +- `species_id` (int, optional) +- `breed_id` (int, optional) +- `state` (string, optional) +- `district` (string, optional) +- `min_price`, `max_price` (optional) +- `listing_type` (string, optional) +- `page`, `page_size` (for pagination) + +**Request** + +```http +GET /listings?species_id=1&state=Maharashtra&page=1&page_size=20 +``` + +**Response (200 OK)** + +```json +{ + "items": [ + { + "id": "listing-uuid", + "title": "High-yield Gir cow for sale", + "price": 55000, + "currency": "INR", + "is_negotiable": true, + "listing_type": "sale", + "status": "active", + "species_id": 1, + "breed_id": 10, + "animal_id": "animal-uuid", + "thumbnail_url": "https://cdn.app.com/listings/abc1.jpg", + "location_summary": { + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati" + }, + "created_at": "2025-11-22T10:00:00Z" + } + ], + "page": 1, + "page_size": 20, + "total": 1 +} +``` + +--- + +### 2.3 Get Listing Detail + +#### `GET /listings/{listing_id}` + +**Purpose** + +Get full details of a single listing (including animal, location, media). + +**Response (200 OK)** + +```json +{ + "id": "listing-uuid", + "seller_id": "UUID-of-seller", + "animal_id": "animal-uuid", + "title": "High-yield Gir cow for sale", + "price": 55000, + "currency": "INR", + "is_negotiable": true, + "listing_type": "sale", + "status": "active", + "views_count": 120, + "bookmarks_count": 10, + "enquiries_call_count": 5, + "enquiries_whatsapp_count": 8, + "clicks_count": 14, + "created_at": "2025-11-22T10:00:00Z", + "updated_at": "2025-11-22T11:00:00Z", + "animal": { + "...": "full animal object as above" + }, + "media": [ + { + "id": "media-uuid", + "media_url": "https://cdn.app.com/listings/abc1.jpg", + "media_type": "image", + "is_primary": true, + "sort_order": 1 + } + ] +} +``` + +--- + +### 2.4 Update Listing (and Animal) + +#### `PUT /listings/{listing_id}` + +**Purpose** + +Edit listing fields and animal details (e.g. price, status, description, suggested care). + +**Request (JSON)** + +Only fields to update need to be sent (PATCH style with PUT semantics). + +```json +{ + "title": "Gir cow – price reduced", + "price": 52000, + "status": "active", + "animal": { + "description": "Price reduced, urgent sale.", + "suggested_care": "Green fodder, clean water, regular deworming." + } +} +``` + +**Response (200 OK)** + +Returns updated listing object (same shape as `GET /listings/{id}`). + +--- + +### 2.5 Create / Capture Location + +#### `POST /locations` + +**Purpose** + +Create a new location. Used for: + +- Saved address for a user (farm/home) +- Captured location for an animal/listing (not necessarily saved) + +**Request (JSON)** + +```json +{ + "user_id": "UUID-of-user-or-null", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high" +} +``` + +- For **captured-only** (not saved): set `user_id = null`, `is_saved_address = false`. + +**Response (201 Created)** + +```json +{ + "id": "location-uuid", + "user_id": "UUID-of-user-or-null", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high", + "created_at": "2025-11-22T10:05:00Z", + "updated_at": "2025-11-22T10:05:00Z" +} +``` + +--- + +### 2.6 Update Location (PUT for Locations) + +#### `PUT /locations/{location_id}` + +**Purpose** + +Update location details OR convert a captured location into a saved address for a user (e.g. mark as farm). + +**Request (JSON)** + +```json +{ + "user_id": "UUID-of-user", + "is_saved_address": true, + "location_type": "farm", + "city_village": "New Village Name", + "pincode": "413103" +} +``` + +**Response (200 OK)** + +```json +{ + "id": "location-uuid", + "user_id": "UUID-of-user", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "New Village Name", + "pincode": "413103", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high", + "created_at": "2025-11-22T10:05:00Z", + "updated_at": "2025-11-22T11:00:00Z" +} +``` + +--- + +### 2.7 Get Saved Locations for a User + +#### `GET /users/{user_id}/locations` + +**Purpose** + +Fetch all saved addresses for a given user (farm, home, etc.). + +**Response (200 OK)** + +```json +{ + "items": [ + { + "id": "location-uuid-1", + "user_id": "UUID-of-user", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high", + "created_at": "2025-11-22T10:00:00Z", + "updated_at": "2025-11-22T10:00:00Z" + } + ] +} +``` + +--- + +### 2.8 Add Media to Listing + +#### `POST /listings/{listing_id}/media` + +**Purpose** + +Attach new images/videos to a listing after creation. + +**Request (JSON)** + +```json +{ + "items": [ + { + "media_url": "https://cdn.app.com/listings/abc2.jpg", + "media_type": "image", + "is_primary": false, + "sort_order": 2 + }, + { + "media_url": "https://cdn.app.com/listings/abc3.mp4", + "media_type": "video", + "is_primary": false, + "sort_order": 3 + } + ] +} +``` + +**Response (201 Created)** + +```json +{ + "media": [ + { + "id": "media-uuid-2", + "listing_id": "listing-uuid", + "media_url": "https://cdn.app.com/listings/abc2.jpg", + "media_type": "image", + "is_primary": false, + "sort_order": 2 + }, + { + "id": "media-uuid-3", + "listing_id": "listing-uuid", + "media_url": "https://cdn.app.com/listings/abc3.mp4", + "media_type": "video", + "is_primary": false, + "sort_order": 3 + } + ] +} +``` + +--- + +### 2.9 Update Media (PUT for Images/Media) + +#### `PUT /listing-media/{media_id}` + +**Purpose** + +Update a single media item (e.g. mark as primary, change sort order, fix URL). + +**Request (JSON)** + +```json +{ + "is_primary": true, + "sort_order": 1 +} +``` + +**Response (200 OK)** + +```json +{ + "id": "media-uuid-2", + "listing_id": "listing-uuid", + "media_url": "https://cdn.app.com/listings/abc2.jpg", + "media_type": "image", + "is_primary": true, + "sort_order": 1, + "created_at": "2025-11-22T10:10:00Z", + "updated_at": "2025-11-22T10:20:00Z" +} +``` + +---````markdown + +# Livestock Marketplace – Listing Service Spec + +This document covers: + +1. All DB tables required for animal listings +2. API endpoints – purpose, request, and response formats + +--- + +## 1. Database Tables + +> Convention: Every table has `created_at` and `updated_at` (`TIMESTAMP`). + +### 1.1 `users` (reference) + +Minimal definition (assuming you already have auth elsewhere). + +| Column | Type | Constraints | Description | +| ---------- | --------- | ---------------- | ---------------------- | +| id | UUID | PK | User ID (seller/buyer) | +| name | VARCHAR | NOT NULL | Display name | +| phone | VARCHAR | UNIQUE, NOT NULL | Phone number | +| created_at | TIMESTAMP | NOT NULL | Row created time | +| updated_at | TIMESTAMP | NOT NULL | Row last updated time | + +**Relationships** + +- 1 `user` → N `listings` +- 1 `user` → N `locations` (saved addresses only) + +--- + +### 1.2 `species` + +| Column | Type | Constraints | Description | +| ---------- | --------- | ----------- | -------------------------- | +| id | INT | PK | Species ID | +| name | VARCHAR | UNIQUE | e.g. Cattle, Buffalo, Goat | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `species` → N `breeds` +- 1 `species` → N `animals` + +--- + +### 1.3 `breeds` + +| Column | Type | Constraints | Description | +| ----------- | --------- | --------------- | ---------------- | +| id | INT | PK | Breed ID | +| species_id | INT | FK → species.id | Parent species | +| name | VARCHAR | NOT NULL | e.g. Gir, Murrah | +| description | TEXT | NULL | Optional notes | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `species` → N `breeds` +- 1 `breed` → N `animals` + +--- + +### 1.4 `locations` + +Used both for: + +- **Captured locations** (no user, not saved; `user_id = NULL`, `is_saved_address = false`) +- **Saved addresses** for a user (e.g. farm, home; `user_id` set, `is_saved_address = true`) + +| Column | Type | Constraints | Description | +| ----------------- | --------- | --------------------------- | --------------------------------------------------------------------- | +| id | UUID | PK | Location ID | +| user_id | UUID | FK → users.id, NULLABLE | Owner if this is a saved address; NULL if just captured for a listing | +| is_saved_address | BOOLEAN | NOT NULL, default false | True if user chose to save it as an address | +| location_type | VARCHAR | NULL (enum suggestion) | e.g. `farm`, `home`, `office`, `other` | +| country | VARCHAR | NULL | Country | +| state | VARCHAR | NULL | State | +| district | VARCHAR | NULL | District | +| city_village | VARCHAR | NULL | City / village | +| pincode | VARCHAR | NULL | Postal code | +| lat | DECIMAL | NULL | Latitude | +| lng | DECIMAL | NULL | Longitude | +| source_type | VARCHAR | NOT NULL, default `unknown` | `device_gps`, `manual`, `unknown` | +| source_confidence | VARCHAR | NOT NULL, default `medium` | `high`, `medium`, `low` | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Interpretation** + +- **Captured only**: `user_id = NULL`, `is_saved_address = false` +- **Captured + saved as user’s farm/home**: set `user_id`, `is_saved_address = true`, `location_type = 'farm'` (or similar). + +**Relationships** + +- 1 `user` → N `locations` (saved addresses) +- 1 `location` → N `animals` (many animals can share same farm address) + +--- + +### 1.5 `animals` + +One animal per listing (enforced via unique constraint at `listings.animal_id`). + +| Column | Type | Constraints | Description | +| -------------------------- | --------- | ------------------------- | ----------------------------------------------- | +| id | UUID | PK | Animal ID | +| species_id | INT | FK → species.id, NOT NULL | Species | +| breed_id | INT | FK → breeds.id, NULL | Breed (optional) | +| sex | VARCHAR | NOT NULL | `M`, `F`, `Neutered` | +| age_months | INT | NULL | Age in months | +| weight_kg | DECIMAL | NULL | Approx weight | +| color_markings | VARCHAR | NULL | Color / markings | +| quantity | INT | NOT NULL, default 1 | Number of animals in this listing | +| purpose | VARCHAR | NOT NULL | `dairy`, `meat`, `breeding`, `pet`, `work`, etc | +| health_status | VARCHAR | NOT NULL | `healthy`, `minor_issues`, `serious_issues` | +| vaccinated | BOOLEAN | NOT NULL, default false | | +| dewormed | BOOLEAN | NOT NULL, default false | | +| previous_pregnancies_count | INT | NULL | For females, number of previous pregnancies | +| pregnancy_status | VARCHAR | NULL | `not_pregnant`, `pregnant`, `recently_calved` | +| milk_yield_litre_per_day | DECIMAL | NULL | Avg daily milk yield | +| ear_tag_no | VARCHAR | NULL | Tag / registration ID | +| description | TEXT | NULL | Detailed description | +| suggested_care | TEXT | NULL | Suggested food & accessories (free-text) | +| location_id | UUID | FK → locations.id, NULL | Location of animal (farm etc.). NULL if unknown | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `species` → N `animals` +- 1 `breed` → N `animals` +- 1 `location` → N `animals` +- 1 `animal` ↔ 1 `listing` (via `listings.animal_id` UNIQUE) + +--- + +### 1.6 `listings` + +One listing per animal (1–1). + +| Column | Type | Constraints | Description | +| ------------------------ | --------- | --------------------------------- | ------------------------------------- | +| id | UUID | PK | Listing ID | +| seller_id | UUID | FK → users.id, NOT NULL | Seller | +| animal_id | UUID | FK → animals.id, UNIQUE, NOT NULL | The animal this listing is for | +| title | VARCHAR | NOT NULL | Listing title | +| price | DECIMAL | NOT NULL | Asking price | +| currency | VARCHAR | NOT NULL, e.g. `INR` | Currency code | +| is_negotiable | BOOLEAN | NOT NULL, default true | Price negotiable | +| listing_type | VARCHAR | NOT NULL | `sale`, `stud_service`, `adoption` | +| status | VARCHAR | NOT NULL, default `active` | `active`, `sold`, `expired`, `hidden` | +| views_count | INT | NOT NULL, default 0 | Total views | +| bookmarks_count | INT | NOT NULL, default 0 | Times bookmarked | +| enquiries_call_count | INT | NOT NULL, default 0 | Phone enquiries | +| enquiries_whatsapp_count | INT | NOT NULL, default 0 | WhatsApp enquiries | +| clicks_count | INT | NOT NULL, default 0 | Other CTA clicks (e.g. “View number”) | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `user` → N `listings` +- 1 `animal` ↔ 1 `listing` +- 1 `listing` → N `listing_media` + +--- + +### 1.7 `listing_media` (images / videos) + +| Column | Type | Constraints | Description | +| ---------- | --------- | ----------------------- | -------------------------------- | +| id | UUID | PK | Media ID | +| listing_id | UUID | FK → listings.id | Parent listing | +| media_url | VARCHAR | NOT NULL | URL to image/video | +| media_type | VARCHAR | NOT NULL | `image`, `video` | +| is_primary | BOOLEAN | NOT NULL, default false | True if main display image/video | +| sort_order | INT | NOT NULL, default 0 | For ordering media in gallery | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `listing` → N `listing_media` + +--- + +### 1.8 Relationship Summary + +- **1–N** + + - `users` → `listings` + - `users` → `locations` (saved addresses) + - `species` → `breeds` + - `species` → `animals` + - `breeds` → `animals` + - `locations` → `animals` + - `listings` → `listing_media` + +- **1–1** + + - `animals` ↔ `listings` (enforced via `listings.animal_id` UNIQUE) + +- **N–M** + - None currently; all many-to-many are avoided in this MVP schema. + +--- + +## 2. API Endpoints + +### 2.1 Create Listing (with Animal + optional Location) + +#### `POST /listings` + +**Purpose** + +Create a new listing and its animal. Optionally: + +- Use an existing `location_id` or +- Create a new captured/saved location in the same call. + +**Request (JSON)** + +```json +{ + "seller_id": "UUID-of-seller", + "title": "High-yield Gir cow for sale", + "price": 55000, + "currency": "INR", + "is_negotiable": true, + "listing_type": "sale", + "animal": { + "species_id": 1, + "breed_id": 10, + "sex": "F", + "age_months": 36, + "weight_kg": 450, + "color_markings": "Brown with white patches", + "quantity": 1, + "purpose": "dairy", + "health_status": "healthy", + "vaccinated": true, + "dewormed": true, + "previous_pregnancies_count": 1, + "pregnancy_status": "pregnant", + "milk_yield_litre_per_day": 15, + "ear_tag_no": "TAG-12345", + "description": "Calm nature, easy to handle.", + "suggested_care": "Green fodder, mineral mix, clean shed.", + "location_id": "existing-location-uuid", + "new_location": { + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high", + "save_as_address": true, + "location_type": "farm" + } + }, + "media": [ + { + "media_url": "https://cdn.app.com/listings/abc1.jpg", + "media_type": "image", + "is_primary": true, + "sort_order": 1 + } + ] +} +``` + +Notes: + +- Client can either: + + - Provide `location_id`, **or** + - Provide `new_location` object. If `save_as_address = true`, backend should create a `locations` row with `user_id = seller_id`, `is_saved_address = true`. + +- Media is optional in this first call; can also be added later via media APIs. + +**Response (201 Created)** + +```json +{ + "listing": { + "id": "listing-uuid", + "seller_id": "UUID-of-seller", + "animal_id": "animal-uuid", + "title": "High-yield Gir cow for sale", + "price": 55000, + "currency": "INR", + "is_negotiable": true, + "listing_type": "sale", + "status": "active", + "views_count": 0, + "bookmarks_count": 0, + "enquiries_call_count": 0, + "enquiries_whatsapp_count": 0, + "clicks_count": 0, + "created_at": "2025-11-22T10:00:00Z", + "updated_at": "2025-11-22T10:00:00Z", + "animal": { + "id": "animal-uuid", + "species_id": 1, + "breed_id": 10, + "sex": "F", + "age_months": 36, + "weight_kg": 450, + "color_markings": "Brown with white patches", + "quantity": 1, + "purpose": "dairy", + "health_status": "healthy", + "vaccinated": true, + "dewormed": true, + "previous_pregnancies_count": 1, + "pregnancy_status": "pregnant", + "milk_yield_litre_per_day": 15, + "ear_tag_no": "TAG-12345", + "description": "Calm nature, easy to handle.", + "suggested_care": "Green fodder, mineral mix, clean shed.", + "location": { + "id": "location-uuid", + "user_id": "UUID-of-seller", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high" + } + }, + "media": [ + { + "id": "media-uuid", + "media_url": "https://cdn.app.com/listings/abc1.jpg", + "media_type": "image", + "is_primary": true, + "sort_order": 1 + } + ] + } +} +``` + +--- + +### 2.2 List / Search Listings + +#### `GET /listings` + +**Purpose** + +List active listings with optional filters (species, location, price, etc.). + +**Query parameters (examples)** + +- `species_id` (int, optional) +- `breed_id` (int, optional) +- `state` (string, optional) +- `district` (string, optional) +- `min_price`, `max_price` (optional) +- `listing_type` (string, optional) +- `page`, `page_size` (for pagination) + +**Request** + +```http +GET /listings?species_id=1&state=Maharashtra&page=1&page_size=20 +``` + +**Response (200 OK)** + +```json +{ + "items": [ + { + "id": "listing-uuid", + "title": "High-yield Gir cow for sale", + "price": 55000, + "currency": "INR", + "is_negotiable": true, + "listing_type": "sale", + "status": "active", + "species_id": 1, + "breed_id": 10, + "animal_id": "animal-uuid", + "thumbnail_url": "https://cdn.app.com/listings/abc1.jpg", + "location_summary": { + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati" + }, + "created_at": "2025-11-22T10:00:00Z" + } + ], + "page": 1, + "page_size": 20, + "total": 1 +} +``` + +--- + +### 2.3 Get Listing Detail + +#### `GET /listings/{listing_id}` + +**Purpose** + +Get full details of a single listing (including animal, location, media). + +**Response (200 OK)** + +```json +{ + "id": "listing-uuid", + "seller_id": "UUID-of-seller", + "animal_id": "animal-uuid", + "title": "High-yield Gir cow for sale", + "price": 55000, + "currency": "INR", + "is_negotiable": true, + "listing_type": "sale", + "status": "active", + "views_count": 120, + "bookmarks_count": 10, + "enquiries_call_count": 5, + "enquiries_whatsapp_count": 8, + "clicks_count": 14, + "created_at": "2025-11-22T10:00:00Z", + "updated_at": "2025-11-22T11:00:00Z", + "animal": { + "...": "full animal object as above" + }, + "media": [ + { + "id": "media-uuid", + "media_url": "https://cdn.app.com/listings/abc1.jpg", + "media_type": "image", + "is_primary": true, + "sort_order": 1 + } + ] +} +``` + +--- + +### 2.4 Update Listing (and Animal) + +#### `PUT /listings/{listing_id}` + +**Purpose** + +Edit listing fields and animal details (e.g. price, status, description, suggested care). + +**Request (JSON)** + +Only fields to update need to be sent (PATCH style with PUT semantics). + +```json +{ + "title": "Gir cow – price reduced", + "price": 52000, + "status": "active", + "animal": { + "description": "Price reduced, urgent sale.", + "suggested_care": "Green fodder, clean water, regular deworming." + } +} +``` + +**Response (200 OK)** + +Returns updated listing object (same shape as `GET /listings/{id}`). + +--- + +### 2.5 Create / Capture Location + +#### `POST /locations` + +**Purpose** + +Create a new location. Used for: + +- Saved address for a user (farm/home) +- Captured location for an animal/listing (not necessarily saved) + +**Request (JSON)** + +```json +{ + "user_id": "UUID-of-user-or-null", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high" +} +``` + +- For **captured-only** (not saved): set `user_id = null`, `is_saved_address = false`. + +**Response (201 Created)** + +```json +{ + "id": "location-uuid", + "user_id": "UUID-of-user-or-null", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high", + "created_at": "2025-11-22T10:05:00Z", + "updated_at": "2025-11-22T10:05:00Z" +} +``` + +--- + +### 2.6 Update Location (PUT for Locations) + +#### `PUT /locations/{location_id}` + +**Purpose** + +Update location details OR convert a captured location into a saved address for a user (e.g. mark as farm). + +**Request (JSON)** + +```json +{ + "user_id": "UUID-of-user", + "is_saved_address": true, + "location_type": "farm", + "city_village": "New Village Name", + "pincode": "413103" +} +``` + +**Response (200 OK)** + +```json +{ + "id": "location-uuid", + "user_id": "UUID-of-user", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "New Village Name", + "pincode": "413103", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high", + "created_at": "2025-11-22T10:05:00Z", + "updated_at": "2025-11-22T11:00:00Z" +} +``` + +--- + +### 2.7 Get Saved Locations for a User + +#### `GET /users/{user_id}/locations` + +**Purpose** + +Fetch all saved addresses for a given user (farm, home, etc.). + +**Response (200 OK)** + +```json +{ + "items": [ + { + "id": "location-uuid-1", + "user_id": "UUID-of-user", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high", + "created_at": "2025-11-22T10:00:00Z", + "updated_at": "2025-11-22T10:00:00Z" + } + ] +} +``` + +--- + +### 2.8 Add Media to Listing + +#### `POST /listings/{listing_id}/media` + +**Purpose** + +Attach new images/videos to a listing after creation. + +**Request (JSON)** + +```json +{ + "items": [ + { + "media_url": "https://cdn.app.com/listings/abc2.jpg", + "media_type": "image", + "is_primary": false, + "sort_order": 2 + }, + { + "media_url": "https://cdn.app.com/listings/abc3.mp4", + "media_type": "video", + "is_primary": false, + "sort_order": 3 + } + ] +} +``` + +**Response (201 Created)** + +```json +{ + "media": [ + { + "id": "media-uuid-2", + "listing_id": "listing-uuid", + "media_url": "https://cdn.app.com/listings/abc2.jpg", + "media_type": "image", + "is_primary": false, + "sort_order": 2 + }, + { + "id": "media-uuid-3", + "listing_id": "listing-uuid", + "media_url": "https://cdn.app.com/listings/abc3.mp4", + "media_type": "video", + "is_primary": false, + "sort_order": 3 + } + ] +} +``` + +--- + +### 2.9 Update Media (PUT for Images/Media) + +#### `PUT /listing-media/{media_id}` + +**Purpose** + +Update a single media item (e.g. mark as primary, change sort order, fix URL). + +**Request (JSON)** + +```json +{ + "is_primary": true, + "sort_order": 1 +} +``` + +**Response (200 OK)** + +```json +{ + "id": "media-uuid-2", + "listing_id": "listing-uuid", + "media_url": "https://cdn.app.com/listings/abc2.jpg", + "media_type": "image", + "is_primary": true, + "sort_order": 1, + "created_at": "2025-11-22T10:10:00Z", + "updated_at": "2025-11-22T10:20:00Z" +} +``` + +--- + +````markdown +# Livestock Marketplace – Listing Service Spec + +This document covers: + +1. All DB tables required for animal listings +2. API endpoints – purpose, request, and response formats + +--- + +## 1. Database Tables + +> Convention: Every table has `created_at` and `updated_at` (`TIMESTAMP`). + +### 1.1 `users` (reference) + +Minimal definition (assuming you already have auth elsewhere). + +| Column | Type | Constraints | Description | +| ---------- | --------- | ---------------- | ---------------------- | +| id | UUID | PK | User ID (seller/buyer) | +| name | VARCHAR | NOT NULL | Display name | +| phone | VARCHAR | UNIQUE, NOT NULL | Phone number | +| created_at | TIMESTAMP | NOT NULL | Row created time | +| updated_at | TIMESTAMP | NOT NULL | Row last updated time | + +**Relationships** + +- 1 `user` → N `listings` +- 1 `user` → N `locations` (saved addresses only) + +--- + +### 1.2 `species` + +| Column | Type | Constraints | Description | +| ---------- | --------- | ----------- | -------------------------- | +| id | INT | PK | Species ID | +| name | VARCHAR | UNIQUE | e.g. Cattle, Buffalo, Goat | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `species` → N `breeds` +- 1 `species` → N `animals` + +--- + +### 1.3 `breeds` + +| Column | Type | Constraints | Description | +| ----------- | --------- | --------------- | ---------------- | +| id | INT | PK | Breed ID | +| species_id | INT | FK → species.id | Parent species | +| name | VARCHAR | NOT NULL | e.g. Gir, Murrah | +| description | TEXT | NULL | Optional notes | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `species` → N `breeds` +- 1 `breed` → N `animals` + +--- + +### 1.4 `locations` + +Used both for: + +- **Captured locations** (no user, not saved; `user_id = NULL`, `is_saved_address = false`) +- **Saved addresses** for a user (e.g. farm, home; `user_id` set, `is_saved_address = true`) + +| Column | Type | Constraints | Description | +| ----------------- | --------- | --------------------------- | --------------------------------------------------------------------- | +| id | UUID | PK | Location ID | +| user_id | UUID | FK → users.id, NULLABLE | Owner if this is a saved address; NULL if just captured for a listing | +| is_saved_address | BOOLEAN | NOT NULL, default false | True if user chose to save it as an address | +| location_type | VARCHAR | NULL (enum suggestion) | e.g. `farm`, `home`, `office`, `other` | +| country | VARCHAR | NULL | Country | +| state | VARCHAR | NULL | State | +| district | VARCHAR | NULL | District | +| city_village | VARCHAR | NULL | City / village | +| pincode | VARCHAR | NULL | Postal code | +| lat | DECIMAL | NULL | Latitude | +| lng | DECIMAL | NULL | Longitude | +| source_type | VARCHAR | NOT NULL, default `unknown` | `device_gps`, `manual`, `unknown` | +| source_confidence | VARCHAR | NOT NULL, default `medium` | `high`, `medium`, `low` | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Interpretation** + +- **Captured only**: `user_id = NULL`, `is_saved_address = false` +- **Captured + saved as user’s farm/home**: set `user_id`, `is_saved_address = true`, `location_type = 'farm'` (or similar). + +**Relationships** + +- 1 `user` → N `locations` (saved addresses) +- 1 `location` → N `animals` (many animals can share same farm address) + +--- + +### 1.5 `animals` + +One animal per listing (enforced via unique constraint at `listings.animal_id`). + +| Column | Type | Constraints | Description | +| -------------------------- | --------- | ------------------------- | ----------------------------------------------- | +| id | UUID | PK | Animal ID | +| species_id | INT | FK → species.id, NOT NULL | Species | +| breed_id | INT | FK → breeds.id, NULL | Breed (optional) | +| sex | VARCHAR | NOT NULL | `M`, `F`, `Neutered` | +| age_months | INT | NULL | Age in months | +| weight_kg | DECIMAL | NULL | Approx weight | +| color_markings | VARCHAR | NULL | Color / markings | +| quantity | INT | NOT NULL, default 1 | Number of animals in this listing | +| purpose | VARCHAR | NOT NULL | `dairy`, `meat`, `breeding`, `pet`, `work`, etc | +| health_status | VARCHAR | NOT NULL | `healthy`, `minor_issues`, `serious_issues` | +| vaccinated | BOOLEAN | NOT NULL, default false | | +| dewormed | BOOLEAN | NOT NULL, default false | | +| previous_pregnancies_count | INT | NULL | For females, number of previous pregnancies | +| pregnancy_status | VARCHAR | NULL | `not_pregnant`, `pregnant`, `recently_calved` | +| milk_yield_litre_per_day | DECIMAL | NULL | Avg daily milk yield | +| ear_tag_no | VARCHAR | NULL | Tag / registration ID | +| description | TEXT | NULL | Detailed description | +| suggested_care | TEXT | NULL | Suggested food & accessories (free-text) | +| location_id | UUID | FK → locations.id, NULL | Location of animal (farm etc.). NULL if unknown | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `species` → N `animals` +- 1 `breed` → N `animals` +- 1 `location` → N `animals` +- 1 `animal` ↔ 1 `listing` (via `listings.animal_id` UNIQUE) + +--- + +### 1.6 `listings` + +One listing per animal (1–1). + +| Column | Type | Constraints | Description | +| ------------------------ | --------- | --------------------------------- | ------------------------------------- | +| id | UUID | PK | Listing ID | +| seller_id | UUID | FK → users.id, NOT NULL | Seller | +| animal_id | UUID | FK → animals.id, UNIQUE, NOT NULL | The animal this listing is for | +| title | VARCHAR | NOT NULL | Listing title | +| price | DECIMAL | NOT NULL | Asking price | +| currency | VARCHAR | NOT NULL, e.g. `INR` | Currency code | +| is_negotiable | BOOLEAN | NOT NULL, default true | Price negotiable | +| listing_type | VARCHAR | NOT NULL | `sale`, `stud_service`, `adoption` | +| status | VARCHAR | NOT NULL, default `active` | `active`, `sold`, `expired`, `hidden` | +| views_count | INT | NOT NULL, default 0 | Total views | +| bookmarks_count | INT | NOT NULL, default 0 | Times bookmarked | +| enquiries_call_count | INT | NOT NULL, default 0 | Phone enquiries | +| enquiries_whatsapp_count | INT | NOT NULL, default 0 | WhatsApp enquiries | +| clicks_count | INT | NOT NULL, default 0 | Other CTA clicks (e.g. “View number”) | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `user` → N `listings` +- 1 `animal` ↔ 1 `listing` +- 1 `listing` → N `listing_media` + +--- + +### 1.7 `listing_media` (images / videos) + +| Column | Type | Constraints | Description | +| ---------- | --------- | ----------------------- | -------------------------------- | +| id | UUID | PK | Media ID | +| listing_id | UUID | FK → listings.id | Parent listing | +| media_url | VARCHAR | NOT NULL | URL to image/video | +| media_type | VARCHAR | NOT NULL | `image`, `video` | +| is_primary | BOOLEAN | NOT NULL, default false | True if main display image/video | +| sort_order | INT | NOT NULL, default 0 | For ordering media in gallery | +| created_at | TIMESTAMP | NOT NULL | | +| updated_at | TIMESTAMP | NOT NULL | | + +**Relationships** + +- 1 `listing` → N `listing_media` + +--- + +### 1.8 Relationship Summary + +- **1–N** + + - `users` → `listings` + - `users` → `locations` (saved addresses) + - `species` → `breeds` + - `species` → `animals` + - `breeds` → `animals` + - `locations` → `animals` + - `listings` → `listing_media` + +- **1–1** + + - `animals` ↔ `listings` (enforced via `listings.animal_id` UNIQUE) + +- **N–M** + - None currently; all many-to-many are avoided in this MVP schema. + +--- + +## 2. API Endpoints + +### 2.1 Create Listing (with Animal + optional Location) + +#### `POST /listings` + +**Purpose** + +Create a new listing and its animal. Optionally: + +- Use an existing `location_id` or +- Create a new captured/saved location in the same call. + +**Request (JSON)** + +```json +{ + "seller_id": "UUID-of-seller", + "title": "High-yield Gir cow for sale", + "price": 55000, + "currency": "INR", + "is_negotiable": true, + "listing_type": "sale", + "animal": { + "species_id": 1, + "breed_id": 10, + "sex": "F", + "age_months": 36, + "weight_kg": 450, + "color_markings": "Brown with white patches", + "quantity": 1, + "purpose": "dairy", + "health_status": "healthy", + "vaccinated": true, + "dewormed": true, + "previous_pregnancies_count": 1, + "pregnancy_status": "pregnant", + "milk_yield_litre_per_day": 15, + "ear_tag_no": "TAG-12345", + "description": "Calm nature, easy to handle.", + "suggested_care": "Green fodder, mineral mix, clean shed.", + "location_id": "existing-location-uuid", + "new_location": { + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high", + "save_as_address": true, + "location_type": "farm" + } + }, + "media": [ + { + "media_url": "https://cdn.app.com/listings/abc1.jpg", + "media_type": "image", + "is_primary": true, + "sort_order": 1 + } + ] +} +``` +```` + +Notes: + +- Client can either: + + - Provide `location_id`, **or** + - Provide `new_location` object. If `save_as_address = true`, backend should create a `locations` row with `user_id = seller_id`, `is_saved_address = true`. + +- Media is optional in this first call; can also be added later via media APIs. + +**Response (201 Created)** + +```json +{ + "listing": { + "id": "listing-uuid", + "seller_id": "UUID-of-seller", + "animal_id": "animal-uuid", + "title": "High-yield Gir cow for sale", + "price": 55000, + "currency": "INR", + "is_negotiable": true, + "listing_type": "sale", + "status": "active", + "views_count": 0, + "bookmarks_count": 0, + "enquiries_call_count": 0, + "enquiries_whatsapp_count": 0, + "clicks_count": 0, + "created_at": "2025-11-22T10:00:00Z", + "updated_at": "2025-11-22T10:00:00Z", + "animal": { + "id": "animal-uuid", + "species_id": 1, + "breed_id": 10, + "sex": "F", + "age_months": 36, + "weight_kg": 450, + "color_markings": "Brown with white patches", + "quantity": 1, + "purpose": "dairy", + "health_status": "healthy", + "vaccinated": true, + "dewormed": true, + "previous_pregnancies_count": 1, + "pregnancy_status": "pregnant", + "milk_yield_litre_per_day": 15, + "ear_tag_no": "TAG-12345", + "description": "Calm nature, easy to handle.", + "suggested_care": "Green fodder, mineral mix, clean shed.", + "location": { + "id": "location-uuid", + "user_id": "UUID-of-seller", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high" + } + }, + "media": [ + { + "id": "media-uuid", + "media_url": "https://cdn.app.com/listings/abc1.jpg", + "media_type": "image", + "is_primary": true, + "sort_order": 1 + } + ] + } +} +``` + +--- + +### 2.2 List / Search Listings + +#### `GET /listings` + +**Purpose** + +List active listings with optional filters (species, location, price, etc.). + +**Query parameters (examples)** + +- `species_id` (int, optional) +- `breed_id` (int, optional) +- `state` (string, optional) +- `district` (string, optional) +- `min_price`, `max_price` (optional) +- `listing_type` (string, optional) +- `page`, `page_size` (for pagination) + +**Request** + +```http +GET /listings?species_id=1&state=Maharashtra&page=1&page_size=20 +``` + +**Response (200 OK)** + +```json +{ + "items": [ + { + "id": "listing-uuid", + "title": "High-yield Gir cow for sale", + "price": 55000, + "currency": "INR", + "is_negotiable": true, + "listing_type": "sale", + "status": "active", + "species_id": 1, + "breed_id": 10, + "animal_id": "animal-uuid", + "thumbnail_url": "https://cdn.app.com/listings/abc1.jpg", + "location_summary": { + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati" + }, + "created_at": "2025-11-22T10:00:00Z" + } + ], + "page": 1, + "page_size": 20, + "total": 1 +} +``` + +--- + +### 2.3 Get Listing Detail + +#### `GET /listings/{listing_id}` + +**Purpose** + +Get full details of a single listing (including animal, location, media). + +**Response (200 OK)** + +```json +{ + "id": "listing-uuid", + "seller_id": "UUID-of-seller", + "animal_id": "animal-uuid", + "title": "High-yield Gir cow for sale", + "price": 55000, + "currency": "INR", + "is_negotiable": true, + "listing_type": "sale", + "status": "active", + "views_count": 120, + "bookmarks_count": 10, + "enquiries_call_count": 5, + "enquiries_whatsapp_count": 8, + "clicks_count": 14, + "created_at": "2025-11-22T10:00:00Z", + "updated_at": "2025-11-22T11:00:00Z", + "animal": { + "...": "full animal object as above" + }, + "media": [ + { + "id": "media-uuid", + "media_url": "https://cdn.app.com/listings/abc1.jpg", + "media_type": "image", + "is_primary": true, + "sort_order": 1 + } + ] +} +``` + +--- + +### 2.4 Update Listing (and Animal) + +#### `PUT /listings/{listing_id}` + +**Purpose** + +Edit listing fields and animal details (e.g. price, status, description, suggested care). + +**Request (JSON)** + +Only fields to update need to be sent (PATCH style with PUT semantics). + +```json +{ + "title": "Gir cow – price reduced", + "price": 52000, + "status": "active", + "animal": { + "description": "Price reduced, urgent sale.", + "suggested_care": "Green fodder, clean water, regular deworming." + } +} +``` + +**Response (200 OK)** + +Returns updated listing object (same shape as `GET /listings/{id}`). + +--- + +### 2.5 Create / Capture Location + +#### `POST /locations` + +**Purpose** + +Create a new location. Used for: + +- Saved address for a user (farm/home) +- Captured location for an animal/listing (not necessarily saved) + +**Request (JSON)** + +```json +{ + "user_id": "UUID-of-user-or-null", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high" +} +``` + +- For **captured-only** (not saved): set `user_id = null`, `is_saved_address = false`. + +**Response (201 Created)** + +```json +{ + "id": "location-uuid", + "user_id": "UUID-of-user-or-null", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high", + "created_at": "2025-11-22T10:05:00Z", + "updated_at": "2025-11-22T10:05:00Z" +} +``` + +--- + +### 2.6 Update Location (PUT for Locations) + +#### `PUT /locations/{location_id}` + +**Purpose** + +Update location details OR convert a captured location into a saved address for a user (e.g. mark as farm). + +**Request (JSON)** + +```json +{ + "user_id": "UUID-of-user", + "is_saved_address": true, + "location_type": "farm", + "city_village": "New Village Name", + "pincode": "413103" +} +``` + +**Response (200 OK)** + +```json +{ + "id": "location-uuid", + "user_id": "UUID-of-user", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "New Village Name", + "pincode": "413103", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high", + "created_at": "2025-11-22T10:05:00Z", + "updated_at": "2025-11-22T11:00:00Z" +} +``` + +--- + +### 2.7 Get Saved Locations for a User + +#### `GET /users/{user_id}/locations` + +**Purpose** + +Fetch all saved addresses for a given user (farm, home, etc.). + +**Response (200 OK)** + +```json +{ + "items": [ + { + "id": "location-uuid-1", + "user_id": "UUID-of-user", + "is_saved_address": true, + "location_type": "farm", + "country": "India", + "state": "Maharashtra", + "district": "Pune", + "city_village": "Baramati", + "pincode": "413102", + "lat": 18.15, + "lng": 74.5833, + "source_type": "device_gps", + "source_confidence": "high", + "created_at": "2025-11-22T10:00:00Z", + "updated_at": "2025-11-22T10:00:00Z" + } + ] +} +``` + +--- + +### 2.8 Add Media to Listing + +#### `POST /listings/{listing_id}/media` + +**Purpose** + +Attach new images/videos to a listing after creation. + +**Request (JSON)** + +```json +{ + "items": [ + { + "media_url": "https://cdn.app.com/listings/abc2.jpg", + "media_type": "image", + "is_primary": false, + "sort_order": 2 + }, + { + "media_url": "https://cdn.app.com/listings/abc3.mp4", + "media_type": "video", + "is_primary": false, + "sort_order": 3 + } + ] +} +``` + +**Response (201 Created)** + +```json +{ + "media": [ + { + "id": "media-uuid-2", + "listing_id": "listing-uuid", + "media_url": "https://cdn.app.com/listings/abc2.jpg", + "media_type": "image", + "is_primary": false, + "sort_order": 2 + }, + { + "id": "media-uuid-3", + "listing_id": "listing-uuid", + "media_url": "https://cdn.app.com/listings/abc3.mp4", + "media_type": "video", + "is_primary": false, + "sort_order": 3 + } + ] +} +``` + +--- + +### 2.9 Update Media (PUT for Images/Media) + +#### `PUT /listing-media/{media_id}` + +**Purpose** + +Update a single media item (e.g. mark as primary, change sort order, fix URL). + +**Request (JSON)** + +```json +{ + "is_primary": true, + "sort_order": 1 +} +``` + +**Response (200 OK)** + +```json +{ + "id": "media-uuid-2", + "listing_id": "listing-uuid", + "media_url": "https://cdn.app.com/listings/abc2.jpg", + "media_type": "image", + "is_primary": true, + "sort_order": 1, + "created_at": "2025-11-22T10:10:00Z", + "updated_at": "2025-11-22T10:20:00Z" +} +``` + +--- + +### 2.10 Create a Custom Requirement + +#### `POST /requirements` + +**Purpose** + +Custom Requirements allow buyers to express needs such as +"Looking for a cow giving more than 10 litres of milk per day." +These relate to an animal_id and are visible to sellers while listing animals. + +**Request Body** + +```json +{ + "buyer_id": 12, + "animal_id": 201, + "title": "Cow giving 10+ litres milk", + "description": "Healthy cow with high milk output", + "min_price": 30000, + "max_price": 60000, + "location": "Pune" +} +``` + +**Response** + +```json +{ + "requirement_id": 501, + "message": "Custom requirement created successfully" +} +``` + +--- + +### 2.11 Update an Existing Requirement + +#### `PUT /requirements/{requirement_id}` + +**Request Body** + +```json +{ + "title": "Cow giving 12+ litres", + "max_price": 65000 +} +``` + +--- + +### 2.12 Delete a Requirement + +#### `DELETE /requirements/{requirement_id}` + +--- + +### 2.13 Get All Requirements for a Buyer + +#### `GET /requirements/buyer/{buyer_id}` + +**Purpose** + +Retrieves all active and past requirements of a buyer. + +--- + +### 2.14 Get Matching Requirements for an Animal (For Sellers) + +#### `GET /requirements/matching?animal_id={animal_id}` + +**Purpose** + +Used when a seller lists an animal so the system can show matching requirements. diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..3bce4b4 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,903 @@ +{ + "name": "buysellservice_livingai", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "buysellservice_livingai", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "cors": "^2.8.5", + "express": "^5.1.0", + "pg": "^8.16.3" + } + }, + "node_modules/accepts": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz", + "integrity": "sha512-5cvg6CtKwfgdmVqY1WIiXKc3Q1bkRqGLi+2W/6ao+6Y7gu/RCwRuAhGEzh5B4KlszSuTLgZYuqFqo5bImjNKng==", + "dependencies": { + "mime-types": "^3.0.0", + "negotiator": "^1.0.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/body-parser": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.2.1.tgz", + "integrity": "sha512-nfDwkulwiZYQIGwxdy0RUmowMhKcFVcYXUU7m4QlKYim1rUtg83xm2yjZ40QjDuc291AJjjeSc9b++AWHSgSHw==", + "dependencies": { + "bytes": "^3.1.2", + "content-type": "^1.0.5", + "debug": "^4.4.3", + "http-errors": "^2.0.0", + "iconv-lite": "^0.7.0", + "on-finished": "^2.4.1", + "qs": "^6.14.0", + "raw-body": "^3.0.1", + "type-is": "^2.0.1" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/bytes": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", + "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/call-bound": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.4.tgz", + "integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "get-intrinsic": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/content-disposition": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-1.0.1.tgz", + "integrity": "sha512-oIXISMynqSqm241k6kcQ5UwttDILMK4BiurCfGEREw6+X9jkkpEe5T9FZaApyLGGOnFuyMWZpdolTXMtvEJ08Q==", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/content-type": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", + "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz", + "integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie-signature": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.2.2.tgz", + "integrity": "sha512-D76uU73ulSXrD1UXF4KE2TMxVVwhsnCgfAyTg9k8P6KGZjlXKrOLe4dJQKI3Bxi5wjesZoFXJWElNWBjPZMbhg==", + "engines": { + "node": ">=6.6.0" + } + }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/depd": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==" + }, + "node_modules/encodeurl": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz", + "integrity": "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-object-atoms": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", + "integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==", + "dependencies": { + "es-errors": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==" + }, + "node_modules/etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/express": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/express/-/express-5.1.0.tgz", + "integrity": "sha512-DT9ck5YIRU+8GYzzU5kT3eHGA5iL+1Zd0EutOmTE9Dtk+Tvuzd23VBU+ec7HPNSTxXYO55gPV/hq4pSBJDjFpA==", + "dependencies": { + "accepts": "^2.0.0", + "body-parser": "^2.2.0", + "content-disposition": "^1.0.0", + "content-type": "^1.0.5", + "cookie": "^0.7.1", + "cookie-signature": "^1.2.1", + "debug": "^4.4.0", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "finalhandler": "^2.1.0", + "fresh": "^2.0.0", + "http-errors": "^2.0.0", + "merge-descriptors": "^2.0.0", + "mime-types": "^3.0.0", + "on-finished": "^2.4.1", + "once": "^1.4.0", + "parseurl": "^1.3.3", + "proxy-addr": "^2.0.7", + "qs": "^6.14.0", + "range-parser": "^1.2.1", + "router": "^2.2.0", + "send": "^1.1.0", + "serve-static": "^2.2.0", + "statuses": "^2.0.1", + "type-is": "^2.0.1", + "vary": "^1.1.2" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/finalhandler": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-2.1.0.tgz", + "integrity": "sha512-/t88Ty3d5JWQbWYgaOGCCYfXRwV1+be02WqYYlL6h0lEiUAMPM8o8qKGO01YIkOHzka2up08wvgYD0mDiI+q3Q==", + "dependencies": { + "debug": "^4.4.0", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "on-finished": "^2.4.1", + "parseurl": "^1.3.3", + "statuses": "^2.0.1" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/forwarded": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", + "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/fresh": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-2.0.0.tgz", + "integrity": "sha512-Rx/WycZ60HOaqLKAi6cHRKKI7zxWbJ31MhntmtwMoaTeF7XFH9hhBp8vITaMidfljRQ6eYWCKkaTK+ykVJHP2A==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-intrinsic": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/hasown": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", + "integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/http-errors": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz", + "integrity": "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==", + "dependencies": { + "depd": "~2.0.0", + "inherits": "~2.0.4", + "setprototypeof": "~1.2.0", + "statuses": "~2.0.2", + "toidentifier": "~1.0.1" + }, + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/iconv-lite": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.0.tgz", + "integrity": "sha512-cf6L2Ds3h57VVmkZe+Pn+5APsT7FpqJtEhhieDCvrE2MK5Qk9MyffgQyuxQTm6BChfeZNtcOLHp9IcWRVcIcBQ==", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "node_modules/ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==", + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/is-promise": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-4.0.0.tgz", + "integrity": "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==" + }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/media-typer": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-1.1.0.tgz", + "integrity": "sha512-aisnrDP4GNe06UcKFnV5bfMNPBUw4jsLGaWwWfnH3v02GnBuXX2MCVn5RbrWo0j3pczUilYblq7fQ7Nw2t5XKw==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/merge-descriptors": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-2.0.0.tgz", + "integrity": "sha512-Snk314V5ayFLhp3fkUREub6WtjBfPdCPY1Ln8/8munuLuiYhsABgBVWsozAG+MWMbVEvcdcpbi9R7ww22l9Q3g==", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/mime-db": { + "version": "1.54.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.54.0.tgz", + "integrity": "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-3.0.2.tgz", + "integrity": "sha512-Lbgzdk0h4juoQ9fCKXW4by0UJqj+nOOrI9MJ1sSj4nI8aI2eo1qmvQEie4VD1glsS250n15LsWsYtCugiStS5A==", + "dependencies": { + "mime-db": "^1.54.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/negotiator": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-1.0.0.tgz", + "integrity": "sha512-8Ofs/AUQh8MaEcrlq5xOX0CQ9ypTF5dl78mjlMNfOK08fzpgTHQRQPBxcPlEtIw0yRpws+Zo/3r+5WRby7u3Gg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/object-inspect": { + "version": "1.13.4", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz", + "integrity": "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/on-finished": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz", + "integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/path-to-regexp": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.3.0.tgz", + "integrity": "sha512-7jdwVIRtsP8MYpdXSwOS0YdD0Du+qOoF/AEPIt88PcCFrZCzx41oxku1jD88hZBwbNUIEfpqvuhjFaMAqMTWnA==", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/pg": { + "version": "8.16.3", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.16.3.tgz", + "integrity": "sha512-enxc1h0jA/aq5oSDMvqyW3q89ra6XIIDZgCX9vkMrnz5DFTw/Ny3Li2lFQ+pt3L6MCgm/5o2o8HW9hiJji+xvw==", + "dependencies": { + "pg-connection-string": "^2.9.1", + "pg-pool": "^3.10.1", + "pg-protocol": "^1.10.3", + "pg-types": "2.2.0", + "pgpass": "1.0.5" + }, + "engines": { + "node": ">= 16.0.0" + }, + "optionalDependencies": { + "pg-cloudflare": "^1.2.7" + }, + "peerDependencies": { + "pg-native": ">=3.0.1" + }, + "peerDependenciesMeta": { + "pg-native": { + "optional": true + } + } + }, + "node_modules/pg-cloudflare": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/pg-cloudflare/-/pg-cloudflare-1.2.7.tgz", + "integrity": "sha512-YgCtzMH0ptvZJslLM1ffsY4EuGaU0cx4XSdXLRFae8bPP4dS5xL1tNB3k2o/N64cHJpwU7dxKli/nZ2lUa5fLg==", + "optional": true + }, + "node_modules/pg-connection-string": { + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.9.1.tgz", + "integrity": "sha512-nkc6NpDcvPVpZXxrreI/FOtX3XemeLl8E0qFr6F2Lrm/I8WOnaWNhIPK2Z7OHpw7gh5XJThi6j6ppgNoaT1w4w==" + }, + "node_modules/pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/pg-pool": { + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.10.1.tgz", + "integrity": "sha512-Tu8jMlcX+9d8+QVzKIvM/uJtp07PKr82IUOYEphaWcoBhIYkoHpLXN3qO59nAI11ripznDsEzEv8nUxBVWajGg==", + "peerDependencies": { + "pg": ">=8.0" + } + }, + "node_modules/pg-protocol": { + "version": "1.10.3", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.10.3.tgz", + "integrity": "sha512-6DIBgBQaTKDJyxnXaLiLR8wBpQQcGWuAESkRBX/t6OwA8YsqP+iVSiond2EDy6Y/dsGk8rh/jtax3js5NeV7JQ==" + }, + "node_modules/pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "dependencies": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/pgpass": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz", + "integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==", + "dependencies": { + "split2": "^4.1.0" + } + }, + "node_modules/postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==", + "engines": { + "node": ">=4" + } + }, + "node_modules/postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha512-xy3pmLuQqRBZBXDULy7KbaitYqLcmxigw14Q5sj8QBVLqEwXfeybIKVWiqAXTlcvdvb0+xkOtDbfQMOf4lST1w==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "dependencies": { + "xtend": "^4.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/proxy-addr": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", + "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==", + "dependencies": { + "forwarded": "0.2.0", + "ipaddr.js": "1.9.1" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/qs": { + "version": "6.14.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.14.0.tgz", + "integrity": "sha512-YWWTjgABSKcvs/nWBi9PycY/JiPJqOD4JA6o9Sej2AtvSGarXxKC3OQSk4pAarbdQlKAh5D4FCQkJNkW+GAn3w==", + "dependencies": { + "side-channel": "^1.1.0" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/raw-body": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-3.0.2.tgz", + "integrity": "sha512-K5zQjDllxWkf7Z5xJdV0/B0WTNqx6vxG70zJE4N0kBs4LovmEYWJzQGxC9bS9RAKu3bgM40lrd5zoLJ12MQ5BA==", + "dependencies": { + "bytes": "~3.1.2", + "http-errors": "~2.0.1", + "iconv-lite": "~0.7.0", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/router": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/router/-/router-2.2.0.tgz", + "integrity": "sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ==", + "dependencies": { + "debug": "^4.4.0", + "depd": "^2.0.0", + "is-promise": "^4.0.0", + "parseurl": "^1.3.3", + "path-to-regexp": "^8.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "node_modules/send": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/send/-/send-1.2.0.tgz", + "integrity": "sha512-uaW0WwXKpL9blXE2o0bRhoL2EGXIrZxQ2ZQ4mgcfoBxdFmQold+qWsD2jLrfZ0trjKL6vOw0j//eAwcALFjKSw==", + "dependencies": { + "debug": "^4.3.5", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "fresh": "^2.0.0", + "http-errors": "^2.0.0", + "mime-types": "^3.0.1", + "ms": "^2.1.3", + "on-finished": "^2.4.1", + "range-parser": "^1.2.1", + "statuses": "^2.0.1" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/serve-static": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-2.2.0.tgz", + "integrity": "sha512-61g9pCh0Vnh7IutZjtLGGpTA355+OPn2TyDv/6ivP2h/AdAVX9azsoxmg2/M6nZeQZNYBEwIcsne1mJd9oQItQ==", + "dependencies": { + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "parseurl": "^1.3.3", + "send": "^1.2.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==" + }, + "node_modules/side-channel": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.0.tgz", + "integrity": "sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.3", + "side-channel-list": "^1.0.0", + "side-channel-map": "^1.0.1", + "side-channel-weakmap": "^1.0.2" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-list": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.0.tgz", + "integrity": "sha512-FCLHtRD/gnpCiCHEiJLOwdmFP+wzCmDEkc9y7NsYxeF4u7Btsn1ZuwgwJGxImImHicJArLP4R0yX4c2KCrMrTA==", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-map": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz", + "integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-weakmap": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz", + "integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3", + "side-channel-map": "^1.0.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/split2": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz", + "integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==", + "engines": { + "node": ">= 10.x" + } + }, + "node_modules/statuses": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz", + "integrity": "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/type-is": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-2.0.1.tgz", + "integrity": "sha512-OZs6gsjF4vMp32qrCbiVSkrFmXtG/AZhY3t0iAMrMBiAZyV9oALtXO8hsrHbMXF9x6L3grlFuwW2oAz7cav+Gw==", + "dependencies": { + "content-type": "^1.0.5", + "media-typer": "^1.1.0", + "mime-types": "^3.0.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" + }, + "node_modules/xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", + "engines": { + "node": ">=0.4" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..ffdbf6e --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "buysellservice_livingai", + "version": "1.0.0", + "description": "", + "type": "module", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/schari2509/BuySellService_LivingAI.git" + }, + "author": "Soham Chari", + "license": "ISC", + "bugs": { + "url": "https://github.com/schari2509/BuySellService_LivingAI/issues" + }, + "homepage": "https://github.com/schari2509/BuySellService_LivingAI#readme", + "dependencies": { + "cors": "^2.8.5", + "express": "^5.1.0", + "pg": "^8.16.3" + } +} diff --git a/routes/listingRoutes.js b/routes/listingRoutes.js new file mode 100644 index 0000000..aa67fd9 --- /dev/null +++ b/routes/listingRoutes.js @@ -0,0 +1,222 @@ +import express from "express"; +const router = express.Router(); +import { Pool } from "pg"; + +const pool = new Pool({ + user: process.env.DB_USER, + host: process.env.DB_HOST, + database: process.env.DB_NAME, + password: process.env.DB_PASSWORD_D, + port: process.env.DB_PORT, +}); + +// Get all listings +router.get("/", async (req, res) => { + const speciesId = req.query.species_id; + const breedId = req.query.breed_id; + const state = req.query.state; + const district = req.query.district; + const minPrice = req.query.min_price; + const listingType = req.query.listing_type; + + let baseQuery = "SELECT * FROM listings WHERE 1=1"; + const queryParams = []; + let paramIndex = 1; + + if (speciesId) { + baseQuery += ` AND species_id = $${paramIndex}`; + queryParams.push(speciesId); + paramIndex++; + } + if (breedId) { + baseQuery += ` AND breed_id = $${paramIndex}`; + queryParams.push(breedId); + paramIndex++; + } + if (state) { + baseQuery += ` AND state = $${paramIndex}`; + queryParams.push(state); + paramIndex++; + } + if (district) { + baseQuery += ` AND district = $${paramIndex}`; + queryParams.push(district); + paramIndex++; + } + if (minPrice) { + baseQuery += ` AND price >= $${paramIndex}`; + queryParams.push(minPrice); + paramIndex++; + } + if (listingType) { + baseQuery += ` AND listing_type = $${paramIndex}`; + queryParams.push(listingType); + paramIndex++; + } + + try { + const listingsResult = await pool.query(baseQuery, queryParams); + res.status(200).json(listingsResult.rows); + } catch (error) { + res.status(500).json({ + error: "Internal Server Error in fetching listings", + }); + } +}); + +// Get listing by ID +router.get("/:id", async (req, res) => { + const listingId = req.params.id; + try { + const listingResult = await pool.query( + "SELECT * FROM listings WHERE id = $1", + [listingId] + ); + if (listingResult.rows.length === 0) { + return res.status(404).json({ error: "Listing not found" }); + } + res.status(200).json(listingResult.rows[0]); + } catch (error) { + res.status(500).json({ + error: `Internal Server Error in fetching the specified ${id} listing`, + }); + } +}); + +// Update listing by ID +router.put("/:id", async (req, res) => { + const listingId = req.params.id; + const { title, description, price } = req.body; + try { + const updateResult = await pool.query( + "UPDATE listings SET title = $1, description = $2, price = $3 WHERE id = $5 RETURNING *", + [title, description, price, listingId] + ); + if (updateResult.rows.length === 0) { + return res + .status(404) + .json({ error: "Listing not found for update" }); + } + res.status(200).json(updateResult.rows[0]); + } catch (error) { + res.status(500).json({ + error: `Internal Server Error in updating the specified ${id} listing`, + }); + } +}); + +// Submit a new listing +router.post("/", async (req, res) => { + const client = await pool.connect(); + try { + await client.query("BEGIN"); + + const { + seller_id, + title, + price, + currency, + is_negotiable, + listing_type, + animal, + } = req.body; + + // Add a new location if provided + if (!animal.location_id && req.body.new_location) { + animal.location_id = addNewLocation(req.body.new_location); + } + + const animalId = await addAnimalToListing(animal); + + const listingInsertQuery = + "INSERT INTO listings (seller_id, animal_id, title, price, currency, is_negotiable, listing_type, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, NOW(), NOW()) RETURNING *"; + const listingValues = [ + seller_id, + animalId, + title, + price, + currency, + is_negotiable, + listing_type, + ]; + const listingResult = await client.query( + listingInsertQuery, + listingValues + ); + + await client.query("COMMIT"); + res.status(201).json(listingResult.rows[0]); + } catch (error) { + await client.query("ROLLBACK"); + res.status(500).json({ + error: "Internal Server Error in creating new listing", + }); + } finally { + client.release(); + } +}); + +const addAnimalToListing = async (animal) => { + try { + const animalInsertQuery = + "INSERT INTO animals (species_id, breed_id, sex, age_months, weight_kg, color_markings, quantity, purpose, health_status, vaccinated, dewormed, previous_pregnancies_count, pregnancy_status, milk_yield_litre_per_day, ear_tag_no, description, suggested_care, location_id, created_from, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, NOW(), NOW()) RETURNING id"; + const animalValues = [ + animal.species_id, + animal.breed_id, + animal.sex, + animal.age_months, + animal.weight_kg, + animal.color_markings, + animal.quantity, + animal.purpose, + animal.health_status, + animal.vaccinated, + animal.dewormed, + animal.previous_pregnancies_count, + animal.pregnancy_status, + animal.milk_yield_litre_per_day, + animal.ear_tag_no, + animal.description, + animal.suggested_care, + animal.location_id, + ]; + const animalResult = await client.query( + animalInsertQuery, + animalValues + ); + const animalId = animalResult.rows[0].id; + return animalId; + } catch (error) { + throw new Error("Error adding animal to listing: " + error.message); + } +}; + +const addNewLocation = async (location) => { + try { + const locationInsertQuery = + "INSERT INTO locations (user_id, is_saved_address, location_type, country, state, district, city_village, pincode, lat, lng, source_type, source_confidence, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, NOW(), NOW()) RETURNING id"; + const locationValues = [ + location.user_id, + location.is_saved_address, + location.location_type, + location.country, + location.state, + location.district, + location.city_village, + location.pincode, + location.lat, + location.lng, + location.source_type, + location.source_confidence, + ]; + const locationResult = await client.query( + locationInsertQuery, + locationValues + ); + return locationResult.rows[0].id; + } catch (error) { + throw new Error("Error adding new location: " + error.message); + } +}; + +export default router; diff --git a/routes/locationRoutes.js b/routes/locationRoutes.js new file mode 100644 index 0000000..71f96c4 --- /dev/null +++ b/routes/locationRoutes.js @@ -0,0 +1,136 @@ +import express from "express"; +import { Pool } from "pg"; +const router = express.Router(); + +const pool = new Pool({ + user: process.env.DB_USER, + host: process.env.DB_HOST, + database: process.env.DB_NAME, + password: process.env.DB_PASSWORD_D, + port: process.env.DB_PORT, +}); + +// Add a new location +router.post("/", async (req, res) => { + const { + user_id, + is_saved_address, + location_type, + country, + state, + district, + city_village, + pincode, + lat, + lng, + source_type, + source_confidence, + } = req.body; + + try { + const insertQuery = + "INSERT INTO locations (user_id, is_saved_address, location_type, country, state, district, city_village, pincode, lat, lng, source_type, source_confidence) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING *"; + const values = [ + user_id, + is_saved_address, + location_type, + country, + state, + district, + city_village, + pincode, + lat, + lng, + source_type, + source_confidence, + ]; + + const result = await pool.query(insertQuery, values); + res.status(201).json({ + message: "Location added successfully", + location: result.rows[0], + }); + } catch (error) { + console.error("Error adding location:", error); + res.status(500).json({ + error: "Internal server error adding location", + }); + } +}); + +// Update a location by ID +router.put("/:id", async (req, res) => { + const locationId = req.params.id; + const { + user_id, + is_saved_address, + location_type, + country, + state, + district, + city_village, + pincode, + lat, + lng, + source_type, + source_confidence, + } = req.body; + + try { + const updateQuery = + "UPDATE locations SET user_id = $1, is_saved_address = $2, location_type = $3, country = $4, state = $5, district = $6, city_village = $7, pincode = $8, lat = $9, lng = $10, source_type = $11, source_confidence = $12 WHERE id = $13 RETURNING *"; + const values = [ + user_id, + is_saved_address, + location_type, + country, + state, + district, + city_village, + pincode, + lat, + lng, + source_type, + source_confidence, + locationId, + ]; + + const result = await pool.query(updateQuery, values); + if (result.rows.length === 0) { + return res.status(404).json({ error: "Location not found" }); + } + + res.status(200).json({ + message: "Location updated successfully", + location: result.rows[0], + }); + } catch (error) { + console.error("Error updating location:", error); + res.status(500).json({ + error: "Internal server error updating location", + }); + } +}); + +// Get location by ID +router.get("/:id", async (req, res) => { + const locationId = req.params.id; + + try { + const selectQuery = "SELECT * FROM locations WHERE id = $1"; + const result = await pool.query(selectQuery, [locationId]); + + if (result.rows.length === 0) { + return res.status(404).json({ error: "Location not found" }); + } + + res.status(200).json(result.rows[0]); + } catch (error) { + console.error("Error fetching location:", error); + res.status(500).json({ + error: "Internal server error fetching location", + }); + } +}); + +export default router; diff --git a/server.js b/server.js new file mode 100644 index 0000000..eadcac4 --- /dev/null +++ b/server.js @@ -0,0 +1,19 @@ +import express from "express"; +import cors from "cors"; + +const app = express(); +app.use(cors()); +app.use(express.json()); + +const PORT = process.env.PORT || 3200; + +// Add routes here +import listingRoutes from "./routes/listingRoutes.js"; +import locationRoutes from "./routes/locationRoutes.js"; + +app.use("/listings", listingRoutes); +app.use("/locations", locationRoutes); + +app.listen(PORT, () => { + console.log(`BuySellService is running on port ${PORT}`); +});