refactor(backoffice): remove redundant routes and add settings module for credentials

This commit is contained in:
Tiago Yamamoto 2025-12-27 01:10:25 -03:00
parent 94675c2169
commit 6b23d05383
8 changed files with 89 additions and 34 deletions

View file

@ -71,3 +71,11 @@ model Order {
product Product @relation(fields: [productId], references: [id])
createdAt DateTime @default(now())
}
model SystemSettings {
key String @id
value String
category String @default("GENERAL") // e.g. PAYMENT, SHIPPING
isSecure Boolean @default(false) // If true, should not be returned in plain text unless requested specifically
updatedAt DateTime @updatedAt
}

View file

@ -5,6 +5,7 @@ import { InventoryModule } from './inventory/inventory.module';
import { PrismaModule } from './prisma/prisma.module';
import { UsersModule } from './users/users.module';
import { WebhooksModule } from './webhooks/webhooks.module';
import { SettingsModule } from './settings/settings.module';
import { AppController } from './app.controller';
@ -16,6 +17,7 @@ import { AppController } from './app.controller';
UsersModule,
InventoryModule,
WebhooksModule,
SettingsModule,
],
controllers: [AppController],
})

View file

@ -1,7 +1,6 @@
import { Body, Controller, Get, HttpCode, HttpStatus, Post, Req, Res, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { FastifyReply } from 'fastify';
import { CreateUserDto } from '../users/dto/create-user.dto';
import { JwtAuthGuard } from './guards/jwt-auth.guard';
import { RefreshTokenGuard } from './guards/refresh-token.guard';
import { AuthService } from './auth.service';
@ -12,11 +11,6 @@ import { LoginDto } from './dto/login.dto';
export class AuthController {
constructor(private readonly authService: AuthService) { }
@Post('register')
async register(@Body() dto: CreateUserDto, @Res({ passthrough: true }) reply: FastifyReply) {
return this.authService.register(dto, reply);
}
@Post('login')
@HttpCode(HttpStatus.OK)
async login(@Body() dto: LoginDto, @Res({ passthrough: true }) reply: FastifyReply) {
@ -38,11 +32,4 @@ export class AuthController {
async logout(@Req() req: any, @Res({ passthrough: true }) reply: FastifyReply) {
return this.authService.logout(req.user.sub, reply);
}
@Get('profile')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
async profile(@Req() req: any) {
return req.user;
}
}

View file

@ -0,0 +1,25 @@
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags, ApiOperation } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { SettingsService } from './settings.service';
@ApiTags('settings')
@Controller('settings')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
export class SettingsController {
constructor(private readonly settingsService: SettingsService) { }
@Get('payment-gateways')
@ApiOperation({ summary: 'Get payment gateway credentials (Asaas, Stripe)' })
async getPaymentSettings() {
return this.settingsService.getPaymentSettings();
}
@Post('payment-gateways')
@ApiOperation({ summary: 'Update payment gateway credentials' })
async updatePaymentSettings(@Body() settings: Record<string, string>) {
await this.settingsService.updatePaymentSettings(settings);
return { success: true };
}
}

View file

@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { SettingsService } from './settings.service';
import { SettingsController } from './settings.controller';
import { PrismaModule } from '../prisma/prisma.module';
@Module({
imports: [PrismaModule],
controllers: [SettingsController],
providers: [SettingsService],
exports: [SettingsService],
})
export class SettingsModule { }

View file

@ -0,0 +1,40 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
@Injectable()
export class SettingsService {
constructor(private readonly prisma: PrismaService) { }
async getPaymentSettings() {
const settings = await this.prisma.systemSettings.findMany({
where: { category: 'PAYMENT' },
});
// Transform list into object
return settings.reduce((acc: Record<string, string>, curr) => {
acc[curr.key] = curr.value;
return acc;
}, {} as Record<string, string>);
}
async setPaymentSetting(key: string, value: string, isSecure = true) {
return this.prisma.systemSettings.upsert({
where: { key },
update: { value, isSecure },
create: {
key,
value,
category: 'PAYMENT',
isSecure,
},
});
}
// Helper to bulk update
async updatePaymentSettings(settings: Record<string, string>) {
const promises = Object.entries(settings).map(([key, value]) =>
this.setPaymentSetting(key, value, key.includes('SECRET') || key.includes('KEY')),
);
return Promise.all(promises);
}
}

View file

@ -1,17 +0,0 @@
import { Controller, Get, Req, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { UsersService } from './users.service';
@ApiTags('users')
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}
@Get('me')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
async me(@Req() req: any) {
return this.usersService.getSafeUser(req.user.sub);
}
}

View file

@ -1,11 +1,9 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from '../prisma/prisma.module';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
@Module({
imports: [PrismaModule],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
})