feat: implement consumer info update and password change functionality with DTOs
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user