fix: updated layout logic to organize legacy rooms and prevent overlaps
Some checks are pending
Deploy to Production / deploy (push) Waiting to run
Test / backend-test (push) Waiting to run
Test / frontend-test (push) Waiting to run

This commit is contained in:
fullsizemalt 2025-12-17 22:36:09 -08:00
parent 073b1c7e16
commit d01ef2f30c

View file

@ -175,109 +175,93 @@ async function main() {
console.log('Created Facility Floor');
}
// 4. Rooms (Spatial)
const spatialRooms = [
{
name: 'Veg Room', code: 'VEG', type: RoomType.VEG,
posX: 5, posY: 5, width: 30, height: 40, color: '#4ade80',
sections: [
{ name: 'Rack A', code: 'A', rows: 4, columns: 8, spacing: 2, posX: 2, posY: 2 },
{ name: 'Rack B', code: 'B', rows: 4, columns: 8, spacing: 2, posX: 15, posY: 2 }
]
},
{
name: 'Flower Room', code: 'FLO', type: RoomType.FLOWER,
posX: 40, posY: 5, width: 50, height: 60, color: '#a855f7',
sections: [
{ name: 'Bench 1', code: 'B1', rows: 10, columns: 4, spacing: 2, posX: 5, posY: 5 },
{ name: 'Bench 2', code: 'B2', rows: 10, columns: 4, spacing: 2, posX: 20, posY: 5 },
{ name: 'Bench 3', code: 'B3', rows: 10, columns: 4, spacing: 2, posX: 35, posY: 5 }
]
},
{
name: 'Dry Room', code: 'DRY', type: RoomType.DRY,
posX: 5, posY: 50, width: 25, height: 25, color: '#f59e0b',
sections: [
{ name: 'Hangers', code: 'H1', rows: 2, columns: 10, spacing: 1, posX: 2, posY: 2 }
]
}
];
// 4. Rooms (Spatial Layout Organization)
// We define explicit positions for known room names to ensure a clean, non-overlapping layout.
// Any existing rooms matching these names will be moved to these coordinates.
const layoutMap = new Map([
// Legacy Rooms (from original seed)
['Veg Room 1', { x: 5, y: 5, w: 30, h: 40, c: '#4ade80', type: RoomType.VEG }],
['Veg Room 2', { x: 5, y: 55, w: 30, h: 40, c: '#4ade80', type: RoomType.VEG }],
['Flower Room A', { x: 45, y: 5, w: 50, h: 60, c: '#a855f7', type: RoomType.FLOWER }],
['Flower Room B', { x: 45, y: 75, w: 50, h: 60, c: '#a855f7', type: RoomType.FLOWER }],
for (const r of spatialRooms) {
let room = await prisma.facilityRoom.findFirst({
where: { floorId: floor.id, code: r.code }
// New/Demo Rooms
['Veg Room', { x: 5, y: 105, w: 30, h: 40, c: '#4ade80', type: RoomType.VEG }],
['Flower Room', { x: 45, y: 145, w: 50, h: 60, c: '#a855f7', type: RoomType.FLOWER }],
// Common Rooms
['Dry Room', { x: 105, y: 5, w: 25, h: 30, c: '#f59e0b', type: RoomType.DRY }],
['Mother Room', { x: 105, y: 45, w: 25, h: 25, c: '#4ade80', type: RoomType.MOTHER }],
['Clone Room', { x: 105, y: 80, w: 20, h: 20, c: '#4ade80', type: RoomType.CLONE }],
]);
// 1. Update/Move Existing Rooms
const existingRooms = await prisma.facilityRoom.findMany({
where: { floorId: floor.id }
});
if (!room) {
room = await prisma.facilityRoom.create({
data: {
floorId: floor.id,
name: r.name,
code: r.code,
type: r.type,
posX: r.posX,
posY: r.posY,
width: r.width,
height: r.height,
color: r.color
}
});
console.log(`Created Spatial Room: ${r.name}`);
} else {
for (const room of existingRooms) {
if (layoutMap.has(room.name)) {
const layout = layoutMap.get(room.name);
await prisma.facilityRoom.update({
where: { id: room.id },
data: {
posX: r.posX,
posY: r.posY,
width: r.width,
height: r.height,
color: r.color
posX: layout.x,
posY: layout.y,
width: layout.w,
height: layout.h,
color: layout.c
}
});
console.log(`Updated Spatial Room Coords: ${r.name}`);
}
// Sections
for (const s of r.sections) {
let section = await prisma.facilitySection.findFirst({
where: { roomId: room.id, code: s.code }
console.log(`Updated Layout for: ${room.name}`);
// Remove from map so we don't double-create
layoutMap.delete(room.name);
} else {
// Move unknown/other rooms out of the way to a "Garage" area
console.log(`Moving unknown room to storage area: ${room.name}`);
await prisma.facilityRoom.update({
where: { id: room.id },
data: { posX: -100, posY: -100 } // Hide off-map or in negative space
});
if (!section) {
// Create section & positions
const positions = [];
for (let row = 1; row <= s.rows; row++) {
for (let col = 1; col <= s.columns; col++) {
positions.push({ row, column: col, tier: 1, slot: 1 });
}
}
section = await prisma.facilitySection.create({
// 2. Create missing rooms from the map
for (const [name, layout] of layoutMap) {
const room = await prisma.facilityRoom.create({
data: {
floorId: floor.id,
name: name,
code: name.substring(0, 3).toUpperCase(),
type: layout.type,
posX: layout.x,
posY: layout.y,
width: layout.w,
height: layout.h,
color: layout.c
}
});
console.log(`Created Missing Room: ${name}`);
// Add basic sections if it's a new room
if (layout.type === RoomType.VEG || layout.type === RoomType.FLOWER) {
const isVeg = layout.type === RoomType.VEG;
await prisma.facilitySection.create({
data: {
roomId: room.id,
name: s.name,
code: s.code,
name: isVeg ? 'Rack A' : 'Bench 1',
code: isVeg ? 'RA' : 'B1',
type: SectionType.RACK,
posX: s.posX,
posY: s.posY,
width: s.columns * s.spacing,
height: s.rows * s.spacing,
rows: s.rows,
columns: s.columns,
spacing: s.spacing,
posX: 2, posY: 2,
width: 10, height: 20,
rows: 4, columns: 4, spacing: 2,
positions: {
create: positions
create: Array(16).fill({ row: 1, column: 1 }).map((_, i) => ({
row: Math.floor(i / 4) + 1, column: (i % 4) + 1, tier: 1
}))
}
}
});
console.log(`Created Section: ${s.name} in ${r.name}`);
} else {
// Update section pos
await prisma.facilitySection.update({
where: { id: section.id },
data: { posX: s.posX, posY: s.posY }
});
}
}
}