-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
173 lines (156 loc) · 6.36 KB
/
Copy pathserver.js
File metadata and controls
173 lines (156 loc) · 6.36 KB
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
const express = require('express');
const mysql = require('mysql2/promise');
const crypto = require('crypto');
const app = express();
app.use(express.json());
const dbConfig = {
host: 'localhost',
user: 'frankoMysql',
password: '',
database: '',
port: 3306,
};
const translateType = (type) => {
if (type === 'ADT') return 'Adulto';
if (type === 'CHD') return 'Niño';
if (type === 'INF') return 'Infante';
return String(type);
};
const translateGender = (gender) => {
if (gender === 'Female') return 'Femenino';
if (gender === 'Male') return 'Masculino';
return String(gender);
};
app.post('/api/save-booking', async (req, res) => {
const data = req.body;
let connection;
try {
connection = await mysql.createConnection(dbConfig);
await connection.beginTransaction();
console.log("📥 Procesando reserva PNR:", data.booking.pnr);
const executionId = crypto.randomUUID();
const idEscenario = "16";
const [bookingResult] = await connection.execute(
`INSERT INTO booking (
execution_id, id_scenario, browser, username, password,
departure_date, return_date, language, pos, flight_type,
origin, destination, adt_number, tng_number, chd_number,
inf_number, departure_fare, return_fare, service, seat,
payment_method, payment_method_variation, card_number, month, year,
cvv, installments, email, v_holder_name, v_holder_lastname,
voucher1_currency, voucher1, voucher1_pin, voucher2_currency, voucher2,
voucher2_pin, wci_scenario_name, wci_payment_method, wci_payment_method_variation,
pnr, post_office_id, result, booking_stops, departure_cabin,
return_cabin, name_project
) VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?
)`,
[
String(executionId),
String(idEscenario),
null,
null,
null,
String(data.booking.departure_date || ''),
null,
'Español',
'Colombia',
'OW',
String(data.booking.origin || ''),
String(data.booking.destination || ''),
String(data.booking.adt_number || '0'),
'0', // tng_number
String(data.booking.chd_number || '0'),
String(data.booking.inf_number || '0'),
'flex',
null,
null,
null,
'Regular card',
'Regular card',
String(data.booking.card_number || ''),
'12',
'27',
'123',
null,
String(data.booking.email || ''),
null, null, null, null, null, null, null, null,
'test_scenario16_wci',
null, null,
String(data.booking.pnr || ''),
'MADAV08LL',
'PASSED',
null,
'',
'',
'Regresion_WCI_probes'
]
);
const bookingId = bookingResult.insertId;
console.log("✅ Booking guardado con ID:", bookingId);
let passengerIds = [];
if (data.passengers && data.passengers.length > 0) {
for (const pax of data.passengers) {
const [paxResult] = await connection.execute(
`INSERT INTO booking_passengers (
booking_id, passenger_type, first_name, second_name, first_last_name,
second_last_name, birth_date, gender, nationality, document_type,
document_number, frequent_flyer, frequent_flyer_number, holder_prefix,
holder_telephone_number, holder_email
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
String(bookingId),
translateType(pax.passenger_type),
String(pax.first_name || ''),
'',
String(pax.first_last_name || ''),
'',
String(pax.birth_date || ''),
translateGender(pax.gender),
String(pax.nationality || ''),
null, null,
'0',
'',
'',
'',
''
]
);
passengerIds.push(paxResult.insertId);
}
console.log(`✅ ${data.passengers.length} pasajero(s) guardado(s).`);
}
if (data.services && data.services.length > 0) {
const idPassengerAsignado = passengerIds.length > 0 ? passengerIds[0] : null;
for (const srv of data.services) {
await connection.execute(
`INSERT INTO booking_services (
booking_id, id_passenger, category_service, name_service, quantity, is_departure
) VALUES (?, ?, ?, ?, ?, ?)`,
[
String(bookingId),
String(idPassengerAsignado),
'serviceButtonTypeBaggage',
'Equipaje de bodega (23 kg)',
String(srv.quantity || '1'),
'1'
]
);
}
console.log(`✅ ${data.services.length} servicio(s) guardado(s).`);
}
await connection.commit();
res.status(200).json({ success: true, message: 'Reserva guardada exitosamente', bookingId });
} catch (error) {
if (connection) await connection.rollback();
console.error("❌ Error guardando en BD:", error);
res.status(500).json({ success: false, error: error.message });
} finally {
if (connection) await connection.end();
}
});
app.listen(3000, () => {
console.log('🚀 Servidor intermediario corriendo en http://localhost:3000');
});