fix: sanoma different book retrieval
Browse files
server.js
CHANGED
|
@@ -7,6 +7,7 @@ import { fileURLToPath } from 'url';
|
|
| 7 |
import { randomUUID } from 'crypto';
|
| 8 |
import fs from 'fs';
|
| 9 |
import { performBsmartLogin, getBooks, getUserInfo } from './providers/src/bsmart/api.js';
|
|
|
|
| 10 |
|
| 11 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 12 |
|
|
@@ -145,95 +146,18 @@ app.post('/api/sanoma-gedi', async (req, res) => {
|
|
| 145 |
}
|
| 146 |
|
| 147 |
try {
|
| 148 |
-
const
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
for (const base of SANOMA_BASE_URLS) {
|
| 158 |
-
const url = `${base}${pathname}`;
|
| 159 |
-
try {
|
| 160 |
-
const response = await fetch(url, init);
|
| 161 |
-
const payload = await response.json().catch(() => ({}));
|
| 162 |
-
if (!response.ok) {
|
| 163 |
-
const message = payload?.message || payload?.error || `HTTP ${response.status}`;
|
| 164 |
-
throw new Error(`${url}: ${message}`);
|
| 165 |
-
}
|
| 166 |
-
return payload;
|
| 167 |
-
} catch (err) {
|
| 168 |
-
lastError = err;
|
| 169 |
}
|
| 170 |
-
}
|
| 171 |
-
|
| 172 |
-
throw lastError || new Error('Sanoma API request failed');
|
| 173 |
-
}
|
| 174 |
-
|
| 175 |
-
function getAccessToken(userAuth) {
|
| 176 |
-
return userAuth?.result?.data?.access_token
|
| 177 |
-
|| userAuth?.data?.access_token
|
| 178 |
-
|| userAuth?.access_token
|
| 179 |
-
|| userAuth?.token
|
| 180 |
-
|| null;
|
| 181 |
-
}
|
| 182 |
-
|
| 183 |
-
function normalizeBooksPage(payload) {
|
| 184 |
-
const rows = payload?.result?.data || payload?.data || payload?.books || [];
|
| 185 |
-
const totalSize = payload?.result?.total_size ?? payload?.total_size ?? payload?.total ?? rows.length;
|
| 186 |
-
const rawPageSize = payload?.result?.page_size ?? payload?.page_size ?? rows.length;
|
| 187 |
-
const pageSize = rawPageSize || 1;
|
| 188 |
-
|
| 189 |
-
return {
|
| 190 |
-
rows: Array.isArray(rows) ? rows : [],
|
| 191 |
-
pages: Math.max(1, Math.ceil(totalSize / pageSize)),
|
| 192 |
-
};
|
| 193 |
}
|
| 194 |
|
| 195 |
-
const userAuth = await fetchSanomaJson('/login', {
|
| 196 |
-
method: 'POST',
|
| 197 |
-
headers: {
|
| 198 |
-
'Content-Type': 'application/json',
|
| 199 |
-
'X-Timezone-Offset': '+0200'
|
| 200 |
-
},
|
| 201 |
-
body: JSON.stringify({ id, password }),
|
| 202 |
-
});
|
| 203 |
-
|
| 204 |
-
if (!userAuth || (userAuth.code != null && userAuth.code !== 0)) {
|
| 205 |
-
res.status(401).json({ error: 'Failed to log in: ' + (userAuth.message || 'Unknown error') });
|
| 206 |
-
return;
|
| 207 |
-
}
|
| 208 |
-
|
| 209 |
-
const accessToken = getAccessToken(userAuth);
|
| 210 |
-
if (!accessToken) {
|
| 211 |
-
res.status(502).json({ error: 'Login OK ma token non presente nella risposta API' });
|
| 212 |
-
return;
|
| 213 |
-
}
|
| 214 |
-
|
| 215 |
-
const books = {};
|
| 216 |
-
let pages = 1;
|
| 217 |
-
|
| 218 |
-
for (let i = 1; i <= pages; i++) {
|
| 219 |
-
const newBooks = await fetchSanomaJson(`/books?app=true&page=${i}`, {
|
| 220 |
-
headers: { 'X-Auth-Token': 'Bearer ' + accessToken },
|
| 221 |
-
});
|
| 222 |
-
|
| 223 |
-
const pageInfo = normalizeBooksPage(newBooks);
|
| 224 |
-
pages = pageInfo.pages;
|
| 225 |
-
|
| 226 |
-
for (const book of pageInfo.rows) {
|
| 227 |
-
if (!book?.gedi) continue;
|
| 228 |
-
books[book.gedi] = book;
|
| 229 |
-
}
|
| 230 |
-
}
|
| 231 |
-
|
| 232 |
-
const bookList = Object.entries(books).map(([gedi, book]) => ({
|
| 233 |
-
gedi,
|
| 234 |
-
name: book.name || book.title || `GEDI ${gedi}`
|
| 235 |
-
}));
|
| 236 |
-
|
| 237 |
res.status(200).json({ success: true, books: bookList });
|
| 238 |
} catch (err) {
|
| 239 |
res.status(500).json({ error: err.message });
|
|
|
|
| 7 |
import { randomUUID } from 'crypto';
|
| 8 |
import fs from 'fs';
|
| 9 |
import { performBsmartLogin, getBooks, getUserInfo } from './providers/src/bsmart/api.js';
|
| 10 |
+
import * as sanomaProvider from './providers/sanoma.js';
|
| 11 |
|
| 12 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 13 |
|
|
|
|
| 146 |
}
|
| 147 |
|
| 148 |
try {
|
| 149 |
+
const rawBooks = await sanomaProvider.getBooks({ id, password });
|
| 150 |
+
|
| 151 |
+
const bookList = [];
|
| 152 |
+
for (const b of rawBooks) {
|
| 153 |
+
for (const p of b.products) {
|
| 154 |
+
bookList.push({
|
| 155 |
+
gedi: p.id,
|
| 156 |
+
name: p.name
|
| 157 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
}
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
res.status(200).json({ success: true, books: bookList });
|
| 162 |
} catch (err) {
|
| 163 |
res.status(500).json({ error: err.message });
|