Refactor business ID retrieval functions to improve error handling and rename for clarity

This commit is contained in:
THIS ONE IS A LITTLE BIT TRICKY KRUB 2024-10-30 20:56:08 +07:00
parent bbee901bce
commit ad1114088c
2 changed files with 27 additions and 21 deletions

View File

@ -1,25 +1,34 @@
import { SupabaseClient } from "@supabase/supabase-js";
import { getProjectTag, getTagName } from "@/lib/data/query";
async function getBusinessId(supabase: SupabaseClient, projectId: number) {
let { data: project, error } = await supabase
async function getBusinessTypeName(
supabase: SupabaseClient,
projectId: number
) {
let { data: project, error: projectError } = await supabase
.from("project")
.select("business_id")
.eq("id", projectId);
if (error) {
console.error(error);
if (projectError) {
console.error(projectError);
}
return project?.[0]?.business_id;
}
async function getBusinessType(supabase: SupabaseClient, businessId: number) {
let { data: business, error } = await supabase
let { data: business, error: businessError } = await supabase
.from("business")
.select("business_type")
.eq("id", businessId);
if (error) {
console.error(error);
.eq("id", project?.[0]?.business_id);
if (businessError) {
console.error(businessError);
}
return business?.[0]?.business_type;
let { data: business_type, error: businessTypeError } = await supabase
.from("business_type")
.select("value")
.eq("id", business?.[0]?.business_type);
if (businessTypeError) {
console.error(businessError);
}
return business_type;
}
// only use deal that were made at most year ago
@ -194,6 +203,5 @@ export {
dayOftheWeekData,
getInvestorProjectTag,
countTags,
getBusinessId,
getBusinessType,
getBusinessTypeName,
};

View File

@ -8,8 +8,7 @@ import {
dayOftheWeekData,
getInvestorProjectTag,
countTags,
getBusinessId,
getBusinessType,
getBusinessTypeName,
} from "./hook";
import CountUpComponent from "@/components/countUp";
@ -31,16 +30,15 @@ export default async function Portfolio({
const dayOfWeekData = deals ? dayOftheWeekData(deals) : [];
const tags = deals ? await getInvestorProjectTag(supabase, deals) : [];
const tagCount = countTags(tags);
// get business id from project id
const investedBusinessIds = deals
// console.log(investedBusinessIds);
const businessType = deals
? await Promise.all(
deals.map(
async (item) => await getBusinessId(supabase, item.project_id)
async (item) => await getBusinessTypeName(supabase, item.project_id)
)
)
: [];
console.log(investedBusinessIds);
// console.log(tags);
console.log(businessType);
// console.log(tagCount);
return (