Skip to main content

Locations Module

Version: 1.0
Last Updated: Februari 2025
Status: Implemented


1. Executive Summary

1.1 Overview

Locations Module adalah modul manajemen lokasi dengan dukungan struktur hierarkis (parent-child). Lokasi digunakan untuk mengorganisir aset fisik maupun logis perusahaan (gudang, kantor cabang, area penyimpanan, rak, dll.) dalam struktur tree. Setiap lokasi terhubung ke Organization Unit (branch) dan dapat memiliki policies yang mengatur kemampuan operasional lokasi tersebut.

1.2 Problem Statement

Perusahaan perlu sistem untuk:

  • Mengelola lokasi fisik/logis dalam struktur hierarkis yang fleksibel
  • Menjamin integritas hierarki (tanpa circular reference, type hierarchy valid)
  • Mengatur kemampuan operasional per lokasi (inventory, transfer, sales, dll.)
  • Mendukung multi-tenant dengan isolasi data per tenant

1.3 Target Users

PersonaRoleUse Cases
Admin/SetupHR/Admin, IT SetupCreate location structure, configure policies
OperationsWarehouse Manager, Branch ManagerManage locations, move/reassign, toggle status
Finance/ReportingFinance TeamView location tree, usage stats
IntegrationSystem/BackendConsume locations API untuk modul lain (inventory, sales, dll.)

2. Goals & Objectives

2.1 Business Goals

  • Memungkinkan organisasi mendefinisikan struktur lokasi yang sesuai bisnis (warehouse → area → rack → shelf)
  • Mendukung operasional harian: create, update, move, deactivate lokasi
  • Memastikan data konsisten dan integritas hierarki terjaga
  • Menyediakan basis data lokasi untuk integrasi dengan modul inventory, sales, procurement, dll.

2.2 Product Objectives

  1. CRUD Locations — User dapat create, read, update lokasi dengan validasi ketat
  2. Hierarchy Management — Support parent-child, move, type hierarchy rules
  3. Policy Assignment — Assign policies ke lokasi (terpisah dari create/update)
  4. Meta/Reference Data — Categories, types, policies tersedia untuk dropdown/select
  5. Audit & Usage — History perubahan dan statistik penggunaan

3. User Stories

3.1 Location CRUD

IDUser StoryAcceptance Criteria
US-01Sebagai Admin, saya ingin membuat lokasi baru agar dapat mendaftarkan gudang/kantor baruLokasi tersimpan dengan org_unit, type, category, code unik; parent opsional; hierarchy valid
US-02Sebagai Admin, saya ingin melihat daftar lokasi dengan filter agar dapat menemukan lokasi tertentuList dengan filter org_unit, type, category, search; pagination; mode tree
US-03Sebagai Admin, saya ingin melihat detail lokasi agar dapat memeriksa info lengkapDetail termasuk parent, children count, information, attributes
US-04Sebagai Admin, saya ingin update lokasi agar dapat koreksi dataPartial update; validasi circular reference & hierarchy
US-05Sebagai Admin, saya ingin soft delete lokasi agar data tidak hilangis_active = false; tidak bisa jika punya active children atau inventory

3.2 Hierarchy

IDUser StoryAcceptance Criteria
US-06Sebagai Admin, saya ingin memindahkan lokasi ke parent lain agar struktur dapat diubahMove berhasil; path_ltree terupdate; tidak boleh circular
US-07Sebagai Admin, saya ingin melihat children lokasi agar tahu struktur bawahnyaReturn direct children only
US-08Sebagai Admin, saya ingin melihat parents lokasi agar tahu chain ke rootReturn all parents dari root ke direct parent
US-09Sebagai Admin, saya ingin toggle status lokasi agar bisa deactivateTidak bisa deactivate jika ada active children

3.3 Policies

