Untitled

mail@pastecode.io avatar
unknown
javascript
11 days ago
3.7 kB
5
Indexable
Never
"use server";

import { createClient } from "@/lib/supabase/server";
import { Point, findQuadraticFormula } from "@/lib/decarb-progress";

export async function calculateProjections(): Promise<
  [string, string, string, string]
> {
  // get the necessary data from the database
  // get the user's portfolio details
  // get the user's portfolio goals
  // get the user's portfolio emissions
  // get the user's portfolio emissions reductions
  const supabase = createClient();
  const { data: userData, error: userError } = await supabase.auth.getUser();
  const isAuthenticated = !userError && !!userData?.user;
  const userId = isAuthenticated ? userData.user.id : null;

  const { data: portfolioData, error: portfolioError } = await supabase
    .from("portfolio_details")
    .select(
      "id, baseline_year, current_scope_1, current_scope_2, current_scope_3",
    )
    .eq("user_id", userId);

  const id = portfolioData![0]!.id;
  const baseYear = portfolioData![0]!.baseline_year;
  const emissionsScope1 = portfolioData![0]!.current_scope_1;
  const emissionsScope2 = portfolioData![0]!.current_scope_2;
  const emissionsScope3 = portfolioData![0]!.current_scope_3;

  const { data: goalsData, error: goalsError } = await supabase
    .from("portfolio_goals")
    .select("year, scope_1, scope_2, scope_3")
    .eq("portfolio_id", id);

  const year1 = goalsData![0]!.year;
  const reduction1Scope1 = goalsData![0]!.scope_1;
  const reduction1Scope2 = goalsData![0]!.scope_2;
  const reduction1Scope3 = goalsData![0]!.scope_3;

  const year2 = goalsData![1]!.year;
  const reduction2Scope1 = goalsData![1]!.scope_1;
  const reduction2Scope2 = goalsData![1]!.scope_2;
  const reduction2Scope3 = goalsData![1]!.scope_3;

  // Calculate the reductions in emissions for each scope

  const scope1Point1 = emissionsScope1;
  const scope1Point2 = emissionsScope1 * (1 - reduction1Scope1);
  const scope1Point3 = emissionsScope1 * (1 - reduction2Scope1);

  const scope1Points: Point[] = [
    { x: 0, y: scope1Point1 },
    { x: year1 - baseYear, y: scope1Point2 },
    { x: year2 - baseYear, y: scope1Point3 },
  ];
  const scope1Formula = findQuadraticFormula(scope1Points);

  const scope2Point1 = emissionsScope2;
  const scope2Point2 = emissionsScope2 * (1 - reduction1Scope2);
  const scope2Point3 = emissionsScope2 * (1 - reduction2Scope2);

  const scope2Points: Point[] = [
    { x: 0, y: scope2Point1 },
    { x: year1 - baseYear, y: scope2Point2 },
    { x: year2 - baseYear, y: scope2Point3 },
  ];
  const scope2Formula = findQuadraticFormula(scope2Points);

  const scope3Point1 = emissionsScope3;
  const scope3Point2 = emissionsScope3 * (1 - reduction1Scope3);
  const scope3Point3 = emissionsScope3 * (1 - reduction2Scope3);

  const scope3Points: Point[] = [
    { x: 0, y: scope3Point1 },
    { x: year1 - baseYear, y: scope3Point2 },
    { x: year2 - baseYear, y: scope3Point3 },
  ];
  const scope3Formula = findQuadraticFormula(scope3Points);

  const totalPoint1 = emissionsScope1 + emissionsScope2 + emissionsScope3;
  const totalPoint2 =
    emissionsScope1 * (1 - reduction1Scope1) +
    emissionsScope2 * (1 - reduction1Scope2) +
    emissionsScope3 * (1 - reduction1Scope3);
  const totalPoint3 =
    emissionsScope1 * (1 - reduction2Scope1) +
    emissionsScope2 * (1 - reduction2Scope2) +
    emissionsScope3 * (1 - reduction2Scope3);

  const totalPoints: Point[] = [
    { x: 0, y: totalPoint1 },
    { x: year1 - baseYear, y: totalPoint2 },
    { x: year2 - baseYear, y: totalPoint3 },
  ];
  const totalFormula = findQuadraticFormula(totalPoints);

  return [scope1Formula, scope2Formula, scope3Formula, totalFormula];
}
Leave a Comment