Skip to content

Commit

Permalink
feat: finish first endpoint to add new orgs and daughters
Browse files Browse the repository at this point in the history
  • Loading branch information
teresalves committed Jul 25, 2022
1 parent 793aed9 commit 22fd154
Show file tree
Hide file tree
Showing 8 changed files with 4,937 additions and 4,911 deletions.
18 changes: 9 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ services:
NODE_ENV: development
HTTP_PORT: 3000
DEBUG_PORT: 3001
PGUSER: teresalves
PGHOST: mydb
PGDATABASE: pipedriveDb
PGPASSWORD: example
PGPORT: 5432
restart: unless-stopped
volumes:
- .:/app/
Expand All @@ -26,15 +31,10 @@ services:
- 3300:5432
volumes:
- ~/dbdata:/var/lib/postgresql/data


adminer:
image: adminer
restart: always
ports:
- 8081:8080
depends_on:
- mydb
- ./init-database.sh:/docker-entrypoint-initdb.d/init-database.sh
networks:
- pipedrive


networks:
pipedrive:
Expand Down
19 changes: 19 additions & 0 deletions init-database.sh
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
"author": "",
"license": "ISC",
"dependencies": {
"@types/pg": "^8.6.5",
"koa": "2.13.1",
"koa-bodyparser": "^4.3.0",
"koa-router": "10.0.0"
"koa-router": "10.0.0",
"pg": "^8.7.3"
},
"devDependencies": {
"@types/koa": "2.11.6",
Expand Down
10 changes: 5 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import Koa from 'koa';
import Router from 'koa-router';
import { processOrganizations } from './endpoints/organizations';
import bodyParser from 'koa-bodyparser';

import { client, connectToPg } from './postgres-setup';

export const app = new Koa();

const router = new Router();
app.use(bodyParser());
app.use(router.routes());
connectToPg();

router.get('/', (ctx) => {
ctx.body = 'Hello World';
router.get('/', async (ctx) => {
const result = await client.query('SELECT * FROM organisations');
ctx.body = result.rows;
});

router.post('/organisations', (ctx) => {
console.log(ctx.request.body);
processOrganizations(ctx);
});

35 changes: 30 additions & 5 deletions src/endpoints/organizations.ts
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],
);
}
});
}
4 changes: 4 additions & 0 deletions src/endpoints/types/organisations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type OrganisationsBody = {
org_name: string;
daughters?: [OrganisationsBody];
};
19 changes: 19 additions & 0 deletions src/postgres-setup.ts
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;
});
}
Loading

0 comments on commit 22fd154

Please sign in to comment.