IDUser StoryAcceptance Criteria
US-10Sebagai Admin, saya ingin melihat policies lokasi agar tahu kemampuan operasionalList policy key, name, group_name
US-11Sebagai Admin, saya ingin set policies lokasi agar mengatur kemampuanReplace all policies; validasi policy key ada di core
US-12Sebagai Admin, saya ingin sync policies dari mappings agar cepat apply defaultPolicies diganti sesuai core_location_policy_mappings (category+type)
US-13Sebagai Admin, saya ingin melihat daftar semua policies (meta) agar bisa pilih saat setList dari core_location_policy; read-only

3.4 Utility & Meta

IDUser StoryAcceptance Criteria
US-14Sebagai User, saya ingin search lokasi cepat agar dapat autocomplete/typeaheadSearch by name, code, short_name; limit hasil
US-15Sebagai User, saya ingin melihat usage lokasi (inventory count, child count)Return statistics
US-16Sebagai User, saya ingin melihat audit history lokasiPaginated history dari lania-common
US-17Sebagai User, saya ingin melihat categories, types (meta) untuk formList categories, types (filter by category), type detail, attributes schema
US-18Sebagai Admin, saya ingin bulk create lokasi agar efisienCreate banyak lokasi; return success/failed per item
US-19Sebagai User, saya ingin validasi parent sebelum create agar tidak salahEndpoint validate-parent
US-20Sebagai User, saya ingin validasi attributes sebelum submitEndpoint validate-attributes

4. Functional Requirements

4.1 Core Features

FR-01: Create Location

  • Input: org_unit_id, location_type_key, category_key, name, code, is_active (required); parent_location_id, short_name, address, coordinates, attributes (optional)
  • Validation: Org unit exists; type exists; parent exists & active (if provided); type hierarchy valid; code unique in org_unit
  • Output: Location object dengan id, timestamps

FR-02: List Locations

  • Input: Query params: search, org_unit_id, op_unit_id, location_type_key[], category_key, parent_location_id, table_tree, is_active, order_by, order_direction, page, limit
  • Output: Paginated list atau tree structure (subRows)

FR-03: Get Location Detail

  • Input: location id (path)
  • Output: Full detail termasuk relations (parent, type, information, owners, documents, tags)

FR-04: Update Location

  • Input: location id; body partial (semua optional)
  • Validation: Circular reference check; hierarchy re-validation; code uniqueness
  • Output: Updated location

FR-05: Soft Delete Location

  • Validation: No active children; no inventory
  • Action: Set is_active = false

FR-06: Hard Delete Location

  • Validation: No children; must be soft-deleted first
  • Action: Permanent delete

FR-07: Move Location

  • Input: location id; new_parent_id (query)
  • Validation: New parent exists & active; not self; not descendant; type hierarchy valid
  • Output: Updated location (path recalculated)

FR-08: Toggle Status

  • Validation: Cannot deactivate if has active children
  • Action: Toggle is_active

FR-09: Get/Set/Sync Policies

  • Get: Return policies assigned to location
  • Set: Replace policies; validate all keys exist in core_location_policy
  • Sync: Replace with policies from core_location_policy_mappings (by category+type)

FR-10: Meta Endpoints

  • List categories, types, type detail, type attributes, policies
  • Policies adalah core data (read-only, di-seed)

4.2 Hierarchy Rules

RuleDescriptionEnforcement
Type HierarchyChild type level MUST be higher than parent type levelCreate, Update, Move
No Self ReferenceLocation cannot be its own parentUpdate, Move
No Circular ReferenceCannot set parent to a descendantUpdate, Move
Code UniquenessCode must be unique within same org_unitCreate, Update
Active ChildrenCannot deactivate if has active childrenToggle Status, Soft Delete
No Children for Hard DeleteMust remove all children firstHard Delete

5. Non-Functional Requirements

5.1 Security

  • Semua endpoint require authentication (Bearer token)
  • Tenant isolation: data filtered by tenant_id (X-LANYA-TID)
  • Core tables (categories, types, policies) shared; tenant tables (locations) isolated

5.2 Performance

  • Pagination default 20 items
  • Tree mode: return all (no pagination) — consider limit for very large trees
  • LTree index untuk query hierarchy efisien
  • Search dengan limit default 10

5.3 Data Integrity

  • Soft delete default; hard delete hanya untuk cleanup
  • Audit trail ke lania-common untuk create, update, delete, hard delete
  • Policy history table (LocationPolicyHistory) untuk tracking perubahan policies

5.4 Compatibility

  • UUID v4 untuk semua ID
  • ISO 8601 untuk timestamps
  • JSON untuk attributes (flexible schema per type)

6. Business Rules Summary

6.1 Location Lifecycle

  1. Create → Active
  2. Toggle Status → Active/Inactive
  3. Soft Delete → is_active = false (reversible)
  4. Hard Delete → Permanent (irreversible)

6.2 Constraints

  • Lokasi harus punya org_unit (branch)
  • Lokasi harus punya type yang valid
  • Child type level > parent type level
  • Code unique per org_unit
  • Policies tidak di-set saat create; manage terpisah

6.3 Core Data (Read-Only)

  • Categories (core_location_categories)
  • Types (core_location_types)
  • Policies (core_location_policy)
  • Policy Mappings (core_location_policy_mappings)
  • Attribute Definitions & Mappings

Data core di-seed dari JSON; user tidak bisa CRUD.


7. Out of Scope (Current Version)

  • Geographic/map visualization
  • Import/Export locations
  • Location cloning/duplication
  • Custom attributes CRUD (schema fixed per type)
  • Location templates
  • Bulk move/update
  • Location-level permissions (selain policies)
  • Inventory integration logic (hanya usage count)

8. Success Metrics

MetricTargetMeasurement
API Availability99.9%Uptime monitoring
Create/Update Latency< 500msP95 response time
Tree Query (100 nodes)< 1sP95 response time
Zero Data Integrity Violations100%No circular ref, invalid hierarchy in prod

9. Appendix

9.1 API Endpoint Summary

MethodEndpointDescription
POST/locationsCreate location
GET/locationsList locations (filter, pagination, tree)
GET/locations/treeTree structure alias
GET/locations/searchQuick search
GET/locations/:idGet detail
GET/locations/:id/childrenGet children
GET/locations/:id/parentsGet parents
GET/locations/:id/usageUsage stats
GET/locations/:id/policiesGet policies
PUT/locations/:idUpdate location
PUT/locations/:id/policiesSet policies
POST/locations/:id/policies/syncSync policies
PATCH/locations/:id/statusToggle status
POST/locations/:id/moveMove location
DELETE/locations/:idSoft delete
DELETE/locations/hard-delete/:idHard delete
POST/locations/bulkBulk create
POST/locations/validate-parentValidate parent
POST/locations/validate-attributesValidate attributes
GET/location-policiesMeta: list policies
GET/location-categoriesMeta: list categories
GET/location-typesMeta: list types
GET/location-types/:keyMeta: type detail
GET/location-types/:key/attributesMeta: type attributes

9.2 Error Codes Reference

CodeDescription
location.not-foundLocation not found
location.parent-not-foundParent location not found
location.type-not-foundLocation type not found
location.type-hierarchy-invalidType hierarchy invalid
location.circular-reference-selfCannot set as own parent
location.circular-reference-descendantCannot set parent to descendant
location.has-active-childrenHas active children (block deactivate/delete)
location.has-childrenHas children (block hard delete)
location.code-duplicateCode already exists in org unit
location.invalid-policy-keysPolicy keys not valid

9.3 Glossary

TermDefinition
LocationEntity fisik/logis (gudang, kantor, rak, dll.) dalam struktur hierarkis
Organization UnitBranch/cabang yang memiliki lokasi
CategoryKlasifikasi lokasi (warehouse, office, retail, dll.)
TypeTipe spesifik lokasi dalam category (rack, shelf, building, dll.)
PolicyKemampuan operasional (inventory.view, transfer.inbound, dll.)
Soft DeleteSet is_active = false; data tetap ada
Hard DeleteHapus permanent dari database
LTreePostgreSQL extension untuk path hierarkis
PathString path (e.g. 0001.0002.0003) merepresentasikan posisi dalam tree

9.4 Document References