From f703a7899e9f6011879ef33373b21e0877658a2c Mon Sep 17 00:00:00 2001 From: Samrat Sahoo Date: Tue, 5 Mar 2024 16:51:15 -0500 Subject: [PATCH 1/2] start initial integration --- api/src/actions/project.ts | 6 ++++++ api/src/controllers/project/project.ts | 14 +++++++++++++- api/src/models/project.ts | 3 ++- dashboard/api.py | 11 +++++++++-- docker-compose.yml | 2 ++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/api/src/actions/project.ts b/api/src/actions/project.ts index f548437..d45e29d 100644 --- a/api/src/actions/project.ts +++ b/api/src/actions/project.ts @@ -26,4 +26,10 @@ export const getProjectByServerKey = async (serverApiKey: string): Promise { await dbConnect(); return await ProjectModel.findOneAndDelete({ _id: projectId }); +} + +export const getAllProjects = async () => { + await dbConnect(); + return await ProjectModel.find({}).select("projectName _id"); + } \ No newline at end of file diff --git a/api/src/controllers/project/project.ts b/api/src/controllers/project/project.ts index a839474..d44ce73 100644 --- a/api/src/controllers/project/project.ts +++ b/api/src/controllers/project/project.ts @@ -1,4 +1,4 @@ -import { createProject } from "@/src/actions/project"; +import { createProject, getAllProjects, getProjectIDByName } from "@/src/actions/project"; import { relogRequestHandler } from "@/src/middleware/request-middleware"; import APIWrapper from "@/src/utils/api-wrapper"; import { Project } from "@/src/utils/types"; @@ -24,10 +24,22 @@ const projectRoute = APIWrapper({ projectName, clientApiKey, serverApiKey } + const preexistingProject = await getProjectIDByName(projectName); + if (preexistingProject) { + throw new Error("A project with this name already exists") + } + const createdProject = await createProject(project); return createdProject }, }, + GET: { + config: {}, + handler: async (req: Request) => { + return getAllProjects(); + } + } + }); export const project = relogRequestHandler(projectRoute); \ No newline at end of file diff --git a/api/src/models/project.ts b/api/src/models/project.ts index 348abce..5ebc50b 100644 --- a/api/src/models/project.ts +++ b/api/src/models/project.ts @@ -1,4 +1,4 @@ -import mongoose from "mongoose"; +import mongoose, { trusted } from "mongoose"; import { Project } from "@/src/utils/types"; export const ProjectSchema = new mongoose.Schema({ @@ -13,6 +13,7 @@ export const ProjectSchema = new mongoose.Schema({ projectName: { type: String, required: true, + unique: true }, }, { diff --git a/dashboard/api.py b/dashboard/api.py index 30e3679..544b0aa 100644 --- a/dashboard/api.py +++ b/dashboard/api.py @@ -3,7 +3,7 @@ import requests from utils import EventTypes -base_url = "https://analytics.bitsofgood.org" +base_url = "http://api:3001" def get_default_after_time(): @@ -119,4 +119,11 @@ def get_event_types(project_name): def get_projects(): # When we have the project api setup, we will want to retrieve real projects - return [f"project_{i}" for i in range(0, 3)] + # return ["project_1", "project_2", "project_3"] + + project_url = base_url + "/api/project" + + response = requests.get(project_url) + response.raise_for_status() # error + response = response.json() + return [project["projectName"] for project in response["payload"]] diff --git a/docker-compose.yml b/docker-compose.yml index 75d7c7b..e01122d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,6 +24,8 @@ services: volumes: - ./dashboard:/app - ./dashboard/venv:/app/venv + networks: + - bog-analytics-net example-app: build: From 9a388cd0d50733e069139bd2b0288962ff0aa343 Mon Sep 17 00:00:00 2001 From: Samrat Sahoo Date: Tue, 5 Mar 2024 16:53:30 -0500 Subject: [PATCH 2/2] revert back api call for easier testing --- dashboard/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dashboard/api.py b/dashboard/api.py index 544b0aa..c202bd3 100644 --- a/dashboard/api.py +++ b/dashboard/api.py @@ -119,7 +119,7 @@ def get_event_types(project_name): def get_projects(): # When we have the project api setup, we will want to retrieve real projects - # return ["project_1", "project_2", "project_3"] + return ["project_1", "project_2", "project_3"] project_url = base_url + "/api/project"