Skip to content

Commit 6d24e6c

Browse files
author
Jhon
committed
feat(backend): ajuste na busca por ultimas compras
1 parent 3f9318b commit 6d24e6c

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

backend/app/Modules/ChatBot/UseCase/LastPurchaseChat/LastPurchaseChatUseCase.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public function execute(string $chatId): void
2020
if (empty($lastPurchase['purchase'])) {
2121
ResponseChat::interactWithUser($chatId, '😞 Poxa, não encontrei nenhuma compra 😞');
2222
}
23-
ResponseChat::interactWithUser($chatId, $this->formatToUser($lastPurchase));
23+
$this->showToUser($lastPurchase, $chatId);
2424
}
2525

26-
protected function formatToUser(array $lastPurchase): string
26+
protected function showToUser(array $lastPurchase, string $chatId): void
2727
{
2828
$message = "📄 Resumo da compra: \n\n";
2929
$message .= "📅 Data da compra: " . new DateTime($lastPurchase['purchase']->purchase_date)->format('d/m/Y') . "\n";
@@ -32,18 +32,29 @@ protected function formatToUser(array $lastPurchase): string
3232
$message .= "📊 Valor do desconto: R$ " . number_format($lastPurchase['purchase']->discount_value, 2, ',', '.') . "\n";
3333
$message .= "💰 Valor total: R$ " . number_format($lastPurchase['purchase']->total_value, 2, ',', '.') . "\n";
3434
$message .= "━━━━━━━━━━━━━━━━━\n\n";
35-
$message .= "📝 Produtos da Compras:\n\n";
35+
$message .= "📝 Produtos da Compras:\n";
3636

37-
return $message;
38-
foreach ($lastPurchase['products'] as $produto) {
39-
$name = substr($produto->name, 0, 30);
37+
ResponseChat::interactWithUser($chatId, $message);
38+
39+
$products = array_chunk($lastPurchase['products'], 30);
40+
41+
foreach ($products as $product) {
42+
ResponseChat::interactWithUser($chatId, $this->formatProducts($product));
43+
}
44+
}
45+
46+
protected function formatProducts(array $products): string
47+
{
48+
$message = '';
49+
foreach ($products as $product) {
50+
$name = substr($product->name, 0, 30);
4051
if (strlen($name) === 30) {
4152
$name .= "...";
4253
}
4354
$name = str_replace(' ', "\u{00A0}", $name);
44-
$value = number_format($produto->total_value, 2, ',', '.');
55+
$value = number_format($product->total_value, 2, ',', '.');
4556

46-
$message .= "🔹 $name\n $produto->quantity $produto->unit - R$ $value";
57+
$message .= "🔹 $name\n $product->quantity $product->unit - R$ $value";
4758
$message .= " ━━━━━━━━━━━━━━━━━\n";
4859
}
4960
$message .= "😅 Ufa, a lista acabou!\n";

0 commit comments

Comments
 (0)