diff --git a/app/src/app/[lng]/[inventory]/cdp/page.tsx b/app/src/app/[lng]/[inventory]/cdp/page.tsx
index 1032db8f2..0b996dcb4 100644
--- a/app/src/app/[lng]/[inventory]/cdp/page.tsx
+++ b/app/src/app/[lng]/[inventory]/cdp/page.tsx
@@ -12,9 +12,21 @@ import {
} from "@chakra-ui/react";
import React from "react";
import { useTranslation } from "@/i18n/client";
+import { api } from "@/services/api";
-function Page({ params: { lng } }: { params: { lng: string } }) {
+function Page({
+ params: { lng, inventory },
+}: {
+ params: { lng: string; inventory: string };
+}) {
const { t } = useTranslation(lng, "cdp");
+ const [connectToCDP] = api.useConnectToCDPMutation();
+ const handleConnectToCDP = async () => {
+ await connectToCDP({ inventoryId: inventory }).then((res) =>
+ console.log(res),
+ );
+ console.log(inventory);
+ };
return (
@@ -32,7 +44,7 @@ function Page({ params: { lng } }: { params: { lng: string } }) {
{t("submit-data-to-cdp")}
-
diff --git a/app/src/app/[lng]/[inventory]/page.tsx b/app/src/app/[lng]/[inventory]/page.tsx
index e462a0d48..d423505cf 100644
--- a/app/src/app/[lng]/[inventory]/page.tsx
+++ b/app/src/app/[lng]/[inventory]/page.tsx
@@ -655,7 +655,7 @@ export default function Home({ params: { lng } }: { params: { lng: string } }) {
-
+
>
);
}
diff --git a/app/src/app/api/v0/chat/route.ts b/app/src/app/api/v0/chat/[inventory]/route.ts
similarity index 76%
rename from app/src/app/api/v0/chat/route.ts
rename to app/src/app/api/v0/chat/[inventory]/route.ts
index b66cf71f4..cfca117fc 100644
--- a/app/src/app/api/v0/chat/route.ts
+++ b/app/src/app/api/v0/chat/[inventory]/route.ts
@@ -5,6 +5,8 @@ import { experimental_buildOpenAssistantPrompt } from "ai/prompts";
import OpenAI from "openai";
import { ChatCompletionMessageParam } from "openai/resources/chat/completions";
import { apiHandler } from "@/util/api";
+import UserService from "@/backend/UserService";
+import { db } from "@/models";
let Hf: HfInference, openai: OpenAI;
@@ -88,7 +90,40 @@ async function handleOpenAIChat(
return new StreamingTextResponse(stream);
}
-export const POST = apiHandler(async (req: Request) => {
+export const POST = apiHandler(async (req, { params, session }) => {
+
+ // FETCH INVENTORY DATA FROM DB
+
+ const inventory = await UserService.findUserInventory(
+ params.inventory,
+ session,
+ [
+ {
+ model: db.models.InventoryValue,
+ as: "inventoryValues",
+ include: [
+ {
+ model: db.models.GasValue,
+ as: "gasValues",
+ include: [
+ { model: db.models.EmissionsFactor, as: "emissionsFactor" },
+ ],
+ },
+ {
+ model: db.models.DataSource,
+ attributes: ["datasourceId", "sourceType"],
+ as: "dataSource",
+ },
+ ],
+ },
+ ],
+ );
+
+ // Use this log to view actual data in the console
+ console.log(inventory)
+
+ // TODO: Create prompt function based on inventory data returned from query above
+
const { messages } = chatRequest.parse(await req.json());
if (!messages[0].content.startsWith("You are a")) {
messages.unshift({
diff --git a/app/src/components/ChatBot/chat-bot.tsx b/app/src/components/ChatBot/chat-bot.tsx
index e927b89b1..c5e58f35d 100644
--- a/app/src/components/ChatBot/chat-bot.tsx
+++ b/app/src/components/ChatBot/chat-bot.tsx
@@ -51,10 +51,12 @@ function useEnterSubmit(): {
export default function ChatBot({
inputRef,
t,
+ inventoryId,
}: {
userName?: string;
inputRef?: React.Ref;
t: TFunction;
+ inventoryId: string;
}) {
const {
messages,
@@ -65,7 +67,7 @@ export default function ChatBot({
append,
reload,
} = useChat({
- api: "/api/v0/chat",
+ api: `/api/v0/chat/${inventoryId}`,
initialMessages: [
{ id: "-1", content: t("initial-message"), role: "assistant" },
],
diff --git a/app/src/components/ChatBot/chat-popover.tsx b/app/src/components/ChatBot/chat-popover.tsx
index ce17838be..082481747 100644
--- a/app/src/components/ChatBot/chat-popover.tsx
+++ b/app/src/components/ChatBot/chat-popover.tsx
@@ -18,9 +18,11 @@ import { useTranslation } from "@/i18n/client";
export default function ChatPopover({
lng = "en",
+ inventoryId,
}: {
userName?: string;
lng?: string;
+ inventoryId: string;
}) {
const { isOpen, onOpen, onClose } = useDisclosure();
const inputRef = React.useRef(null);
@@ -64,10 +66,16 @@ export default function ChatPopover({
p={6}
>
{t("ask-ai-expert")}
-
+
-
+
diff --git a/app/src/services/api.ts b/app/src/services/api.ts
index 10ac68ba3..79891da77 100644
--- a/app/src/services/api.ts
+++ b/app/src/services/api.ts
@@ -403,6 +403,14 @@ export const api = createApi({
},
transformResponse: (response: { data: [] }) => response.data,
}),
+ connectToCDP: builder.mutation({
+ query: ({inventoryId}) => {
+ return{
+ method: 'POST',
+ url: `/inventory/${inventoryId}/cdp`
+ }
+ }
+ }),
// ActivityValue CRUD
getActivityValues: builder.query({
@@ -503,5 +511,6 @@ export const {
useInviteUserMutation,
useCheckUserMutation,
useMockDataQuery,
+ useConnectToCDPMutation
} = api;
export const { useGetOCCityQuery, useGetOCCityDataQuery } = openclimateAPI;