From ed81c7ff94a87c252b1e77702f6a138eac9ac28d Mon Sep 17 00:00:00 2001
From: hugcubi <hugo.cubino@estudiantes.uva.es>
Date: Wed, 25 Dec 2024 11:22:19 +0100
Subject: [PATCH] =?UTF-8?q?Inclusi=C3=B3n=20de=20m=C3=A1s=20managers=20y?=
 =?UTF-8?q?=20clients?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 poblate/environments/env.js            |  5 ++---
 poblate/environments/env.prod.js       |  6 -----
 poblate/environments/env.production.js |  5 +++++
 poblate/index.js                       | 31 ++++++++++++++++----------
 poblate/mocks/users.json               | 26 +++++++++++++++++----
 5 files changed, 48 insertions(+), 25 deletions(-)
 delete mode 100644 poblate/environments/env.prod.js
 create mode 100644 poblate/environments/env.production.js

diff --git a/poblate/environments/env.js b/poblate/environments/env.js
index 1288628..a534b45 100644
--- a/poblate/environments/env.js
+++ b/poblate/environments/env.js
@@ -1,6 +1,5 @@
-export const env = {
-	authApi: "http://localhost:8101/",
-	userApi: "http://localhost:8201/users",
+exports.env = {
+	authApi: "http://localhost:8101/auth",
 	hotelsApi: "http://localhost:8301/hotels",
 	bookingsApi: "http://localhost:8401/bookings",
 };
diff --git a/poblate/environments/env.prod.js b/poblate/environments/env.prod.js
deleted file mode 100644
index 1288628..0000000
--- a/poblate/environments/env.prod.js
+++ /dev/null
@@ -1,6 +0,0 @@
-export const env = {
-	authApi: "http://localhost:8101/",
-	userApi: "http://localhost:8201/users",
-	hotelsApi: "http://localhost:8301/hotels",
-	bookingsApi: "http://localhost:8401/bookings",
-};
diff --git a/poblate/environments/env.production.js b/poblate/environments/env.production.js
new file mode 100644
index 0000000..1ca1777
--- /dev/null
+++ b/poblate/environments/env.production.js
@@ -0,0 +1,5 @@
+exports.env = {
+	authApi: "http://kong:8000/api/auth",
+	hotelsApi: "http://kong:8000/api/hotels",
+	bookingsApi: "http://kong:8000/api/bookings",
+};
diff --git a/poblate/index.js b/poblate/index.js
index 8fa2252..f51768a 100644
--- a/poblate/index.js
+++ b/poblate/index.js
@@ -1,16 +1,21 @@
 const mockedUsers = require("./mocks/users.json");
 const mockedHotels = require("./mocks/hotels.json");
-const mockedBookings = require("./mocks/bookings.json");
 const axios = require("axios");
 const { jwtDecode } = require("jwt-decode");
+const dev = require("./environments/env");
+const prod = require("./environments/env.production");
 
 // Modo
 const args = process.argv;
 const isProduction = args.includes("--prod");
+const DEBUG = args.includes("--debug");
 
-const authUrl = "http://localhost:8101/auth";
-const hotelsUrl = "http://localhost:8301/hotels";
-const bookingUrl = "http://localhost:8401/bookings";
+const env = isProduction ? prod : dev;
+const { authApi, hotelsApi, bookingsApi } = env;
+
+const debug = (...values) => {
+	if (DEBUG) console.log(...values);
+};
 
 const loj = (data) => {
 	console.log(JSON.stringify(data, null, 2));
@@ -53,11 +58,12 @@ const savePost = async (data, first, second = "") => {
 		try {
 			return await axios.post(first, data);
 		} catch (error) {
-			console.error("ERROR 1");
+			console.error("ERROR Al REGISTRO");
 			return await axios.post(second, data);
 		}
 	} catch (error) {
-		console.error("ERROR 2");
+		console.error("ERROR Al LOGIN");
+		console.error("\nNo se ha podido comunicar con el servicio de auth");
 		process.exit(-1);
 	}
 };
@@ -65,8 +71,8 @@ const savePost = async (data, first, second = "") => {
 async function register(user) {
 	const { data } = await savePost(
 		user,
-		`${authUrl}/register`,
-		`${authUrl}/login`
+		`${authApi}/register`,
+		`${authApi}/login`
 	);
 	const decoded = jwtDecode(data.token);
 	user.id = decoded.id;
@@ -87,7 +93,7 @@ const addUsers = async () => {
 };
 
 const insertHotel = async ({ manager, hotel }) => {
-	const { data } = await axios.post(hotelsUrl, {
+	const { data } = await axios.post(hotelsApi, {
 		...hotel,
 		managerId: manager.id,
 	});
@@ -104,8 +110,7 @@ async function addHotels(managers) {
 }
 
 const insertBookings = async (booking) => {
-	console.log({ booking });
-	const { data } = await axios.post(bookingUrl, booking);
+	const { data } = await axios.post(bookingsApi, booking);
 	return data;
 };
 
@@ -131,10 +136,12 @@ async function addBookings(clients, hotels) {
 }
 
 async function init() {
+	debug("MODE:", isProduction ? "PRODUCTION" : "DEVELOPMENT");
+	debug("ENV:", env.env, "\n");
 	const { managers, clients } = await addUsers();
 	const hotels = await addHotels(managers, 3);
 	await addBookings(clients, hotels);
+	console.log("POBLACIÓN COMPLETADA EXITOSAMENTE");
 }
 
-console.log("MODE:", isProduction ? "PRODUCTION" : "DEVELOPMENT");
 init();
diff --git a/poblate/mocks/users.json b/poblate/mocks/users.json
index a23fb88..78d33d9 100644
--- a/poblate/mocks/users.json
+++ b/poblate/mocks/users.json
@@ -6,14 +6,32 @@
 		"rol": "ADMIN"
 	},
 	{
-		"name": "Hotel",
-		"email": "hotel@dev.com",
+		"name": "Hotel 1",
+		"email": "hotel1@dev.com",
 		"password": "123",
 		"rol": "HOTEL_ADMIN"
 	},
 	{
-		"name": "Client",
-		"email": "client@dev.com",
+		"name": "Hotel 2",
+		"email": "hotel2@dev.com",
+		"password": "123",
+		"rol": "HOTEL_ADMIN"
+	},
+	{
+		"name": "Client 1",
+		"email": "client1@dev.com",
+		"password": "123",
+		"rol": "CLIENT"
+	},
+	{
+		"name": "Client 2",
+		"email": "client2@dev.com",
+		"password": "123",
+		"rol": "CLIENT"
+	},
+	{
+		"name": "Client 3",
+		"email": "client3@dev.com",
 		"password": "123",
 		"rol": "CLIENT"
 	}
-- 
GitLab