forked from jappel/leistungsbilanz-ts
88 lines
3.1 KiB
TypeScript
88 lines
3.1 KiB
TypeScript
import type {
|
|
ExternalModelStateSnapshot,
|
|
} from "../domain/external-model-contracts.js";
|
|
|
|
export interface ExternalCircuitListObjectDto {
|
|
id: string;
|
|
ifcGuid: string;
|
|
selectionMarker: string;
|
|
familyAndType: string;
|
|
sourceCircuitIdentifier: string;
|
|
displayName: string | null;
|
|
category: string | null;
|
|
connectionKind: string | null;
|
|
effectiveQuantity: number;
|
|
powerPerUnitW: number | null;
|
|
sourceRoomNumber: string;
|
|
sourceRoomName: string;
|
|
roomId: string | null;
|
|
linkedProjectDeviceId: string | null;
|
|
circuitDeviceRowId: string | null;
|
|
presenceStatus: "present" | "missing";
|
|
}
|
|
|
|
export interface ExternalCircuitListObjectsDto {
|
|
sourceName: string | null;
|
|
objectCount: number;
|
|
unassignedObjectCount: number;
|
|
objects: ExternalCircuitListObjectDto[];
|
|
}
|
|
|
|
export function createExternalCircuitListObjects(input: {
|
|
state: ExternalModelStateSnapshot;
|
|
distributionBoardId: string;
|
|
}): ExternalCircuitListObjectsDto {
|
|
const mappingById = new Map(
|
|
input.state.roomMappings.map((mapping) => [mapping.id, mapping])
|
|
);
|
|
const objects = input.state.objects
|
|
.filter((object) => object.distributionBoardId === input.distributionBoardId)
|
|
.map((object) => {
|
|
const mapping = object.externalRoomMappingId === null
|
|
? null
|
|
: mappingById.get(object.externalRoomMappingId) ?? null;
|
|
return {
|
|
id: object.id,
|
|
ifcGuid: object.ifcGuid,
|
|
selectionMarker: object.acceptedSourceValues.selectionMarker,
|
|
familyAndType: object.acceptedSourceValues.familyAndType,
|
|
sourceCircuitIdentifier: object.acceptedSourceValues.circuitIdentifier,
|
|
displayName: object.planningValues.displayName,
|
|
category: object.planningValues.category,
|
|
connectionKind: object.planningValues.connectionKind,
|
|
effectiveQuantity: object.planningValues.effectiveQuantity,
|
|
powerPerUnitW: object.planningValues.powerPerUnitW,
|
|
sourceRoomNumber:
|
|
mapping?.sourceRoomNumber ?? object.acceptedSourceValues.roomNumber,
|
|
sourceRoomName:
|
|
mapping?.sourceRoomName ?? object.acceptedSourceValues.roomName,
|
|
roomId: mapping?.roomId ?? null,
|
|
linkedProjectDeviceId: object.linkedProjectDeviceId,
|
|
circuitDeviceRowId: object.circuitDeviceRowId,
|
|
presenceStatus: object.presenceStatus,
|
|
} satisfies ExternalCircuitListObjectDto;
|
|
})
|
|
.sort(compareExternalObjects);
|
|
return {
|
|
sourceName: input.state.source?.name ?? null,
|
|
objectCount: objects.length,
|
|
unassignedObjectCount: objects.filter(
|
|
(object) => object.circuitDeviceRowId === null
|
|
).length,
|
|
objects,
|
|
};
|
|
}
|
|
|
|
function compareExternalObjects(
|
|
left: ExternalCircuitListObjectDto,
|
|
right: ExternalCircuitListObjectDto
|
|
) {
|
|
const assignmentOrder = Number(left.circuitDeviceRowId !== null) -
|
|
Number(right.circuitDeviceRowId !== null);
|
|
if (assignmentOrder !== 0) return assignmentOrder;
|
|
return left.sourceRoomNumber.localeCompare(right.sourceRoomNumber, "de", {
|
|
numeric: true,
|
|
}) || left.selectionMarker.localeCompare(right.selectionMarker, "de", {
|
|
numeric: true,
|
|
}) || left.ifcGuid.localeCompare(right.ifcGuid);
|
|
}
|