# 🚀 คู่มือการ Setup Repository สำหรับทีม ServTrack

## 📋 สารบัญ

1. [ภาพรวม](#ภาพรวม)
2. [การ Clone Repository](#การ-clone-repository)
3. [การ Setup Environment](#การ-setup-environment)
4. [การ Setup Database](#การ-setup-database)
5. [การ Run Application](#การ-run-application)
6. [การ Deploy](#การ-deploy)

---

## 🎯 ภาพรวม

เอกสารนี้เป็นคู่มือสำหรับทีม ServTrack ในการ setup และ deploy Satisfaction Survey System

---

## 📥 การ Clone Repository

### 1. Clone Repository

```bash
# Clone repository
git clone https://github.com/your-org/satisfaction-survey.git
cd satisfaction-survey

# Checkout develop branch (สำหรับ development)
git checkout develop

# หรือ checkout staging branch (สำหรับ staging environment)
git checkout staging

# หรือ checkout main branch (สำหรับ production)
git checkout main
```

### 2. Install Dependencies

```bash
# Install PHP dependencies
composer install

# Install Node.js dependencies (ถ้ามี)
npm install
```

---

## ⚙️ การ Setup Environment

### Development Environment

```bash
# Copy .env.example to .env
cp .env.example .env

# Generate application key
php artisan key:generate

# Edit .env file
# ตั้งค่า:
# - APP_ENV=local
# - SERVTRACK_INTEGRATED=false
# - Database configuration
# - Mail configuration
```

### Staging Environment

```bash
# Copy .env.staging.example to .env
cp .env.staging.example .env

# Generate application key
php artisan key:generate

# Edit .env file
# ตั้งค่า:
# - APP_ENV=staging
# - SERVTRACK_INTEGRATED=true
# - SERVTRACK_BASE_URL=https://staging.servtrack.com
# - Database configuration (staging)
# - Mail configuration (staging)
```

### Production Environment

```bash
# Copy .env.production.example to .env
cp .env.production.example .env

# Generate application key
php artisan key:generate

# Edit .env file
# ตั้งค่า:
# - APP_ENV=production
# - SERVTRACK_INTEGRATED=true
# - SERVTRACK_BASE_URL=https://servtrack.com
# - Database configuration (production)
# - Mail configuration (production)
```

---

## 🗄️ การ Setup Database

### Development (SQLite)

```bash
# SQLite database จะถูกสร้างอัตโนมัติ
# ตรวจสอบว่า database/database.sqlite มีอยู่

# Run migrations
php artisan migrate

# (Optional) Seed test data
php artisan db:seed
```

### Staging/Production (MySQL)

```bash
# สร้าง database
mysql -u root -p
CREATE DATABASE satisfaction_survey_staging CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# หรือ
CREATE DATABASE satisfaction_survey_production CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

# Run migrations
php artisan migrate

# (Optional) Seed initial data
php artisan db:seed
```

---

## 🏃 การ Run Application

### Development

```bash
# Run development server
php artisan serve

# หรือใช้ composer script
composer dev

# Application จะรันที่ http://localhost:8000
```

### Staging/Production

```bash
# Clear cache
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear

# Optimize application
php artisan config:cache
php artisan route:cache
php artisan view:cache

# Run queue worker (ถ้าใช้ queue)
php artisan queue:work

# Setup scheduled tasks (cron)
# เพิ่มใน crontab:
# * * * * * cd /path-to-project && php artisan schedule:run >> /dev/null 2>&1
```

---

## 🚀 การ Deploy

### Deploy to Staging

```bash
# 1. Pull latest code
git checkout staging
git pull origin staging

# 2. Install/Update dependencies
composer install --no-dev --optimize-autoloader
npm install
npm run build

# 3. Run migrations
php artisan migrate --force

# 4. Clear and cache config
php artisan config:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache

# 5. Restart services (ถ้าจำเป็น)
# sudo systemctl restart php-fpm
# sudo systemctl restart nginx
```

### Deploy to Production

```bash
# 1. Pull latest code
git checkout main
git pull origin main

# 2. Install/Update dependencies
composer install --no-dev --optimize-autoloader
npm install
npm run build

# 3. Run migrations (ระวัง!)
php artisan migrate --force

# 4. Clear and cache config
php artisan config:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache

# 5. Restart services
sudo systemctl restart php-fpm
sudo systemctl restart nginx

# 6. Verify deployment
# ตรวจสอบว่า application ทำงานปกติ
```

---

## 🔐 Security Checklist

### Before Deploying to Production

- [ ] ตั้งค่า `APP_DEBUG=false`
- [ ] ตั้งค่า `APP_ENV=production`
- [ ] ตั้งค่า `SERVTRACK_INTEGRATED=true`
- [ ] ตั้งค่า strong database password
- [ ] ตั้งค่า strong `APP_KEY`
- [ ] ตั้งค่า secure mail credentials
- [ ] ตรวจสอบ file permissions (storage, bootstrap/cache)
- [ ] ตั้งค่า HTTPS
- [ ] ตั้งค่า CORS (ถ้าจำเป็น)
- [ ] ตรวจสอบ firewall rules

---

## 📝 Environment Variables ที่สำคัญ

### ServTrack Integration

```env
# ตั้งค่าเป็น true เมื่อ integrate กับ ServTrack
SERVTRACK_INTEGRATED=true

# URL ของ ServTrack
SERVTRACK_BASE_URL=https://servtrack.com
SERVTRACK_LOGIN_URL=https://servtrack.com/login
SERVTRACK_SURVEY_URL=https://servtrack.com/survey
```

### Database

```env
# Development (SQLite)
DB_CONNECTION=sqlite
DB_DATABASE=database/database.sqlite

# Production (MySQL)
DB_CONNECTION=mysql
DB_HOST=your-db-host
DB_PORT=3306
DB_DATABASE=your-database
DB_USERNAME=your-username
DB_PASSWORD=your-password
```

### Mail

```env
# Gmail SMTP
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=your-app-password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="your-email@gmail.com"
MAIL_FROM_NAME="Satisfaction Survey"
```

---

## 🐛 Troubleshooting

### ปัญหา: Permission denied

```bash
# ตั้งค่า permissions สำหรับ storage และ bootstrap/cache
chmod -R 775 storage bootstrap/cache
chown -R www-data:www-data storage bootstrap/cache
```

### ปัญหา: Database connection error

```bash
# ตรวจสอบ database configuration ใน .env
# ตรวจสอบว่า database server ทำงานอยู่
# ตรวจสอบ username/password
```

### ปัญหา: Cache issues

```bash
# Clear all caches
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear

# Rebuild caches
php artisan config:cache
php artisan route:cache
php artisan view:cache
```

---

## 📞 Support

หากมีปัญหาหรือคำถาม กรุณาติดต่อทีมพัฒนา

---

**อัปเดตล่าสุด:** 2024-12-19

