Skip to content

Commit

Permalink
Merge pull request #53 from saleem-hadad/fix/parsing-date-from-sms
Browse files Browse the repository at this point in the history
Fix parsing date from sms
  • Loading branch information
saleem-hadad authored Nov 27, 2023
2 parents 567b362 + 6a4068b commit 640bbd2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
11 changes: 7 additions & 4 deletions app/BusinessLogic/SmsTemplateDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down Expand Up @@ -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);
}
}

14 changes: 7 additions & 7 deletions config/finance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/SmsTransactionProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/SmsTemplateDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
],
];
Expand Down

0 comments on commit 640bbd2

Please sign in to comment.