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

View File

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