All files / lastwill/utilities lastwill-templating.util.ts

100% Statements 54/54
97.1% Branches 67/69
100% Functions 13/13
100% Lines 52/52

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 2343x                     3x         3x                               3x               3x             5x                     3x 5x         3x         4x             3x     6x                 3x 6x       6x                 3x 14x 1x   13x     3x 18x 13x     3x 13x 11x     3x       7x         7x 10x 10x           7x           7x   8x 4x       4x         7x                     3x       7x         7x 7x 12x     7x 9x 12x 12x         9x 4x                 5x                   7x             7x     3x 4x                              
import {
  FinancialAsset,
  Item,
  Organisation,
  Person,
  PersonType,
} from '../../db/entities/lastwill.entity'
import {
  LastWillParagraph,
  TestatorHeader,
} from '../dto/generated-lastwill.dto'
import {
  getPossessivePronouns,
  joinStringArrayForSentence,
} from './grammar.utils'
 
export const PLACEHOLDERS = {
  CITY: '[Stadt]',
  HOUSE_NUMBER: '[Hausnummer]',
  ZIP_CODE: '[PLZ]',
  PERSON_NAME: '[Name]',
  STREET: '[Straßenname]',
  BIRTH_DATE: '[Geburtsdatum]',
  BIRTH_PLACE: '[Geburtsort]',
  COMPANY_NAME: '[Unternehmensname]',
  PERCENTAGE: '[Prozentsatz]',
  CURRENCY: '[Währung]',
  CALCULATED_AMOUNT: '[Errechneter Wert]',
  FINANCIAL_ASSET_LOCATION: '[Bank/Ort]',
  ITEM_NAME: '[Gegenstand]',
}
 
export const PARAGRAPH_TITLES = {
  FINANCIAL: 'Erbeinsetzung',
  FINANCIAL_ADDITIONAL: 'Ersatzerbe',
  ITEM: 'Vermächtnisse',
  GERMAN_LAW: 'Rechtswahl',
  SEVERABILITY_CLAUSE: 'Salvatorische Klausel',
}
 
export function generateTestatorHeader(
  testatorName: string,
  testatorStreet: string,
  testatorhouseNumber: string,
  testatorCity: string,
  testatorZipCode: string,
): TestatorHeader {
  return {
    fullName: testatorName || PLACEHOLDERS.PERSON_NAME,
    AddressStreet: `${testatorStreet || PLACEHOLDERS.STREET} ${
      testatorhouseNumber || PLACEHOLDERS.HOUSE_NUMBER
    }`,
    AddressCity: `${testatorZipCode || PLACEHOLDERS.ZIP_CODE} ${
      testatorCity || PLACEHOLDERS.CITY
    }`,
  }
}
 
export function generateLocationHeader(city: string): string {
  return `${city || PLACEHOLDERS.CITY}, den ${new Date().toLocaleDateString(
    'de-DE',
  )}`
}
 
export function generateInitialText(
  testatorName: string,
  birthdate: string,
  birthPlace: string,
): string {
  return `Ich, ${testatorName || PLACEHOLDERS.PERSON_NAME}, geboren am ${
    getDateorPlaceholder(birthdate) || PLACEHOLDERS.BIRTH_DATE
  } in ${
    birthPlace || PLACEHOLDERS.BIRTH_PLACE
  }, widerrufe mit diesem Testament alle bisher errichteten Verfügungen von Todes wegen und bestimme hiermit Folgendes:`
}
 
export function generateInheritanceForOrganisation(
  organisation: Organisation,
): string {
  return `Das Unternehmen, ${
    organisation.name || PLACEHOLDERS.COMPANY_NAME
  }, aus ${organisation.address?.zipCode || PLACEHOLDERS.ZIP_CODE} ${
    organisation.address?.city || PLACEHOLDERS.CITY
  }, mit einem Anteil in Höhe von ${
    organisation.percentage || PLACEHOLDERS.PERCENTAGE
  } Prozent meines Vermögens`
}
 
export function generateInheritanceForPerson(person: Person) {
  const correctPossessivePronouns = getPossessivePronouns(
    person.type,
    person.gender,
  )
  return `${correctPossessivePronouns}, ${
    person.name || PLACEHOLDERS.PERSON_NAME
  } geboren am ${
    getDateorPlaceholder(person.birthDate) || PLACEHOLDERS.BIRTH_DATE
  } mit einem Anteil von ${
    person.percentage || PLACEHOLDERS.PERCENTAGE
  } Prozent meines Vermögens.`
}
 
export function getOptionalDescription(text: string): string {
  if (!text || text === '') {
    return ''
  }
  return `( ${text} )`
}
 
export function getDateorPlaceholder(dateString: string): string | undefined {
  if (!dateString) return
  return new Date(dateString).toLocaleDateString('de-de')
}
 
