add : reseller
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsNotEmpty, IsString } from "class-validator";
|
||||
|
||||
export class CreateResellerAgentDto {
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
userId: string;
|
||||
}
|
||||
@@ -1,29 +1,60 @@
|
||||
import { Controller, Get, Post, Body } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Body, Delete } from '@nestjs/common';
|
||||
import { ResellerService } from './reseller.service';
|
||||
import { ApiOperation } from '@nestjs/swagger';
|
||||
import { PermissionsDec } from '../../common/decorators/permission.decorator';
|
||||
import { PermissionEnum } from '../users/enums/permission.enum';
|
||||
import { CreateResellerDto } from './dto/create-reseller.dto';
|
||||
|
||||
import { AuthGuards } from '../../common/decorators/auth-guard.decorator';
|
||||
import { AdminRoute } from '../../common/decorators/admin.decorator';
|
||||
import { ResellerRoute } from '../../common/decorators/reseller.decorator';
|
||||
import { UserDec } from '../../common/decorators/user.decorator';
|
||||
import { CreateResellerAgentDto } from './dto/create-reseller-agent.dto';
|
||||
|
||||
@Controller('reseller')
|
||||
@AuthGuards()
|
||||
export class ResellerController {
|
||||
constructor(
|
||||
private readonly resellerService: ResellerService,
|
||||
|
||||
) { }
|
||||
|
||||
|
||||
@ApiOperation({ summary: "create Reseller ==> admin route" })
|
||||
@AdminRoute()
|
||||
@PermissionsDec(PermissionEnum.RESELLER)
|
||||
@Post("reseller")
|
||||
@Post()
|
||||
createReseller(@Body() dto: CreateResellerDto) {
|
||||
return this.resellerService.create(dto)
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "get Resellers ==> admin route" })
|
||||
@AdminRoute()
|
||||
@PermissionsDec(PermissionEnum.RESELLER)
|
||||
@Get("reseller")
|
||||
@Get()
|
||||
getResellers() {
|
||||
return this.resellerService.findAll()
|
||||
}
|
||||
|
||||
// =========== Reseller-->Agent =============
|
||||
|
||||
@ApiOperation({ summary: "get Reseller agents ==> reseller route" })
|
||||
@ResellerRoute()
|
||||
@Get('agents')
|
||||
getResellerAgents(@UserDec("id") userId: string) {
|
||||
return this.resellerService.finResellerAgents(userId)
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Create Reseller agent ==> reseller route" })
|
||||
@ResellerRoute()
|
||||
@Post('agents')
|
||||
createResellerAgent(@UserDec("id") userId: string, @Body() dto: CreateResellerAgentDto) {
|
||||
return this.resellerService.createResellerAgent(userId, dto.userId)
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Remove Reseller agent ==> reseller route" })
|
||||
@ResellerRoute()
|
||||
@Delete('agents')
|
||||
deleteResellerAgent(@UserDec("id") userId: string, @Body() dto: CreateResellerAgentDto) {
|
||||
return this.resellerService.removeResellerAgent(userId, dto.userId)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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