Files
fuz-site/src/pages/api/internet/addons.js
2025-12-11 17:33:34 +01:00

83 lines
1.7 KiB
JavaScript

import Database from "better-sqlite3";
const DB_PATH = "./src/data/ServicesRange.db";
export async function GET() {
const db = new Database(DB_PATH, { readonly: true });
try {
const addonsRows = db
.prepare(
`
SELECT id, name, type, description
FROM internet_addons
ORDER BY id
`
)
.all();
const optionsRows = db
.prepare(
`
SELECT id, addon_id, code, name, price
FROM internet_addon_options
ORDER BY addon_id, id
`
)
.all();
const byAddon = new Map();
for (const addon of addonsRows) {
byAddon.set(addon.id, {
id: addon.id,
name: addon.name,
type: addon.type, // 'checkbox' / 'select'
description: addon.description || "",
options: [],
});
}
for (const opt of optionsRows) {
const parent = byAddon.get(opt.addon_id);
if (!parent) continue;
parent.options.push({
id: opt.id,
code: opt.code,
name: opt.name,
price: opt.price,
});
}
const data = Array.from(byAddon.values());
return new Response(
JSON.stringify({
ok: true,
count: data.length,
data,
}),
{
status: 200,
headers: {
"Content-Type": "application/json; charset=utf-8",
},
}
);
} catch (err) {
console.error("❌ Błąd w /api/internet/addons:", err);
return new Response(
JSON.stringify({
ok: false,
error: err.message || "DB_ERROR",
}),
{
status: 500,
headers: { "Content-Type": "application/json" },
}
);
} finally {
db.close();
}
}