mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 04:16:53 +00:00
fix(api): fix update team settings and make request schema type-safe
This commit is contained in:
@@ -209,17 +209,17 @@ describe('team api', () => {
|
||||
})
|
||||
|
||||
it('PATCH /teams/:teamId/settings updates settings', async () => {
|
||||
mockUpdateSettings.mockResolvedValue({ someKey: 'new_value' })
|
||||
mockUpdateSettings.mockResolvedValue({ transcode: { videoStrategy: 'all' } })
|
||||
|
||||
const res = await app.request('/teams/t1/settings', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ key: 'someKey', value: 'new_value' }),
|
||||
body: JSON.stringify({ key: 'transcode.videoStrategy', value: 'all' }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data.someKey).toBe('new_value')
|
||||
expect(data.transcode.videoStrategy).toBe('all')
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: ResourceType.Team,
|
||||
@@ -227,7 +227,7 @@ describe('team api', () => {
|
||||
permission: Permission.Admin,
|
||||
}),
|
||||
)
|
||||
expect(mockUpdateSettings).toHaveBeenCalledWith('t1', 'someKey', 'new_value')
|
||||
expect(mockUpdateSettings).toHaveBeenCalledWith('t1', 'transcode.videoStrategy', 'all')
|
||||
})
|
||||
|
||||
it('GET /teams/:teamId/user-metadata returns all metadata for the user in the team', async () => {
|
||||
|
||||
@@ -260,6 +260,15 @@ describe('TeamService', () => {
|
||||
|
||||
const newSettings = await teamService.getSettings(team.id)
|
||||
expect(newSettings).toEqual({ theme: 'dark', semanticSearchEnabled: false })
|
||||
|
||||
await teamService.updateSettings(team.id, 'transcode.videoStrategy', 'all')
|
||||
|
||||
const finalSettings = await teamService.getSettings(team.id)
|
||||
expect(finalSettings).toEqual({
|
||||
theme: 'dark',
|
||||
transcode: { videoStrategy: 'all' },
|
||||
semanticSearchEnabled: false,
|
||||
})
|
||||
})
|
||||
|
||||
it('should create sandbox when team is created', async () => {
|
||||
|
||||
@@ -306,7 +306,16 @@ export class TeamService {
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const settings = (team.settings || {}) as any
|
||||
settings[key] = value
|
||||
|
||||
if (key === 'transcode.videoStrategy') {
|
||||
if (!settings.transcode) {
|
||||
settings.transcode = {}
|
||||
}
|
||||
settings.transcode.videoStrategy = value
|
||||
delete settings['transcode.videoStrategy']
|
||||
} else {
|
||||
settings[key] = value
|
||||
}
|
||||
|
||||
const updated = await prisma.team.update({
|
||||
where: { id: teamId },
|
||||
|
||||
@@ -63,12 +63,6 @@ export const listMembersQuerySchema = z.object({
|
||||
.transform((v) => v === 'true'),
|
||||
})
|
||||
|
||||
export const updateTeamSettingsRequestSchema = z.object({
|
||||
key: z.string(),
|
||||
value: z.any(),
|
||||
})
|
||||
export type UpdateTeamSettingsRequest = z.infer<typeof updateTeamSettingsRequestSchema>
|
||||
|
||||
export const VideoTranscodeStrategy = {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
best_match: 'best_match',
|
||||
@@ -77,6 +71,14 @@ export const VideoTranscodeStrategy = {
|
||||
export type VideoTranscodeStrategy =
|
||||
(typeof VideoTranscodeStrategy)[keyof typeof VideoTranscodeStrategy]
|
||||
|
||||
export const updateTeamSettingsRequestSchema = z.discriminatedUnion('key', [
|
||||
z.object({
|
||||
key: z.literal('transcode.videoStrategy'),
|
||||
value: z.nativeEnum(VideoTranscodeStrategy),
|
||||
}),
|
||||
])
|
||||
export type UpdateTeamSettingsRequest = z.infer<typeof updateTeamSettingsRequestSchema>
|
||||
|
||||
export const sandboxSettingsSchema = z.object({
|
||||
allowedDomains: z.array(z.string()),
|
||||
pendingDomains: z.array(z.string()).default([]),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { VideoTranscodeStrategy } from '@shumai/dtos'
|
||||
import { VideoTranscodeStrategy, UpdateTeamSettingsRequest } from '@shumai/dtos'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { AgentsSettings } from '@/ui/components/settings/AgentsSettings'
|
||||
import { ProvidersSettings } from '@/ui/components/settings/ProvidersSettings'
|
||||
@@ -188,11 +188,10 @@ function TeamSettingsPage() {
|
||||
}
|
||||
|
||||
const { mutate: updateSettings } = useMutation({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
mutationFn: async (data: { teamId: string; data: { key: string; value: any } }) => {
|
||||
mutationFn: async (data: { teamId: string; data: UpdateTeamSettingsRequest }) => {
|
||||
const res = await client.api.teams[':teamId'].settings.$patch({
|
||||
param: { teamId: data.teamId },
|
||||
json: { key: data.data.key, value: data.data.value },
|
||||
json: data.data,
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to update settings')
|
||||
return await res.json()
|
||||
|
||||
Reference in New Issue
Block a user