Skip to content

Commit

Permalink
feat: finish second endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
teresalves committed Jul 26, 2022
1 parent 22fd154 commit 0b6dc03
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 245 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
"prettier": "2.2.1",
"supertest": "6.0.1",
"ts-jest": "26.4.4",
"ts-node": "9.1.1",
"ts-node-dev": "1.1.1",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"tslint": "6.1.3",
"typescript": "4.1.3"
"typescript": "^4.7.4"
},
"engines": {
"node": ">=14"
Expand Down
7 changes: 7 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Router from 'koa-router';
import { processOrganizations } from './endpoints/organizations';
import bodyParser from 'koa-bodyparser';
import { client, connectToPg } from './postgres-setup';
import { getOrganisation } from './endpoints/get-organisation';

export const app = new Koa();

Expand All @@ -19,3 +20,9 @@ router.get('/', async (ctx) => {
router.post('/organisations', (ctx) => {
processOrganizations(ctx);
});

router.get('/organisations/:orgName', async (ctx) => {
const result = await getOrganisation(ctx);
ctx.body = result.body;
ctx.status = result.status;
});
59 changes: 59 additions & 0 deletions src/endpoints/get-organisation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ParameterizedContext } from 'koa';
import { client } from '../postgres-setup';

export async function getOrganisation(ctx: ParameterizedContext) {
console.log(ctx.params.orgName);
const name = ctx.params.orgName;
const parents = await client.query(
'SELECT parent FROM organisations_relations WHERE daughter=$1;',
[name],
);
console.log(parents.rows);
let parentQuery = "";
parents.rows.forEach(async (obj) => {
parentQuery = parentQuery + `parent='${obj.parent}' OR `;
})
if (parentQuery !== "") {
parentQuery = parentQuery.substring(0, parentQuery.length - 4)
}
const sisters = await client.query(`SELECT distinct daughter as sister FROM organisations_relations WHERE ${parentQuery};`);
const daughters = await client.query("SELECT daughter FROM organisations_relations WHERE parent=$1;", [name],);
const result = fabricateResponse(parents.rows as [Parent], sisters.rows as [Sister], daughters.rows as [Daughter]);
return {body: result, status: 200};
}

function fabricateResponse(parents: [Parent], sisters: [Sister], daughters: [Daughter]) {
let finalResult: [Relationship?] = [];
console.log("PARENT");
addToFinalResult(parents, "parent", finalResult);
console.log("SISTER");
addToFinalResult(sisters, "sister", finalResult);
console.log("DAUGHTER");
addToFinalResult(daughters, "daughter", finalResult);
console.log(finalResult);
return finalResult;
}

function addToFinalResult<T>(relatives: [T], relationship: string, finalResult: [Relationship?]) {
for(const relative of relatives) {
console.log("Relative", relative);
finalResult.push({org_name: relative[`${relationship}`], relationship_type: relationship})
}
}

type Relationship = {
org_name: string,
relationship_type: string
}

type Parent = {
parent: string
}

type Daughter = {
daughter: string
}

type Sister = {
sister: string
}
8 changes: 4 additions & 4 deletions src/endpoints/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ async function recursiveOrganisationProcess(
baseOrg: OrganisationsBody,
parents: Array<string>,
): Promise<void> {
parents.push(baseOrg.org_name);
const name = (baseOrg.org_name);
parents.push(name);
await client.query(
'INSERT INTO organisations(org_name) VALUES ($1) ON CONFLICT DO NOTHING;',
[baseOrg.org_name],
[name],
);
baseOrg.daughters?.forEach(async (daughter) => {
if (!(daughter.org_name in parents)) {
// Avoid infinite loops
await recursiveOrganisationProcess(daughter, parents); // had to wait because we cant add the relationship before the actual daughter
await client.query(
'INSERT INTO organisations_relations(parent, daughter) VALUES ($1,$2) ON CONFLICT DO NOTHING;',
[baseOrg.org_name, daughter.org_name],
[name, (daughter.org_name)],
);
}
});
Expand Down
Loading

0 comments on commit 0b6dc03

Please sign in to comment.