xiongzhu 2 gadi atpakaļ
vecāks
revīzija
bf33644f04

+ 0 - 75
src/app.controller.spec.ts

@@ -1,75 +0,0 @@
-import { Test, TestingModule } from '@nestjs/testing'
-import { AppController } from './app.controller'
-import { AppService } from './app.service'
-
-class MockResponse {
-    res: any
-    constructor() {
-        this.res = {}
-    }
-    status = jest
-        .fn()
-        .mockReturnThis()
-        .mockImplementationOnce((code) => {
-            this.res.code = code
-            return this
-        })
-    send = jest
-        .fn()
-        .mockReturnThis()
-        .mockImplementationOnce((message) => {
-            this.res.message = message
-            return this
-        })
-    json = jest
-        .fn()
-        .mockReturnThis()
-        .mockImplementationOnce((json) => {
-            this.res.json = json
-            return this
-        })
-}
-
-describe('AppController', () => {
-    let appController: AppController
-    let appService: AppService
-    const response = new MockResponse()
-
-    beforeEach(async () => {
-        const app: TestingModule = await Test.createTestingModule({
-            controllers: [AppController],
-            providers: [
-                {
-                    provide: AppService,
-                    useValue: {
-                        getHello: jest.fn(() => {}),
-                        getSecureResource: jest.fn(() => {})
-                    }
-                }
-            ]
-        }).compile()
-
-        appController = app.get<AppController>(AppController)
-        appService = app.get<AppService>(AppService)
-    })
-
-    describe('root', () => {
-        it('should be defined', () => {
-            expect(appController).toBeDefined()
-        })
-
-        it('should call method getHello() in AppService', () => {
-            const createSpy = jest.spyOn(appService, 'getHello')
-
-            appController.getHello(response as any)
-            expect(createSpy).toHaveBeenCalled()
-        })
-
-        it('should call method getProtectedResource() in AppService', () => {
-            const createSpy = jest.spyOn(appService, 'getSecureResource')
-
-            appController.getProtectedResource(response as any)
-            expect(createSpy).toHaveBeenCalled()
-        })
-    })
-})

+ 0 - 18
src/app.controller.ts

@@ -1,18 +0,0 @@
-import { Controller, Get, Res, HttpStatus, UseGuards } from '@nestjs/common'
-import { AppService } from './app.service'
-import { Response } from 'express'
-
-@Controller()
-export class AppController {
-    constructor(private readonly appService: AppService) {}
-
-    @Get()
-    getHello(@Res() res: Response) {
-        return res.status(HttpStatus.OK).json(this.appService.getHello())
-    }
-
-    @Get('secure')
-    getProtectedResource(@Res() res: Response) {
-        return res.status(HttpStatus.OK).json(this.appService.getSecureResource())
-    }
-}

+ 4 - 8
src/app.module.ts

@@ -2,13 +2,12 @@ import { AliyunModule } from './aliyun/aliyun.module'
 import { Module } from '@nestjs/common'
 import { ConfigModule, ConfigService } from '@nestjs/config'
 import { TypeOrmModule } from '@nestjs/typeorm'
-import { AppController } from './app.controller'
-import { AppService } from './app.service'
-import { UsersModule } from './users/users.module'
 import { ThrottlerModule } from '@nestjs/throttler'
 import * as Yup from 'yup'
-import { SmsModule } from './sms/sms.module'
 import { DevtoolsModule } from '@nestjs/devtools-integration'
+import { APP_FILTER } from '@nestjs/core'
+import { UsersModule } from './users/users.module'
+import { SmsModule } from './sms/sms.module'
 import { AuthModule } from './auth/auth.module'
 import { FileModule } from './file/file.module'
 import { ChatModule } from './chat/chat.module'
@@ -19,9 +18,7 @@ import { CommissionModule } from './commission/commission.module'
 import { UserBalanceModule } from './user-balance/user-balance.module'
 import { WithdrawModule } from './withdraw/withdraw.module'
 import { SysConfigModule } from './sys-config/sys-config.module'
-import { APP_FILTER } from '@nestjs/core'
 import { AllExceptionsFilter } from './filters/all-exceptions-filter.filter'
-
 @Module({
     imports: [
         DevtoolsModule.register({
@@ -80,9 +77,8 @@ import { AllExceptionsFilter } from './filters/all-exceptions-filter.filter'
         WithdrawModule,
         SysConfigModule
     ],
-    controllers: [AppController],
+    controllers: [],
     providers: [
-        AppService,
         {
             provide: APP_FILTER,
             useClass: AllExceptionsFilter

+ 0 - 37
src/app.service.spec.ts

@@ -1,37 +0,0 @@
-import { Test, TestingModule } from '@nestjs/testing'
-import { AppService } from './app.service'
-
-describe('AppService', () => {
-    let service: AppService
-
-    beforeEach(async () => {
-        const module: TestingModule = await Test.createTestingModule({
-            providers: [AppService]
-        }).compile()
-
-        service = module.get<AppService>(AppService)
-    })
-
-    describe('App service', () => {
-        it('should be defined', () => {
-            expect(service).toBeDefined()
-        })
-
-        describe('getHello() method', () => {
-            it('should return message "This is a simple example of item returned by your APIs"', () => {
-                expect(service.getHello()).toEqual({
-                    message: 'This is a simple example of item returned by your APIs.'
-                })
-            })
-        })
-
-        describe('getSecureResource() method', () => {
-            it('should return message "Access to protected resources granted! This protected resource is displayed when the token is successfully provided"', () => {
-                expect(service.getSecureResource()).toEqual({
-                    message:
-                        'Access to protected resources granted! This protected resource is displayed when the token is successfully provided.'
-                })
-            })
-        })
-    })
-})

+ 0 - 20
src/app.service.ts

@@ -1,20 +0,0 @@
-import { Injectable, Logger } from '@nestjs/common'
-
-@Injectable()
-export class AppService {
-    constructor() {
-        Logger.log(`RUNNING IN ${process.env.NODE_ENV || 'dev'} MODE`)
-    }
-    getHello() {
-        return {
-            message: 'This is a simple example of item returned by your APIs.'
-        }
-    }
-
-    getSecureResource() {
-        return {
-            message:
-                'Access to protected resources granted! This protected resource is displayed when the token is successfully provided.'
-        }
-    }
-}

+ 8 - 8
src/filters/all-exceptions-filter.filter.ts

@@ -1,8 +1,7 @@
-import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus } from '@nestjs/common'
+import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus, Logger } from '@nestjs/common'
 import { HttpAdapterHost } from '@nestjs/core'
-import { EntityNotFoundError } from 'typeorm'
 
-@Catch(EntityNotFoundError)
+@Catch()
 export class AllExceptionsFilter implements ExceptionFilter {
     constructor(private readonly httpAdapterHost: HttpAdapterHost) {}
 
@@ -15,12 +14,13 @@ export class AllExceptionsFilter implements ExceptionFilter {
 
         const httpStatus = exception instanceof HttpException ? exception.getStatus() : HttpStatus.INTERNAL_SERVER_ERROR
 
-        const responseBody = {
-            statusCode: httpStatus,
-            timestamp: new Date().toISOString(),
-            path: httpAdapter.getRequestUrl(ctx.getRequest())
+        const res = {
+            status: httpStatus,
+            path: httpAdapter.getRequestUrl(ctx.getRequest()),
+            message: (exception as any).message
         }
+        Logger.error(`status=${res.status}, path=${res.path}, msg=${res.message}`, 'GlobalExceptionFilter')
 
-        httpAdapter.reply(ctx.getResponse(), responseBody, httpStatus)
+        httpAdapter.reply(ctx.getResponse(), res, httpStatus)
     }
 }

+ 2 - 3
src/main.ts

@@ -1,16 +1,15 @@
-import { HttpAdapterHost, NestFactory } from '@nestjs/core'
+import { NestFactory } from '@nestjs/core'
 import { AppModule } from './app.module'
 import { Logger, ValidationPipe } from '@nestjs/common'
 import { ConfigService } from '@nestjs/config'
 import { configureSwaggerDocs } from './helpers/configure-swagger-docs.helper'
 import { NestExpressApplication } from '@nestjs/platform-express'
-import { writeFileSync } from 'fs'
 import { join } from 'path'
 
 async function bootstrap() {
     const app = await NestFactory.create<NestExpressApplication>(AppModule, {
         snapshot: true,
-        abortOnError: true,
+        abortOnError: true
     })
     const configService = app.get<ConfigService>(ConfigService)
 

+ 4 - 1
src/sys-config/entities/sys-config.entity.ts

@@ -1,4 +1,4 @@
-import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm'
+import { Column, CreateDateColumn, Entity, Index, PrimaryGeneratedColumn } from 'typeorm'
 
 export enum SysConfigType {
     String = 'string',
@@ -24,4 +24,7 @@ export class SysConfig {
 
     @Column({ length: 255 })
     remark: string
+
+    @CreateDateColumn()
+    createdAt: Date
 }

+ 5 - 2
src/sys-config/sys-config.admin.controller.ts

@@ -1,4 +1,4 @@
-import { Body, Controller, Get, Param, Post } from '@nestjs/common'
+import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common'
 import { ApiTags } from '@nestjs/swagger'
 import { Public } from '../auth/public.decorator'
 import { SysConfigService } from './sys-config.service'
@@ -11,5 +11,8 @@ import { SysConfig } from './entities/sys-config.entity'
 export class SysConfigAdminController {
     constructor(private readonly sysConfigService: SysConfigService) {}
 
-   
+    @Put()
+    public async create(@Body() sysConfig: Partial<SysConfig>) {
+        return await this.sysConfigService.save(sysConfig)
+    }
 }

+ 8 - 0
src/sys-config/sys-config.service.ts

@@ -43,4 +43,12 @@ export class SysConfigService {
         }
         return conf
     }
+
+    async save(sysConfig: Partial<SysConfig>) {
+        try {
+            return await this.sysConfigRepository.save(sysConfig)
+        } catch (error) {
+            throw new InternalServerErrorException(error.message)
+        }
+    }
 }