add : reseller
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { UsersService } from '../users/providers/users.service';
|
||||
import { CreateResellerDto } from './dto/create-reseller.dto';
|
||||
import { ResellerRepository } from './repositories/reseller.repository';
|
||||
@@ -37,4 +37,47 @@ export class ResellerService {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async finResellerAgents(userId: string) {
|
||||
const reseller = await this.FindOneOrFailByUserId(userId)
|
||||
|
||||
return reseller.agents
|
||||
}
|
||||
|
||||
async FindOneOrFailByUserId(userId: string) {
|
||||
const reseller = await this.resellerRepository.findOne({
|
||||
where: { owner: { id: userId } },
|
||||
relations: { agents: true }
|
||||
})
|
||||
|
||||
if (!reseller) {
|
||||
throw new NotFoundException()
|
||||
}
|
||||
|
||||
return reseller
|
||||
}
|
||||
|
||||
async createResellerAgent(userId: string, userTobeAgent: string) {
|
||||
const reseller = await this.FindOneOrFailByUserId(userId)
|
||||
|
||||
const { user } = await this.usersService.findOneById(userTobeAgent)
|
||||
|
||||
user.reseller = reseller
|
||||
|
||||
await this.usersService.save(user)
|
||||
|
||||
return user
|
||||
}
|
||||
|
||||
async removeResellerAgent(userId: string, agentToBeDeleted: string) {
|
||||
await this.FindOneOrFailByUserId(userId)
|
||||
|
||||
const { user } = await this.usersService.findOneById(agentToBeDeleted)
|
||||
|
||||
user.reseller = undefined
|
||||
|
||||
await this.usersService.save(user)
|
||||
|
||||
return user
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user