mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-28 03:00:29 -04:00
* feat(db-sync): added general sync file and implemented environment sync * improvement(workflows-store): structured workflows store system better and added getter for values across stores * fix(stores): deleted workflows/types since unused * improvement(db-sync): added workflow event syncs and debounce * improvement(db-sync): clean and upgraded db-sync system; environment sync implemented * improvement(db-sync): added batch sync with registry; init bug needs fixing * improvement(db-sync): finalized sync system and implemented for workflow * fix(db-sync): fixed client-side rendering * improvement(db-sync): created backwards sync system; environment implemented * improvement(db-sync): added colors to db * fix(db-sync): color sync with db * improvement(db-sync): added workflow backwards sync; fixing color bug and race condition * fix(db-stores): color sync * feature(db-sync): db-sync complete; need to sync history * improvement(db-sync): added scheduling * fix(db-sync): environment sync to db
78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { eq } from 'drizzle-orm'
|
|
import { nanoid } from 'nanoid'
|
|
import { z } from 'zod'
|
|
import { db } from '@/db'
|
|
import { settings } from '@/db/schema'
|
|
|
|
const SettingsSchema = z.object({
|
|
userId: z.string(),
|
|
isAutoConnectEnabled: z.boolean().default(true),
|
|
})
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const body = await request.json()
|
|
const { userId, isAutoConnectEnabled } = SettingsSchema.parse(body)
|
|
|
|
// Store the settings
|
|
await db
|
|
.insert(settings)
|
|
.values({
|
|
id: nanoid(),
|
|
userId,
|
|
general: { isAutoConnectEnabled },
|
|
updatedAt: new Date(),
|
|
})
|
|
.onConflictDoUpdate({
|
|
target: [settings.userId],
|
|
set: {
|
|
general: { isAutoConnectEnabled },
|
|
updatedAt: new Date(),
|
|
},
|
|
})
|
|
|
|
return NextResponse.json({ success: true }, { status: 200 })
|
|
} catch (error: any) {
|
|
console.error('Settings update error:', error)
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
try {
|
|
const { searchParams } = new URL(request.url)
|
|
const userId = searchParams.get('userId')
|
|
|
|
if (!userId) {
|
|
return NextResponse.json({ error: 'userId is required' }, { status: 400 })
|
|
}
|
|
|
|
const result = await db.select().from(settings).where(eq(settings.userId, userId)).limit(1)
|
|
|
|
if (!result.length) {
|
|
return NextResponse.json(
|
|
{
|
|
data: {
|
|
isAutoConnectEnabled: true, // Return default values
|
|
},
|
|
},
|
|
{ status: 200 }
|
|
)
|
|
}
|
|
|
|
const generalSettings = result[0].general as { isAutoConnectEnabled: boolean }
|
|
return NextResponse.json(
|
|
{
|
|
data: {
|
|
isAutoConnectEnabled: generalSettings.isAutoConnectEnabled,
|
|
},
|
|
},
|
|
{ status: 200 }
|
|
)
|
|
} catch (error: any) {
|
|
console.error('Settings fetch error:', error)
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
}
|