Skip to content

Commit

Permalink
More customer fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aguilaair committed Sep 11, 2021
1 parent 7aef785 commit ca5a194
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
18 changes: 9 additions & 9 deletions lib/utils/getCustomerDetailsFromMetadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ Future<PapercupsCustomer> 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.
Expand Down
30 changes: 23 additions & 7 deletions lib/utils/getPastCustomerMessages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Future<Map<String, dynamic>> getPastCustomerMessages(
client = Client();
}
List<PapercupsMessage> rMsgs = [];
PapercupsCustomer newCust;

try {
// Get messages.
Expand All @@ -27,10 +28,17 @@ Future<Map<String, dynamic>> 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"],
Expand All @@ -47,16 +55,20 @@ Future<Map<String, dynamic>> 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,
),
);
});
// Get the customer details.
var customerData = data["customer"];
c = PapercupsCustomer(
newCust = PapercupsCustomer(
createdAt: parseDateFromUTC(customerData["created_at"]),
email: customerData["email"],
externalId: customerData["external_id"],
Expand All @@ -69,11 +81,15 @@ Future<Map<String, dynamic>> 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,
};
}

0 comments on commit ca5a194

Please sign in to comment.