# TrackAff — Deploy Guide (Apache + root folder)

Domain: **shop.trackopia.in** · Same server, ek hi domain pe dono:
- `/api/*` → NestJS API (127.0.0.1:**4100**)
- `/*` → Next.js Web (127.0.0.1:**3100**) — /admin (merchant, embedded) + /dashboard (affiliate)

Backend tracking = **Marcadeo** (`partners.marcadeo.com`) — kuch deploy nahi karna.
Server pe **Node, PM2, PostgreSQL/pgAdmin, Apache already installed** hain, isliye install steps skip.

Yeh guide maan ke chalti hai ki code tumhare **root project folder** me unzipped hai
(jaise `~/shop.trackopia.in/` ya `/var/www/shop.trackopia.in/`) — matlab `apps/` aur
`deploy/` seedhe usi folder me hain, koi `marcadeo-shopify` wrapper nahi. Neeche `$APP`
= wahi folder.

---

## 0. Pehle confirm
```bash
ping shop.trackopia.in            # VPS ki IP aani chahiye (DNS A record)
sudo systemctl status postgresql  # Postgres chal raha hai
sudo lsof -i:4100; sudo lsof -i:3100   # dono FREE hone chahiye (koi output nahi)
```
Agar 4100/3100 busy hain to batao — ports badal denge.

---

## 1. Code root folder me
```bash
cd ~/shop.trackopia.in            # <- apna root folder (yahin sab rahega)
unzip trackaff-deploy.zip         # apps/, deploy/, DEPLOY.md seedha yahin
```
Ab saare commands **isi folder se** chalenge.

---

## 2. Database (pgAdmin GUI se aasaan)
pgAdmin me:
- Nayi **Database**: `trackaff`
- Nayi **Login Role**: `trackaff` + strong password + Privileges: Can login ✅

CREATE DATABASE trackaff;
CREATE USER trackaff WITH PASSWORD 'trAckAFf';
GRANT ALL PRIVILEGES ON DATABASE trackaff TO trackaff;
\c trackaff
GRANT ALL ON SCHEMA public TO trackaff;
\q


Phir `apps/api/.env` me **sirf** `DATABASE_URL` server wala kar do (baaki keys already bhari hain):
```bash
DATABASE_URL="postgresql://trackaff:STRONG_PASSWORD@localhost:5432/trackaff?schema=public"
```

---

## 3. API build
```bash
cd apps/api
npm install
npx prisma generate
npx prisma db push        # saari tables ban jayengi
npm run build             # dist/ banega
cd ../..
```

---

## 4. Web build
```bash
cd apps/web
rm -f .env.local          # ZAROORI — warna localhost pe point karega
npm install
npm run build
cd ../..
```

---

## 5. Dono apps PM2 se
```bash
pm2 start deploy/ecosystem.config.js
pm2 save
pm2 startup               # jo command bataye use chalao (reboot pe auto-start)
pm2 status                # trackaff-api + trackaff-web dono "online"
```
Local test: `curl http://127.0.0.1:4100/api/shopify/status` → JSON.

---

## 6. Apache reverse proxy
```bash
sudo a2enmod proxy proxy_http headers
sudo cp deploy/apache-trackaff.conf /etc/apache2/sites-available/trackaff.conf
sudo a2ensite trackaff
sudo apache2ctl configtest && sudo systemctl reload apache2
```
> Agar `shop.trackopia.in` ka koi purana vhost pehle se hai (static serve kar raha),
> to use disable karo (`sudo a2dissite <uska-naam>`) — warna conflict hoga.

Test (abhi http): `http://shop.trackopia.in/api/shopify/status` → JSON.

---

## 7. SSL (HTTPS)
```bash
sudo apt install -y python3-certbot-apache
sudo certbot --apache -d shop.trackopia.in
```
Email + terms → ho gaya. Ab `https://shop.trackopia.in` chalega (auto-renew set).

---

## 8. Partners app — App URL fix
Dev dashboard → TrackAff → Versions → config:
- **App URL** → `https://shop.trackopia.in/admin`
- **Redirect URL** → `https://shop.trackopia.in/api/auth/callback`
- Embed ✅ · Scopes ✅ · **Release**

---

## 9. Real install test 🎉
Normal browser tab me (dev store daalo):
```
https://shop.trackopia.in/api/auth?shop=YOUR-DEV-STORE.myshopify.com
```
→ Shopify **Install** → app admin khulega.

**Verify:** Marcadeo panel → Shopify Merchants me naya advertiser; Shopify admin → Apps → TrackAff embedded khule.

---

## Update loop (issue aaye to)
```bash
cd ~/shop.trackopia.in
# (naya code upload/unzip, ya git pull)
cd apps/api && npm run build && cd ../..
cd apps/web && npm run build && cd ../..
pm2 restart all
```
