|
| 1 | +import type { ColumnMapping, MappingConfig } from '../types/schemaTypes'; |
| 2 | +import type { SchemaRegistry } from '../types/interpreterTypes'; |
| 3 | +import { mapCsvToSchema } from '../schema/columnMapper'; |
| 4 | +import { getTransform } from '../transforms/registry'; |
| 5 | +import { hasAimData } from './aimUtils'; |
| 6 | +import { |
| 7 | + buildFamEntries, |
| 8 | + buildAppFinRecords, |
| 9 | + buildEmploymentStatuses, |
| 10 | + buildLLDDFields, |
| 11 | +} from './builders'; |
| 12 | +import { generateUUID } from '../utils/uuid'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Maps a single CSV row to ILR structure with support for multiple LearningDelivery aims |
| 16 | + * |
| 17 | + * Detects which aims (1-5) have data and creates separate LearningDelivery elements for each. |
| 18 | + * Auto-generates AimSeqNumber based on the aim group number. |
| 19 | + * |
| 20 | + * @param csvRow - Object with CSV column headers as keys |
| 21 | + * @param config - Mapping configuration with aim-grouped mappings |
| 22 | + * @param registry - Schema registry for path validation |
| 23 | + * @returns Nested object structure with multiple LearningDelivery elements |
| 24 | + */ |
| 25 | +export function mapCsvToSchemaWithAims( |
| 26 | + csvRow: Record<string, string>, |
| 27 | + config: MappingConfig, |
| 28 | + registry: SchemaRegistry |
| 29 | +): Record<string, unknown> { |
| 30 | + // Separate learner-level and aim-specific mappings |
| 31 | + const learnerMappings = config.mappings.filter((m) => !m.aimNumber); |
| 32 | + const aimMappings = config.mappings.filter((m) => m.aimNumber); |
| 33 | + |
| 34 | + // Group aim mappings by aim number |
| 35 | + const aimGroups = new Map<number, ColumnMapping[]>(); |
| 36 | + for (const mapping of aimMappings) { |
| 37 | + if (!mapping.aimNumber) continue; |
| 38 | + if (!aimGroups.has(mapping.aimNumber)) { |
| 39 | + aimGroups.set(mapping.aimNumber, []); |
| 40 | + } |
| 41 | + aimGroups.get(mapping.aimNumber)!.push(mapping); |
| 42 | + } |
| 43 | + |
| 44 | + // Apply learner-level mappings using generic mapper |
| 45 | + const result = mapCsvToSchema(csvRow, learnerMappings, registry); |
| 46 | + |
| 47 | + // Build LLDD fields with conditional logic |
| 48 | + const llddFields = buildLLDDFields(csvRow, 'Primary additional needs'); |
| 49 | + const messagePart = result.Message as Record<string, unknown>; |
| 50 | + if (messagePart && messagePart.Learner) { |
| 51 | + const learnerArray = messagePart.Learner as Record<string, unknown>[]; |
| 52 | + if (learnerArray.length > 0) { |
| 53 | + learnerArray[0].LLDDHealthProb = llddFields.LLDDHealthProb; |
| 54 | + if (llddFields.LLDDandHealthProblem) { |
| 55 | + learnerArray[0].LLDDandHealthProblem = llddFields.LLDDandHealthProblem; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Build employment status entries |
| 61 | + const statuses = buildEmploymentStatuses(csvRow, config.employmentStatuses); |
| 62 | + if (statuses.length > 0) { |
| 63 | + const messagePart = result.Message as Record<string, unknown>; |
| 64 | + if (messagePart && messagePart.Learner) { |
| 65 | + const learnerArray = messagePart.Learner as Record<string, unknown>[]; |
| 66 | + if (learnerArray.length > 0) learnerArray[0].LearnerEmploymentStatus = statuses; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Detect which aims have data and build LearningDelivery elements |
| 71 | + const detectionField = config.aimDetectionField || 'Programme aim {n} Learning ref'; |
| 72 | + const learningDeliveries: Record<string, unknown>[] = []; |
| 73 | + |
| 74 | + for (const [aimNumber, mappings] of aimGroups.entries()) { |
| 75 | + if (!hasAimData(csvRow, aimNumber, detectionField)) continue; |
| 76 | + |
| 77 | + const delivery: Record<string, unknown> = { AimSeqNumber: aimNumber }; |
| 78 | + |
| 79 | + for (const mapping of mappings) { |
| 80 | + const columnKey = Object.keys(csvRow).find( |
| 81 | + (key) => key.trim().toLowerCase() === mapping.csvColumn.trim().toLowerCase() |
| 82 | + ); |
| 83 | + |
| 84 | + if (!columnKey) continue; |
| 85 | + |
| 86 | + const rawValue = csvRow[columnKey]; |
| 87 | + const value = mapping.transform ? getTransform(mapping.transform)(rawValue) : rawValue; |
| 88 | + |
| 89 | + // Extract just the field name from the full path |
| 90 | + // e.g., "Message.Learner.LearningDelivery.LearnAimRef" -> "LearnAimRef" |
| 91 | + const pathParts = mapping.xsdPath.split('.'); |
| 92 | + const fieldName = pathParts[pathParts.length - 1]; |
| 93 | + |
| 94 | + delivery[fieldName] = value; |
| 95 | + } |
| 96 | + |
| 97 | + // Generate SWSupAimId (software-generated UUID for tracking) |
| 98 | + delivery.SWSupAimId = generateUUID(); |
| 99 | + |
| 100 | + // Build FAM entries for this aim |
| 101 | + const fams = buildFamEntries(csvRow, config.famTemplates, aimNumber); |
| 102 | + if (fams.length > 0) delivery.LearningDeliveryFAM = fams; |
| 103 | + |
| 104 | + // Build AppFinRecord entries for this aim |
| 105 | + const fins = buildAppFinRecords(csvRow, config.appFinTemplates, aimNumber); |
| 106 | + if (fins.length > 0) delivery.AppFinRecord = fins; |
| 107 | + |
| 108 | + learningDeliveries.push(delivery); |
| 109 | + } |
| 110 | + |
| 111 | + // Inject LearningDelivery array into result structure |
| 112 | + if (learningDeliveries.length > 0) { |
| 113 | + const messagePart = result.Message as Record<string, unknown>; |
| 114 | + if (messagePart && messagePart.Learner) { |
| 115 | + const learnerArray = messagePart.Learner as Record<string, unknown>[]; |
| 116 | + if (learnerArray.length > 0) { |
| 117 | + learnerArray[0].LearningDelivery = learningDeliveries; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return result; |
| 123 | +} |
0 commit comments