From ca5a1943049c5393e5d71e8df64a6347b9b53c01 Mon Sep 17 00:00:00 2001 From: Aguilaair Date: Sat, 11 Sep 2021 19:02:18 +0200 Subject: [PATCH] More customer fixes --- lib/utils/getCustomerDetailsFromMetadata.dart | 18 +++++------ lib/utils/getPastCustomerMessages.dart | 30 ++++++++++++++----- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/utils/getCustomerDetailsFromMetadata.dart b/lib/utils/getCustomerDetailsFromMetadata.dart index db1223c..729bf1e 100644 --- a/lib/utils/getCustomerDetailsFromMetadata.dart +++ b/lib/utils/getCustomerDetailsFromMetadata.dart @@ -32,20 +32,20 @@ Future getCustomerDetailsFromMetadata( // Generating the Papercups customer data. c = PapercupsCustomer( id: data["customer_id"], - externalId: c == null ? null : c.externalId, - email: c == null ? null : c.email, - createdAt: c == null ? null : c.createdAt, - firstSeen: c == null ? null : c.firstSeen, - lastSeenAt: c == null ? null : c.lastSeenAt, - name: c == null ? null : c.name, - phone: c == null ? null : c.phone, - updatedAt: c == null ? null : c.updatedAt, + externalId: p.customer?.externalId, + email: p.customer?.email, + createdAt: c?.createdAt, + firstSeen: c?.firstSeen, + lastSeenAt: c?.lastSeenAt, + name: p.customer?.name, + phone: p.customer?.otherMetadata?["phoneNumber"], + updatedAt: c?.updatedAt, ); } catch (e) { throw (e); } // Function to set the client. - if(c.id != null){ + if (c.id != null) { sc(c); } // Closing HTTP client. diff --git a/lib/utils/getPastCustomerMessages.dart b/lib/utils/getPastCustomerMessages.dart index 879c45d..a800d49 100644 --- a/lib/utils/getPastCustomerMessages.dart +++ b/lib/utils/getPastCustomerMessages.dart @@ -15,6 +15,7 @@ Future> getPastCustomerMessages( client = Client(); } List rMsgs = []; + PapercupsCustomer newCust; try { // Get messages. @@ -27,10 +28,17 @@ Future> getPastCustomerMessages( ); // JSON Decode. - var data = jsonDecode(res.body)["data"][0]; + var data = jsonDecode(res.body)["data"]; + try { + data = data[0]; + } catch (e) { + return { + "msgs": rMsgs, + "cust": c, + }; + } - // For every message generate a PapercupsMessage object and add it to the list. - data["messages"].forEach((val) { + data[0]["messages"].forEach((val) { rMsgs.add( PapercupsMessage( accountId: val["account_id"], @@ -47,8 +55,12 @@ Future> getPastCustomerMessages( email: val["user"]["email"], id: val["user"]["id"], role: val["user"]["role"], - fullName: (val["user"]["full_name"] != null) ? val["user"]["full_name"] : null, - profilePhotoUrl: (val["user"]["profile_photo_url"] != null) ? val["user"]["profile_photo_url"] : null, + fullName: (val["user"]["full_name"] != null) + ? val["user"]["full_name"] + : null, + profilePhotoUrl: (val["user"]["profile_photo_url"] != null) + ? val["user"]["profile_photo_url"] + : null, ) : null, ), @@ -56,7 +68,7 @@ Future> getPastCustomerMessages( }); // Get the customer details. var customerData = data["customer"]; - c = PapercupsCustomer( + newCust = PapercupsCustomer( createdAt: parseDateFromUTC(customerData["created_at"]), email: customerData["email"], externalId: customerData["external_id"], @@ -69,11 +81,15 @@ Future> getPastCustomerMessages( ); } catch (e) { print("An error ocurred while getting past customer data."); + return { + "msgs": [], + "cust": c, + }; } client.close(); // Return messages and customer details. return { "msgs": rMsgs, - "cust": c, + "cust": newCust, }; }