From f1c3f28f958508bb2c25c1d292984d963041dc6e Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Fri, 24 Jul 2026 11:47:45 +0700 Subject: [PATCH] Add toggle status functionality for business types and update listing filter --- AGENTS.md | 47 +++++++++++++++++++ app/routers/business_types.py | 13 ++++- app/schemas/business_type.py | 4 ++ app/services/business_type_service.py | 23 +++++++-- .../2026-07-24-toggle-status-business-type.md | 33 +++++++++++++ 5 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 AGENTS.md create mode 100644 docs/history/2026-07-24-toggle-status-business-type.md diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..43cbe49 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,47 @@ +# AGENTS.md + +## Aturan Utama + +### Dokumentasi Perubahan + +**WAJIB** catat setiap perubahan yang dilakukan ke dalam folder `docs/history/` setelah selesai mengerjakan suatu fitur/bugfix/refactor. + +#### Format Penulisan + +Buat file markdown dengan nama format: `YYYY-MM-DD-singkatan-perubahan.md` + +Contoh: `2026-07-24-sidebar-icon-mode.md` + +#### Struktur Dokumen + +```markdown +# Judul Perubahan + +**Tanggal:** YYYY-MM-DD +**Status:** Selesai + +## Tujuan +Deskripsi singkat tujuan perubahan. + +## Yang Dikerjakan + +### 1. Judul Sub Perubahan +- Detail perubahan yang dilakukan +- Sebelum → Sesudah (jika ada perubahan value) + +## File yang Diubah + +| File | Aksi | Detail | +|------|------|--------| +| `path/to/file` | Diubah/Dibuat/Dihapus | Penjelasan singkat | + +## Notes +Catatan tambahan jika perlu. +``` + +#### Aturan Tambahan + +- Selalu cantumkan file apa saja yang diubah, dibuat, atau dihapus +- Gunakan tabel untuk perubahan value (sebelum → sesudah) +- Cantumkan behavior/efek perubahan jika relevan +- Dokumen harus dalam Bahasa Indonesia diff --git a/app/routers/business_types.py b/app/routers/business_types.py index be53acc..3c672ca 100644 --- a/app/routers/business_types.py +++ b/app/routers/business_types.py @@ -6,6 +6,7 @@ from app.responses import created_response from app.schemas.business_type import ( BusinessTypeCreate, BusinessTypeResponse, + BusinessTypeToggleStatus, BusinessTypeUpdate, ) from app.services.business_type_service import ( @@ -13,6 +14,7 @@ from app.services.business_type_service import ( delete_business_type, get_business_type_by_id, get_business_type_list, + toggle_business_type_status, update_business_type, ) @@ -20,8 +22,8 @@ router = APIRouter(prefix="/business-types", tags=["business-types"]) @router.get("/", response_model=list[BusinessTypeResponse]) -def list_business_types(db: Session = Depends(get_db)): - return get_business_type_list(db) +def list_business_types(is_active: bool = None, db: Session = Depends(get_db)): + return get_business_type_list(db, is_active=is_active) @router.get("/{id}", response_model=BusinessTypeResponse) @@ -48,3 +50,10 @@ def update_business_type_route( @router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT) def delete_business_type_route(id: str, db: Session = Depends(get_db)): delete_business_type(db, id) + + +@router.patch("/{id}/active", response_model=BusinessTypeResponse) +def toggle_business_type_status_route( + id: str, data: BusinessTypeToggleStatus, db: Session = Depends(get_db) +): + return toggle_business_type_status(db, id, data) diff --git a/app/schemas/business_type.py b/app/schemas/business_type.py index fba4c5d..9155a59 100644 --- a/app/schemas/business_type.py +++ b/app/schemas/business_type.py @@ -16,6 +16,10 @@ class BusinessTypeUpdate(BaseModel): is_active: bool | None = None +class BusinessTypeToggleStatus(BaseModel): + is_active: bool + + class BusinessTypeResponse(BaseModel): id: str code: str diff --git a/app/services/business_type_service.py b/app/services/business_type_service.py index 09329b6..e931a7a 100644 --- a/app/services/business_type_service.py +++ b/app/services/business_type_service.py @@ -4,11 +4,14 @@ from fastapi import HTTPException, status from sqlalchemy.orm import Session from app.models.business_type import BusinessType -from app.schemas.business_type import BusinessTypeCreate, BusinessTypeUpdate +from app.schemas.business_type import BusinessTypeCreate, BusinessTypeToggleStatus, BusinessTypeUpdate -def get_business_type_list(db: Session): - return db.query(BusinessType).filter(BusinessType.deleted_at.is_(None)).all() +def get_business_type_list(db: Session, is_active: bool = None): + query = db.query(BusinessType).filter(BusinessType.deleted_at.is_(None)) + if is_active is not None: + query = query.filter(BusinessType.is_active == is_active) + return query.all() def get_business_type_by_id(db: Session, id: str): @@ -72,3 +75,17 @@ def delete_business_type(db: Session, id: str): bt.deleted_at = datetime.now() db.commit() + + +def toggle_business_type_status(db: Session, id: str, data: BusinessTypeToggleStatus): + bt = db.query(BusinessType).filter(BusinessType.id == id, BusinessType.deleted_at.is_(None)).first() + if not bt: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail={"message": "The item does not exist"}, + ) + + bt.is_active = data.is_active + db.commit() + db.refresh(bt) + return bt diff --git a/docs/history/2026-07-24-toggle-status-business-type.md b/docs/history/2026-07-24-toggle-status-business-type.md new file mode 100644 index 0000000..caa6a60 --- /dev/null +++ b/docs/history/2026-07-24-toggle-status-business-type.md @@ -0,0 +1,33 @@ +# Endpoint Toggle Status Business Type + +**Tanggal:** 2026-07-24 +**Status:** Selesai + +## Tujuan +Menambahkan endpoint terpisah untuk toggle status (is_active) Business Type agar tidak bergabung dengan endpoint update umum. + +## Yang Dikerjakan + +### 1. Schema Toggle Status +- Menambahkan `BusinessTypeToggleStatus` dengan field `is_active: bool` (required) + +### 2. Service Toggle Status +- Menambahkan `toggle_business_type_status()` — update field `is_active` saja pada entity +- Validasi entity exists dan tidak terhapus (soft delete) + +### 3. Endpoint PATCH +- Menambahkan `PATCH /v1/business-types/{id}/active` +- Request body: `{ "is_active": true/false }` +- Response: `BusinessTypeResponse` (entity terbaru) + +## File yang Diubah + +| File | Aksi | Detail | +|------|------|--------| +| `api/app/schemas/business_type.py` | Diubah | Tambah `BusinessTypeToggleStatus` schema | +| `api/app/services/business_type_service.py` | Diubah | Tambah `toggle_business_type_status()` function | +| `api/app/routers/business_types.py` | Diubah | Tambah import + `PATCH /{id}/active` endpoint | + +## Notes +- Endpoint PATCH terpisah dari PUT agar frontend bisa toggle status tanpa mengirim field lain +- Frontend menggunakan optimistic update setelah PATCH berhasil