summaryrefslogtreecommitdiff
path: root/DOC_IMPORT_PRODUITS.md
blob: 890d6511c8609c8f653515101c24408cf0694e9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# Import CSV Produits (Medusa)

## FR - Objectif
Importer des produits depuis un CSV dans Medusa en mode upsert (creation ou mise a jour) sur le champ `external_id`.

## FR - Prerequis
- Backend Medusa operationnel
- Dependances installees (`npm install` dans `backend/`)
- Fichier CSV present dans `backend/data/products-import.csv` (ou un autre chemin)

## FR - Commande rapide
Depuis `backend/` :
```
npm run import:products
```

Options:
```
node scripts/import-products.js --file ./data/products-import.csv --report ./data/import-report.json
node scripts/import-products.js --file ./data/products-import.csv --dry-run
node scripts/import-products.js --file ./data/products-import.csv --currency eur
```

## FR - Schema CSV
Colonnes minimales:
- `external_id` (obligatoire, cle d'upsert)
- `title` (obligatoire)
- `handle` (optionnel, genere depuis title si absent)
- `description` (optionnel)
- `thumbnail` (optionnel)
- `option_title` (optionnel, defaut: Taille)
- `option_value` (optionnel, defaut: variant_title)
- `variant_title` (optionnel, defaut: option_value)
- `variant_sku` (optionnel)
- `price_amount` (obligatoire, entier en centimes)
- `currency_code` (optionnel, defaut: eur)
- `inventory_quantity` (optionnel, defaut: 0)
- `manage_inventory` (optionnel, true/false)

Colonnes attributs (optionnelles, stockees dans `product.metadata`):
- `volume` (ex: 200ml, 500ml)
- `type_meche` (ex: bois, coton)
- `parfum` (ex: lavande, vanille)
- `couleur` (ex: blanche, bleue)
- `type_cire` (ex: soja, colza, abeille)

Ces attributs sont descriptifs (pas de variantes). Ils sont affiches sur la
fiche produit du storefront. Si une colonne est vide, l'attribut n'est pas affiche.

Exemple:
```
external_id,title,handle,description,thumbnail,option_title,option_value,variant_title,variant_sku,price_amount,currency_code,inventory_quantity,manage_inventory,volume,type_meche,parfum,couleur,type_cire
prod-001,Bougie Lavande,bougie-lavande,"Bougie artisanale a la lavande.",,Taille,200ml,200ml,SKU-BOU-LAV-200,1200,eur,50,true,200ml,coton,lavande,blanche,soja
```

## FR - Comportement
- Un produit est trouve par `external_id`.
- Si `external_id` existe: mise a jour du produit.
- Si `external_id` n'existe pas: creation du produit.
- Une ligne CSV = une variante. Regrouper plusieurs variantes avec le meme `external_id`.
- Une seule option produit est supportee dans ce script (ex: Taille).

## FR - Git flow (branche + push)
Nom de branche suggere: `feature/import-csv-produits`

### Creer et developper la feature
```
git flow feature start import-csv-produits
git status
git add -A
git commit -m "feat: attributs bougies (metadata) + import CSV"
git push -u origin feature/import-csv-produits
```

### Tester sur huitral (avant de finir la feature)
```
# Sur huitral
cd /var/www/lucien-sens-bon
git fetch origin
git checkout feature/import-csv-produits
git reset --hard origin/feature/import-csv-produits

# Dry-run (test sans ecriture en base)
docker compose run --rm backend npm run import:products -- --dry-run

# Import reel
docker compose run --rm backend npm run import:products

# Rebuild storefront (pour afficher les attributs metadata)
docker compose up -d --build storefront

# Verifier
curl -s http://localhost:9000/store/products | python3 -m json.tool | head -30
```

### Finir la feature et merger dans develop
```
# Sur le poste de dev
git flow feature finish import-csv-produits
git push origin develop
git push origin --delete feature/import-csv-produits
```

### Deployer develop sur huitral
```
# Sur huitral
cd /var/www/lucien-sens-bon
git fetch origin
git checkout develop
git reset --hard origin/develop
docker compose up -d --build storefront
```

## FR - Alternative : remettre la branche feature dans main (sans git-flow)
Si tu veux ramener le contenu de la feature vers `main` directement :
```
git checkout main
git merge feature/import-csv-produits
git push origin main
```

Si tu veux utiliser la branche distante directement :
```
git checkout main
git merge origin/feature/import-csv-produits
git push origin main
```

## FR - Cas courant: commit fait sur main avant la feature
Si un commit a ete fait sur `main` avant la creation de la feature, il faut le recuperer:
```
git log --oneline main -n 5
git cherry-pick <hash_du_commit>
```
Alternative si plusieurs commits:
```
git cherry-pick <hash1> <hash2>
```
Ou merger `main` (si votre process l'autorise):
```
git merge main
```

## FR - Troubleshoot git-flow
**Erreur**: `Fatal: Working tree contains unstaged changes. Aborting.`

Cause: git-flow exige un working tree propre.

Actions:
```
git status
git add -A
git commit -m "WIP: prepare import CSV"
git flow init -d
```

Alternative (mettre de cote):
```
git stash -u
git flow init -d
git stash pop
```

**Erreur**: `git: 'flow' is not a git command`

Installe git-flow:
```
sudo apt update
sudo apt install git-flow
```

---

## DE - Ziel
Produkte per CSV in Medusa importieren, mit Upsert per `external_id` (anlegen oder aktualisieren).

## DE - Voraussetzungen
- Medusa Backend laeuft
- Abhaengigkeiten installiert (`npm install` in `backend/`)
- CSV Datei in `backend/data/products-import.csv` (oder eigener Pfad)

## DE - Schnellstart
Aus `backend/`:
```
npm run import:products
```

Optionen:
```
node scripts/import-products.js --file ./data/products-import.csv --report ./data/import-report.json
node scripts/import-products.js --file ./data/products-import.csv --dry-run
node scripts/import-products.js --file ./data/products-import.csv --currency eur
```

## DE - CSV Schema
Pflichtspalten:
- `external_id` (Pflicht, Upsert-Schluessel)
- `title` (Pflicht)
- `handle` (optional, wird aus title erzeugt)
- `description` (optional)
- `thumbnail` (optional)
- `option_title` (optional, Default: Taille)
- `option_value` (optional, Default: variant_title)
- `variant_title` (optional, Default: option_value)
- `variant_sku` (optional)
- `price_amount` (Pflicht, integer in Cent)
- `currency_code` (optional, Default: eur)
- `inventory_quantity` (optional, Default: 0)
- `manage_inventory` (optional, true/false)

Attribut-Spalten (optional, in `product.metadata` gespeichert):
- `volume` (z.B. 200ml, 500ml)
- `type_meche` (z.B. bois, coton)
- `parfum` (z.B. lavande, vanille)
- `couleur` (z.B. blanche, bleue)
- `type_cire` (z.B. soja, colza, abeille)

Diese Attribute sind beschreibend (keine Varianten). Sie werden auf der
Produktseite im Storefront angezeigt. Leere Spalten werden nicht angezeigt.

Beispiel:
```
external_id,title,handle,description,thumbnail,option_title,option_value,variant_title,variant_sku,price_amount,currency_code,inventory_quantity,manage_inventory,volume,type_meche,parfum,couleur,type_cire
prod-001,Bougie Lavande,bougie-lavande,"Bougie artisanale a la lavande.",,Taille,200ml,200ml,SKU-BOU-LAV-200,1200,eur,50,true,200ml,coton,lavande,blanche,soja
```

## DE - Verhalten
- Produkt wird per `external_id` gefunden.
- Wenn `external_id` existiert: Produkt wird aktualisiert.
- Wenn `external_id` nicht existiert: Produkt wird angelegt.
- Eine CSV Zeile = eine Variante. Mehrere Varianten mit gleichem `external_id` gruppieren.
- Nur eine Produktoption ist in diesem Script vorgesehen (z.B. Taille).

## DE - Git flow (Branch + Push)
Branch Vorschlag: `feature/import-csv-produits`

### Feature erstellen und entwickeln
```
git flow feature start import-csv-produits
git status
git add -A
git commit -m "feat: Kerzen-Attribute (metadata) + CSV Import"
git push -u origin feature/import-csv-produits
```

### Auf huitral testen (vor Feature-Abschluss)
```
# Auf huitral
cd /var/www/lucien-sens-bon
git fetch origin
git checkout feature/import-csv-produits
git reset --hard origin/feature/import-csv-produits

# Dry-run (Test ohne DB-Schreibvorgang)
docker compose run --rm backend npm run import:products -- --dry-run

# Echter Import
docker compose run --rm backend npm run import:products

# Storefront neu bauen (fuer Metadata-Attribute)
docker compose up -d --build storefront
```

### Feature abschliessen und in develop mergen
```
# Auf dem Entwicklungsrechner
git flow feature finish import-csv-produits
git push origin develop
git push origin --delete feature/import-csv-produits
```

### Develop auf huitral deployen
```
# Auf huitral
cd /var/www/lucien-sens-bon
git fetch origin
git checkout develop
git reset --hard origin/develop
docker compose up -d --build storefront
```

## DE - Alternative: Feature-Branch direkt in main (ohne git-flow)
Wenn du den Feature-Branch direkt nach `main` holen willst:
```
git checkout main
git merge feature/import-csv-produits
git push origin main
```

Wenn du direkt den Remote-Branch verwenden willst:
```
git checkout main
git merge origin/feature/import-csv-produits
git push origin main
```

## DE - Typisch: Commit auf main vor der Feature
Wenn ein Commit bereits auf `main` erstellt wurde, muss er in die Feature:
```
git log --oneline main -n 5
git cherry-pick <commit_hash>
```
Alternative fuer mehrere Commits:
```
git cherry-pick <hash1> <hash2>
```
Oder `main` mergen (falls erlaubt):
```
git merge main
```

## DE - Troubleshooting git-flow
**Fehler**: `Fatal: Working tree contains unstaged changes. Aborting.`

Ursache: git-flow braucht ein sauberes Working Tree.

Schritte:
```
git status
git add -A
git commit -m "WIP: prepare import CSV"
git flow init -d
```

Alternative (parken):
```
git stash -u
git flow init -d
git stash pop
```

**Fehler**: `git: 'flow' is not a git command`

git-flow installieren:
```
sudo apt update
sudo apt install git-flow
```