refactor(backoffice): remove redundant routes and add settings module for credentials
This commit is contained in:
parent
94675c2169
commit
6b23d05383
8 changed files with 89 additions and 34 deletions
|
|
@ -71,3 +71,11 @@ model Order {
|
||||||
product Product @relation(fields: [productId], references: [id])
|
product Product @relation(fields: [productId], references: [id])
|
||||||
createdAt DateTime @default(now())
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import { InventoryModule } from './inventory/inventory.module';
|
||||||
import { PrismaModule } from './prisma/prisma.module';
|
import { PrismaModule } from './prisma/prisma.module';
|
||||||
import { UsersModule } from './users/users.module';
|
import { UsersModule } from './users/users.module';
|
||||||
import { WebhooksModule } from './webhooks/webhooks.module';
|
import { WebhooksModule } from './webhooks/webhooks.module';
|
||||||
|
import { SettingsModule } from './settings/settings.module';
|
||||||
|
|
||||||
import { AppController } from './app.controller';
|
import { AppController } from './app.controller';
|
||||||
|
|
||||||
|
|
@ -16,6 +17,7 @@ import { AppController } from './app.controller';
|
||||||
UsersModule,
|
UsersModule,
|
||||||
InventoryModule,
|
InventoryModule,
|
||||||
WebhooksModule,
|
WebhooksModule,
|
||||||
|
SettingsModule,
|
||||||
],
|
],
|
||||||
controllers: [AppController],
|
controllers: [AppController],
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
import { Body, Controller, Get, HttpCode, HttpStatus, Post, Req, Res, UseGuards } from '@nestjs/common';
|
import { Body, Controller, Get, HttpCode, HttpStatus, Post, Req, Res, UseGuards } from '@nestjs/common';
|
||||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||||
import { FastifyReply } from 'fastify';
|
import { FastifyReply } from 'fastify';
|
||||||
import { CreateUserDto } from '../users/dto/create-user.dto';
|
|
||||||
import { JwtAuthGuard } from './guards/jwt-auth.guard';
|
import { JwtAuthGuard } from './guards/jwt-auth.guard';
|
||||||
import { RefreshTokenGuard } from './guards/refresh-token.guard';
|
import { RefreshTokenGuard } from './guards/refresh-token.guard';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
|
|
@ -10,12 +9,7 @@ import { LoginDto } from './dto/login.dto';
|
||||||
@ApiTags('auth')
|
@ApiTags('auth')
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
constructor(private readonly authService: AuthService) {}
|
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')
|
@Post('login')
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
|
|
@ -38,11 +32,4 @@ export class AuthController {
|
||||||
async logout(@Req() req: any, @Res({ passthrough: true }) reply: FastifyReply) {
|
async logout(@Req() req: any, @Res({ passthrough: true }) reply: FastifyReply) {
|
||||||
return this.authService.logout(req.user.sub, reply);
|
return this.authService.logout(req.user.sub, reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('profile')
|
|
||||||
@UseGuards(JwtAuthGuard)
|
|
||||||
@ApiBearerAuth()
|
|
||||||
async profile(@Req() req: any) {
|
|
||||||
return req.user;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
25
backoffice/src/settings/settings.controller.ts
Normal file
25
backoffice/src/settings/settings.controller.ts
Normal 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 };
|
||||||
|
}
|
||||||
|
}
|
||||||
12
backoffice/src/settings/settings.module.ts
Normal file
12
backoffice/src/settings/settings.module.ts
Normal 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 { }
|
||||||
40
backoffice/src/settings/settings.service.ts
Normal file
40
backoffice/src/settings/settings.service.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,12 +1,10 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { PrismaModule } from '../prisma/prisma.module';
|
import { PrismaModule } from '../prisma/prisma.module';
|
||||||
import { UsersController } from './users.controller';
|
|
||||||
import { UsersService } from './users.service';
|
import { UsersService } from './users.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [PrismaModule],
|
imports: [PrismaModule],
|
||||||
controllers: [UsersController],
|
|
||||||
providers: [UsersService],
|
providers: [UsersService],
|
||||||
exports: [UsersService],
|
exports: [UsersService],
|
||||||
})
|
})
|
||||||
export class UsersModule {}
|
export class UsersModule { }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue