Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -3,11 +3,10 @@ import { JwtService } from '@nestjs/jwt'
|
||||
import { AppController } from './application.controller'
|
||||
import { AppService } from './application.service'
|
||||
import { ApplicationAuthModule } from './auth/auth.module'
|
||||
import { ApplicationConfigModule } from './config/config.module'
|
||||
|
||||
@Module({
|
||||
controllers: [AppController],
|
||||
providers: [AppService, JwtService],
|
||||
imports: [ApplicationConfigModule, ApplicationAuthModule],
|
||||
imports: [ApplicationAuthModule],
|
||||
})
|
||||
export class ApplicationModule {}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
import { Public } from '@/common/decorators/public.decorator'
|
||||
import { TokenAccount } from '@/common/decorators/tokenInfo.decorator'
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { ConfigService } from './config.service'
|
||||
import { CreateConfigDto } from './dto/create-config.dto'
|
||||
|
||||
@Controller('app/config')
|
||||
export class ConfigController {
|
||||
constructor(private readonly configService: ConfigService) {}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') uuid: string) {
|
||||
return this.configService.findOne(uuid)
|
||||
}
|
||||
|
||||
@Post()
|
||||
@Public()
|
||||
create(@TokenAccount('userId') consumerId: string, @Body() data: CreateConfigDto) {
|
||||
return this.configService.create(consumerId, data)
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { ConfigController } from './config.controller'
|
||||
import { ConfigService } from './config.service'
|
||||
|
||||
@Module({
|
||||
controllers: [ConfigController],
|
||||
providers: [ConfigService],
|
||||
})
|
||||
export class ApplicationConfigModule {}
|
||||
@@ -1,83 +0,0 @@
|
||||
import { QUERY_CONSTANTS } from '@/common/queryConstants'
|
||||
import { ConsumerDevicesSelect } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateConfigDto } from './dto/create-config.dto'
|
||||
|
||||
@Injectable()
|
||||
export class ConfigService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
private readonly defaultSelect: ConsumerDevicesSelect = {
|
||||
uuid: true,
|
||||
app_version: true,
|
||||
brand: true,
|
||||
browser_name: true,
|
||||
build_number: true,
|
||||
device: true,
|
||||
os_version: true,
|
||||
fcm_token: true,
|
||||
model: true,
|
||||
platform: true,
|
||||
release_number: true,
|
||||
sdk_version: true,
|
||||
consumer: {
|
||||
select: {
|
||||
id: true,
|
||||
...QUERY_CONSTANTS.CONSUMER.infoSelect,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
async findOne(uuid: string) {
|
||||
const config = await this.prisma.consumerDevices.findUnique({
|
||||
where: {
|
||||
uuid,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
return ResponseMapper.single(config)
|
||||
}
|
||||
|
||||
async create(consumer_id: string, data: CreateConfigDto) {
|
||||
const prevConfig = await this.prisma.consumerDevices.findUnique({
|
||||
where: {
|
||||
uuid: data.uuid,
|
||||
},
|
||||
select: {
|
||||
consumer_id: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (prevConfig && consumer_id !== prevConfig.consumer_id) {
|
||||
throw new BadRequestException('این دستگاه با کاربری دیگری ثبت شده است.')
|
||||
}
|
||||
|
||||
const config = await this.prisma.consumerDevices.upsert({
|
||||
where: {
|
||||
uuid: data.uuid,
|
||||
},
|
||||
update: {
|
||||
...data,
|
||||
consumer: {
|
||||
connect: {
|
||||
id: consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
create: {
|
||||
...data,
|
||||
consumer: {
|
||||
connect: {
|
||||
id: consumer_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
return ResponseMapper.create(config)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
import type { ConfigService } from '../config.service'
|
||||
|
||||
export type ConfigServiceCreateResponseDto = Awaited<ReturnType<ConfigService['create']>>
|
||||
export type ConfigServiceFindOneResponseDto = Awaited<ReturnType<ConfigService['findOne']>>
|
||||
@@ -1,66 +0,0 @@
|
||||
import { ApplicationPlatform, ApplicationPublisher } from '@/generated/prisma/enums'
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsEnum, IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreateConfigDto {
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
uuid: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsEnum(ApplicationPublisher)
|
||||
publisher: ApplicationPublisher
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
app_version: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
build_number: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsEnum(ApplicationPlatform)
|
||||
platform: ApplicationPlatform
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
brand: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
model: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
device: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
os_version: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
sdk_version: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
release_number: string
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
browser_name: string
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
user_agent: string
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
fcm_token?: string
|
||||
}
|
||||
|
||||
export class UpdateConfigDto extends PartialType(CreateConfigDto) {}
|
||||
Reference in New Issue
Block a user