add: role in jwt

This commit is contained in:
ilham 2021-12-09 21:30:10 +07:00
parent 19c951c331
commit a057088a93
2 changed files with 56 additions and 38 deletions

View File

@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import { hashPassword } from '../helper/hash_password';
import { JwtService } from '@nestjs/jwt';
import { User } from '../users/entities/user.entity';
@Injectable()
export class AuthService {
@ -22,10 +23,11 @@ export class AuthService {
return null;
}
async login(user: any) {
async login(user: User) {
const payload = {
username: user.username,
sub: user.userId,
sub: user.id,
role: user.roles.name,
};
return {

View File

@ -1,4 +1,10 @@
import { forwardRef, HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import {
forwardRef,
HttpException,
HttpStatus,
Inject,
Injectable,
} from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { Connection, EntityNotFoundError, Repository } from 'typeorm';
@ -10,14 +16,18 @@ import { CoaService } from 'src/transaction/coa.service';
import { balanceType, coaType } from 'src/helper/enum-list';
import { RoleService } from 'src/configurable/roles.service';
import { InputCoaDto } from 'src/transaction/dto/input-coa.dto';
import * as uuid from "uuid";
import * as uuid from 'uuid';
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User)
private usersRepository: Repository<User>,
@Inject(forwardRef(() => CoaService))
@Inject(
forwardRef(() => {
return CoaService;
}),
)
private coaService: CoaService,
private roleService: RoleService,
private connection: Connection,
@ -27,51 +37,54 @@ export class UsersService {
const roles = await this.roleService.findOne(createUserDto.roleId);
const superior = await this.findByUsername(currentUser.username);
const salt = randomStringGenerator();
let userData = new User();
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);
let dataCoaWallet = new InputCoaDto();
const result = await manager.insert(User, userData);
const dataCoaWallet = new InputCoaDto();
dataCoaWallet.user = userData;
dataCoaWallet.balanceType = balanceType.CREDIT;
dataCoaWallet.type = coaType.WALLET;
dataCoaWallet.coaEntityManager = manager;
if(createUserDto.superior){
let dataCoaAP = new InputCoaDto();
if (createUserDto.superior) {
const dataCoaAP = new InputCoaDto();
dataCoaAP.user = userData;
dataCoaAP.balanceType = balanceType.CREDIT;
dataCoaAP.relatedUserId = superior.id;
dataCoaAP.type = coaType.ACCOUNT_PAYABLE;
dataCoaAP.coaEntityManager = manager;
let dataCoaAR = new InputCoaDto();
const dataCoaAR = new InputCoaDto();
dataCoaAR.user = userData;
dataCoaAR.balanceType = balanceType.DEBIT;
dataCoaAR.relatedUserId = superior.id;
dataCoaAR.type = coaType.ACCOUNT_RECEIVABLE;
dataCoaAR.coaEntityManager = manager;
await this.coaService.create(dataCoaAP);
await this.coaService.create(dataCoaAR);
}
await this.coaService.create(dataCoaWallet);
})
await this.coaService.create(dataCoaWallet);
});
return userData;
}
findAll(page:number) {
findAll(page: number) {
return this.usersRepository.findAndCount({
skip: page * 10,
take: 10,
@ -81,12 +94,12 @@ export class UsersService {
});
}
findByRoles(relationId:string,page:number) {
findByRoles(relationId: string, page: number) {
return this.usersRepository.findAndCount({
skip: page * 10,
take: 10,
where:{
roles:relationId
where: {
roles: relationId,
},
order: {
updatedAt: 'DESC',
@ -96,7 +109,7 @@ export class UsersService {
async findExist(id: string) {
try {
return await this.usersRepository.findOneOrFail(id);
return await this.usersRepository.findOneOrFail(id);
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
@ -114,9 +127,9 @@ export class UsersService {
async findByUsername(username: string) {
try {
return await this.usersRepository.findOneOrFail({
username:username
});
return await this.usersRepository.findOneOrFail({
username: username,
});
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
@ -133,17 +146,19 @@ export class UsersService {
}
async findOne(id: string) {
const coa = await this.coaService.findByUser(id,coaType.WALLET);
const coa = await this.coaService.findByUser(id, coaType.WALLET);
try {
const userData = await this.usersRepository.findOneOrFail({
where: {
id: id
const userData = await this.usersRepository.findOneOrFail({
where: {
id: id,
},
relations:["roles","superior"]
relations: ['roles', 'superior'],
});
return {
...userData,
wallet:coa.amount,
wallet: coa.amount,
};
} catch (e) {
if (e instanceof EntityNotFoundError) {
@ -207,6 +222,7 @@ export class UsersService {
where: {
username,
},
relations: ['roles'],
});
}
}