From 6a4068b44440967f58ff21921ff3fbecc3bba6a5 Mon Sep 17 00:00:00 2001 From: Saleem Hadad Date: Mon, 27 Nov 2023 16:58:26 +0400 Subject: [PATCH] Fix parsing date from sms --- app/BusinessLogic/SmsTemplateDetector.php | 11 +++++++---- config/finance.php | 14 +++++++------- tests/Feature/SmsTransactionProcessorTest.php | 3 ++- tests/Unit/SmsTemplateDetectorTest.php | 4 ++-- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/BusinessLogic/SmsTemplateDetector.php b/app/BusinessLogic/SmsTemplateDetector.php index 692d365..9a78f0d 100644 --- a/app/BusinessLogic/SmsTemplateDetector.php +++ b/app/BusinessLogic/SmsTemplateDetector.php @@ -45,6 +45,12 @@ private function extractSmsInformation($template, $sms): array if(empty($matchedParts[$i])) { continue; } + if($keys[$i] == "date") { + $date = $matchedParts[$i]; + $date = str_replace("/", "-", $date); + $smsInformation["datetime"] = $date; + } + $smsInformation[$keys[$i]] = $matchedParts[$i]; } @@ -96,10 +102,7 @@ private function extractPlaceholdersKeys($string): mixed */ public function getMaskedSmsTemplate(mixed $template): string|array|null { - // special case for datetime - $templateCopy = str_replace("{datetime}", "(.*?(?=\.))", $template); - - return preg_replace("/\{.*?}/", "(.*?)", $templateCopy); + return preg_replace("/\{.*?}/", "(.*?)", $template); } } diff --git a/config/finance.php b/config/finance.php index bbf7464..2d66223 100644 --- a/config/finance.php +++ b/config/finance.php @@ -30,13 +30,13 @@ 'Purchase of AED {amount} with {card} at {brand},', 'Payment of AED {amount} to {brand} with {card}.', '{brand} of AED {amount} has been credited into ', - 'AED {amount} has been debited from {account} using {card} at {brand} on {datetime}.', - '{brand} of AED {amount} has been credited to your {account} on {datetime}.', - 'Your {brand} of AED {amount} has been credited to your {account} on {datetime}.', - 'Outward {brand} of AED {amount} is debited from your {account}. Your {card} as of {datetime}.', - 'An ATM cash {brand} of AED{amount} has been debited from your {account} on {datetime}.', - '{brand} PAYMENT for {card} via MOBAPP of AED {amount} was debited from {account}.', - 'Your Cr.Card {card} was used for AED{amount} on{datetime}at {brand},{ignore}. {ignore}', + 'AED {amount} has been debited from {account} using {card} at {brand} on {date} {time}.', + '{brand} of AED {amount} has been credited to your {account} on {date} {time}.', + 'Your {brand} of AED {amount} has been credited to your {account} on {date} {time}.', + 'Outward {brand} of AED {amount} is debited from your {account}. Your {card} as of {date} {time}.', + 'An ATM cash {brand} of AED{amount} has been debited from your {account} on {date} {time}.', + '{brand} PAYMENT for {card} via MOBAPP of AED {amount} was debited from {date} {time}.', + 'Your Cr.Card {card} was used for AED{amount} on {date} {time} at {brand},{ignore}. {ignore}', ], 'reports' => [ (new SectionDivider)->withTitle("🎖️ Account Overview"), diff --git a/tests/Feature/SmsTransactionProcessorTest.php b/tests/Feature/SmsTransactionProcessorTest.php index d59797b..6edc310 100644 --- a/tests/Feature/SmsTransactionProcessorTest.php +++ b/tests/Feature/SmsTransactionProcessorTest.php @@ -126,11 +126,12 @@ public function it_creates_transaction_with_provided_datetime_if_passed_and_vali { $knownBrand = Brand::factory()->create(['name' => 'someBrand']); - $sms = "AED 5.65 has been debited from account 8118 using debit card at someBrand on 25-06-2022 13:29. Your avl"; + $sms = "AED 5.65 has been debited from account 8118 using debit card at someBrand on 25-06-2022 13:29."; $sut = app(SmsTransactionProcessor::class); $sut->process($sms); + $smsFromDB = Sms::first(); $this->assertEquals($knownBrand->name, $smsFromDB->transaction->brand->name); $this->assertEquals('5.65', $smsFromDB->transaction->amount); diff --git a/tests/Unit/SmsTemplateDetectorTest.php b/tests/Unit/SmsTemplateDetectorTest.php index 904ad5d..e36080e 100644 --- a/tests/Unit/SmsTemplateDetectorTest.php +++ b/tests/Unit/SmsTemplateDetectorTest.php @@ -36,12 +36,12 @@ public function it_returns_correct_matched_template_with_extracted_data() 'brand' => 'InternetProvider', ] ], - 'AED {amount} has been debited from {account} using {card} at {brand} on {datetime}.' => [ + 'AED {amount} has been debited from {account} using {card} at {brand} on {date} {time}.' => [ 'message' => 'AED 100 has been debited from SavingsAccount using DebitCard at Supermarket on 25-12-2023 14:00.', 'expectedData' => [ 'amount' => '100', 'brand' => 'Supermarket', - 'datetime' => '25-12-2023 14:00' + 'datetime' => '25-12-2023' ] ], ];