Files
rack-planner/server/services/portService.ts
jason 231de3d005 Initial scaffold: full-stack RackMapper application
Complete project scaffold with working auth, REST API, Prisma/SQLite
schema, Docker config, and React frontend for both Rack Planner and
Service Mapper modules. Both server and client pass TypeScript strict
mode with zero errors. Initial migration applied.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 21:48:56 -05:00

48 lines
1.4 KiB
TypeScript

import { prisma } from '../lib/prisma';
import { AppError } from '../types/index';
import type { VlanMode } from '../lib/constants';
const portInclude = {
vlans: { include: { vlan: true } },
};
export async function updatePort(
id: string,
data: {
label?: string;
mode?: VlanMode;
nativeVlan?: number | null;
notes?: string;
vlans?: Array<{ vlanId: string; tagged: boolean }>;
}
) {
const existing = await prisma.port.findUnique({ where: { id } });
if (!existing) throw new AppError('Port not found', 404, 'NOT_FOUND');
const { vlans: vlanAssignments, ...portData } = data;
return prisma.$transaction(async (tx) => {
await tx.port.update({ where: { id }, data: portData });
if (vlanAssignments !== undefined) {
if (vlanAssignments.length > 0) {
const vlanIds = vlanAssignments.map((v) => v.vlanId);
const found = await tx.vlan.findMany({ where: { id: { in: vlanIds } } });
if (found.length !== vlanIds.length) {
throw new AppError('One or more VLANs not found', 404, 'VLAN_NOT_FOUND');
}
}
await tx.portVlan.deleteMany({ where: { portId: id } });
if (vlanAssignments.length > 0) {
await tx.portVlan.createMany({
data: vlanAssignments.map(({ vlanId, tagged }) => ({ portId: id, vlanId, tagged })),
});
}
}
return tx.port.findUnique({ where: { id }, include: portInclude });
});
}