From 4ac14dca771d6db752d4b811ccf5636da75f8ee9 Mon Sep 17 00:00:00 2001 From: Kevin Koech Date: Tue, 12 Sep 2023 11:34:14 +0300 Subject: [PATCH] Remove unused files --- .../src/lib/ecosystem/airtable.old.js | 347 ------------------ .../src/lib/ecosystem/ecosystem.old.js | 118 ------ 2 files changed, 465 deletions(-) delete mode 100644 apps/charterafrica/src/lib/ecosystem/airtable.old.js delete mode 100644 apps/charterafrica/src/lib/ecosystem/ecosystem.old.js diff --git a/apps/charterafrica/src/lib/ecosystem/airtable.old.js b/apps/charterafrica/src/lib/ecosystem/airtable.old.js deleted file mode 100644 index c0227407f..000000000 --- a/apps/charterafrica/src/lib/ecosystem/airtable.old.js +++ /dev/null @@ -1,347 +0,0 @@ -import * as Sentry from "@sentry/nextjs"; -import Airtable from "airtable"; - -import api from "@/charterafrica/lib/payload"; -import { - CONTRIBUTORS_COLLECTION, - TOOL_COLLECTION, -} from "@/charterafrica/payload/utils/collections"; -import fetchJson, { FetchError } from "@/charterafrica/utils/fetchJson"; - -const airtable = new Airtable({ - apiKey: process.env.AIRTABLE_API_TOKEN, -}); - -function getter(data, key) { - return key ? data?.[key] : null; -} - -async function mapSupportersToFields(supporters, config, tableData) { - const { - schema: { partnerTableColumns }, - } = config; - const { name, url, logo } = partnerTableColumns; - const { partners } = tableData; - const mapped = supporters.map((id) => { - try { - const { fields } = partners.find((item) => id === item.id); - return { - name: getter(fields, name), - website: getter(fields, url), - logo: getter(fields, logo), - }; - } catch (error) { - Sentry.captureMessage(error.message); - return null; - } - }); - return mapped.filter(Boolean); -} - -async function getToolsPerAirtableId(ids) { - if (!ids || !ids?.length) { - return []; - } - const { docs } = await api.getCollection(TOOL_COLLECTION, { - where: { - airtableId: { - in: ids?.join(","), - }, - }, - }); - return docs.map(({ id }) => id); -} - -async function mapSocialMediaToFields(socialMedia, config, tableData) { - const { - schema: { socialMediaTableColumns }, - } = config; - const { name, url } = socialMediaTableColumns; - const { socialMedia: socialMediaData } = tableData; - const mapped = socialMedia.map((id) => { - try { - const { fields } = socialMediaData.find((item) => item.id === id); - return { - name: getter(fields, name), - website: getter(fields, url), - }; - } catch (error) { - Sentry.captureMessage(error.message); - return null; - } - }); - return mapped.filter(Boolean); -} - -export const getListFromAirtable = async ({ baseId, tableIdOrName }) => { - const base = airtable.base(baseId); - return base(tableIdOrName).select().all(); -}; - -export const processOrganisationFromAirTable = async ( - data, - config, - tableData, -) => { - try { - const { - schema: { organisationTableColumns }, - } = config; - const description = { - en: getter(data, organisationTableColumns.description.en), - pt: getter(data, organisationTableColumns.description.pt), - fr: getter(data, organisationTableColumns.description.fr), - }; - const partners = await mapSupportersToFields( - getter(data, organisationTableColumns.partners) || [], - config, - tableData, - ); - const supporters = await mapSupportersToFields( - getter(data, organisationTableColumns.supporters) || [], - config, - tableData, - ); - const socialMedia = await mapSocialMediaToFields( - getter(data, organisationTableColumns.socialMedia) || [], - config, - tableData, - ); - const tools = await getToolsPerAirtableId( - getter(data, organisationTableColumns.tools), - ); - const commonData = { - airtableId: data.id, - avatarUrl: getter(data, organisationTableColumns.avatarUrl)?.[0]?.url, - externalId: getter(data, organisationTableColumns.slug), - type: getter(data, organisationTableColumns.type), - repoLink: getter(data, organisationTableColumns.source.url), - supporters, - partners, - socialMedia, - tools, - }; - if (!commonData.externalId) { - const message = `Missing external ID for ${data.id}`; - Sentry.captureMessage(message); - throw new FetchError(message, data, 500); - } - if (!tools.length) { - const message = `Organisation ${data.id} is not assigned to any tool and has been skipped`; - Sentry.captureMessage(message); - throw new FetchError(message, data, 500); - } - return { - en: { - ...commonData, - description: description.en, - }, - fr: { - ...commonData, - description: description.fr, - }, - pt: { - ...commonData, - description: description.pt, - }, - }; - } catch (error) { - Sentry.captureMessage(error.message); - return null; - } -}; - -export const processContributorFromAirtable = async ( - data, - config, - tableData, -) => { - try { - const { - schema: { contributorTableColumns }, - } = config; - const socialMedia = await mapSocialMediaToFields( - getter(data, contributorTableColumns.socialMedia) || [], - config, - tableData, - ); - const description = { - en: getter(data, contributorTableColumns.description.en), - pt: getter(data, contributorTableColumns.description.pt), - fr: getter(data, contributorTableColumns.description.fr), - }; - const defaultData = { - airtableId: data.id, - avatarUrl: getter(data, contributorTableColumns.avatarUrl)?.[0]?.url, - externalId: getter(data, contributorTableColumns.slug), - repoLink: `https://github.com/${getter( - data, - contributorTableColumns.slug, - )}`, - socialMedia, - }; - return { - en: { - ...defaultData, - description: description.en, - }, - fr: { - ...defaultData, - description: description.fr, - }, - pt: { - ...defaultData, - description: description.pt, - }, - }; - } catch (error) { - Sentry.captureMessage(error.message); - return null; - } -}; - -export const processToolFromAirtable = async (data, config, tableData) => { - try { - const { - schema: { toolTableColumns }, - } = config; - const theme = { - en: getter(data, toolTableColumns.theme.en)?.[0], - pt: getter(data, toolTableColumns.theme.pt)?.[0], - fr: getter(data, toolTableColumns.theme.fr)?.[0], - }; - const description = { - en: getter(data, toolTableColumns.description.en), - pt: getter(data, toolTableColumns.description.pt), - fr: getter(data, toolTableColumns.description.fr), - }; - const { docs: contrib } = await api.getCollection(CONTRIBUTORS_COLLECTION, { - where: { - airtableId: { - in: getter(data, toolTableColumns.contributors)?.join(","), - }, - }, - }); - const operatingCountries = getter( - data, - toolTableColumns.operatingCountries, - ); - const homeCountry = getter(data, toolTableColumns.homeCountry); - const partners = await mapSupportersToFields( - getter(data, toolTableColumns.partners) || [], - config, - tableData, - ); - const supporters = await mapSupportersToFields( - getter(data, toolTableColumns.supporters) || [], - config, - tableData, - ); - const socialMedia = await mapSocialMediaToFields( - getter(data, toolTableColumns.socialMedia) || [], - config, - tableData, - ); - const defaultData = { - airtableId: data.id, - avatarUrl: getter(data, toolTableColumns.avatarUrl)?.[0]?.url, - externalId: getter(data, toolTableColumns.slug) || " ", - repoLink: getter(data, toolTableColumns.source.url), - name: getter(data, toolTableColumns.name), - link: getter(data, toolTableColumns.url), - operatingCountries, - contributors: contrib.map(({ id }) => id), - supporters, - partners, - homeCountry, - socialMedia, - }; - - return { - en: { - ...defaultData, - description: description.en, - theme: theme.en, - }, - fr: { - ...defaultData, - description: description.fr, - theme: theme.fr, - }, - pt: { - ...defaultData, - description: description.pt, - theme: theme.pt, - }, - }; - } catch (error) { - Sentry.captureMessage(error.message); - return null; - } -}; - -const cache = {}; - -function getFromCache(key) { - const rawData = cache[key]; - if (rawData) { - const { lastUpdated, value } = rawData; - const fiveMinutesAgo = new Date(); - fiveMinutesAgo.setMinutes(fiveMinutesAgo.getMinutes() - 5); - if (fiveMinutesAgo < lastUpdated) { - return value; - } - } - return null; -} - -export async function schema(req) { - const { url } = req.query; - const headers = { - "Content-Type": "application/json", - Authorization: `Bearer ${process.env.AIRTABLE_API_TOKEN}`, - }; - const cached = getFromCache(url); - if (cached) { - return cached; - } - const value = fetchJson.get(`https://api.airtable.com/v0${url}`, { - headers, - }); - cache[url] = { value, lastUpdated: new Date() }; - return value; -} - -export async function getAirtableData(config) { - const { - baseId, - schema: { - toolTableId, - organisationTableId, - contributorTableId, - partnersTableId, - socialMediaTableId, - }, - } = config; - const tools = await getListFromAirtable({ - baseId, - tableIdOrName: toolTableId, - }); - const contributors = await getListFromAirtable({ - baseId, - tableIdOrName: contributorTableId, - }); - const organisations = await getListFromAirtable({ - baseId, - tableIdOrName: organisationTableId, - }); - const socialMedia = await getListFromAirtable({ - baseId, - tableIdOrName: socialMediaTableId, - }); - const partners = await getListFromAirtable({ - baseId, - tableIdOrName: partnersTableId, - }); - return { tools, organisations, contributors, socialMedia, partners }; -} diff --git a/apps/charterafrica/src/lib/ecosystem/ecosystem.old.js b/apps/charterafrica/src/lib/ecosystem/ecosystem.old.js deleted file mode 100644 index 47cbf4490..000000000 --- a/apps/charterafrica/src/lib/ecosystem/ecosystem.old.js +++ /dev/null @@ -1,118 +0,0 @@ -import * as Sentry from "@sentry/nextjs"; - -import api from "../payload"; - -import { - getAirtableData, - processToolFromAirtable, - processOrganisationFromAirTable, - processContributorFromAirtable, -} from "@/charterafrica/lib/ecosystem/airtable.old"; -import { createCollection } from "@/charterafrica/lib/ecosystem/payload"; -import { - ORGANIZATION_COLLECTION, - CONTRIBUTORS_COLLECTION, - TOOL_COLLECTION, - ECOSYSTEM_GLOBAL, -} from "@/charterafrica/payload/utils/collections"; - -const bulkMarkDeleted = async (collection, fromSource) => { - const dataIds = fromSource.map(({ id }) => id).join(","); - const { docs: toDelete } = await api.getCollection(collection, { - where: { - airtableId: { - not_in: dataIds, - }, - }, - }); - Promise.all( - toDelete.map(async ({ id }) => { - try { - await api.updateCollection(collection, id, { - deletedAt: new Date(), - }); - } catch (error) { - Sentry.captureMessage(error.message); - } - }), - ); -}; -const processTools = async (config, tableData) => { - const { tools: toolsFromAirtable } = tableData; - await bulkMarkDeleted(TOOL_COLLECTION, toolsFromAirtable); - const processedToolPromises = toolsFromAirtable.map(async (data) => { - const airtableData = await processToolFromAirtable( - { - ...data.fields, - id: data.id, - }, - config, - tableData, - ); - // Only get git data if not exist in database - return createCollection(TOOL_COLLECTION, airtableData, config); - }); - return Promise.allSettled(processedToolPromises); -}; - -const processOrganisations = async (config, tableData) => { - const { organisations: organisationsFromAirtable } = tableData; - await bulkMarkDeleted(ORGANIZATION_COLLECTION, organisationsFromAirtable); - const processedOrgPromises = organisationsFromAirtable.map(async (data) => { - const airtableData = await processOrganisationFromAirTable( - { - ...data.fields, - id: data.id, - }, - config, - tableData, - ); - // Only get git data if not exist in database - return createCollection(ORGANIZATION_COLLECTION, airtableData, config); - }); - return Promise.allSettled(processedOrgPromises); -}; - -const processContributors = async (config, tableData) => { - const { contributors: contributorsFromAirtTable } = tableData; - await bulkMarkDeleted(CONTRIBUTORS_COLLECTION, contributorsFromAirtTable); - const processedContributors = contributorsFromAirtTable.map(async (data) => { - const airtableData = await processContributorFromAirtable( - { - ...data.fields, - id: data.id, - }, - config, - tableData, - ); - // Only get git data if not exist in database - return createCollection(CONTRIBUTORS_COLLECTION, airtableData, config); - }); - return Promise.allSettled(processedContributors); -}; - -export const updateEcosystemContent = async (req, res) => { - // For all list in database query Github API. using ETAG - res.status(200).json({}); -}; - -const execute = async () => { - Sentry.captureEvent({ - message: `Update Ecosystem List process started at ${new Date().toString()}`, - level: "info", - }); - const config = await api.findGlobal(ECOSYSTEM_GLOBAL, {}); - const tableData = await getAirtableData(config); - await processContributors(config, tableData); - await processTools(config, tableData); - await processOrganisations(config, tableData); - Sentry.captureEvent({ - message: `Update Ecosystem List process completed ${new Date().toString()}`, - level: "info", - }); -}; - -export const updateEcosystemList = async () => { - execute(); - return { message: "PROCESS_STARTED" }; -};