add: member by superrior

This commit is contained in:
ilham 2021-12-10 01:02:33 +07:00
parent 09c84f2cf5
commit 77d9a95d8a
6 changed files with 56 additions and 34 deletions

View File

@ -2,17 +2,15 @@ import {
Entity,
Column,
PrimaryGeneratedColumn,
UpdateDateColumn,
DeleteDateColumn,
VersionColumn,
CreateDateColumn,
ManyToOne,
OneToMany,
} from 'typeorm';
import { ProductCategories } from './product-category.entity';
import { BaseModel } from '../../config/basemodel.entity';
import { Product } from './product.entity';
@Entity()
export class ProductSubCategories extends BaseModel{
export class ProductSubCategories extends BaseModel {
@PrimaryGeneratedColumn('uuid')
id: string;
@ -21,4 +19,7 @@ export class ProductSubCategories extends BaseModel{
@ManyToOne(() => ProductCategories, (categories) => categories.subCategories)
category: ProductCategories;
@OneToMany(() => Product, (product) => product.subCategories)
product: Product;
}

View File

@ -13,7 +13,7 @@ import { ProductSubCategories } from './product-sub-category.entity';
import { BaseModel } from '../../config/basemodel.entity';
@Entity()
export class Product extends BaseModel{
export class Product extends BaseModel {
@PrimaryGeneratedColumn('uuid')
id: string;
@ -30,13 +30,17 @@ export class Product extends BaseModel{
price: number;
@Column({
nullable:true
nullable: true,
})
basePrice: number;
@ManyToOne(
() => ProductSubCategories,
(subCategories) => subCategories.category,
() => {
return ProductSubCategories;
},
(subCategories) => {
return subCategories.product;
},
)
subCategories: ProductSubCategories;
}

View File

@ -55,8 +55,9 @@ export class ProductService {
findAllByCategories(page, categories) {
return this.productRepository.findAndCount({
where: {
subCategories: categories,
join: {
alias: 'subCategories',
innerJoin: { subCategories: 'roles.users' },
},
skip: page * 10,
take: 10,
@ -130,7 +131,7 @@ export class ProductService {
endDate: updatePriceProductDto.endDate,
});
return
return true;
}
async remove(id: string) {

View File

@ -1,9 +1,5 @@
import { Roles } from 'src/configurable/entities/roles.entity';
import {
Entity,
Column,
PrimaryGeneratedColumn, ManyToOne,
} from 'typeorm';
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm';
import { BaseModel } from '../../config/basemodel.entity';
import { hashPassword } from '../../helper/hash_password';
@ -33,7 +29,7 @@ export class User extends BaseModel {
},
)
superior: User;
@ManyToOne(
() => {
return Roles;

View File

@ -9,12 +9,12 @@ import {
ParseUUIDPipe,
HttpStatus,
Query,
Request
Request,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import {Public} from "../auth/public.decorator";
import { Public } from '../auth/public.decorator';
@Controller({
path: 'users',
@ -24,12 +24,9 @@ export class UsersController {
constructor(private readonly usersService: UsersService) {}
@Post()
async create(
@Request() req,
@Body() createUserDto: CreateUserDto
) {
async create(@Request() req, @Body() createUserDto: CreateUserDto) {
return {
data: await this.usersService.create(createUserDto,req.user),
data: await this.usersService.create(createUserDto, req.user),
statusCode: HttpStatus.CREATED,
message: 'success',
};
@ -48,13 +45,22 @@ export class UsersController {
};
}
@Get('find-by-supperior')
async findBySuperrior(@Request() req, @Query('page') page: number) {
return {
data: await this.usersService.findBySuperrior(req.user.id, page),
statusCode: HttpStatus.OK,
message: 'success',
};
}
@Get('find-by-roles/:id')
async findByRoles(
@Param('id', ParseUUIDPipe) id: string,
@Query('page') page: number
) {
@Param('id', ParseUUIDPipe) id: string,
@Query('page') page: number,
) {
return {
data: await this.usersService.findByRoles(id,page),
data: await this.usersService.findByRoles(id, page),
statusCode: HttpStatus.OK,
message: 'success',
};

View File

@ -41,11 +41,11 @@ export class UsersService {
const userData = new User();
userData.id = uuid.v4();
(userData.username = createUserDto.username),
(userData.password = await hashPassword(createUserDto.password, salt)),
(userData.salt = salt),
(userData.superior = superior),
(userData.roles = roles);
userData.username = createUserDto.username;
userData.password = await hashPassword(createUserDto.password, salt);
userData.salt = salt;
userData.superior = superior;
userData.roles = roles;
await this.connection.transaction(async (manager) => {
const result = await manager.insert(User, userData);
@ -107,6 +107,20 @@ export class UsersService {
});
}
findBySuperrior(superriorId: string, page: number) {
return this.usersRepository.findAndCount({
skip: page * 10,
take: 10,
where: {
superior: superriorId,
},
relations: ['roles'],
order: {
updatedAt: 'DESC',
},
});
}
async findExist(id: string) {
try {
return await this.usersRepository.findOneOrFail(id);