diff --git a/setup-database.sh b/setup-database.sh new file mode 100644 index 0000000..41a99da --- /dev/null +++ b/setup-database.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +echo "🚀 Configuration de la base de données PostgreSQL pour l'inventaire industriel" + +# Vérifier si PostgreSQL est installé +if ! command -v psql &> /dev/null; then + echo "📦 Installation de PostgreSQL..." + sudo dnf install -y postgresql postgresql-server postgresql-contrib + + # Initialiser la base de données PostgreSQL + sudo postgresql-setup --initdb + + # Démarrer et activer PostgreSQL + sudo systemctl start postgresql + sudo systemctl enable postgresql + + echo "✅ PostgreSQL installé et démarré" +else + echo "✅ PostgreSQL déjà installé" +fi + +# Créer l'utilisateur et la base de données +echo "🔧 Configuration de la base de données..." + +# Se connecter en tant qu'utilisateur postgres +sudo -u postgres psql << EOF +-- Créer l'utilisateur postgres avec mot de passe +ALTER USER postgres PASSWORD 'password'; + +-- Créer la base de données +CREATE DATABASE inventory_db; + +-- Donner les privilèges +GRANT ALL PRIVILEGES ON DATABASE inventory_db TO postgres; + +-- Se connecter à la base de données +\c inventory_db; + +-- Créer le schéma public si nécessaire +CREATE SCHEMA IF NOT EXISTS public; + +-- Donner les privilèges sur le schéma +GRANT ALL ON SCHEMA public TO postgres; + +\q +EOF + +echo "✅ Base de données configurée" + +# Générer le client Prisma +echo "🔧 Génération du client Prisma..." +npx prisma generate + +# Créer et appliquer les migrations +echo "🔧 Création des migrations..." +npx prisma migrate dev --name init + +echo "🎉 Configuration terminée !" +echo "📊 Base de données: inventory_db" +echo "🔗 URL: postgresql://postgres:password@localhost:5432/inventory_db" +echo "🚀 Pour démarrer l'API: npm run start:dev" \ No newline at end of file diff --git a/test-add-custom-fields.sh b/test-add-custom-fields.sh new file mode 100755 index 0000000..c3e16e3 --- /dev/null +++ b/test-add-custom-fields.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Script pour ajouter les champs personnalisés manquants à une machine existante + +MACHINE_ID="cmdn9ifhw0004ijrv0kqjzjyd" + +echo "Ajout des champs personnalisés manquants pour la machine $MACHINE_ID..." + +curl -X POST "http://localhost:3000/api/machines/$MACHINE_ID/add-custom-fields" \ + -H "Content-Type: application/json" \ + -w "\nStatus: %{http_code}\n" + +echo "Terminé !" \ No newline at end of file diff --git a/test-api-simple.sh b/test-api-simple.sh new file mode 100755 index 0000000..f4db3c8 --- /dev/null +++ b/test-api-simple.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +echo "🔍 Test de l'API backend..." + +# Test de base +echo "📡 Test GET /api/sites" +curl -X GET http://localhost:3000/api/sites -H "Content-Type: application/json" + +echo -e "\n\n📡 Test POST /api/sites" +curl -X POST http://localhost:3000/api/sites \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Site de test", + "description": "Site de test pour vérification API" + }' + +echo -e "\n\n✅ Tests terminés" \ No newline at end of file diff --git a/test-api.sh b/test-api.sh new file mode 100755 index 0000000..ffc5725 --- /dev/null +++ b/test-api.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +echo "🧪 Test de l'API d'inventaire industriel" + +# Attendre que l'API soit prête +echo "⏳ Attente du démarrage de l'API..." +sleep 5 + +# URL de base +BASE_URL="http://localhost:3000/api" + +echo "📊 Test des endpoints..." + +# Test 1: Créer un site +echo "🏭 Création d'un site..." +SITE_RESPONSE=$(curl -s -X POST $BASE_URL/sites \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Usine de Production A", + "description": "Site principal de production" + }') + +echo "Réponse création site: $SITE_RESPONSE" + +# Extraire l'ID du site +SITE_ID=$(echo $SITE_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4) + +if [ -n "$SITE_ID" ]; then + echo "✅ Site créé avec ID: $SITE_ID" + + # Test 2: Créer un type de machine + echo "🔧 Création d'un type de machine..." + TYPE_MACHINE_RESPONSE=$(curl -s -X POST $BASE_URL/types/machines \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Presse Hydraulique", + "description": "Machine de pressage industriel" + }') + + echo "Réponse création type machine: $TYPE_MACHINE_RESPONSE" + + # Test 3: Créer une machine + echo "⚙️ Création d'une machine..." + MACHINE_RESPONSE=$(curl -s -X POST $BASE_URL/machines \ + -H "Content-Type: application/json" \ + -d "{ + \"name\": \"Presse HP-2000\", + \"siteId\": \"$SITE_ID\", + \"reference\": \"HP-2000-001\", + \"prestataire\": \"IndustriePress\", + \"prix\": \"150000.00\", + \"emplacement\": \"Zone A - Secteur 3\" + }") + + echo "Réponse création machine: $MACHINE_RESPONSE" + + # Test 4: Lister tous les sites + echo "📋 Liste de tous les sites..." + SITES_LIST=$(curl -s -X GET $BASE_URL/sites) + echo "Liste des sites: $SITES_LIST" + + echo "✅ Tests terminés avec succès!" +else + echo "❌ Erreur lors de la création du site" +fi + +echo "" +echo "🎯 Pour tester manuellement:" +echo "curl -X GET $BASE_URL/sites" +echo "curl -X GET $BASE_URL/machines" +echo "curl -X GET $BASE_URL/types/machines" \ No newline at end of file diff --git a/test-complete-api.sh b/test-complete-api.sh new file mode 100755 index 0000000..a641c53 --- /dev/null +++ b/test-complete-api.sh @@ -0,0 +1,322 @@ +#!/bin/bash + +echo "🧪 Test complet de l'API d'inventaire industriel" +echo "================================================" + +# URL de base +BASE_URL="http://localhost:3000/api" + +# Couleurs pour l'affichage +GREEN='\033[0;32m' +RED='\033[0;31m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Fonction pour afficher les résultats +print_result() { + if [ $1 -eq 0 ]; then + echo -e "${GREEN}✅ $2${NC}" + else + echo -e "${RED}❌ $2${NC}" + fi +} + +echo -e "${BLUE}1. Test des Sites${NC}" +echo "-------------------" + +# Créer un site +SITE_RESPONSE=$(curl -s -X POST $BASE_URL/sites \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Usine de Production B", + "description": "Site secondaire de production" + }') + +if echo "$SITE_RESPONSE" | grep -q '"id"'; then + SITE_ID=$(echo $SITE_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4) + print_result 0 "Site créé avec ID: $SITE_ID" +else + print_result 1 "Échec création site" + echo "$SITE_RESPONSE" +fi + +# Lister tous les sites +SITES_LIST=$(curl -s -X GET $BASE_URL/sites) +if echo "$SITES_LIST" | grep -q '"id"'; then + print_result 0 "Liste des sites récupérée" +else + print_result 1 "Échec récupération sites" +fi + +echo -e "\n${BLUE}2. Test des Types${NC}" +echo "----------------" + +# Créer un type de machine +TYPE_MACHINE_RESPONSE=$(curl -s -X POST $BASE_URL/types/machines \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Convoyeur à Bande", + "description": "Système de transport de matériaux" + }') + +if echo "$TYPE_MACHINE_RESPONSE" | grep -q '"id"'; then + TYPE_MACHINE_ID=$(echo $TYPE_MACHINE_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4) + print_result 0 "Type machine créé avec ID: $TYPE_MACHINE_ID" +else + print_result 1 "Échec création type machine" + echo "$TYPE_MACHINE_RESPONSE" +fi + +# Créer un type de composant +TYPE_COMPOSANT_RESPONSE=$(curl -s -X POST $BASE_URL/types/composants \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Moteur Électrique", + "description": "Moteur pour entraînement mécanique" + }') + +if echo "$TYPE_COMPOSANT_RESPONSE" | grep -q '"id"'; then + TYPE_COMPOSANT_ID=$(echo $TYPE_COMPOSANT_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4) + print_result 0 "Type composant créé avec ID: $TYPE_COMPOSANT_ID" +else + print_result 1 "Échec création type composant" + echo "$TYPE_COMPOSANT_RESPONSE" +fi + +# Créer un type de pièce +TYPE_PIECE_RESPONSE=$(curl -s -X POST $BASE_URL/types/pieces \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Roulement à Billes", + "description": "Roulement pour rotation fluide" + }') + +if echo "$TYPE_PIECE_RESPONSE" | grep -q '"id"'; then + TYPE_PIECE_ID=$(echo $TYPE_PIECE_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4) + print_result 0 "Type pièce créé avec ID: $TYPE_PIECE_ID" +else + print_result 1 "Échec création type pièce" + echo "$TYPE_PIECE_RESPONSE" +fi + +echo -e "\n${BLUE}3. Test des Machines${NC}" +echo "---------------------" + +# Créer une machine +MACHINE_RESPONSE=$(curl -s -X POST $BASE_URL/machines \ + -H "Content-Type: application/json" \ + -d "{ + \"name\": \"Convoyeur CB-500\", + \"siteId\": \"$SITE_ID\", + \"reference\": \"CB-500-001\", + \"prestataire\": \"TransportTech\", + \"prix\": \"75000.00\", + \"emplacement\": \"Zone B - Secteur 1\", + \"typeMachineId\": \"$TYPE_MACHINE_ID\" + }") + +if echo "$MACHINE_RESPONSE" | grep -q '"id"'; then + MACHINE_ID=$(echo $MACHINE_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4) + print_result 0 "Machine créée avec ID: $MACHINE_ID" +else + print_result 1 "Échec création machine" + echo "$MACHINE_RESPONSE" +fi + +echo -e "\n${BLUE}4. Test des Composants${NC}" +echo "----------------------" + +# Créer un composant principal +COMPOSANT_RESPONSE=$(curl -s -X POST $BASE_URL/composants \ + -H "Content-Type: application/json" \ + -d "{ + \"name\": \"Moteur Principal\", + \"machineId\": \"$MACHINE_ID\", + \"reference\": \"MP-001\", + \"prestataire\": \"MotorTech\", + \"prix\": \"15000.00\", + \"emplacement\": \"Zone moteur\", + \"typeComposantId\": \"$TYPE_COMPOSANT_ID\" + }") + +if echo "$COMPOSANT_RESPONSE" | grep -q '"id"'; then + COMPOSANT_ID=$(echo $COMPOSANT_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4) + print_result 0 "Composant créé avec ID: $COMPOSANT_ID" +else + print_result 1 "Échec création composant" + echo "$COMPOSANT_RESPONSE" +fi + +# Créer un sous-composant +SOUS_COMPOSANT_RESPONSE=$(curl -s -X POST $BASE_URL/composants \ + -H "Content-Type: application/json" \ + -d "{ + \"name\": \"Réducteur de Vitesse\", + \"machineId\": \"$MACHINE_ID\", + \"parentComposantId\": \"$COMPOSANT_ID\", + \"reference\": \"RV-001\", + \"prestataire\": \"GearTech\", + \"prix\": \"8000.00\", + \"emplacement\": \"Zone réducteur\", + \"typeComposantId\": \"$TYPE_COMPOSANT_ID\" + }") + +if echo "$SOUS_COMPOSANT_RESPONSE" | grep -q '"id"'; then + SOUS_COMPOSANT_ID=$(echo $SOUS_COMPOSANT_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4) + print_result 0 "Sous-composant créé avec ID: $SOUS_COMPOSANT_ID" +else + print_result 1 "Échec création sous-composant" + echo "$SOUS_COMPOSANT_RESPONSE" +fi + +# Tester la hiérarchie des composants +HIERARCHY_RESPONSE=$(curl -s -X GET $BASE_URL/composants/hierarchy/$MACHINE_ID) +if echo "$HIERARCHY_RESPONSE" | grep -q '"id"'; then + print_result 0 "Hiérarchie des composants récupérée" +else + print_result 1 "Échec récupération hiérarchie" +fi + +echo -e "\n${BLUE}5. Test des Pièces${NC}" +echo "----------------" + +# Créer une pièce attachée à la machine +PIECE_MACHINE_RESPONSE=$(curl -s -X POST $BASE_URL/pieces \ + -H "Content-Type: application/json" \ + -d "{ + \"name\": \"Bande Transporteuse\", + \"machineId\": \"$MACHINE_ID\", + \"reference\": \"BT-001\", + \"prestataire\": \"BeltTech\", + \"prix\": \"5000.00\", + \"emplacement\": \"Zone bande\", + \"typePieceId\": \"$TYPE_PIECE_ID\" + }") + +if echo "$PIECE_MACHINE_RESPONSE" | grep -q '"id"'; then + PIECE_MACHINE_ID=$(echo $PIECE_MACHINE_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4) + print_result 0 "Pièce machine créée avec ID: $PIECE_MACHINE_ID" +else + print_result 1 "Échec création pièce machine" + echo "$PIECE_MACHINE_RESPONSE" +fi + +# Créer une pièce attachée au composant +PIECE_COMPOSANT_RESPONSE=$(curl -s -X POST $BASE_URL/pieces \ + -H "Content-Type: application/json" \ + -d "{ + \"name\": \"Roulement Moteur\", + \"composantId\": \"$COMPOSANT_ID\", + \"reference\": \"RM-001\", + \"prestataire\": \"BearingTech\", + \"prix\": \"500.00\", + \"emplacement\": \"Zone roulement\", + \"typePieceId\": \"$TYPE_PIECE_ID\" + }") + +if echo "$PIECE_COMPOSANT_RESPONSE" | grep -q '"id"'; then + PIECE_COMPOSANT_ID=$(echo $PIECE_COMPOSANT_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4) + print_result 0 "Pièce composant créée avec ID: $PIECE_COMPOSANT_ID" +else + print_result 1 "Échec création pièce composant" + echo "$PIECE_COMPOSANT_RESPONSE" +fi + +echo -e "\n${BLUE}6. Test des Documents${NC}" +echo "---------------------" + +# Créer un document pour la machine +DOCUMENT_MACHINE_RESPONSE=$(curl -s -X POST $BASE_URL/documents \ + -H "Content-Type: application/json" \ + -d "{ + \"name\": \"Manuel d'utilisation\", + \"filename\": \"manuel_convoyeur.pdf\", + \"path\": \"/documents/manuel_convoyeur.pdf\", + \"mimeType\": \"application/pdf\", + \"size\": 2048576, + \"machineId\": \"$MACHINE_ID\" + }") + +if echo "$DOCUMENT_MACHINE_RESPONSE" | grep -q '"id"'; then + DOCUMENT_MACHINE_ID=$(echo $DOCUMENT_MACHINE_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4) + print_result 0 "Document machine créé avec ID: $DOCUMENT_MACHINE_ID" +else + print_result 1 "Échec création document machine" + echo "$DOCUMENT_MACHINE_RESPONSE" +fi + +# Créer un document pour le composant +DOCUMENT_COMPOSANT_RESPONSE=$(curl -s -X POST $BASE_URL/documents \ + -H "Content-Type: application/json" \ + -d "{ + \"name\": \"Schéma électrique\", + \"filename\": \"schema_moteur.pdf\", + \"path\": \"/documents/schema_moteur.pdf\", + \"mimeType\": \"application/pdf\", + \"size\": 1048576, + \"composantId\": \"$COMPOSANT_ID\" + }") + +if echo "$DOCUMENT_COMPOSANT_RESPONSE" | grep -q '"id"'; then + DOCUMENT_COMPOSANT_ID=$(echo $DOCUMENT_COMPOSANT_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4) + print_result 0 "Document composant créé avec ID: $DOCUMENT_COMPOSANT_ID" +else + print_result 1 "Échec création document composant" + echo "$DOCUMENT_COMPOSANT_RESPONSE" +fi + +echo -e "\n${BLUE}7. Test des Requêtes Spécialisées${NC}" +echo "--------------------------------" + +# Tester la récupération de la hiérarchie complète +FULL_HIERARCHY=$(curl -s -X GET $BASE_URL/sites) +if echo "$FULL_HIERARCHY" | grep -q '"machines"'; then + print_result 0 "Hiérarchie complète récupérée" +else + print_result 1 "Échec récupération hiérarchie complète" +fi + +# Tester la récupération des pièces par machine +PIECES_MACHINE=$(curl -s -X GET $BASE_URL/pieces/machine/$MACHINE_ID) +if echo "$PIECES_MACHINE" | grep -q '"id"'; then + print_result 0 "Pièces de la machine récupérées" +else + print_result 1 "Échec récupération pièces machine" +fi + +# Tester la récupération des documents par machine +DOCUMENTS_MACHINE=$(curl -s -X GET $BASE_URL/documents/machine/$MACHINE_ID) +if echo "$DOCUMENTS_MACHINE" | grep -q '"id"'; then + print_result 0 "Documents de la machine récupérés" +else + print_result 1 "Échec récupération documents machine" +fi + +echo -e "\n${BLUE}8. Résumé des Tests${NC}" +echo "-------------------" + +echo "📊 Statistiques des tests :" +echo "• Sites créés : 1" +echo "• Types créés : 3 (machine, composant, pièce)" +echo "• Machines créées : 1" +echo "• Composants créés : 2 (avec hiérarchie)" +echo "• Pièces créées : 2 (machine + composant)" +echo "• Documents créés : 2 (machine + composant)" + +echo -e "\n🎯 Endpoints testés :" +echo "✅ POST /api/sites" +echo "✅ GET /api/sites" +echo "✅ POST /api/types/machines" +echo "✅ POST /api/types/composants" +echo "✅ POST /api/types/pieces" +echo "✅ POST /api/machines" +echo "✅ POST /api/composants" +echo "✅ GET /api/composants/hierarchy/:machineId" +echo "✅ POST /api/pieces" +echo "✅ GET /api/pieces/machine/:machineId" +echo "✅ POST /api/documents" +echo "✅ GET /api/documents/machine/:machineId" + +echo -e "\n🚀 API prête pour le développement frontend !" +echo "📋 URL de base : $BASE_URL" +echo "🔗 Documentation : http://localhost:3000/api (à implémenter avec Swagger)" \ No newline at end of file diff --git a/test-generator-api.sh b/test-generator-api.sh new file mode 100755 index 0000000..dc17676 --- /dev/null +++ b/test-generator-api.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +echo "🧪 Test de l'API du générateur de types de machines" +echo "==================================================" + +# URL de base +BASE_URL="http://localhost:3000/api" + +# Test de création d'un type de machine avec structure hiérarchique +echo -e "\n📡 Test POST /api/types/machines" +curl -X POST $BASE_URL/types/machines \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Presse hydraulique", + "description": "Machine de formage par compression hydraulique", + "category": "Production", + "maintenanceFrequency": "Mensuelle", + "components": [ + { + "name": "Système hydraulique", + "subComponents": [ + { + "name": "Pompe hydraulique", + "subComponents": [ + {"name": "Rotor"}, + {"name": "Stator"}, + {"name": "Joint d'\''étanchéité"} + ] + }, + { + "name": "Cylindre principal", + "subComponents": [ + {"name": "Piston"}, + {"name": "Tige"}, + {"name": "Joint de piston"} + ] + } + ] + }, + { + "name": "Système mécanique", + "subComponents": [ + { + "name": "Banc de machine", + "subComponents": [ + {"name": "Poutre supérieure"}, + {"name": "Poutre inférieure"}, + {"name": "Colonnes"} + ] + } + ] + } + ], + "criticalParts": ["Pompe hydraulique", "Cylindre principal", "Soupapes de sécurité"], + "specifications": { + "force": "100-5000 tonnes", + "course": "100-800 mm", + "vitesse": "5-50 mm/s" + } + }' + +echo -e "\n\n📡 Test GET /api/types/machines" +curl -X GET $BASE_URL/types/machines -H "Content-Type: application/json" + +echo -e "\n\n✅ Tests terminés" \ No newline at end of file diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts new file mode 100644 index 0000000..4df6580 --- /dev/null +++ b/test/app.e2e-spec.ts @@ -0,0 +1,25 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { INestApplication } from '@nestjs/common'; +import * as request from 'supertest'; +import { App } from 'supertest/types'; +import { AppModule } from './../src/app.module'; + +describe('AppController (e2e)', () => { + let app: INestApplication; + + beforeEach(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + imports: [AppModule], + }).compile(); + + app = moduleFixture.createNestApplication(); + await app.init(); + }); + + it('/ (GET)', () => { + return request(app.getHttpServer()) + .get('/') + .expect(200) + .expect('Hello World!'); + }); +}); diff --git a/test/jest-e2e.json b/test/jest-e2e.json new file mode 100644 index 0000000..e9d912f --- /dev/null +++ b/test/jest-e2e.json @@ -0,0 +1,9 @@ +{ + "moduleFileExtensions": ["js", "json", "ts"], + "rootDir": ".", + "testEnvironment": "node", + "testRegex": ".e2e-spec.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + } +}