add e2e with add groups
This commit is contained in:
parent
45a69eea8a
commit
b599e79273
19
api.http
19
api.http
@ -13,3 +13,22 @@ Content-Type: application/json
|
||||
GET http://localhost:3000/users
|
||||
Accept: application/json
|
||||
Authorization: Bearer {{auth_token}}
|
||||
|
||||
### POST create user
|
||||
POST http://localhost:3000/users
|
||||
Content-Type: application/json
|
||||
Accept: application/json
|
||||
Authorization: Bearer {{auth_token}}
|
||||
|
||||
{
|
||||
"username": "test1",
|
||||
"password": "123456",
|
||||
"email": "test1@gmail.com",
|
||||
"groups": [
|
||||
{
|
||||
"id": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -13,5 +13,5 @@ export class CreateUserDto {
|
||||
password: string;
|
||||
|
||||
@IsArray()
|
||||
roles: string[];
|
||||
groups: [{ id: number }];
|
||||
}
|
||||
|
||||
@ -28,8 +28,7 @@ export class UserController {
|
||||
create(
|
||||
@Body(new ValidationPipe()) createUserDto: CreateUserDto,
|
||||
): Promise<User> {
|
||||
const { roles, ...user } = createUserDto;
|
||||
return this.userService.create(user, roles);
|
||||
return this.userService.create(createUserDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
|
||||
@ -5,11 +5,13 @@ import { UserController } from './user.controller';
|
||||
import { User } from '../entity/user';
|
||||
import { UserGroup } from '../entity/user-group';
|
||||
import { UserRole } from '../entity/user-role';
|
||||
import { UserGroupService } from './user-group.service';
|
||||
import { UserRoleService } from './user-role.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([User, UserGroup, UserRole])],
|
||||
providers: [UserService],
|
||||
providers: [UserService, UserGroupService, UserRoleService],
|
||||
controllers: [UserController],
|
||||
exports: [UserService],
|
||||
exports: [UserService, UserGroupService, UserRoleService],
|
||||
})
|
||||
export class UserModule {}
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { User } from '../entity/user';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { FindOptionsRelations } from 'typeorm/find-options/FindOptionsRelations';
|
||||
import { UserGroupService } from './user-group.service';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UserGroup } from '../entity/user-group';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
@ -29,14 +31,25 @@ export class UserService {
|
||||
return this.usersRepository.findOne({ where: { username }, relations });
|
||||
}
|
||||
|
||||
async create(user: Partial<User>, groups?: string[]): Promise<User> {
|
||||
async create(userDto: Partial<CreateUserDto>): Promise<User> {
|
||||
const { groups, ...user } = userDto;
|
||||
if (user.password) {
|
||||
user.password = await bcrypt.hash(user.password, 12);
|
||||
}
|
||||
const newUser = this.usersRepository.create(user);
|
||||
if (groups?.length) {
|
||||
|
||||
if (groups) {
|
||||
const userGroups: UserGroup[] = [];
|
||||
for (const group of groups) {
|
||||
const userGroup = await this.userGroupService.findOne(group.id);
|
||||
if (userGroup) {
|
||||
userGroups.push(userGroup);
|
||||
} else {
|
||||
throw new NotFoundException('User group does not exist:' + group.id);
|
||||
}
|
||||
}
|
||||
(user as Partial<User>).groups = userGroups;
|
||||
}
|
||||
const newUser = this.usersRepository.create(user);
|
||||
|
||||
return this.usersRepository.save(newUser);
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user