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
This commit is contained in:
parent
e34df722bb
commit
deadb04803
5 changed files with 28 additions and 27 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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' }]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,14 +45,20 @@ export async function messagingRoutes(fastify: FastifyInstance) {
|
|||
|
||||
const announcements = await prisma.announcement.findMany({
|
||||
where: {
|
||||
AND: [
|
||||
{
|
||||
OR: [
|
||||
{ targetRoles: { isEmpty: true } }, // No targeting = everyone
|
||||
{ targetRoles: { has: userRole } }
|
||||
],
|
||||
]
|
||||
},
|
||||
{
|
||||
OR: [
|
||||
{ expiresAt: null },
|
||||
{ expiresAt: { gt: new Date() } }
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
include: {
|
||||
createdBy: { select: { id: true, name: true } },
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue