Skip to content

Commit

Permalink
Merge pull request #95 from riccardofano/invert-graph
Browse files Browse the repository at this point in the history
Invert graph tree
  • Loading branch information
riccardofano authored Feb 6, 2024
2 parents 0f33bb0 + 61f7931 commit 90a2b05
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 8 deletions.
72 changes: 72 additions & 0 deletions core/invertGraph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { CategoryName } from './types/Category'
import { Person, PersonList } from './types/Person'
import mappings from './invertedMappings.json'

type InvertedPerson = {
id: string
name: string
available: boolean
relation: string
relatedTo: string
}

type InvertedPersonList = Record<string, InvertedPerson>

export function defaultRoot(): Person {
return {
id: '0',
name: 'Defunto',
available: false,
degree: 0,
root: null,
category: 'children',
relatives: [],
}
}

export function invertGraph(root: Person, list: InvertedPersonList): PersonList {
const personList: PersonList = { '0': root }
for (const person of Object.values(list)) {
const degree = relationIdToDegree(person.relation)
const category = relationIdToCategory(person.relation)
const root = person.relatedTo === '' ? '0' : person.relatedTo

personList[person.id] = {
id: person.id,
name: person.name,
available: person.available,
degree,
root,
category,
relatives: [],
}
}

for (const person of Object.values(personList)) {
if (!person.root) {
continue
}

personList[person.root].relatives.push(person.id)
}

return personList
}

function relationIdToDegree(relation: string): number {
const mapping = mappings[relation as keyof typeof mappings]
if (!mapping) {
throw new Error('Unknown relation')
}

return mapping.degree
}

function relationIdToCategory(relation: string): CategoryName {
const mapping = mappings[relation as keyof typeof mappings]
if (!mapping) {
throw new Error('Unknown relation')
}

return mapping.category as CategoryName
}
22 changes: 22 additions & 0 deletions core/invertedMappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"figlio": { "category": "children", "degree": 1 },
"nipote in linea retta": { "category": "children", "degree": 2 },
"pronipote in linea retta": { "category": "children", "degree": 3 },
"abnipote in linea retta": { "category": "children", "degree": 4 },
"genitore": { "category": "ascendants", "degree": 1 },
"fratello": { "category": "bilinear", "degree": 2 },
"nipote in linea collaterale": { "category": "children", "degree": 3 },
"pronipote in linea collaterale": { "category": "children", "degree": 4 },
"abnipote in linea collaterale": { "category": "children", "degree": 5 },
"zio": { "category": "others", "degree": 3 },
"cugino": { "category": "children", "degree": 4 },
"figlio di cugino": { "category": "children", "degree": 5 },
"nipote di cugino": { "category": "children", "degree": 6 },
"nonno": { "category": "ascendants", "degree": 2 },
"prozio": { "category": "others", "degree": 4 },
"secondo cugino": { "category": "children", "degree": 5 },
"figlio di secondo cugino": { "category": "children", "degree": 6 },
"bisavo": { "category": "ascendants", "degree": 3 },
"trisavo": { "category": "children", "degree": 4 },
"coniuge": { "category": "spouse", "degree": 1 }
}
16 changes: 10 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pages/api/health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NextApiRequest, NextApiResponse } from 'next'

export default function handler(req: NextApiRequest, res: NextApiResponse) {
return res.status(200).send('Hello')
}
9 changes: 7 additions & 2 deletions pages/api/inheritance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { calculateInheritance } from '../../core/inheritance'

export default function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'POST') return res.status(405).send({ error: 'Method not allowed' })
if (req.headers['content-type'] !== 'application/json')
if (req.method !== 'POST') {
return res.status(405).send({ error: 'Method not allowed' })
}
if (req.headers['content-type'] !== 'application/json') {
return res.status(400).send({ error: 'Content type not allowed' })
}

// TODO: Don't require sender to set back pointers for people, only the relatives array

try {
const result = calculateInheritance(req.body)
Expand Down
21 changes: 21 additions & 0 deletions pages/api/inverted/inheritance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { calculateInheritance } from '../../../core/inheritance'
import { defaultRoot, invertGraph } from '../../../core/invertGraph'

export default function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'POST') {
return res.status(405).send({ error: 'Method not allowed' })
}

if (req.headers['content-type'] !== 'application/json') {
return res.status(400).send({ error: 'Content type not allowed' })
}

try {
const graph = invertGraph(defaultRoot(), req.body)
const result = calculateInheritance(graph)
return res.json(result)
} catch (error) {
return res.status(500).send({ error: 'Failed to parse body' })
}
}

0 comments on commit 90a2b05

Please sign in to comment.