Working state 1
This commit is contained in:
90
src/app/api/webhooks/scraper/route.ts
Normal file
90
src/app/api/webhooks/scraper/route.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { query } from '@/lib/db';
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const hotelId = searchParams.get('hotelId');
|
||||
const body = await req.json();
|
||||
|
||||
const {
|
||||
price,
|
||||
roomSizeM2,
|
||||
hotelName,
|
||||
rating,
|
||||
reviewCount,
|
||||
address,
|
||||
image,
|
||||
nights = 1,
|
||||
persons = 2,
|
||||
} = body;
|
||||
|
||||
if (!hotelId) {
|
||||
return NextResponse.json({ error: 'hotelId fehlt' }, { status: 400 });
|
||||
}
|
||||
|
||||
const parseNum = (val: any): number | null => {
|
||||
if (typeof val === 'number') return val;
|
||||
if (!val || typeof val !== 'string') return null;
|
||||
const match = val.replace(',', '.').match(/\d+(\.\d+)?/);
|
||||
return match ? parseFloat(match[0]) : null;
|
||||
};
|
||||
|
||||
const numPrice = parseNum(price);
|
||||
|
||||
// Automatisch 10 % Genius Rabatt berechnen
|
||||
let numGenius: number | null = null;
|
||||
if (numPrice !== null) {
|
||||
numGenius = parseFloat((numPrice * 0.90).toFixed(2));
|
||||
}
|
||||
|
||||
// Ratenberechnung auf Basis des Genius-Preises
|
||||
const basePrice = numGenius || numPrice;
|
||||
let pricePerNight = null;
|
||||
let pricePerPersonPerNight = null;
|
||||
|
||||
if (basePrice !== null && nights > 0 && persons > 0) {
|
||||
// 1. Zimmerpreis pro Nacht
|
||||
pricePerNight = parseFloat((basePrice / nights).toFixed(2));
|
||||
// 2. Preis pro Person & Nacht
|
||||
pricePerPersonPerNight = parseFloat((basePrice / (nights * persons)).toFixed(2));
|
||||
}
|
||||
|
||||
// 1. In Preishistorie eintragen
|
||||
if (numPrice !== null) {
|
||||
await query(
|
||||
`INSERT INTO hotel_price_history
|
||||
(hotel_id, price_numeric, genius_price_numeric, price_raw, price_per_night, price_per_person, nights, persons)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
|
||||
[
|
||||
hotelId,
|
||||
numPrice,
|
||||
numGenius,
|
||||
price ? String(price) : null,
|
||||
pricePerNight,
|
||||
pricePerPersonPerNight,
|
||||
nights,
|
||||
persons,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// 2. Hotel Stammdaten aktualisieren
|
||||
await query(
|
||||
`UPDATE hotels
|
||||
SET name = COALESCE($1, name),
|
||||
rating = COALESCE($2, rating),
|
||||
review_count = COALESCE($3, review_count),
|
||||
address = COALESCE($4, address),
|
||||
image_url = COALESCE($5, image_url),
|
||||
room_size_m2 = COALESCE($6, room_size_m2)
|
||||
WHERE id = $7`,
|
||||
[hotelName, rating, reviewCount, address, image, roomSizeM2, hotelId]
|
||||
);
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error: any) {
|
||||
console.error('Webhook Error:', error);
|
||||
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user