feat: add queries mapping pricing config and catalog to engine types

This commit is contained in:
Carlos Narro
2026-05-30 12:33:11 +02:00
parent 892c257182
commit c00c571549

View File

@@ -0,0 +1,54 @@
import { eq } from 'drizzle-orm';
import { db } from './index';
import { pricingConfig, catalogItems, tenants } from './schema';
import { TENANT_SLUG } from '@/lib/funnel';
import type { PricingConfig, CatalogItem, ManoObraKey } from '@/budget/types';
async function getTenantId(): Promise<string> {
const [tenant] = await db.select().from(tenants).where(eq(tenants.slug, TENANT_SLUG)).limit(1);
if (!tenant) throw new Error(`Tenant "${TENANT_SLUG}" no existe. ¿Has corrido npm run db:seed?`);
return tenant.id;
}
const MANO_OBRA_DEFAULT: Record<ManoObraKey, number> = {
demolicion: 0,
fontaneria: 0,
electricidad: 0,
mano_de_obra: 0,
};
export async function getPricingConfig(): Promise<PricingConfig> {
const tenantId = await getTenantId();
const [row] = await db
.select()
.from(pricingConfig)
.where(eq(pricingConfig.tenantId, tenantId))
.limit(1);
if (!row) {
return { alturaTechoDefault: 2.5, factorZona: {}, manoObra: { ...MANO_OBRA_DEFAULT } };
}
return {
alturaTechoDefault: row.alturaTechoDefault,
factorZona: row.factorZona,
manoObra: { ...MANO_OBRA_DEFAULT, ...(row.manoObra as Record<ManoObraKey, number>) },
};
}
export async function getCatalog(): Promise<CatalogItem[]> {
const tenantId = await getTenantId();
const rows = await db.select().from(catalogItems).where(eq(catalogItems.tenantId, tenantId));
return rows.map((r) => ({
id: r.id,
categoria: r.categoria,
nombre: r.nombre,
calidad: r.calidad,
precioUnit: r.precioUnit,
unidad: r.unidad,
descriptorRender: r.descriptorRender,
esDefault: r.esDefault,
sku: r.sku,
}));
}
export { getTenantId };