-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from riccardofano/invert-graph
Invert graph tree
- Loading branch information
Showing
6 changed files
with
137 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) | ||
} | ||
} |