UNPKG

revit-cli

Version:

A scalable CLI tool for Revit communication and data manipulation

166 lines 6.61 kB
"use strict"; /** * Room service layer for API communication * Centralizes room-related API operations */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RoomService = void 0; const room_utils_js_1 = require("../utils/room-utils.js"); class RoomService { constructor(revitConnector, logger) { this.revitConnector = revitConnector; this.logger = logger; } /** * Retrieves all rooms from the Revit API * @returns Promise resolving to array of room data */ async getAllRooms() { try { this.logger.info('Fetching rooms from Revit API...'); const response = await this.revitConnector.request({ method: 'GET', url: '/rooms' }); // Handle nested response structure const rooms = response.data?.data || response.data || []; if (!Array.isArray(rooms)) { throw new Error('Invalid response format: rooms data is not an array'); } this.logger.info(`Successfully retrieved ${rooms.length} rooms`); return rooms; } catch (error) { this.logger.error('Failed to fetch rooms:', error); throw new Error(`Failed to retrieve rooms: ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * Updates a single room with new parameters * @param roomId - ID of the room to update * @param parameters - Parameters to update * @returns Promise resolving to update response */ async updateRoom(roomId, parameters) { try { // Validate parameters const validation = (0, room_utils_js_1.validateUpdateParameters)(parameters); if (!validation.isValid) { throw new Error(`Invalid parameters: ${validation.errors.join(', ')}`); } this.logger.info(`Updating room ${roomId} with parameters:`, parameters); const updateRequest = { id: roomId, parameters }; const response = await this.revitConnector.request({ method: 'POST', url: '/rooms/update', data: updateRequest }); this.logger.info(`Successfully updated room ${roomId}`); return response.data; } catch (error) { this.logger.error(`Failed to update room ${roomId}:`, error); throw new Error(`Failed to update room: ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * Performs bulk update on multiple rooms * @param options - Bulk update options including filter and parameters * @returns Promise resolving to bulk update result */ async bulkUpdateRooms(options) { try { // Validate parameters const validation = (0, room_utils_js_1.validateUpdateParameters)(options.parameters); if (!validation.isValid) { throw new Error(`Invalid parameters: ${validation.errors.join(', ')}`); } // Get all rooms and filter them const allRooms = await this.getAllRooms(); const filteredRooms = (0, room_utils_js_1.filterRooms)(allRooms, options.filter); if (filteredRooms.length === 0) { this.logger.warn('No rooms match the specified filter criteria'); return { totalProcessed: 0, successCount: 0, failureCount: 0, updatedRooms: [], errors: [] }; } this.logger.info(`Found ${filteredRooms.length} rooms matching filter criteria`); if (options.dryRun) { this.logger.info('Dry run mode - no actual updates will be performed'); return { totalProcessed: filteredRooms.length, successCount: filteredRooms.length, failureCount: 0, updatedRooms: filteredRooms, errors: [] }; } // Perform updates const result = { totalProcessed: filteredRooms.length, successCount: 0, failureCount: 0, updatedRooms: [], errors: [] }; for (const room of filteredRooms) { try { const updateResponse = await this.updateRoom(room.id, options.parameters); if (updateResponse.success && updateResponse.room) { result.successCount++; result.updatedRooms.push(updateResponse.room); } else { result.failureCount++; result.errors.push({ roomId: room.id, error: updateResponse.error || 'Update failed without specific error' }); } } catch (error) { result.failureCount++; result.errors.push({ roomId: room.id, error: error instanceof Error ? error.message : 'Unknown error' }); } } this.logger.info(`Bulk update completed: ${result.successCount} successful, ${result.failureCount} failed`); return result; } catch (error) { this.logger.error('Bulk update operation failed:', error); throw new Error(`Bulk update failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * Gets a specific room by ID * @param roomId - ID of the room to retrieve * @returns Promise resolving to room data or null if not found */ async getRoomById(roomId) { try { this.logger.info(`Fetching room with ID: ${roomId}`); const response = await this.revitConnector.request({ method: 'GET', url: `/rooms/${roomId}` }); const room = response.data?.data || response.data; return room || null; } catch (error) { this.logger.error(`Failed to fetch room ${roomId}:`, error); return null; } } } exports.RoomService = RoomService; //# sourceMappingURL=room-service.js.map