export function getFormattedCurrencyValues(value: number, currency: string) {
  if (!value || !currency) return ''
  return `(${value.toString()} ${currency})`
}
 
export function generateFinancialInheritancePragraphs(
  heirs: (Person | Organisation)[],
  financialAssets: FinancialAsset[],
): LastWillParagraph[] {
  const mainParagraph: LastWillParagraph = {
    title: PARAGRAPH_TITLES.FINANCIAL,
    contents: [],
  }
 
  const financialAssetList = financialAssets.map((asset) => {
    const assetLocation = asset.where || PLACEHOLDERS.FINANCIAL_ASSET_LOCATION
    return `${assetLocation} ${getFormattedCurrencyValues(
      asset.amount,
      asset.currency,
    )}`
  })
 
  mainParagraph.contents.push(
    `Als Erbe meines Vermögens, aufgeteilt auf ${joinStringArrayForSentence(
      financialAssetList,
    )}, setze ich folgende Personen ein:`,
  )
 
  for (const heir of heirs) {
    // Company
    if (heir.type === PersonType.ORGANISATION) {
      mainParagraph.contents.push(
        generateInheritanceForOrganisation(heir as Organisation),
      )
    } else {
      mainParagraph.contents.push(generateInheritanceForPerson(heir as Person))
    }
  }
 
  // The second paragraph is a standard paragraph handling death of one of the heirs
  return [
    mainParagraph,
    {
      title: PARAGRAPH_TITLES.FINANCIAL_ADDITIONAL,
      contents: [
        'Sollte einer der Erben, vor mir verstorben sein, erhalten die verbliebenen Erben diesen Erbteil entsprechend dem Verhältnis der von mir vorgegebenen Erbanteile.',
      ],
    },
  ]
}
 
export function generateItemInheritanceParagraph(
  heirs: (Person | Organisation)[],
  items: Item[],
): LastWillParagraph {
  const paragraph: LastWillParagraph = {
    title: PARAGRAPH_TITLES.ITEM,
    contents: [],
  }
  // Build hashmap for efficiency
  const itemMap: Map<string, Item> = new Map<string, Item>()
  for (const item of items) {
    itemMap.set(item.id, item)
  }
 
  for (const heir of heirs) {
    const itemNames = heir.itemIds.map((id) => {
      const itemName = itemMap.get(id)?.name || PLACEHOLDERS.ITEM_NAME
      return `${itemName} ${getOptionalDescription(
        itemMap.get(id)?.description,
      )}`
    })
 
    if (heir.type === PersonType.ORGANISATION) {
      paragraph.contents.push(
        `Ich vermache dem Unternehmen ${
          heir.name || PLACEHOLDERS.COMPANY_NAME
        } die folgenden Erbgegenstände ohne Anrechnung auf den Erbanteil: ${joinStringArrayForSentence(
          itemNames,
        )}`,
      )
    } else {
      // Add birthdate
      paragraph.contents.push(
        `Ich vermache ${heir.name || PLACEHOLDERS.PERSON_NAME}, geboren am ${
          getDateorPlaceholder(heir.birthDate) || PLACEHOLDERS.BIRTH_DATE
        } , die folgenden Erbgegenstände ohne Anrechnung auf den Erbanteil: ${joinStringArrayForSentence(
          itemNames,
        )}`,
      )
    }
  }
  // Legal gibberish
  paragraph.contents.push(
    ...[
      'Die Vermächtnisse fallen jeweils mit dem Erbfall an und sind sofort fällig.',
      'Etwaige Kosten der Vermächtniserfüllung haben die jeweiligen Vermächtnisnehmer zu tragen.',
      'Ersatzvermächtnisnehmer sind nicht bestimmt. Das jeweilige Vermächtnis entfällt ersatzlos, wenn der/die Vermächtnisnehmer/-in vor oder nach dem Erbfall, gleich aus welchem Grunde, wegfällt.',
    ],
  )
  return paragraph
}
 
export function getLegalClauses(): LastWillParagraph[] {
  return [
    {
      title: PARAGRAPH_TITLES.GERMAN_LAW,
      contents: [
        'Auf meinen gesamten Nachlass sowie für Fragen, die die Wirksamkeit dieses Testaments betreffen, soll deutsches Erbrecht anwendbar sein. Diese Rechtswahl soll auch dann weiterhin Gültigkeit haben, wenn ich meinen letzten gewöhnlichen Aufenthalt im Ausland habe.',
      ],
    },
    {
      title: PARAGRAPH_TITLES.SEVERABILITY_CLAUSE,
      contents: [
        'Sollte eine der in diesem Testament enthaltenen Anordnungen unwirksam sein, so behalten dennoch alle anderen Anordnungen ihre Wirkung.',
      ],
    },
  ]
}