managarten/apps-archived/finance/apps/backend/src/settings/settings.service.ts
Till-JS ace7fa8f7f chore: archive finance, mail, moodlit apps and rename voxel-lava
- Move finance, mail, moodlit to apps-archived for later development
- Rename games/voxel-lava to games/voxelava

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 13:13:15 +01:00

57 lines
1.4 KiB
TypeScript

import { Injectable, Inject } from '@nestjs/common';
import { eq } from 'drizzle-orm';
import { DATABASE_CONNECTION, type Database } from '../db/connection';
import { userSettings } from '../db/schema';
import { UpdateSettingsDto } from './dto';
const DEFAULT_SETTINGS = {
defaultCurrency: 'EUR',
locale: 'de-DE',
dateFormat: 'dd.MM.yyyy',
weekStartsOn: 1, // Monday
};
@Injectable()
export class SettingsService {
constructor(@Inject(DATABASE_CONNECTION) private db: Database) {}
async get(userId: string) {
const [settings] = await this.db
.select()
.from(userSettings)
.where(eq(userSettings.userId, userId));
if (!settings) {
// Create default settings
const [newSettings] = await this.db
.insert(userSettings)
.values({
userId,
...DEFAULT_SETTINGS,
})
.returning();
return newSettings;
}
return settings;
}
async update(userId: string, dto: UpdateSettingsDto) {
// Ensure settings exist
await this.get(userId);
const [settings] = await this.db
.update(userSettings)
.set({
...(dto.defaultCurrency !== undefined && { defaultCurrency: dto.defaultCurrency }),
...(dto.locale !== undefined && { locale: dto.locale }),
...(dto.dateFormat !== undefined && { dateFormat: dto.dateFormat }),
...(dto.weekStartsOn !== undefined && { weekStartsOn: dto.weekStartsOn }),
updatedAt: new Date(),
})
.where(eq(userSettings.userId, userId))
.returning();
return settings;
}
}