refactor user accounts structure
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import { ComplexSelect } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateComplexDto, UpdateComplexDto } from './dto/complex.dto'
|
||||
|
||||
@Injectable()
|
||||
export class BusinessActivityComplexesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
defaultSelect = {
|
||||
id: true,
|
||||
name: true,
|
||||
address: true,
|
||||
tax_id: true,
|
||||
created_at: true,
|
||||
} as ComplexSelect
|
||||
|
||||
async findAll(user_id: string, business_activity_id: string) {
|
||||
const accounts = await this.prisma.complex.findMany({
|
||||
where: {
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
user: {
|
||||
id: user_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.list(accounts)
|
||||
}
|
||||
|
||||
async findOne(user_id: string, business_activity_id: string, id: string) {
|
||||
const account = await this.prisma.complex.findUnique({
|
||||
where: {
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
user: {
|
||||
id: user_id,
|
||||
},
|
||||
},
|
||||
id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return ResponseMapper.single(account)
|
||||
}
|
||||
|
||||
async create(business_id: string, data: CreateComplexDto) {
|
||||
const account = await this.prisma.complex.create({
|
||||
data: {
|
||||
...data,
|
||||
business_activity: {
|
||||
connect: {
|
||||
id: business_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return ResponseMapper.create(account)
|
||||
}
|
||||
|
||||
async update(
|
||||
user_id: string,
|
||||
business_activity_id: string,
|
||||
id: string,
|
||||
data: UpdateComplexDto,
|
||||
) {
|
||||
const account = await this.prisma.complex.update({
|
||||
where: {
|
||||
business_activity: {
|
||||
id: business_activity_id,
|
||||
user: {
|
||||
id: user_id,
|
||||
},
|
||||
},
|
||||
id,
|
||||
},
|
||||
data,
|
||||
})
|
||||
return ResponseMapper.update(account)
|
||||
}
|
||||
|
||||
// async delete(id: string) {
|
||||
// await this.prisma.complex.delete({ where: { id } })
|
||||
// return ResponseMapper.delete()
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user