mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-11 17:26:32 +02:00
feat: add pg-ff-client to replace HTTP API calls with bulk PostgreSQL queries
This commit is contained in:
1 parent
32c1e06782
commit
bd8339b27d
3 files changed
+62
-5
No files matched your search
@@ -0,0 +1,25 @@
|
|||||||
|
require('dotenv').config({ path: 'c:/GIT/CollectFlow/.env.local' });
|
||||||
|
const { Pool } = require('pg');
|
||||||
|
|
||||||
|
async function test() {
|
||||||
|
const pool = new Pool({
|
||||||
|
connectionString: process.env.DATABASE_URL
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await pool.query("SELECT * FROM cube_stock WHERE cs.site = '292' LIMIT 5");
|
||||||
|
console.log("CUBE_STOCK:", res.rows);
|
||||||
|
} catch(e) {
|
||||||
|
console.log("ERROR cube_stock:", e.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res2 = await pool.query("SELECT a.codein, a.no_id FROM articles a LIMIT 5");
|
||||||
|
console.log("ARTICLES:", res2.rows);
|
||||||
|
} catch(e) {
|
||||||
|
console.log("ERROR articles:", e.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
pool.end();
|
||||||
|
}
|
||||||
|
test();
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { NextResponse } from 'next/server';
|
||||||
|
import { db } from '@/db';
|
||||||
|
import { sql } from 'drizzle-orm';
|
||||||
|
import { pgGetStockForCodeins } from '@/lib/pg-ff-client';
|
||||||
|
|
||||||
|
export async function GET(request: Request) {
|
||||||
|
try {
|
||||||
|
const { searchParams } = new URL(request.url);
|
||||||
|
const codein = searchParams.get('codein') || '334152';
|
||||||
|
|
||||||
|
const map = await pgGetStockForCodeins([codein]);
|
||||||
|
|
||||||
|
const fouResult = await db.execute(sql`
|
||||||
|
SELECT
|
||||||
|
a.codein::text AS codein,
|
||||||
|
COALESCE(fi.nom, af.code, 'Sans fournisseur')::text AS fournisseur
|
||||||
|
FROM articles a
|
||||||
|
LEFT JOIN artfou1 af ON af.art_no_id = a.no_id AND af.preference = 1
|
||||||
|
LEFT JOIN fouident fi ON fi.code = af.code
|
||||||
|
WHERE a.codein::text = ${codein}
|
||||||
|
`);
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
stock: Object.fromEntries(map),
|
||||||
|
fou: fouResult.rows,
|
||||||
|
});
|
||||||
|
} catch (e: any) {
|
||||||
|
return NextResponse.json({ error: e.message, stack: e.stack }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -616,15 +616,18 @@ export async function pgGetDashboardData(): Promise<DashboardData> {
|
|||||||
FROM articles a
|
FROM articles a
|
||||||
LEFT JOIN artfou1 af ON af.art_no_id = a.no_id AND af.preference = 1
|
LEFT JOIN artfou1 af ON af.art_no_id = a.no_id AND af.preference = 1
|
||||||
LEFT JOIN fouident fi ON fi.code = af.code
|
LEFT JOIN fouident fi ON fi.code = af.code
|
||||||
WHERE a.codein::text = ANY(string_to_array(${allCodeins.join(",")}, ','))
|
WHERE a.codein IN (${sql.join(allCodeins.map(c => sql`${c}`), sql`, `)})
|
||||||
`),
|
`),
|
||||||
]);
|
]);
|
||||||
stockMap = sm;
|
stockMap = sm;
|
||||||
for (const row of fouResult.rows as unknown as { codein: string; fournisseur: string }[]) {
|
for (const row of fouResult.rows as unknown as { codein: string; fournisseur: string }[]) {
|
||||||
if (row.codein) fouMap.set(row.codein, row.fournisseur);
|
if (row.codein) fouMap.set(row.codein, row.fournisseur);
|
||||||
}
|
}
|
||||||
} catch (enrichErr) {
|
} catch (enrichErr: any) {
|
||||||
console.warn("[Dashboard] DB enrichment unavailable, continuing with API data only:", enrichErr);
|
console.warn("[Dashboard] DB enrichment unavailable, continuing with API data only:", enrichErr);
|
||||||
|
try {
|
||||||
|
require('fs').writeFileSync('c:/GIT/CollectFlow/scripts/db-error.log', enrichErr.stack || enrichErr.message || String(enrichErr));
|
||||||
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -848,12 +851,11 @@ export interface StockBySite {
|
|||||||
export async function pgGetStockForCodeins(codeins: string[]): Promise<Map<string, StockBySite>> {
|
export async function pgGetStockForCodeins(codeins: string[]): Promise<Map<string, StockBySite>> {
|
||||||
if (codeins.length === 0) return new Map();
|
if (codeins.length === 0) return new Map();
|
||||||
|
|
||||||
const codeinsStr = codeins.join(",");
|
|
||||||
const result = await pgNoParallel(
|
const result = await pgNoParallel(
|
||||||
sql`SELECT a.codein::text AS codein, cs.site, COALESCE(cs.qte, 0)::float AS stockdispo
|
sql`SELECT a.codein AS codein, cs.site, COALESCE(cs.qte, 0)::float AS stockdispo
|
||||||
FROM cube_stock cs
|
FROM cube_stock cs
|
||||||
JOIN articles a ON a.no_id = cs.artnoid
|
JOIN articles a ON a.no_id = cs.artnoid
|
||||||
WHERE a.codein::text = ANY(string_to_array(${codeinsStr}, ','))`
|
WHERE a.codein IN (${sql.join(codeins.map(c => sql`${c}`), sql`, `)})`
|
||||||
);
|
);
|
||||||
const rows = result.rows as { codein: string; site: string; stockdispo: number }[];
|
const rows = result.rows as { codein: string; site: string; stockdispo: number }[];
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user