From 873ea9380a01ca7710db876d064492113b6d5587 Mon Sep 17 00:00:00 2001
From: Steve Reis <stevereis93@gmail.com>
Date: Tue, 11 Oct 2022 14:01:50 +0200
Subject: [PATCH] fix: Logout was always creating a DB record

---
 api/src/auth/auth.resolver.ts       |  4 ++--
 api/src/auth/auth.service.ts        |  6 +++---
 api/src/users/users.service.spec.ts |  6 +++---
 api/src/users/users.service.ts      | 15 +++++++++++++--
 4 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/api/src/auth/auth.resolver.ts b/api/src/auth/auth.resolver.ts
index 27c2694..bca4f7f 100644
--- a/api/src/auth/auth.resolver.ts
+++ b/api/src/auth/auth.resolver.ts
@@ -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(
diff --git a/api/src/auth/auth.service.ts b/api/src/auth/auth.service.ts
index fd21107..343f0ef 100644
--- a/api/src/auth/auth.service.ts
+++ b/api/src/auth/auth.service.ts
@@ -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);
diff --git a/api/src/users/users.service.spec.ts b/api/src/users/users.service.spec.ts
index 47fa24b..b4227b2 100644
--- a/api/src/users/users.service.spec.ts
+++ b/api/src/users/users.service.spec.ts
@@ -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);
     });
diff --git a/api/src/users/users.service.ts b/api/src/users/users.service.ts
index 773a770..2717251 100644
--- a/api/src/users/users.service.ts
+++ b/api/src/users/users.service.ts
@@ -1,6 +1,6 @@
 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,
-- 
GitLab