# Hotfix 1: Customer Sub Group

## 1. Files Added

- `drizzle/0011_brief_hobgoblin.sql`
- `docs/implementation/hotfix-1-customer-sub-group.md`

## 2. Files Modified

- `src/db/schema.ts`
- `src/db/seeds/foundation.seed.ts`
- `src/features/crm/customers/api/types.ts`
- `src/features/crm/customers/schemas/customer.schema.ts`
- `src/features/crm/customers/server/service.ts`
- `src/app/api/crm/customers/route.ts`
- `src/app/api/crm/customers/[id]/route.ts`
- `src/components/forms/fields/select-field.tsx`
- `src/features/crm/customers/components/customer-form-sheet.tsx`
- `src/features/crm/customers/components/customer-columns.tsx`
- `src/features/crm/customers/components/customer-detail.tsx`

## 3. Schema Field Used/Added

- Added optional field `customerSubGroup` to `crm_customers`
- Field is stored with the same option-id pattern already used by `customerGroup`

## 4. Master Options Seeded

- `crm_customer_group`
  - `industrial`
  - `construction`
  - `government`
  - `dealer`
  - `other`
- `crm_customer_sub_group`
  - `factory`
  - `warehouse`
  - `logistics`
  - `contractor`
  - `consultant`
  - `state_enterprise`
  - `government_agency`
  - `reseller`
  - `other`

Child options resolve and store `parentId` from the seeded `crm_customer_group` rows during idempotent seed execution.

## 5. Form Behavior Added

- Add/Edit Customer now includes `Customer Sub Group`
- Sub group is disabled until a customer group is selected
- Edit mode loads the saved group and sub group
- Submit payload now persists `customerSubGroup`

## 6. Parent/Child Filtering Behavior

- Customer sub group options are filtered by `parentId === selectedCustomerGroup`
- Changing group clears the sub group when the selected child no longer belongs to the new parent
- Placeholder shows `Select customer group first` until a parent group is chosen

## 7. Validation Added

- API schemas now accept `customerSubGroup`
- Server validation checks that:
  - the selected sub group exists in `crm_customer_sub_group`
  - a sub group cannot be submitted without a group
  - the selected sub group belongs to the selected group parent
- Invalid parent/child combinations return a user-safe `400` error

## 8. Verification Result

- `npx tsc --noEmit` passed
- CRUD wiring, list/detail output, and form filtering compile successfully

## 9. Remaining Risks

- Database migration `drizzle/0011_brief_hobgoblin.sql` still needs to be applied in the target environment
- Seed changes were updated in code, but I did not run a live seed against the database in this session
- Existing customer rows will keep `customerSubGroup = null` until edited or backfilled
