All files / backend/src/api/handlers/helpers workspace-helper.ts

0% Statements 0/10
0% Branches 0/7
0% Functions 0/2
0% Lines 0/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                                                                                                             
/**
 * Workspace Handler Helper
 *
 * Provides utility functions for working with WorkspaceService in handlers.
 */
 
import type { AppContainer } from '../../../config/container.js';
import type { WorkspaceService } from '../../../services/workspace/workspace-service.js';
import { SiteService } from '../../../services/site/site-service.js';
 
/**
 * Create a WorkspaceService instance for the given workspace
 */
export async function createWorkspaceServiceForParams(
  container: AppContainer,
  siteKey: string,
  workspaceKey: string
): Promise<WorkspaceService> {
  // Get site configuration
  const siteConfig = await container.libraryService.getSiteConf(siteKey);
 
  // Create SiteService instance
  const siteService = new SiteService(
    siteConfig,
    container.siteSourceFactory,
    container.syncFactory
  );
 
  // Get workspace head to find the path
  const workspace = await siteService.getWorkspaceHead(workspaceKey);
 
  if (!workspace) {
    throw new Error(`Workspace not found: ${workspaceKey} for site: ${siteKey}`);
  }
 
  // Create and return WorkspaceService
  return container.createWorkspaceService(workspace.path, workspaceKey, siteKey);
}
 
/**
 * Get the currently mounted WorkspaceService (using app state)
 */
export function getCurrentWorkspaceService(container: AppContainer): WorkspaceService {
  const { state } = container;
  if (!state.currentSitePath || !state.currentWorkspaceKey || !state.currentSiteKey) {
    throw new Error('No workspace is currently mounted');
  }
 
  return container.createWorkspaceService(
    state.currentSitePath,
    state.currentWorkspaceKey,
    state.currentSiteKey
  );
}