-
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.
feat: finish first endpoint to add new orgs and daughters
- Loading branch information
1 parent
793aed9
commit 22fd154
Showing
8 changed files
with
4,937 additions
and
4,911 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
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,19 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL | ||
CREATE TABLE organisations ( | ||
org_name VARCHAR ( 50 ) PRIMARY KEY | ||
); | ||
CREATE TABLE organisations_relations ( | ||
parent VARCHAR ( 50 ), | ||
daughter VARCHAR ( 50 ), | ||
PRIMARY KEY (parent, daughter), | ||
CONSTRAINT FK_parent FOREIGN KEY(parent) | ||
REFERENCES organisations(org_name), | ||
CONSTRAINT FK_daughter FOREIGN KEY(daughter) | ||
REFERENCES organisations(org_name) | ||
); | ||
EOSQL |
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
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 |
---|---|---|
@@ -1,7 +1,32 @@ | ||
import { ParameterizedContext } from "koa"; | ||
import { IRouterParamContext } from "koa-router"; | ||
import { ParameterizedContext } from 'koa'; | ||
import { client } from '../postgres-setup'; | ||
import { OrganisationsBody } from './types/organisations'; | ||
|
||
export async function processOrganizations( | ||
ctx: ParameterizedContext, | ||
): Promise<void> { | ||
const org: OrganisationsBody = ctx.request.body; | ||
await recursiveOrganisationProcess(org, []); | ||
ctx.body = 'OK'; | ||
} | ||
|
||
export function processOrganizations(ctx: ParameterizedContext<any, IRouterParamContext<any, {}>>) { | ||
console.log(ctx.request.body); | ||
} | ||
async function recursiveOrganisationProcess( | ||
baseOrg: OrganisationsBody, | ||
parents: Array<string>, | ||
): Promise<void> { | ||
parents.push(baseOrg.org_name); | ||
await client.query( | ||
'INSERT INTO organisations(org_name) VALUES ($1) ON CONFLICT DO NOTHING;', | ||
[baseOrg.org_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], | ||
); | ||
} | ||
}); | ||
} |
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,4 @@ | ||
export type OrganisationsBody = { | ||
org_name: string; | ||
daughters?: [OrganisationsBody]; | ||
}; |
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,19 @@ | ||
import { Client } from 'pg'; | ||
|
||
const clientConfig = { | ||
host: process.env.PGHOST || 'localhost', | ||
user: process.env.PGUSER || 'teresalves', | ||
password: process.env.PGPASSWORD || 'example', | ||
port: Number(process.env.PGPORT) || 3300, | ||
database: process.env.PGDATABASE || 'pipedriveDb', | ||
}; | ||
|
||
export const client = new Client(clientConfig); | ||
|
||
export function connectToPg() { | ||
client.connect().catch((error: any) => { | ||
// console.log('error connecting') | ||
// console.log(error); | ||
throw error; | ||
}); | ||
} |
Oops, something went wrong.