mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-04-08 03:00:28 -04:00
feat(backend): add routes for updating user via settings
This commit is contained in:
@@ -9,13 +9,14 @@ import stripe
|
||||
from autogpt_libs.auth.middleware import auth_middleware
|
||||
from autogpt_libs.feature_flag.client import feature_flag
|
||||
from autogpt_libs.utils.cache import thread_cached
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
||||
from fastapi import APIRouter, Body, Depends, HTTPException, Request, Response
|
||||
from typing_extensions import Optional, TypedDict
|
||||
|
||||
from prisma.enums import NotificationType
|
||||
import backend.data.block
|
||||
import backend.server.integrations.router
|
||||
import backend.server.routers.analytics
|
||||
from backend.data import execution as execution_db
|
||||
from backend.data.notifications import NotificationPreference, NotificationPreferenceDTO
|
||||
from backend.data import graph as graph_db
|
||||
from backend.data.api_key import (
|
||||
APIKeyError,
|
||||
@@ -39,7 +40,12 @@ from backend.data.credit import (
|
||||
get_user_credit_model,
|
||||
set_auto_top_up,
|
||||
)
|
||||
from backend.data.user import get_or_create_user
|
||||
from backend.data.user import (
|
||||
get_or_create_user,
|
||||
get_user_notification_preference,
|
||||
update_user_notification_preference,
|
||||
update_user_email,
|
||||
)
|
||||
from backend.executor import ExecutionManager, ExecutionScheduler, scheduler
|
||||
from backend.integrations.creds_manager import IntegrationCredentialsManager
|
||||
from backend.integrations.webhooks.graph_lifecycle_hooks import (
|
||||
@@ -107,6 +113,41 @@ async def get_or_create_user_route(user_data: dict = Depends(auth_middleware)):
|
||||
return user.model_dump()
|
||||
|
||||
|
||||
@v1_router.post(
|
||||
"/auth/user/email", tags=["auth"], dependencies=[Depends(auth_middleware)]
|
||||
)
|
||||
async def update_user_email_route(
|
||||
user_id: Annotated[str, Depends(get_user_id)], email: str
|
||||
) -> dict[str, str]:
|
||||
await update_user_email(user_id, email)
|
||||
|
||||
return {"email": email}
|
||||
|
||||
|
||||
@v1_router.get(
|
||||
"/auth/user/preferences",
|
||||
tags=["auth"],
|
||||
dependencies=[Depends(auth_middleware)],
|
||||
)
|
||||
async def get_preferences(
|
||||
user_id: Annotated[str, Depends(get_user_id)],
|
||||
) -> NotificationPreference:
|
||||
return await get_user_notification_preference(user_id)
|
||||
|
||||
|
||||
@v1_router.post(
|
||||
"/auth/user/preferences",
|
||||
tags=["auth"],
|
||||
dependencies=[Depends(auth_middleware)],
|
||||
)
|
||||
async def update_preferences(
|
||||
user_id: Annotated[str, Depends(get_user_id)],
|
||||
preferences: NotificationPreferenceDTO = Body(...),
|
||||
) -> NotificationPreference:
|
||||
output = await update_user_notification_preference(user_id, preferences)
|
||||
return output
|
||||
|
||||
|
||||
########################################################
|
||||
##################### Blocks ###########################
|
||||
########################################################
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import getServerSupabase from "@/lib/supabase/getServerSupabase";
|
||||
import BackendApi from "@/lib/autogpt-server-api";
|
||||
import {
|
||||
NotificationPreference,
|
||||
NotificationType,
|
||||
} from "@/lib/autogpt-server-api/types";
|
||||
|
||||
export async function updateSettings(formData: FormData) {
|
||||
const supabase = getServerSupabase();
|
||||
@@ -27,20 +32,40 @@ export async function updateSettings(formData: FormData) {
|
||||
const { error: emailError } = await supabase.auth.updateUser({
|
||||
email,
|
||||
});
|
||||
const api = new BackendApi();
|
||||
await api.updateUserEmail(email);
|
||||
|
||||
if (emailError) {
|
||||
throw new Error(`${emailError.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: @ntindle Handle updating notification preferences here
|
||||
// const preferencesError = {};
|
||||
//
|
||||
// if (preferencesError) {
|
||||
// throw new SettingsError(
|
||||
// `Failed to update preferences: ${preferencesError.message}`,
|
||||
// );
|
||||
// }
|
||||
const preferencesError = {};
|
||||
|
||||
if (preferencesError) {
|
||||
throw new SettingsError(
|
||||
`Failed to update preferences: ${preferencesError.message}`,
|
||||
);
|
||||
}
|
||||
|
||||
const api = new BackendApi();
|
||||
const preferences: NotificationPreference = {
|
||||
email: user?.email || "",
|
||||
preferences: {
|
||||
agent_run: formData.get("notifyOnAgentRun") === "true",
|
||||
zero_balance: formData.get("notifyOnZeroBalance") === "true",
|
||||
low_balance: formData.get("notifyOnLowBalance") === "true",
|
||||
block_execution_failed:
|
||||
formData.get("notifyOnBlockExecutionFailed") === "true",
|
||||
continuous_agent_error:
|
||||
formData.get("notifyOnContinuousAgentError") === "true",
|
||||
daily_summary: formData.get("notifyOnDailySummary") === "true",
|
||||
weekly_summary: formData.get("notifyOnWeeklySummary") === "true",
|
||||
monthly_summary: formData.get("notifyOnMonthlySummary") === "true",
|
||||
},
|
||||
daily_limit: 0,
|
||||
};
|
||||
await api.updateUserPreferences(preferences);
|
||||
|
||||
revalidatePath("/profile/settings");
|
||||
return { success: true };
|
||||
|
||||
Reference in New Issue
Block a user