From d83a9ee388acb3e361fb85c9ad03114296e6e8cd Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Fri, 14 Nov 2025 15:30:36 -0500 Subject: [PATCH 1/4] update getAccountEmail to use ilike --- controllers/SubscriptionsController.ts | 2 +- .../account_emails/getAccountEmails.ts | 26 +++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/controllers/SubscriptionsController.ts b/controllers/SubscriptionsController.ts index 38c7a09a..0997653c 100644 --- a/controllers/SubscriptionsController.ts +++ b/controllers/SubscriptionsController.ts @@ -25,7 +25,7 @@ export const getSubscriptionsHandler = async ( } // Get account emails to check if any belong to enterprise domain - const accountEmails = await getAccountEmails(accountId); + const accountEmails = await getAccountEmails({ account_id: accountId }); if (!accountEmails || accountEmails.length === 0) { res.status(404).json({ diff --git a/lib/supabase/account_emails/getAccountEmails.ts b/lib/supabase/account_emails/getAccountEmails.ts index f421a683..a76a3933 100644 --- a/lib/supabase/account_emails/getAccountEmails.ts +++ b/lib/supabase/account_emails/getAccountEmails.ts @@ -3,13 +3,29 @@ import { Tables } from "../../../types/database.types"; type AccountEmail = Tables<"account_emails">; +type GetAccountEmailsParams = { + account_id?: string; + queryEmail?: string; +}; + +/** + * Selects account_emails with optional filters + */ export const getAccountEmails = async ( - accountId: string + params: GetAccountEmailsParams ): Promise => { - const { data, error } = await serverClient - .from("account_emails") - .select("*") - .eq("account_id", accountId); + let query = serverClient.from("account_emails").select("*"); + + // Add filters based on provided parameters + if (params.account_id) { + query = query.eq("account_id", params.account_id); + } + + if (params.queryEmail) { + query = query.ilike("email", `%${params.queryEmail}%`); + } + + const { data, error } = await query; if (error) { console.error("Error fetching account emails:", error); From 2aefb23754dad5d7699a6c1ebfc37774d317dc50 Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Fri, 14 Nov 2025 15:38:17 -0500 Subject: [PATCH 2/4] enterprise lib folder. --- controllers/SubscriptionsController.ts | 2 +- lib/{ => enterprise}/isEnterprise.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename lib/{ => enterprise}/isEnterprise.ts (75%) diff --git a/controllers/SubscriptionsController.ts b/controllers/SubscriptionsController.ts index 0997653c..ef10268d 100644 --- a/controllers/SubscriptionsController.ts +++ b/controllers/SubscriptionsController.ts @@ -1,6 +1,6 @@ import { Request, Response } from "express"; import { getActiveSubscriptionDetails } from "../lib/stripe/getActiveSubscriptionDetails"; -import isEnterprise from "../lib/isEnterprise"; +import isEnterprise from "@/lib/enterprise/isEnterprise"; import { getAccountEmails } from "../lib/supabase/account_emails/getAccountEmails"; /** diff --git a/lib/isEnterprise.ts b/lib/enterprise/isEnterprise.ts similarity index 75% rename from lib/isEnterprise.ts rename to lib/enterprise/isEnterprise.ts index 0826955e..0282cc8b 100644 --- a/lib/isEnterprise.ts +++ b/lib/enterprise/isEnterprise.ts @@ -1,5 +1,5 @@ -import { ENTERPRISE_DOMAINS } from "./consts"; -import extractDomain from "./email/extractDomain"; +import { ENTERPRISE_DOMAINS } from "../consts"; +import extractDomain from "../email/extractDomain"; /** * Returns true if the provided email belongs to a known enterprise domain. From ea7a897437753dc214d348e1925ce47c22e4b834 Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Fri, 14 Nov 2025 15:41:00 -0500 Subject: [PATCH 3/4] SRP - getAllEnterpriseAccounts --- .../getArtistsProHandler.ts | 39 ++++++++----------- lib/enterprise/getAllEnterpriseAccounts.ts | 21 ++++++++++ 2 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 lib/enterprise/getAllEnterpriseAccounts.ts diff --git a/controllers/ArtistsProController/getArtistsProHandler.ts b/controllers/ArtistsProController/getArtistsProHandler.ts index 1cb6cefe..e5a28c83 100644 --- a/controllers/ArtistsProController/getArtistsProHandler.ts +++ b/controllers/ArtistsProController/getArtistsProHandler.ts @@ -1,33 +1,28 @@ import { Request, Response } from "express"; +import { getAllEnterpriseAccounts } from "@/lib/enterprise/getAllEnterpriseAccounts"; /** * Handles GET requests for artists list - * Returns mock artists data + * Returns enterprise emails */ export const getArtistsProHandler = async ( req: Request, res: Response ): Promise => { - const mockResponse = { - status: "success", - artists: [ - { - id: "1", - name: "Artist One", - timestamp: 1234567890, - }, - { - id: "2", - name: "Artist Two", - timestamp: 1234567891, - }, - { - id: "3", - name: "Artist Three", - timestamp: 1234567892, - }, - ], - }; + try { + const allEnterpriseEmails = await getAllEnterpriseAccounts(); - res.status(200).json(mockResponse); + res.status(200).json({ + status: "success", + artists: allEnterpriseEmails, + }); + } catch (error) { + console.error("[ERROR] Error in getArtistsProHandler:", error); + res.status(500).json({ + status: "error", + artists: [], + error: + error instanceof Error ? error.message : "An unexpected error occurred", + }); + } }; diff --git a/lib/enterprise/getAllEnterpriseAccounts.ts b/lib/enterprise/getAllEnterpriseAccounts.ts new file mode 100644 index 00000000..0fd40b9d --- /dev/null +++ b/lib/enterprise/getAllEnterpriseAccounts.ts @@ -0,0 +1,21 @@ +import { getAccountEmails } from "@/lib/supabase/account_emails/getAccountEmails"; +import { ENTERPRISE_DOMAINS } from "@/lib/consts"; +import { Tables } from "@/types/database.types"; + +type AccountEmail = Tables<"account_emails">; + +/** + * Gets all account emails that belong to enterprise domains + * @returns Array of account emails from enterprise domains + */ +export const getAllEnterpriseAccounts = async (): Promise => { + const allEnterpriseEmails: AccountEmail[] = []; + + // Query for each enterprise domain using queryEmail parameter + for (const domain of ENTERPRISE_DOMAINS) { + const emails = await getAccountEmails({ queryEmail: `@${domain}` }); + allEnterpriseEmails.push(...emails); + } + + return allEnterpriseEmails; +}; From c4badf8a2bfdd7c019b3afd2009db22f374e60c9 Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Fri, 14 Nov 2025 15:43:44 -0500 Subject: [PATCH 4/4] absolute imports --- lib/enterprise/isEnterprise.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/enterprise/isEnterprise.ts b/lib/enterprise/isEnterprise.ts index 0282cc8b..2f592374 100644 --- a/lib/enterprise/isEnterprise.ts +++ b/lib/enterprise/isEnterprise.ts @@ -1,5 +1,5 @@ -import { ENTERPRISE_DOMAINS } from "../consts"; -import extractDomain from "../email/extractDomain"; +import { ENTERPRISE_DOMAINS } from "@/lib/consts"; +import extractDomain from "@/lib/email/extractDomain"; /** * Returns true if the provided email belongs to a known enterprise domain.