File size: 2,082 Bytes
57a889c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Body, Controller, Get, HttpCode, HttpException, Post, Put, UseGuards } from '@nestjs/common';
import type { User } from '../../types';
import { SettingsService } from './settings.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { CurrentUser } from '../auth/current-user.decorator';

const MASKED_VALUE = '••••••••';

/**
 * /api/settings — per-user key/value preferences.
 *
 * Byte-identical to the legacy Express route (server/src/routes/settings.ts):
 * get-all, single upsert (400 without a key, no-op on the masked sentinel), and
 * bulk upsert (400 without an object, 500 on a write error). All answer 200.
 */
@Controller('api/settings')
@UseGuards(JwtAuthGuard)
export class SettingsController {
  constructor(private readonly settings: SettingsService) {}

  @Get()
  list(@CurrentUser() user: User) {
    return { settings: this.settings.getUserSettings(user.id) };
  }

  @Put()
  upsert(@CurrentUser() user: User, @Body() body: { key?: string; value?: unknown }) {
    if (!body.key) {
      throw new HttpException({ error: 'Key is required' }, 400);
    }
    // The client echoes a redacted secret back unchanged — treat as a no-op.
    if (body.value === MASKED_VALUE) {
      return { success: true, key: body.key, unchanged: true };
    }
    this.settings.upsertSetting(user.id, body.key, body.value);
    return { success: true, key: body.key, value: body.value };
  }

  @Post('bulk')
  @HttpCode(200) // Express answers bulk with res.json (200), not the POST-default 201.
  bulk(@CurrentUser() user: User, @Body() body: { settings?: unknown }) {
    if (!body.settings || typeof body.settings !== 'object') {
      throw new HttpException({ error: 'Settings object is required' }, 400);
    }
    try {
      const updated = this.settings.bulkUpsertSettings(user.id, body.settings as Record<string, unknown>);
      return { success: true, updated };
    } catch (err) {
      console.error('Error saving settings:', err);
      throw new HttpException({ error: 'Error saving settings' }, 500);
    }
  }
}