From 443d6f4bad5f924d19c413e5d78e9cde09a1d25f Mon Sep 17 00:00:00 2001
From: Bruno Muniz Azevedo Filho <bruno@elixxir.io>
Date: Mon, 18 Jul 2022 15:17:14 -0300
Subject: [PATCH] Fixes comments on MR #52

---
 Sources/Integration/Session/Session+UD.swift |  7 ++--
 Sources/Integration/Session/Session.swift    | 37 +++++++++-----------
 2 files changed, 20 insertions(+), 24 deletions(-)

diff --git a/Sources/Integration/Session/Session+UD.swift b/Sources/Integration/Session/Session+UD.swift
index 21514741..630d822a 100644
--- a/Sources/Integration/Session/Session+UD.swift
+++ b/Sources/Integration/Session/Session+UD.swift
@@ -42,11 +42,10 @@ extension Session {
                 switch $0 {
                 case .success(_):
                     self.username = value
-                    let query = Contact.Query(id: [self.client.bindings.myId])
 
-                    if var myself = try? self.dbManager.fetchContacts(query).first {
-                        myself.username = value
-                        _ = try? self.dbManager.saveContact(myself)
+                    if var me = try? self.myContact() {
+                        me.username = value
+                        _ = try? self.dbManager.saveContact(me)
                     }
 
                     completion(.success(nil))
diff --git a/Sources/Integration/Session/Session.swift b/Sources/Integration/Session/Session.swift
index fc69e656..ef6180d7 100644
--- a/Sources/Integration/Session/Session.swift
+++ b/Sources/Integration/Session/Session.swift
@@ -183,6 +183,15 @@ public final class Session: SessionType {
     }
 
     private func continueInitialization() throws {
+        var myContact = try self.myContact()
+        myContact.marshaled = client.bindings.meMarshalled
+        myContact.username = username
+        myContact.email = email
+        myContact.phone = phone
+        myContact.authStatus = .friend
+        myContact.isRecent = false
+        _ = try dbManager.saveContact(myContact)
+
         setupBindings()
         networkMonitor.start()
 
@@ -194,26 +203,6 @@ public final class Session: SessionType {
             }
             .store(in: &cancellables)
 
-        /// Create a contact for myself, for foreign key purposes
-        ///
-        if var myself = try? dbManager.fetchContacts(.init(id: [client.bindings.myId])).first {
-            myself.username = username
-            _ = try? dbManager.saveContact(myself)
-        } else {
-            _ = try? dbManager.saveContact(.init(
-                id: client.bindings.myId,
-                marshaled: client.bindings.meMarshalled,
-                username: username,
-                email: email,
-                phone: phone,
-                nickname: nil,
-                photo: nil,
-                authStatus: .friend,
-                isRecent: false,
-                createdAt: Date()
-            ))
-        }
-
         registerUnfinishedUploadTransfers()
         registerUnfinishedDownloadTransfers()
 
@@ -487,4 +476,12 @@ public final class Session: SessionType {
             }
             .store(in: &cancellables)
     }
+
+    func myContact() throws -> Contact {
+        if let contact = try dbManager.fetchContacts(.init(id: [client.bindings.myId])).first {
+            return contact
+        } else {
+            return try dbManager.saveContact(.init(id: client.bindings.myId))
+        }
+    }
 }
-- 
GitLab