All files / services heirs.ts

100% Statements 22/22
100% Branches 14/14
100% Functions 9/9
100% Lines 17/17

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      30x                   30x 56x   56x 47x   39x               30x 231x 4x           30x 80x     30x 76x     30x 30x 30x   30x    
import { DropdownButtonOptions } from '../types/form'
import { HeirsTypes, Organisation, Person } from '../types/lastWill'
 
export const heirsTypes = {
	mother: 'Mutter',
	father: 'Vater',
	child: 'Kind',
	siblings: 'Geschwisterteil',
	other: 'Andere Person',
	organisation: 'Organisation',
	partner: 'Partner',
} as const
 
export const determineHeirRelationship = (heir: Person | Organisation) => {
	const isPerson = heir.type !== 'organisation'
 
	if (isPerson && heir.type === 'siblings' && heir.gender === 'male') return 'Bruder'
	if (isPerson && heir.type === 'siblings' && heir.gender === 'female') return 'Schwester'
 
	return (
		{
			...heirsTypes,
			partner: 'Partner*in',
		} as const
	)[heir.type]
}
 
export const getPersonAddHeirsOptions = (setDropdownOption: SetDropdownOptionFunction): DropdownButtonOptions[] =>
	Object.entries(heirsTypes).map(([type, label]) => ({
		onClick: () => setDropdownOption(type as HeirsTypes),
		label: `${label} hinzufügen`,
	}))
 
export type SetDropdownOptionFunction = (type: HeirsTypes) => void
 
export const getHeirsWithoutPercentage = (heirs: (Person | Organisation)[]): (Person | Organisation)[] => {
	return heirs.filter((heir) => !heir.percentage)
}
 
export const getSumOfPercentage = (heirs: (Person | Organisation)[]): number => {
	return heirs.reduce((acc, curr) => acc + (curr.percentage || 0), 0)
}
 
export const getPercentageLeftPerHeir = (heirs: (Person | Organisation)[]): number => {
	const heirWithoutPercentage = getHeirsWithoutPercentage(heirs)
	const sumOfPercantege = getSumOfPercentage(heirs)
 
	return Math.floor((100 - sumOfPercantege) / (heirWithoutPercentage.length || 1))
}