# Multi-Organization Support - Quick Start Guide

## What Changed?

Your database now supports **multiple organizations** (e.g., Navy, Army, Air Force) in a single system.

## New Structure

```
Organizations (Bangladesh Navy, Bangladesh Army, etc.)
   ↓
All data is now organization-specific:
   • Units
   • Departments
   • Designations
   • Ranks
   • Roles
   • Users
   • Conversations
   • Notifications
   • Feedback
   • Audit Logs
```

## Quick Deploy (Development)

### Option 1: Using Deploy Script (Recommended)
```bash
cd backend/database

# Drop and recreate database, then import schema
mysql -u root -p < deploy_fresh_schema.sql
mysql -u root -p cms_db < database_schema.sql
```

### Option 2: Manual Commands
```bash
cd backend/database

# Drop and recreate database
mysql -u root -p -e "DROP DATABASE IF EXISTS cms_db;"
mysql -u root -p -e "CREATE DATABASE cms_db;"

# Import new schema
mysql -u root -p cms_db < database_schema.sql
```

**Note**: The fresh deploy approach prevents duplicate constraint errors from previous installations.

## Production Migration

```bash
# 1. BACKUP FIRST!
mysqldump -u root -p cms_db > backup_$(date +%Y%m%d_%H%M%S).sql

# 2. Run migration
mysql -u root -p cms_db < migration_add_organizations.sql

# 3. Verify
mysql -u root -p cms_db -e "SELECT * FROM organizations;"
```

## Default Organization

All existing data belongs to:
- **ID**: 1
- **Name**: Bangladesh Navy
- **Code**: BN

## Files Created

1. **database_schema.sql** - Full schema with organizations (✅ UPDATED)
2. **deploy_fresh_schema.sql** - Quick deploy script for fresh installations (NEW)
3. **migration_add_organizations.sql** - Production migration script (NEW)
4. **ORGANIZATION_SCHEMA_MIGRATION_GUIDE.md** - Detailed migration guide
5. **SCHEMA_VERIFICATION_SUMMARY.md** - Verification checklist
6. **ORGANIZATION_QUICK_START.md** - This file

## SQL Errors Fixed

✅ **FIXED #1**: "Column count doesn't match value count"
- All 126 designation INSERT statements updated with organization_id
- All other table INSERTs updated
- All column counts now match

✅ **FIXED #2**: "Duplicate check constraint name 'feedback_chk_1'"
- Removed explicit constraint name from feedback table
- Database will auto-generate constraint name
- Use fresh deploy to avoid conflicts from old schema

## Next Steps

### Backend (Node.js)
```javascript
// Add organization filtering to queries
const contacts = await Contact.findAll({
  organization_id: req.user.organization_id || 1
});
```

### Frontend (React)
```typescript
// Display organization context
const { organization } = useAuth();
console.log('Current org:', organization.name);
```

## Verification

```sql
-- Check organizations
SELECT * FROM organizations;

-- Check data belongs to org 1
SELECT COUNT(*) FROM users WHERE organization_id = 1;
SELECT COUNT(*) FROM units WHERE organization_id = 1;

-- List all org foreign keys
SELECT TABLE_NAME, CONSTRAINT_NAME 
FROM information_schema.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME = 'organizations';
```

## Adding New Organizations

```sql
-- Example: Add Bangladesh Army
INSERT INTO organizations (name, code, type, description) VALUES
  ('Bangladesh Army', 'BA', 'Military', 'Army Forces of Bangladesh');

-- Add army-specific data
INSERT INTO units (name, code, organization_id) VALUES
  ('Army Headquarters', 'AHQ', 2);
```

## Support

📄 See detailed guides:
- `ORGANIZATION_SCHEMA_MIGRATION_GUIDE.md` - Full migration documentation
- `SCHEMA_VERIFICATION_SUMMARY.md` - Testing & verification

## Summary

✅ Organizations table created  
✅ 11 tables updated with organization_id  
✅ 9 foreign key constraints added  
✅ All SQL errors fixed  
✅ Migration scripts ready  
✅ Documentation complete  

**Status**: Ready for deployment! 🚀

