From deadb04803d99636b910b7261d57aca70889043d Mon Sep 17 00:00:00 2001 From: fullsizemalt <106900403+fullsizemalt@users.noreply.github.com> Date: Thu, 11 Dec 2025 11:43:54 -0800 Subject: [PATCH] fix(backend): Add Batch-FacilityPlant relation and fix Prisma queries - Added bi-directional relation between Batch and FacilityPlant in schema.prisma - Fixed logic in insights.routes.ts to use simplified plant count query - Fixed duplicate property in messaging.routes.ts findMany filter - Fixed null/undefined type mismatch in audit.routes.ts extractClientInfo --- backend/prisma/schema.prisma | 2 ++ backend/src/routes/audit.routes.ts | 4 ++-- backend/src/routes/insights.routes.ts | 16 ++++++++++------ backend/src/routes/layout.routes.ts | 13 +------------ backend/src/routes/messaging.routes.ts | 20 +++++++++++++------- 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma index dfbc524..77dceff 100644 --- a/backend/prisma/schema.prisma +++ b/backend/prisma/schema.prisma @@ -161,6 +161,7 @@ model Batch { weights WeightLog[] notes BatchNote[] photos BatchPhoto[] + facilityPlants FacilityPlant[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@ -610,6 +611,7 @@ model FacilityPlant { id String @id @default(uuid()) tagNumber String @unique // METRC tag batchId String? + batch Batch? @relation(fields: [batchId], references: [id]) position FacilityPosition @relation(fields: [positionId], references: [id]) positionId String @unique address String // Full hierarchical address diff --git a/backend/src/routes/audit.routes.ts b/backend/src/routes/audit.routes.ts index 8aed6c5..b723a35 100644 --- a/backend/src/routes/audit.routes.ts +++ b/backend/src/routes/audit.routes.ts @@ -8,8 +8,8 @@ const prisma = new PrismaClient(); function extractClientInfo(request: FastifyRequest) { return { ipAddress: request.ip, - userAgent: request.headers['user-agent'] || null, - sessionId: (request as any).sessionId || null + userAgent: request.headers['user-agent'] || undefined, + sessionId: (request as any).sessionId || undefined }; } diff --git a/backend/src/routes/insights.routes.ts b/backend/src/routes/insights.routes.ts index 18198ee..5d048ee 100644 --- a/backend/src/routes/insights.routes.ts +++ b/backend/src/routes/insights.routes.ts @@ -151,16 +151,20 @@ export async function insightsRoutes(fastify: FastifyInstance) { // Get batch info const batch = await prisma.batch.findUnique({ - where: { id: batchId }, - include: { - plants: true - } + where: { id: batchId } }); if (!batch) { return reply.status(404).send({ error: 'Batch not found' }); } + // Get actual plant count from facility plants + const actualPlantCount = await prisma.facilityPlant.count({ + where: { batchId } + }); + + const effectivePlantCount = actualPlantCount > 0 ? actualPlantCount : (batch.plantCount || 0); + // Get environment data if available let envData: any = {}; @@ -190,7 +194,7 @@ export async function insightsRoutes(fastify: FastifyInstance) { const result = predictYield({ strain: batch.strain || 'default', - plantCount: batch.plants?.length || batch.plantCount || 0, + plantCount: effectivePlantCount, stage: batch.stage || 'VEG', daysInStage, ...envData @@ -212,7 +216,7 @@ export async function insightsRoutes(fastify: FastifyInstance) { name: batch.name, strain: batch.strain, stage: batch.stage, - plantCount: batch.plants?.length || batch.plantCount + plantCount: effectivePlantCount }, environment: envData }; diff --git a/backend/src/routes/layout.routes.ts b/backend/src/routes/layout.routes.ts index c7d481b..587f997 100644 --- a/backend/src/routes/layout.routes.ts +++ b/backend/src/routes/layout.routes.ts @@ -250,18 +250,7 @@ export async function layoutRoutes(fastify: FastifyInstance, options: FastifyPlu include: { positions: { include: { - plant: { - include: { - batch: { - select: { - id: true, - name: true, - strain: true, - stage: true - } - } - } - } + plant: true }, orderBy: [{ row: 'asc' }, { column: 'asc' }] } diff --git a/backend/src/routes/messaging.routes.ts b/backend/src/routes/messaging.routes.ts index 768655f..3b1fb15 100644 --- a/backend/src/routes/messaging.routes.ts +++ b/backend/src/routes/messaging.routes.ts @@ -45,13 +45,19 @@ export async function messagingRoutes(fastify: FastifyInstance) { const announcements = await prisma.announcement.findMany({ where: { - OR: [ - { targetRoles: { isEmpty: true } }, // No targeting = everyone - { targetRoles: { has: userRole } } - ], - OR: [ - { expiresAt: null }, - { expiresAt: { gt: new Date() } } + AND: [ + { + OR: [ + { targetRoles: { isEmpty: true } }, // No targeting = everyone + { targetRoles: { has: userRole } } + ] + }, + { + OR: [ + { expiresAt: null }, + { expiresAt: { gt: new Date() } } + ] + } ] }, include: {