Source: tree/treeQueue/treeMigrate.js

// @ts-check
/**
 * @import {treeAction} from './../../types.js'
 */

import { addUpdateList } from '../../server.ws.js'
import { errorLoggerUpdate } from '../../utils/requestLogger.js'
import { migrateFamilyAction } from './treeMigrate.familyAction.js'
import { migratePersonAction } from './treeMigrate.personAction.js'

/**
 * Handles migration actions for moving objects between different groups or targets.
 * Delegates to type-specific submodules.
 *
 * @async
 * @function migrateAction
 * @param {treeAction} action - The migration action object containing source and target information
 * @returns {Promise<void>}
 * @throws {Error} Throws an error if migration is attempted for unsupported object types
 */
export const migrateAction = async (action) => {
    try {
        if (!['group', 'person', 'extern', 'family', 'familyB'].includes(action.Type)) {
            throw new Error('migrate action only supported for types person, extern, family, familyB and group')
        }

        if (action.Type === 'family' || action.Type === 'familyB') {
            await migrateFamilyAction(action)
        } else if (action.Type === 'person' || action.Type === 'extern') {
            await migratePersonAction(action)
        } else if (action.Type === 'group') {
            // group migration not yet tested / implemented – no-op for now
        }

        // WebSocket update for non-person/extern types (currently only group, which is a stub)
        if (action.Type !== 'person' && action.Type !== 'extern') {
            addUpdateList(action.UIDnewTarget)
        }
    }
    catch (e) {
        errorLoggerUpdate(e || new Error('treeMigrate: Unknown error occurred'))
    }
}