mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-21 22:31:46 +00:00
refactoring: TypeScript migration, security fixes,
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const dataDir = path.join(__dirname, '../../data');
|
||||
const dbPath = path.join(dataDir, 'travel.db');
|
||||
const baselinePath = path.join(dataDir, 'travel-baseline.db');
|
||||
|
||||
function resetDemoUser() {
|
||||
function resetDemoUser(): void {
|
||||
if (!fs.existsSync(baselinePath)) {
|
||||
console.log('[Demo Reset] No baseline found, skipping. Admin must save baseline first.');
|
||||
return;
|
||||
@@ -15,13 +15,14 @@ function resetDemoUser() {
|
||||
|
||||
// Save admin's current credentials and API keys (these should survive the reset)
|
||||
const adminEmail = process.env.DEMO_ADMIN_EMAIL || 'admin@nomad.app';
|
||||
let adminData = null;
|
||||
interface AdminData { password_hash: string; maps_api_key: string | null; openweather_api_key: string | null; unsplash_api_key: string | null; avatar: string | null; }
|
||||
let adminData: AdminData | undefined = undefined;
|
||||
try {
|
||||
adminData = db.prepare(
|
||||
'SELECT password_hash, maps_api_key, openweather_api_key, unsplash_api_key, avatar FROM users WHERE email = ?'
|
||||
).get(adminEmail);
|
||||
} catch (e) {
|
||||
console.error('[Demo Reset] Failed to read admin data:', e.message);
|
||||
).get(adminEmail) as AdminData | undefined;
|
||||
} catch (e: unknown) {
|
||||
console.error('[Demo Reset] Failed to read admin data:', e instanceof Error ? e.message : e);
|
||||
}
|
||||
|
||||
// Flush WAL to main DB file
|
||||
@@ -36,8 +37,8 @@ function resetDemoUser() {
|
||||
// Remove WAL/SHM files if they exist (stale from old connection)
|
||||
try { fs.unlinkSync(dbPath + '-wal'); } catch (e) {}
|
||||
try { fs.unlinkSync(dbPath + '-shm'); } catch (e) {}
|
||||
} catch (e) {
|
||||
console.error('[Demo Reset] Failed to restore baseline:', e.message);
|
||||
} catch (e: unknown) {
|
||||
console.error('[Demo Reset] Failed to restore baseline:', e instanceof Error ? e.message : e);
|
||||
reinitialize();
|
||||
return;
|
||||
}
|
||||
@@ -59,15 +60,15 @@ function resetDemoUser() {
|
||||
adminData.avatar,
|
||||
adminEmail
|
||||
);
|
||||
} catch (e) {
|
||||
console.error('[Demo Reset] Failed to restore admin credentials:', e.message);
|
||||
} catch (e: unknown) {
|
||||
console.error('[Demo Reset] Failed to restore admin credentials:', e instanceof Error ? e.message : e);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[Demo Reset] Database restored from baseline');
|
||||
}
|
||||
|
||||
function saveBaseline() {
|
||||
function saveBaseline(): void {
|
||||
const { db } = require('../db/database');
|
||||
|
||||
// Flush WAL so baseline file is self-contained
|
||||
@@ -77,8 +78,8 @@ function saveBaseline() {
|
||||
console.log('[Demo] Baseline saved');
|
||||
}
|
||||
|
||||
function hasBaseline() {
|
||||
function hasBaseline(): boolean {
|
||||
return fs.existsSync(baselinePath);
|
||||
}
|
||||
|
||||
module.exports = { resetDemoUser, saveBaseline, hasBaseline };
|
||||
export { resetDemoUser, saveBaseline, hasBaseline };
|
||||
@@ -1,6 +1,7 @@
|
||||
const bcrypt = require('bcryptjs');
|
||||
import bcrypt from 'bcryptjs';
|
||||
import Database from 'better-sqlite3';
|
||||
|
||||
function seedDemoData(db) {
|
||||
function seedDemoData(db: Database.Database): { adminId: number; demoId: number } {
|
||||
const ADMIN_USER = process.env.DEMO_ADMIN_USER || 'admin';
|
||||
const ADMIN_EMAIL = process.env.DEMO_ADMIN_EMAIL || 'admin@nomad.app';
|
||||
const ADMIN_PASS = process.env.DEMO_ADMIN_PASS || 'admin12345';
|
||||
@@ -8,7 +9,7 @@ function seedDemoData(db) {
|
||||
const DEMO_PASS = 'demo12345';
|
||||
|
||||
// Create admin user if not exists
|
||||
let admin = db.prepare('SELECT id FROM users WHERE email = ?').get(ADMIN_EMAIL);
|
||||
let admin = db.prepare('SELECT id FROM users WHERE email = ?').get(ADMIN_EMAIL) as { id: number } | undefined;
|
||||
if (!admin) {
|
||||
const hash = bcrypt.hashSync(ADMIN_PASS, 10);
|
||||
const r = db.prepare('INSERT INTO users (username, email, password_hash, role) VALUES (?, ?, ?, ?)').run(ADMIN_USER, ADMIN_EMAIL, hash, 'admin');
|
||||
@@ -19,7 +20,7 @@ function seedDemoData(db) {
|
||||
}
|
||||
|
||||
// Create demo user if not exists
|
||||
let demo = db.prepare('SELECT id FROM users WHERE email = ?').get(DEMO_EMAIL);
|
||||
let demo = db.prepare('SELECT id FROM users WHERE email = ?').get(DEMO_EMAIL) as { id: number } | undefined;
|
||||
if (!demo) {
|
||||
const hash = bcrypt.hashSync(DEMO_PASS, 10);
|
||||
const r = db.prepare('INSERT INTO users (username, email, password_hash, role) VALUES (?, ?, ?, ?)').run('demo', DEMO_EMAIL, hash, 'user');
|
||||
@@ -33,7 +34,7 @@ function seedDemoData(db) {
|
||||
db.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('allow_registration', 'false')").run();
|
||||
|
||||
// Check if admin already has example trips
|
||||
const adminTrips = db.prepare('SELECT COUNT(*) as count FROM trips WHERE user_id = ?').get(admin.id);
|
||||
const adminTrips = db.prepare('SELECT COUNT(*) as count FROM trips WHERE user_id = ?').get(admin.id) as { count: number };
|
||||
if (adminTrips.count > 0) {
|
||||
console.log('[Demo] Example trips already exist, ensuring demo membership');
|
||||
ensureDemoMembership(db, admin.id, demo.id);
|
||||
@@ -52,15 +53,15 @@ function seedDemoData(db) {
|
||||
return { adminId: admin.id, demoId: demo.id };
|
||||
}
|
||||
|
||||
function ensureDemoMembership(db, adminId, demoId) {
|
||||
const trips = db.prepare('SELECT id FROM trips WHERE user_id = ?').all(adminId);
|
||||
function ensureDemoMembership(db: Database.Database, adminId: number, demoId: number): void {
|
||||
const trips = db.prepare('SELECT id FROM trips WHERE user_id = ?').all(adminId) as { id: number }[];
|
||||
const insertMember = db.prepare('INSERT OR IGNORE INTO trip_members (trip_id, user_id, invited_by) VALUES (?, ?, ?)');
|
||||
for (const trip of trips) {
|
||||
insertMember.run(trip.id, demoId, adminId);
|
||||
}
|
||||
}
|
||||
|
||||
function seedExampleTrips(db, adminId, demoId) {
|
||||
function seedExampleTrips(db: Database.Database, adminId: number, demoId: number): void {
|
||||
const insertTrip = db.prepare('INSERT INTO trips (user_id, title, description, start_date, end_date, currency) VALUES (?, ?, ?, ?, ?, ?)');
|
||||
const insertDay = db.prepare('INSERT INTO days (trip_id, day_number, date) VALUES (?, ?, ?)');
|
||||
const insertPlace = db.prepare('INSERT INTO places (trip_id, name, lat, lng, address, category_id, place_time, duration_minutes, notes, image_url, google_place_id, website, phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
@@ -73,17 +74,17 @@ function seedExampleTrips(db, adminId, demoId) {
|
||||
|
||||
// Category IDs: 1=Hotel, 2=Restaurant, 3=Attraction, 5=Transport, 7=Bar/Cafe, 8=Beach, 9=Nature, 6=Entertainment
|
||||
|
||||
// ─── Trip 1: Tokyo & Kyoto ─────────────────────────────────────────────────
|
||||
// --- Trip 1: Tokyo & Kyoto ---
|
||||
const trip1 = insertTrip.run(adminId, 'Tokyo & Kyoto', 'Two weeks in Japan — from the neon-lit streets of Tokyo to the serene temples of Kyoto.', '2026-04-15', '2026-04-21', 'JPY');
|
||||
const t1 = Number(trip1.lastInsertRowid);
|
||||
|
||||
const t1days = [];
|
||||
const t1days: number[] = [];
|
||||
for (let i = 0; i < 7; i++) {
|
||||
const d = insertDay.run(t1, i + 1, `2026-04-${15 + i}`);
|
||||
t1days.push(Number(d.lastInsertRowid));
|
||||
}
|
||||
|
||||
const t1places = [
|
||||
const t1places: [number, string, number, number, string, number, string, number, string, string | null, string | null, string | null, string | null][] = [
|
||||
[t1, 'Hotel Shinjuku Granbell', 35.6938, 139.7035, '2-14-5 Kabukicho, Shinjuku City, Tokyo 160-0021, Japan', 1, '15:00', 60, 'Check-in from 3 PM. Steps from Shinjuku Station.', null, 'ChIJdaGEJBeMGGARYgt8sLBv6lM', 'https://www.grfranbellhotel.jp/shinjuku/', '+81 3-5155-2666'],
|
||||
[t1, 'Senso-ji Temple', 35.7148, 139.7967, '2 Chome-3-1 Asakusa, Taito City, Tokyo 111-0032, Japan', 3, '09:00', 90, 'Oldest temple in Tokyo. Fewer tourists in the early morning.', null, 'ChIJ8T1GpMGOGGARDYGSgpoOdfg', 'https://www.senso-ji.jp/', '+81 3-3842-0181'],
|
||||
[t1, 'Shibuya Crossing', 35.6595, 139.7004, '2 Chome-2-1 Dogenzaka, Shibuya City, Tokyo 150-0043, Japan', 3, '18:00', 45, 'World\'s busiest pedestrian crossing. Most impressive at night.', null, 'ChIJLyzOhmyLGGARMKWbl5z6wGg', null, null],
|
||||
@@ -127,7 +128,7 @@ function seedExampleTrips(db, adminId, demoId) {
|
||||
insertNote.run(t1days[6], t1, 'Last evening — farewell dinner at Pontocho Alley', '19:00', 'Star', 1);
|
||||
|
||||
// Packing
|
||||
const t1packing = [
|
||||
const t1packing: [string, number, string, number][] = [
|
||||
['Passport', 1, 'Documents', 0], ['Japan Rail Pass', 1, 'Documents', 1],
|
||||
['Power adapter Type A/B', 0, 'Electronics', 2], ['Camera + charger', 0, 'Electronics', 3],
|
||||
['Comfortable walking shoes', 0, 'Clothing', 4], ['Rain jacket', 0, 'Clothing', 5],
|
||||
@@ -150,17 +151,17 @@ function seedExampleTrips(db, adminId, demoId) {
|
||||
|
||||
insertMember.run(t1, demoId, adminId);
|
||||
|
||||
// ─── Trip 2: Barcelona Long Weekend ────────────────────────────────────────
|
||||
// --- Trip 2: Barcelona Long Weekend ---
|
||||
const trip2 = insertTrip.run(adminId, 'Barcelona Long Weekend', 'Gaudi, tapas, and Mediterranean vibes — a long weekend in the Catalan capital.', '2026-05-21', '2026-05-24', 'EUR');
|
||||
const t2 = Number(trip2.lastInsertRowid);
|
||||
|
||||
const t2days = [];
|
||||
const t2days: number[] = [];
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const d = insertDay.run(t2, i + 1, `2026-05-${21 + i}`);
|
||||
t2days.push(Number(d.lastInsertRowid));
|
||||
}
|
||||
|
||||
const t2places = [
|
||||
const t2places: [number, string, number, number, string, number, string, number, string, string | null, string | null, string | null, string | null][] = [
|
||||
[t2, 'W Barcelona', 41.3686, 2.1920, 'Placa de la Rosa dels Vents 1, 08039 Barcelona, Spain', 1, '14:00', 60, 'Right on the beach. Rooftop bar with panoramic views!', null, 'ChIJKfj5C8yjpBIRCPC3RPI0JO4', 'https://www.marriott.com/hotels/travel/bcnwh-w-barcelona/', '+34 932 95 28 00'],
|
||||
[t2, 'Sagrada Familia', 41.4036, 2.1744, 'C/ de Mallorca, 401, 08013 Barcelona, Spain', 3, '10:00', 120, 'Gaudi\'s masterpiece. Book tickets online in advance — sells out fast!', null, 'ChIJk_s92NyipBIRUMnDG8Kq2Js', 'https://sagradafamilia.org/', '+34 932 08 04 14'],
|
||||
[t2, 'Park Guell', 41.4145, 2.1527, '08024 Barcelona, Spain', 3, '09:00', 90, 'Mosaic terrace with city views. Book early for the Monumental Zone.', null, 'ChIJ4eQMeOmipBIRb65JRUzGE8k', 'https://parkguell.barcelona/', '+34 934 09 18 31'],
|
||||
@@ -204,17 +205,17 @@ function seedExampleTrips(db, adminId, demoId) {
|
||||
|
||||
insertMember.run(t2, demoId, adminId);
|
||||
|
||||
// ─── Trip 3: New York City ─────────────────────────────────────────────────
|
||||
// --- Trip 3: New York City ---
|
||||
const trip3 = insertTrip.run(adminId, 'New York City', 'The city that never sleeps — iconic landmarks, world-class food, and Broadway lights.', '2026-09-18', '2026-09-22', 'USD');
|
||||
const t3 = Number(trip3.lastInsertRowid);
|
||||
|
||||
const t3days = [];
|
||||
const t3days: number[] = [];
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const d = insertDay.run(t3, i + 1, `2026-09-${18 + i}`);
|
||||
t3days.push(Number(d.lastInsertRowid));
|
||||
}
|
||||
|
||||
const t3places = [
|
||||
const t3places: [number, string, number, number, string, number, string, number, string, string | null, string | null, string | null, string | null][] = [
|
||||
[t3, 'The Plaza Hotel', 40.7645, -73.9744, '768 5th Ave, New York, NY 10019, USA', 1, '15:00', 60, 'Iconic luxury hotel on Central Park. The lobby alone is worth a visit.', null, 'ChIJYbISlAVYwokRn6ORbSPV0xk', 'https://www.theplazany.com/', '+1 212-759-3000'],
|
||||
[t3, 'Statue of Liberty', 40.6892, -74.0445, 'Liberty Island, New York, NY 10004, USA', 3, '09:00', 180, 'Book crown access tickets months in advance. Ferry from Battery Park.', null, 'ChIJPTacEpBQwokRKwIlDXelxkA', 'https://www.nps.gov/stli/', '+1 212-363-3200'],
|
||||
[t3, 'Central Park', 40.7829, -73.9654, 'Central Park, New York, NY 10024, USA', 9, '10:00', 120, 'Bethesda Fountain, Bow Bridge, and Strawberry Fields. Rent bikes!', null, 'ChIJ4zGFAZpYwokRGUGph3Mf37k', 'https://www.centralparknyc.org/', null],
|
||||
@@ -251,7 +252,7 @@ function seedExampleTrips(db, adminId, demoId) {
|
||||
insertNote.run(t3days[4], t3, 'Flight departs JFK at 17:00 — last bagel at Russ & Daughters!', '10:00', 'Plane', 0);
|
||||
|
||||
// Packing
|
||||
const t3packing = [
|
||||
const t3packing: [string, number, string, number][] = [
|
||||
['Passport', 1, 'Documents', 0], ['ESTA confirmation', 1, 'Documents', 1],
|
||||
['Travel insurance', 0, 'Documents', 2], ['Comfortable sneakers', 0, 'Clothing', 3],
|
||||
['Light jacket', 0, 'Clothing', 4], ['Portable charger', 0, 'Electronics', 5],
|
||||
@@ -275,4 +276,4 @@ function seedExampleTrips(db, adminId, demoId) {
|
||||
console.log('[Demo] 3 example trips seeded and shared with demo user');
|
||||
}
|
||||
|
||||
module.exports = { seedDemoData };
|
||||
export { seedDemoData };
|
||||
Reference in New Issue
Block a user