fix(crew): keep first legacy skipper when several are present
Prefer canonical skipper id and stop overwriting activeSkipperId during legacy crew migration and read-only share conversion. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { PersonData } from '../types/person.js'
|
||||
import {
|
||||
legacyCrewRecordsToLogbookSelection,
|
||||
pickActiveSkipperId
|
||||
} from './personSnapshots.js'
|
||||
|
||||
function person(overrides: Partial<PersonData> & { role: PersonData['role'] }): PersonData {
|
||||
return {
|
||||
name: overrides.name ?? 'Test',
|
||||
address: '',
|
||||
birthDate: '',
|
||||
phone: '',
|
||||
nationality: '',
|
||||
passportNumber: '',
|
||||
bloodType: '',
|
||||
allergies: '',
|
||||
diseases: '',
|
||||
role: overrides.role
|
||||
}
|
||||
}
|
||||
|
||||
describe('pickActiveSkipperId', () => {
|
||||
it('returns null for empty list', () => {
|
||||
expect(pickActiveSkipperId([])).toBeNull()
|
||||
})
|
||||
|
||||
it('prefers canonical skipper payload id', () => {
|
||||
expect(pickActiveSkipperId(['other-skipper', 'skipper', 'third'])).toBe('skipper')
|
||||
})
|
||||
|
||||
it('keeps first skipper when canonical id is absent', () => {
|
||||
expect(pickActiveSkipperId(['alpha', 'beta'])).toBe('alpha')
|
||||
})
|
||||
})
|
||||
|
||||
describe('legacyCrewRecordsToLogbookSelection', () => {
|
||||
it('does not let a later skipper overwrite the active skipper', () => {
|
||||
const selection = legacyCrewRecordsToLogbookSelection([
|
||||
{ payloadId: 'skipper', data: person({ role: 'skipper', name: 'Primary' }) },
|
||||
{ payloadId: 'co-skipper', data: person({ role: 'skipper', name: 'Secondary' }) },
|
||||
{ payloadId: 'crew-1', data: person({ role: 'crew', name: 'Crew' }) }
|
||||
])
|
||||
|
||||
expect(selection.activeSkipperId).toBe('skipper')
|
||||
expect(selection.activeCrewIds).toEqual(['crew-1'])
|
||||
expect(Object.keys(selection.snapshotsById)).toEqual(['skipper', 'co-skipper', 'crew-1'])
|
||||
})
|
||||
|
||||
it('uses first skipper when canonical id is missing', () => {
|
||||
const selection = legacyCrewRecordsToLogbookSelection([
|
||||
{ payloadId: 'first-skip', data: person({ role: 'skipper', name: 'First' }) },
|
||||
{ payloadId: 'second-skip', data: person({ role: 'skipper', name: 'Second' }) }
|
||||
])
|
||||
|
||||
expect(selection.activeSkipperId).toBe('first-skip')
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,39 @@
|
||||
import type { LogbookCrewSelectionData, PersonData, PersonSnapshot } from '../types/person.js'
|
||||
|
||||
/** Prefer canonical legacy id `skipper`, otherwise keep the first skipper encountered. */
|
||||
export function pickActiveSkipperId(skipperIds: readonly string[]): string | null {
|
||||
if (skipperIds.length === 0) return null
|
||||
return skipperIds.find((id) => id === 'skipper') ?? skipperIds[0]
|
||||
}
|
||||
|
||||
export function isSkipperRecord(payloadId: string, data: PersonData): boolean {
|
||||
return payloadId === 'skipper' || data.role === 'skipper'
|
||||
}
|
||||
|
||||
/** Build logbook crew selection from legacy per-logbook crew records (read-only share / migration). */
|
||||
export function legacyCrewRecordsToLogbookSelection(
|
||||
crews: Array<{ payloadId: string; data: PersonData }>
|
||||
): LogbookCrewSelectionData {
|
||||
const snapshotsById: Record<string, PersonSnapshot> = {}
|
||||
const skipperIds: string[] = []
|
||||
const activeCrewIds: string[] = []
|
||||
|
||||
for (const c of crews) {
|
||||
snapshotsById[c.payloadId] = personToSnapshot(c.payloadId, c.data)
|
||||
if (isSkipperRecord(c.payloadId, c.data)) {
|
||||
if (!skipperIds.includes(c.payloadId)) skipperIds.push(c.payloadId)
|
||||
} else {
|
||||
activeCrewIds.push(c.payloadId)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
activeSkipperId: pickActiveSkipperId(skipperIds),
|
||||
activeCrewIds,
|
||||
snapshotsById
|
||||
}
|
||||
}
|
||||
|
||||
export function personToSnapshot(id: string, data: PersonData): PersonSnapshot {
|
||||
return {
|
||||
id,
|
||||
|
||||
Reference in New Issue
Block a user