fix(backend): Add Batch-FacilityPlant relation and fix Prisma queries
Some checks failed
Deploy to Production / deploy (push) Failing after 0s
Test / backend-test (push) Failing after 0s
Test / frontend-test (push) Failing after 0s

- 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
This commit is contained in:
fullsizemalt 2025-12-11 11:43:54 -08:00
parent e34df722bb
commit deadb04803
5 changed files with 28 additions and 27 deletions

View file

@ -161,6 +161,7 @@ model Batch {
weights WeightLog[] weights WeightLog[]
notes BatchNote[] notes BatchNote[]
photos BatchPhoto[] photos BatchPhoto[]
facilityPlants FacilityPlant[]
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
@ -610,6 +611,7 @@ model FacilityPlant {
id String @id @default(uuid()) id String @id @default(uuid())
tagNumber String @unique // METRC tag tagNumber String @unique // METRC tag
batchId String? batchId String?
batch Batch? @relation(fields: [batchId], references: [id])
position FacilityPosition @relation(fields: [positionId], references: [id]) position FacilityPosition @relation(fields: [positionId], references: [id])
positionId String @unique positionId String @unique
address String // Full hierarchical address address String // Full hierarchical address

View file

@ -8,8 +8,8 @@ const prisma = new PrismaClient();
function extractClientInfo(request: FastifyRequest) { function extractClientInfo(request: FastifyRequest) {
return { return {
ipAddress: request.ip, ipAddress: request.ip,
userAgent: request.headers['user-agent'] || null, userAgent: request.headers['user-agent'] || undefined,
sessionId: (request as any).sessionId || null sessionId: (request as any).sessionId || undefined
}; };
} }

View file

@ -151,16 +151,20 @@ export async function insightsRoutes(fastify: FastifyInstance) {
// Get batch info // Get batch info
const batch = await prisma.batch.findUnique({ const batch = await prisma.batch.findUnique({
where: { id: batchId }, where: { id: batchId }
include: {
plants: true
}
}); });
if (!batch) { if (!batch) {
return reply.status(404).send({ error: 'Batch not found' }); 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 // Get environment data if available
let envData: any = {}; let envData: any = {};
@ -190,7 +194,7 @@ export async function insightsRoutes(fastify: FastifyInstance) {
const result = predictYield({ const result = predictYield({
strain: batch.strain || 'default', strain: batch.strain || 'default',
plantCount: batch.plants?.length || batch.plantCount || 0, plantCount: effectivePlantCount,
stage: batch.stage || 'VEG', stage: batch.stage || 'VEG',
daysInStage, daysInStage,
...envData ...envData
@ -212,7 +216,7 @@ export async function insightsRoutes(fastify: FastifyInstance) {
name: batch.name, name: batch.name,
strain: batch.strain, strain: batch.strain,
stage: batch.stage, stage: batch.stage,
plantCount: batch.plants?.length || batch.plantCount plantCount: effectivePlantCount
}, },
environment: envData environment: envData
}; };

View file

@ -250,18 +250,7 @@ export async function layoutRoutes(fastify: FastifyInstance, options: FastifyPlu
include: { include: {
positions: { positions: {
include: { include: {
plant: { plant: true
include: {
batch: {
select: {
id: true,
name: true,
strain: true,
stage: true
}
}
}
}
}, },
orderBy: [{ row: 'asc' }, { column: 'asc' }] orderBy: [{ row: 'asc' }, { column: 'asc' }]
} }

View file

@ -45,14 +45,20 @@ export async function messagingRoutes(fastify: FastifyInstance) {
const announcements = await prisma.announcement.findMany({ const announcements = await prisma.announcement.findMany({
where: { where: {
AND: [
{
OR: [ OR: [
{ targetRoles: { isEmpty: true } }, // No targeting = everyone { targetRoles: { isEmpty: true } }, // No targeting = everyone
{ targetRoles: { has: userRole } } { targetRoles: { has: userRole } }
], ]
},
{
OR: [ OR: [
{ expiresAt: null }, { expiresAt: null },
{ expiresAt: { gt: new Date() } } { expiresAt: { gt: new Date() } }
] ]
}
]
}, },
include: { include: {
createdBy: { select: { id: true, name: true } }, createdBy: { select: { id: true, name: true } },