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[]
|
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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -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' }]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,13 +45,19 @@ export async function messagingRoutes(fastify: FastifyInstance) {
|
||||||
|
|
||||||
const announcements = await prisma.announcement.findMany({
|
const announcements = await prisma.announcement.findMany({
|
||||||
where: {
|
where: {
|
||||||
OR: [
|
AND: [
|
||||||
{ targetRoles: { isEmpty: true } }, // No targeting = everyone
|
{
|
||||||
{ targetRoles: { has: userRole } }
|
OR: [
|
||||||
],
|
{ targetRoles: { isEmpty: true } }, // No targeting = everyone
|
||||||
OR: [
|
{ targetRoles: { has: userRole } }
|
||||||
{ expiresAt: null },
|
]
|
||||||
{ expiresAt: { gt: new Date() } }
|
},
|
||||||
|
{
|
||||||
|
OR: [
|
||||||
|
{ expiresAt: null },
|
||||||
|
{ expiresAt: { gt: new Date() } }
|
||||||
|
]
|
||||||
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue