Skip to content

Commit 68a1a0b

Browse files
committed
tables
1 parent 290a927 commit 68a1a0b

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

src/components/companies.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import _ from 'lodash';
2+
import { openDB } from './db';
3+
4+
interface SQLiteCompanyInfoResponse {
5+
company_id: string;
6+
description: string;
7+
categories: string;
8+
}
9+
10+
interface CompanyInfo {
11+
company_id: string;
12+
description: string;
13+
categories: string[];
14+
}
15+
16+
export const getCompanyInfo = async (companyId: string): Promise<CompanyInfo[]> => {
17+
const db = await openDB();
18+
const res: SQLiteCompanyInfoResponse[] = await db.all(
19+
'SELECT * FROM companies WHERE id = ?',
20+
companyId,
21+
);
22+
const cleaned = res.map((row) => {
23+
return {
24+
...row,
25+
categories: row.categories.split(','),
26+
};
27+
});
28+
return cleaned;
29+
};
30+
31+
export const getCompaniesByUserId = async (userId: string): Promise<string[]> => {
32+
const db = await openDB();
33+
34+
const res = await db.all('SELECT company_id FROM people_companies WHERE user_id = ?', userId);
35+
return res.map((row) => row.company_id);
36+
};
37+
38+
export const getUsersByCompanyId = async (companyId: string): Promise<string[]> => {
39+
const db = await openDB();
40+
41+
const res = await db.all('SELECT user_id FROM people_companies WHERE company_id = ?', companyId);
42+
return res.map((row) => row.user_id);
43+
};

src/components/db.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,27 @@ const addSQLColumnIfNotExists = async (
165165
if (columns.length == 0) {
166166
await db.run(`ALTER TABLE ${tableName} ADD COLUMN ${columnName} ${columnDataType}`);
167167
}
168+
const initCompaniesTable = async (db: Database): Promise<void> => {
169+
// the company_id will match the crunchbase entity_id, which is a unique identifier for each company
170+
// see https://app.swaggerhub.com/apis-docs/Crunchbase/crunchbase-enterprise_api/1.0.3#/Entity/get_entities_organizations__entity_id_
171+
await db.run(
172+
`
173+
CREATE TABLE IF NOT EXISTS companies (
174+
company_id VARCHAR(30) PRIMARY KEY NOT NULL,
175+
description TEXT
176+
categories TEXT
177+
`,
178+
);
179+
};
180+
181+
const initPeopleCompaniesTable = async (db: Database): Promise<void> => {
182+
await db.run(
183+
`
184+
CREATE TABLE IF NOT EXISTS people_companies (
185+
user_id VARCHAR(255) TEXT NOT NULL,
186+
FOREIGNKEY(company_id) REFERENCES companies(company_id) NOT NULL
187+
)`,
188+
);
168189
};
169190

170191
const initTables = async (db: Database): Promise<void> => {
@@ -177,6 +198,8 @@ const initTables = async (db: Database): Promise<void> => {
177198
await initUserCoinTable(db);
178199
await initUserProfileTable(db);
179200
await initRpsGameInfo(db);
201+
await initCompaniesTable(db);
202+
await initPeopleCompaniesTable(db);
180203
};
181204

182205
export const openDB = async (): Promise<Database> => {
@@ -189,4 +212,4 @@ export const openDB = async (): Promise<Database> => {
189212
logger.info({ message: 'Initialized database and tables.', where: 'openDB' });
190213
}
191214
return db;
192-
};
215+
};

src/utils/companyInfo.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fetch from 'node-fetch';
44
const CRUNCHBASE_ORGANIZATION_URL = 'crunchbase.com/organization/';
55
const CRUNCHBASE_ORGANIZATION_API_URL = 'https://api.crunchbase.com/api/v4/entities/organizations/';
66

7+
const fields = ['categories', 'short_description', 'website'];
78
// eslint-disable-next-line @typescript-eslint/no-unused-vars
89
const getCompanyInfo = async (companyCrunchbaseLink: string) => {
910
// check if url contains crunchbase.com/organization
@@ -25,7 +26,9 @@ const getCompanyInfo = async (companyCrunchbaseLink: string) => {
2526
}
2627

2728
const response = await fetch(
28-
`${CRUNCHBASE_ORGANIZATION_API_URL}${companyName}?user_key=${process.env.CRUNCHBASE_API_KEY}&field_ids=categories,short_description`,
29+
`${CRUNCHBASE_ORGANIZATION_API_URL}${companyName}?user_key=${
30+
vars.CRUNCHBASE_API_KEY
31+
}&field_ids=${fields.join(',')}`,
2932
);
3033
const data = await response.json();
3134
if (data.error) {

0 commit comments

Comments
 (0)