Skip to content
Snippets Groups Projects
Commit 873ea938 authored by Steve Reis's avatar Steve Reis
Browse files

fix: Logout was always creating a DB record

parent b7f9dd3e
No related branches found
No related tags found
No related merge requests found
......@@ -92,9 +92,9 @@ export class AuthResolver {
if (user) {
this.logger.verbose(`${user.username} logged out`);
try {
if (this.engineService.has('logout')) {
if (this.engineService.has('logout'))
await this.engineService.logout(req);
}
this.authService.logout(user);
} catch (e) {
this.logger.warn(
......
......@@ -55,7 +55,7 @@ export class AuthService {
const hashRefresh = await this.getHash(refreshToken);
this.usersService.update(user.id, {
this.usersService.save(user.id, {
refreshToken: hashRefresh,
});
......@@ -71,8 +71,8 @@ export class AuthService {
*/
async logout(user: User): Promise<void> {
try {
if (user.id)
await this.usersService.update(user.id, { refreshToken: null });
if (!user || !user.id) return;
await this.usersService.update(user.id, { refreshToken: null });
} catch (err) {
//user not found or others errors
AuthService.logger.debug('Error while logging out user', err);
......
......@@ -65,12 +65,12 @@ describe('UsersService', () => {
});
});
describe('updateUser', () => {
it('should return an updated user', async () => {
describe('saveUser', () => {
it('should return an user', async () => {
const expectedUser = { ...user, ...updateData };
usersRepository.save.mockResolvedValue(expectedUser);
const result = await service.update('idThatExist', updateData);
const result = await service.save('idThatExist', updateData);
expect(result).toStrictEqual(expectedUser);
});
......
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Repository, UpdateResult } from 'typeorm';
import { UpdateUserInput } from './inputs/update-user.input';
import { User } from './models/user.model';
......@@ -37,7 +37,18 @@ export class UsersService {
* @param {UserDataUpdate} data - update params
* @returns The updated user.
*/
async update(id: string, data: UserDataUpdate): Promise<InternalUser> {
async update(id: string, data: UserDataUpdate): Promise<UpdateResult> {
return this.userRepository.update({ id }, data);
}
/**
* Saves user in the database.
* If user does not exist in the database then inserts, otherwise updates.
* @param {string} id - The id of the user to update.
* @param {UserDataUpdate} data - update params
* @returns The updated user.
*/
async save(id: string, data: UserDataUpdate): Promise<InternalUser> {
const updateData = {
id,
...data,
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment