feat: add update partner endpoint

This commit is contained in:
caturbgs
2021-12-14 15:18:25 +07:00
parent 26e7401163
commit 6b1352e671
4 changed files with 126 additions and 19 deletions

View File

@@ -216,7 +216,7 @@ export class UsersService {
}
}
async update(id: string, updateUserDto: UpdateUserDto) {
async update(id: string, updateUserDto: UpdateUserDto, currentUser: any) {
try {
await this.usersRepository.findOneOrFail(id);
} catch (e) {
@@ -233,9 +233,32 @@ export class UsersService {
}
}
// const result = await this.usersRepository.update(id, updateUserDto);
const check = await this.usersRepository.findOne({
username: updateUserDto.username,
id: Not(id),
});
return this.usersRepository.findOneOrFail(id);
if (check) {
throw new HttpException(
{
statusCode: HttpStatus.NOT_ACCEPTABLE,
error: 'Username Already Exist',
},
HttpStatus.NOT_FOUND,
);
}
const userData = new User();
userData.id = id;
userData.username = updateUserDto.username;
userData.partner = updateUserDto.partner;
await this.connection.transaction(async (manager) => {
const result = await manager.update(User, { id: id }, userData);
});
return userData;
}
async remove(id: string) {
@@ -266,4 +289,27 @@ export class UsersService {
relations: ['roles'],
});
}
async findOneByPartner(partnerId: string) {
try {
return this.usersRepository.findOneOrFail({
relations: ['roles'],
where: {
partner: partnerId,
},
});
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
{
statusCode: HttpStatus.NOT_FOUND,
error: 'Data not found',
},
HttpStatus.NOT_FOUND,
);
} else {
throw e;
}
}
}
}