feat: implement consumer info update and password change functionality with DTOs
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import { ConsumerInfo } from '@/common/decorators/consumerInfo.decorator'
|
||||
import { Controller, Get } from '@nestjs/common'
|
||||
import { Body, Controller, Get, Patch, Put } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { ConsumerService } from './consumer.service'
|
||||
import type { ConsumerServiceGetInfoResponseDto } from './dto/consumer-response.dto'
|
||||
import type {
|
||||
ConsumerServiceGetInfoResponseDto,
|
||||
ConsumerServiceUpdateInfoResponseDto,
|
||||
} from './dto/consumer-response.dto'
|
||||
import { UpdateConsumerInfoDto } from './dto/update-info-request.dto'
|
||||
import { UpdateConsumerPasswordDto } from './dto/update-password-request.dto'
|
||||
|
||||
@ApiTags('Consumer')
|
||||
@Controller('consumer')
|
||||
@@ -10,7 +15,26 @@ export class ConsumerController {
|
||||
constructor(private service: ConsumerService) {}
|
||||
|
||||
@Get('')
|
||||
async findOne(@ConsumerInfo('id') consumerId: string): Promise<ConsumerServiceGetInfoResponseDto> {
|
||||
async findOne(
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
): Promise<ConsumerServiceGetInfoResponseDto> {
|
||||
return this.service.getInfo(consumerId)
|
||||
}
|
||||
|
||||
@Patch('')
|
||||
async updateInfo(
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
@Body() data: UpdateConsumerInfoDto,
|
||||
): Promise<ConsumerServiceUpdateInfoResponseDto> {
|
||||
return this.service.updateInfo(consumerId, data)
|
||||
}
|
||||
|
||||
@Put('update-password')
|
||||
async updatePassword(
|
||||
@ConsumerInfo('id') consumerId: string,
|
||||
@ConsumerInfo('account_id') accountId: string,
|
||||
@Body() data: UpdateConsumerPasswordDto,
|
||||
) {
|
||||
return this.service.updatePassword(consumerId, accountId, data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import { QUERY_CONSTANTS } from '@/common/queryConstants'
|
||||
import { ResponseMapper } from '@/common/response/response-mapper'
|
||||
import { PasswordUtil } from '@/common/utils'
|
||||
import { ConsumerUpdateInput } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import consumer_mappersUtil from '../../common/utils/mappers/consumer_mappers.util'
|
||||
import { UpdateConsumerInfoDto } from './dto/update-info-request.dto'
|
||||
import { UpdateConsumerPasswordDto } from './dto/update-password-request.dto'
|
||||
|
||||
@Injectable()
|
||||
export class ConsumerService {
|
||||
@@ -22,4 +26,71 @@ export class ConsumerService {
|
||||
|
||||
return ResponseMapper.single(consumer_mappersUtil(consumer))
|
||||
}
|
||||
|
||||
async updateInfo(consumer_id: string, data: UpdateConsumerInfoDto) {
|
||||
const consumerExists = await this.prisma.consumer.findUnique({
|
||||
where: {
|
||||
id: consumer_id,
|
||||
},
|
||||
select: {
|
||||
type: true,
|
||||
},
|
||||
})
|
||||
|
||||
let dataForUpdate: ConsumerUpdateInput = {}
|
||||
|
||||
if (consumerExists?.type === 'LEGAL') {
|
||||
dataForUpdate = {
|
||||
legal: {
|
||||
update: {
|
||||
name: data.company_name,
|
||||
registration_code: data.registration_code,
|
||||
},
|
||||
},
|
||||
}
|
||||
} else if (consumerExists?.type === 'INDIVIDUAL') {
|
||||
dataForUpdate = {
|
||||
individual: {
|
||||
update: {
|
||||
first_name: data.first_name,
|
||||
last_name: data.last_name,
|
||||
mobile_number: data.mobile_number,
|
||||
national_code: data.national_code,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
const consumer = await this.prisma.consumer.update({
|
||||
where: {
|
||||
id: consumer_id,
|
||||
},
|
||||
data: {
|
||||
...dataForUpdate,
|
||||
},
|
||||
})
|
||||
|
||||
return ResponseMapper.update(consumer)
|
||||
}
|
||||
|
||||
async updatePassword(
|
||||
consumer_id: string,
|
||||
accountId: string,
|
||||
data: UpdateConsumerPasswordDto,
|
||||
) {
|
||||
const consumer = await this.prisma.consumerAccount.update({
|
||||
where: {
|
||||
id: accountId,
|
||||
consumer_id,
|
||||
},
|
||||
data: {
|
||||
account: {
|
||||
update: {
|
||||
password: await PasswordUtil.hash(data.password),
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return ResponseMapper.update(consumer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
import type { ConsumerService } from '../consumer.service'
|
||||
|
||||
export type ConsumerServiceGetInfoResponseDto = Awaited<ReturnType<ConsumerService['getInfo']>>
|
||||
export type ConsumerServiceGetInfoResponseDto = Awaited<
|
||||
ReturnType<ConsumerService['getInfo']>
|
||||
>
|
||||
export type ConsumerServiceUpdateInfoResponseDto = Awaited<
|
||||
ReturnType<ConsumerService['updateInfo']>
|
||||
>
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { IsString } from 'class-validator'
|
||||
|
||||
export class UpdateConsumerInfoDto {
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
first_name: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
last_name: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
mobile_number: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
national_code: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
company_name: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
registration_code: string
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { IsString } from 'class-validator'
|
||||
|
||||
export class UpdateConsumerPasswordDto {
|
||||
@IsString()
|
||||
@ApiProperty({ required: true })
|
||||
password: string
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
import { TokenAccount } from '@/common/decorators/tokenInfo.decorator'
|
||||
import { Controller, Get } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { StatisticsService } from './statistics.service'
|
||||
import type { StatisticsServiceGetInvoicesResponseDto } from './dto/statistics-response.dto'
|
||||
import { StatisticsService } from './statistics.service'
|
||||
|
||||
@ApiTags('ConsumerStatistics')
|
||||
@Controller('consumer/statistics')
|
||||
@@ -10,7 +10,9 @@ export class StatisticsController {
|
||||
constructor(private readonly service: StatisticsService) {}
|
||||
|
||||
@Get('invoices')
|
||||
async getInvoices(@TokenAccount('userId') consumerId: string): Promise<StatisticsServiceGetInvoicesResponseDto> {
|
||||
async getInvoices(
|
||||
@TokenAccount('userId') consumerId: string,
|
||||
): Promise<StatisticsServiceGetInvoicesResponseDto> {
|
||||
return this.service.getInvoices(consumerId)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user