update admin user business
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { IsOptional, IsString } from 'class-validator'
|
||||
import { IsNumber, IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreatePartnerDto {
|
||||
@IsString()
|
||||
@@ -7,14 +7,10 @@ export class CreatePartnerDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
code?: string
|
||||
}
|
||||
|
||||
export class UpdatePartnerDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
name?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
code?: string
|
||||
@IsNumber()
|
||||
license_quota?: number
|
||||
}
|
||||
|
||||
export class UpdatePartnerDto extends CreatePartnerDto {}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import { CreatePartnerLicenseDto, UpdateLicenseDto } from './dto/license.dto'
|
||||
import { PartnerLicensesService } from './licenses.service'
|
||||
|
||||
@@ -24,7 +24,7 @@ export class PartnerLicensesController {
|
||||
return this.licensesService.create(partnerId, data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
@Patch(':id')
|
||||
async update(
|
||||
@Param('partnerId') partnerId: string,
|
||||
@Param('id') id: string,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import { AccountsService } from './accounts.service'
|
||||
import { CreateAccountDto, UpdateAccountDto } from './dto/account.dto'
|
||||
|
||||
@@ -21,7 +21,7 @@ export class AccountsController {
|
||||
return this.accountsService.create(partnerId, data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
@Patch(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdateAccountDto) {
|
||||
return this.accountsService.update(id, data)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { PasswordUtil } from 'common/utils/password.util'
|
||||
import { AccountType } from 'generated/prisma/enums'
|
||||
import { AccountStatus, AccountType } from 'generated/prisma/enums'
|
||||
import { CreateAccountDto, UpdateAccountDto } from './dto/account.dto'
|
||||
|
||||
@Injectable()
|
||||
@@ -38,7 +38,7 @@ export class AccountsService {
|
||||
}
|
||||
|
||||
async create(partnerId: string, data: CreateAccountDto) {
|
||||
const { username, status, password, ...userData } = data
|
||||
const { username, password, ...userData } = data
|
||||
|
||||
const result = await this.prisma.$transaction(async tx => {
|
||||
let user = await tx.user.upsert({
|
||||
@@ -53,7 +53,7 @@ export class AccountsService {
|
||||
|
||||
const dataToCreate = {
|
||||
username,
|
||||
status,
|
||||
status: AccountStatus.ACTIVE,
|
||||
password: hashedPassword,
|
||||
user_id: user.id,
|
||||
type: AccountType.PARTNER,
|
||||
|
||||
@@ -6,8 +6,9 @@ export class CreateAccountDto extends CreateUserDto {
|
||||
@IsString()
|
||||
username: string
|
||||
|
||||
@IsEnum(AccountStatus)
|
||||
status: AccountStatus
|
||||
@IsOptional()
|
||||
@IsEnum(AccountType)
|
||||
type?: AccountType
|
||||
|
||||
@IsString()
|
||||
password: string
|
||||
@@ -30,7 +31,7 @@ export class UpdateAccountDto {
|
||||
@IsString()
|
||||
password?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
user_id?: string
|
||||
// @IsOptional()
|
||||
// @IsString()
|
||||
// user_id?: string
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import { CreatePartnerDto, UpdatePartnerDto } from './dto/partner.dto'
|
||||
import { PartnersService } from './partners.service'
|
||||
|
||||
@@ -21,7 +21,7 @@ export class PartnersController {
|
||||
return this.partnersService.create(data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
@Patch(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdatePartnerDto) {
|
||||
return this.partnersService.update(id, data)
|
||||
}
|
||||
|
||||
@@ -8,15 +8,41 @@ export class PartnersService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll() {
|
||||
// const partners = await this.prisma.$transaction(async tx => {
|
||||
// const partners = await this.prisma.partner.findMany().then()
|
||||
// const preparedPartners = await partners.map(async partner => {
|
||||
// const relatedLicenseCount = await this.prisma.license.count({
|
||||
// where: {
|
||||
// partner_id: partner.id,
|
||||
// },
|
||||
// })
|
||||
|
||||
// return {
|
||||
// ...partner,
|
||||
// remained_license: Math.min((partner?.licenseQuota, 0) - relatedLicenseCount, 0),
|
||||
// }
|
||||
// })
|
||||
// console.log(preparedPartners)
|
||||
const partners = await this.prisma.partner.findMany()
|
||||
|
||||
return ResponseMapper.list(partners)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const partner = await this.prisma.partner.findUnique({
|
||||
where: { id },
|
||||
const [partner, relatedLicenseCount] = await this.prisma.$transaction([
|
||||
this.prisma.partner.findUnique({
|
||||
where: { id },
|
||||
}),
|
||||
this.prisma.license.count({
|
||||
where: {
|
||||
partner_id: id,
|
||||
},
|
||||
}),
|
||||
])
|
||||
return ResponseMapper.single({
|
||||
...partner,
|
||||
remained_license: Math.min((partner?.license_quota, 0) - relatedLicenseCount, 0),
|
||||
})
|
||||
return ResponseMapper.single(partner)
|
||||
}
|
||||
|
||||
async create(data: CreatePartnerDto) {
|
||||
|
||||
Reference in New Issue
Block a user