Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions controllers/ArtistsProController/getArtistsProHandler.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { Request, Response } from "express";
import { getAllEnterpriseAccounts } from "@/lib/enterprise/getAllEnterpriseAccounts";
import { getSubscriberAccountEmails } from "@/lib/stripe/getSubscriberAccountEmails";
import { getEnterpriseArtists } from "@/lib/enterprise/getEnterpriseArtists";

/**
* Handles GET requests for artists list
* Returns enterprise emails and account emails from active subscriptions
* Returns artists associated with pro accounts (enterprise and active subscriptions)
*/
export const getArtistsProHandler = async (
req: Request,
res: Response
): Promise<void> => {
try {
const [allEnterpriseEmails, subscriptionAccountEmails] = await Promise.all([
getAllEnterpriseAccounts(),
getSubscriberAccountEmails(),
]);

const artists = [...allEnterpriseEmails, ...subscriptionAccountEmails];
const artists = await getEnterpriseArtists();

res.status(200).json({
status: "success",
Expand Down
38 changes: 38 additions & 0 deletions lib/enterprise/getEnterpriseArtists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getAllEnterpriseAccounts } from "@/lib/enterprise/getAllEnterpriseAccounts";
import { getSubscriberAccountEmails } from "@/lib/stripe/getSubscriberAccountEmails";
import { selectAccountArtistIds } from "@/lib/supabase/account_artist_ids/selectAccountArtistIds";

/**
* Gets all artists associated with pro accounts (enterprise and active subscriptions)
* @returns Array of unique artist_ids for pro accounts
*/
export const getEnterpriseArtists = async (): Promise<string[]> => {
const [allEnterpriseEmails, subscriptionAccountEmails] = await Promise.all([
getAllEnterpriseAccounts(),
getSubscriberAccountEmails(),
]);

// Extract unique account_ids from enterprise emails and subscription account emails
const accountIds = new Set(
[...allEnterpriseEmails, ...subscriptionAccountEmails]
.map((email) => email.account_id)
.filter((id): id is string => Boolean(id))
);

if (accountIds.size === 0) {
return [];
}

// Get artist_ids and deduplicate using Set
const accountArtistIds = await selectAccountArtistIds({
account_ids: Array.from(accountIds),
});

return Array.from(
new Set(
accountArtistIds
.map((record) => record.artist_id)
.filter((id): id is string => Boolean(id))
)
);
};
35 changes: 35 additions & 0 deletions lib/supabase/account_artist_ids/selectAccountArtistIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import serverClient from "@/lib/supabase/serverClient";
import { Tables } from "@/types/database.types";

type AccountArtistId = Tables<"account_artist_ids">;

type SelectAccountArtistIdsParams = {
artist_id?: string;
account_ids?: string[];
};

/**
* Selects account_artist_ids with optional filters
*/
export async function selectAccountArtistIds(
params: SelectAccountArtistIdsParams
): Promise<AccountArtistId[]> {
let query = serverClient.from("account_artist_ids").select("*");

// Add filters based on provided parameters
if (params.artist_id) {
query = query.eq("artist_id", params.artist_id);
}

if (params.account_ids && params.account_ids.length > 0) {
query = query.in("account_id", params.account_ids);
}

const { data, error } = await query;

if (error) {
throw new Error(`Failed to fetch account_artist_ids: ${error.message}`);
}

return data || [];
}
25 changes: 9 additions & 16 deletions lib/supabase/getArtistEmails.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import supabase from "./serverClient";
import { selectAccountArtistIds } from "./account_artist_ids/selectAccountArtistIds";

interface ArtistEmailsResponse {
emails: string[];
Expand All @@ -14,28 +15,20 @@ const getArtistEmails = async (
artistId: string
): Promise<ArtistEmailsResponse> => {
try {
const { data: accountArtists, error: accountArtistsError } = await supabase
.from("account_artist_ids")
.select("account_id")
.eq("artist_id", artistId);
if (accountArtistsError) {
console.error(
"[ERROR] Failed to get account_artist_ids:",
accountArtistsError
);
return {
emails: [],
error: new Error("Failed to get artist accounts"),
};
}
if (!accountArtists?.length) {
const accountArtists = await selectAccountArtistIds({
artist_id: artistId,
});

if (!accountArtists || accountArtists.length === 0) {
return {
emails: [],
error: new Error("No accounts found for artist"),
};
}

const accountIds = accountArtists.map((aa) => aa.account_id);
const accountIds = accountArtists
.map((aa) => aa.account_id)
.filter((accountId): accountId is string => Boolean(accountId));

const { data: accountEmails, error: emailsError } = await supabase
.from("account_emails")
Expand Down