From b1f602cee25c419487d02a054fb6ed5d49fccdc5 Mon Sep 17 00:00:00 2001 From: Victor Zheng Date: Mon, 21 Nov 2022 21:36:44 -0500 Subject: [PATCH 1/6] done --- config/vars.template.json | 3 ++- src/utils/companyinfo.ts | 35 +++++++++++++++++++++++++++++++++++ src/utils/validateUrl.ts | 12 ++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/utils/companyinfo.ts create mode 100644 src/utils/validateUrl.ts diff --git a/config/vars.template.json b/config/vars.template.json index 976a261c..b4b1046b 100644 --- a/config/vars.template.json +++ b/config/vars.template.json @@ -6,5 +6,6 @@ "OFFICE_STATUS_CHANNEL_ID": "CHANNEL_ID", "RESUME_CHANNEL_ID": "CHANNEL_ID", "IRC_USER_ID": "USER_ID", - "MOD_USER_ID_FOR_BAN_APPEAL": "USER_ID" + "MOD_USER_ID_FOR_BAN_APPEAL": "USER_ID", + "CRUNCHBASE_API_KEY": "CRUNCHBASE_KEY" } diff --git a/src/utils/companyinfo.ts b/src/utils/companyinfo.ts new file mode 100644 index 00000000..ae9a8038 --- /dev/null +++ b/src/utils/companyinfo.ts @@ -0,0 +1,35 @@ +import { vars } from '../config'; +import { isValidUrl } from './validateUrl'; +import fetch from 'node-fetch'; + +const getCompanyInfo = async (companyCrunchbaseLink: string) => { + // check if url contains crunchbase.com/organization + // if not, return null + if ( + isValidUrl(companyCrunchbaseLink) && + !companyCrunchbaseLink.includes('crunchbase.com/organization/') + ) { + throw new Error('Invalid URL'); + } + + let companyName; + // check if urlPattern matches company_crunchbase_link + if (companyCrunchbaseLink.includes('crunchbase.com/organization/')) { + // grab everything after the last / character + companyName = companyCrunchbaseLink.split('/').pop(); + } else { + // assume they just supplied the id + companyName = companyCrunchbaseLink; + } + + const response = await fetch( + `https://api.crunchbase.com/api/v4/entities/organizations/${companyName}?user_key=${vars.CRUNCHBASE_API_KEY}&field_ids=categories,short_description`, + ); + const data = await response.json(); + if (data.error) { + throw new Error(data.error); + } + console.log(data); + + return data; +}; diff --git a/src/utils/validateUrl.ts b/src/utils/validateUrl.ts new file mode 100644 index 00000000..4e85112c --- /dev/null +++ b/src/utils/validateUrl.ts @@ -0,0 +1,12 @@ +export const isValidUrl = (url: string): boolean => { + const urlPattern = new RegExp( + '^(https?:\\/\\/)?' + // protocol + '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name + '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address + '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path + '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string + '(\\#[-a-z\\d_]*)?$', + 'i', + ); // fragment locator + return urlPattern.test(url); +}; From 47a0c5483a46d5c6b836d38c3d16ae55454e5d40 Mon Sep 17 00:00:00 2001 From: Victor Zheng Date: Mon, 21 Nov 2022 21:43:13 -0500 Subject: [PATCH 2/6] fixed readme --- docs/SETUP.md | 4 ++-- src/utils/companyinfo.ts | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/SETUP.md b/docs/SETUP.md index 0903046e..4369f709 100644 --- a/docs/SETUP.md +++ b/docs/SETUP.md @@ -33,8 +33,8 @@ Then, add your bot to the Discord testing server. - `OFFICE_STATUS_CHANNEL_ID`: the ID of the office hours channel. - `IRC_USER_ID`: the user ID of the irc-bridge bot. - `MOD_USER_ID_FOR_BAN_APPEAL`: the user ID of the mod tagged in the appeal messages for bans. - -Note that this file will not be pushed to the remote. +- `CRUNCHBASE_API_KEY`: api key if you wish to use the upcoming enroll company feature. (Create an account here)[https://www.crunchbase.com/home]. NOTE: feature is still under construction. + Note that this file will not be pushed to the remote. 6. Make an `.env` file in the root folder of the project, and put your Discord bot's token, which can be found in the Discord Developer Portal. The format of the `.env` file should be as follows. diff --git a/src/utils/companyinfo.ts b/src/utils/companyinfo.ts index ae9a8038..95a1ae1a 100644 --- a/src/utils/companyinfo.ts +++ b/src/utils/companyinfo.ts @@ -2,6 +2,7 @@ import { vars } from '../config'; import { isValidUrl } from './validateUrl'; import fetch from 'node-fetch'; +// eslint-disable-next-line @typescript-eslint/no-unused-vars const getCompanyInfo = async (companyCrunchbaseLink: string) => { // check if url contains crunchbase.com/organization // if not, return null From a9fd5b072d1d22ec6af0df093ac6c1e02f9e3afe Mon Sep 17 00:00:00 2001 From: Victor Zheng Date: Thu, 24 Nov 2022 16:40:56 -0500 Subject: [PATCH 3/6] comments --- docs/SETUP.md | 2 +- src/utils/companyinfo.ts | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/docs/SETUP.md b/docs/SETUP.md index 4369f709..69f648c1 100644 --- a/docs/SETUP.md +++ b/docs/SETUP.md @@ -33,7 +33,7 @@ Then, add your bot to the Discord testing server. - `OFFICE_STATUS_CHANNEL_ID`: the ID of the office hours channel. - `IRC_USER_ID`: the user ID of the irc-bridge bot. - `MOD_USER_ID_FOR_BAN_APPEAL`: the user ID of the mod tagged in the appeal messages for bans. -- `CRUNCHBASE_API_KEY`: api key if you wish to use the upcoming enroll company feature. (Create an account here)[https://www.crunchbase.com/home]. NOTE: feature is still under construction. +- `CRUNCHBASE_API_KEY`: API key if you wish to use the upcoming enroll company feature. (Create an account here)[https://www.crunchbase.com/home]. NOTE: feature is still under construction. Note that this file will not be pushed to the remote. 6. Make an `.env` file in the root folder of the project, and put your Discord bot's token, which can be found in the Discord Developer Portal. The format of the `.env` file should be as follows. diff --git a/src/utils/companyinfo.ts b/src/utils/companyinfo.ts index 95a1ae1a..a17a020f 100644 --- a/src/utils/companyinfo.ts +++ b/src/utils/companyinfo.ts @@ -2,20 +2,23 @@ import { vars } from '../config'; import { isValidUrl } from './validateUrl'; import fetch from 'node-fetch'; +const CRUNCHBASE_ORGANIZATION_URL = 'crunchbase.com/organization/'; +const CRUNCHBASE_ORGANIZATION_API_URL = 'https://api.crunchbase.com/api/v4/entities/organizations/'; + // eslint-disable-next-line @typescript-eslint/no-unused-vars const getCompanyInfo = async (companyCrunchbaseLink: string) => { // check if url contains crunchbase.com/organization - // if not, return null + // if not, throw error if ( isValidUrl(companyCrunchbaseLink) && - !companyCrunchbaseLink.includes('crunchbase.com/organization/') + !companyCrunchbaseLink.includes(CRUNCHBASE_ORGANIZATION_URL) ) { throw new Error('Invalid URL'); } let companyName; - // check if urlPattern matches company_crunchbase_link - if (companyCrunchbaseLink.includes('crunchbase.com/organization/')) { + // check if urlPattern matches companyCrunchbaseLink + if (companyCrunchbaseLink.includes(CRUNCHBASE_ORGANIZATION_URL)) { // grab everything after the last / character companyName = companyCrunchbaseLink.split('/').pop(); } else { @@ -24,13 +27,12 @@ const getCompanyInfo = async (companyCrunchbaseLink: string) => { } const response = await fetch( - `https://api.crunchbase.com/api/v4/entities/organizations/${companyName}?user_key=${vars.CRUNCHBASE_API_KEY}&field_ids=categories,short_description`, + `${CRUNCHBASE_ORGANIZATION_API_URL}${companyName}?user_key=${vars.CRUNCHBASE_API_KEY}&field_ids=categories,short_description`, ); const data = await response.json(); if (data.error) { throw new Error(data.error); } - console.log(data); return data; }; From 4fbbb1b154ff80668f54a4d7c264c565feb3ad16 Mon Sep 17 00:00:00 2001 From: Victor Zheng Date: Thu, 24 Nov 2022 19:32:30 -0500 Subject: [PATCH 4/6] temp commit for renaming case --- src/utils/{companyinfo.ts => temp.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/utils/{companyinfo.ts => temp.ts} (100%) diff --git a/src/utils/companyinfo.ts b/src/utils/temp.ts similarity index 100% rename from src/utils/companyinfo.ts rename to src/utils/temp.ts From 0c072e1487daeb55fa19aa4eb1fe0ea456711405 Mon Sep 17 00:00:00 2001 From: Victor Zheng Date: Thu, 24 Nov 2022 19:32:43 -0500 Subject: [PATCH 5/6] rename properly --- src/utils/{temp.ts => companyInfo.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/utils/{temp.ts => companyInfo.ts} (100%) diff --git a/src/utils/temp.ts b/src/utils/companyInfo.ts similarity index 100% rename from src/utils/temp.ts rename to src/utils/companyInfo.ts From 8e90f275a0be9321268b831d6810c3fd78e3f14c Mon Sep 17 00:00:00 2001 From: Victor Zheng Date: Thu, 24 Nov 2022 19:34:07 -0500 Subject: [PATCH 6/6] last comment --- src/utils/companyInfo.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils/companyInfo.ts b/src/utils/companyInfo.ts index a17a020f..426a1907 100644 --- a/src/utils/companyInfo.ts +++ b/src/utils/companyInfo.ts @@ -17,12 +17,11 @@ const getCompanyInfo = async (companyCrunchbaseLink: string) => { } let companyName; - // check if urlPattern matches companyCrunchbaseLink if (companyCrunchbaseLink.includes(CRUNCHBASE_ORGANIZATION_URL)) { // grab everything after the last / character companyName = companyCrunchbaseLink.split('/').pop(); } else { - // assume they just supplied the id + // assume they just supplied the id if we can't find the CRUNCHBASE_ORGANIZATION_URL companyName = companyCrunchbaseLink; }