From c0b5a02609aedcf80741877c5ed3f61ecb94b8c8 Mon Sep 17 00:00:00 2001 From: Werner Hahn Date: Sat, 30 Sep 2017 15:12:41 +0200 Subject: [PATCH] =?utf8?q?WebshopApi:=20Trigram=20Abh=C3=A4ngigkeit=20aufg?= =?utf8?q?el=C3=B6st.=20Wird=20nur=20genutzt,=20wenn=20auch=20installiert?= =?utf8?q?=20Tests=20dementsprechend=20angepasst=20und=20erweitert?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/DB/ShopOrder.pm | 67 ++++++++++++++++++++++++++++++++++++--------- t/shop/shop_order.t | 30 ++++++++++++++++---- 2 files changed, 79 insertions(+), 18 deletions(-) diff --git a/SL/DB/ShopOrder.pm b/SL/DB/ShopOrder.pm index 55dfc0640..e02969486 100644 --- a/SL/DB/ShopOrder.pm +++ b/SL/DB/ShopOrder.pm @@ -111,14 +111,19 @@ sub convert_to_sales_order { sub check_for_existing_customers { my ($self, %params) = @_; - - my $name = $self->billing_lastname ne '' ? $self->billing_firstname . " " . $self->billing_lastname : ''; - my $lastname = $self->billing_lastname ne '' ? "%" . $self->billing_lastname . "%" : ''; - my $company = $self->billing_company ne '' ? "%" . $self->billing_company . "%" : ''; - my $street = $self->billing_street ne '' ? $self->billing_street : ''; - - # Fuzzysearch for street to find e.g. "Dorfstrasse - Dorfstr. - Dorfstraße" - my $fs_query = <billing_lastname ne '' ? $self->billing_firstname . " " . $self->billing_lastname : ''; + my $lastname = $self->billing_lastname ne '' ? "%" . $self->billing_lastname . "%" : ''; + my $company = $self->billing_company ne '' ? "%" . $self->billing_company . "%" : ''; + my $street = $self->billing_street ne '' ? $self->billing_street : ''; + my $street_not_fuzzy = $self->billing_street ne '' ? "%" . $self->billing_street . "%" : ''; + my $zipcode = $self->billing_street ne '' ? $self->billing_zipcode : ''; + my $email = $self->billing_street ne '' ? $self->billing_email : ''; + + if($self->check_trgm) { + # Fuzzysearch for street to find e.g. "Dorfstrasse - Dorfstr. - Dorfstraße" + my $fs_query = <billing_zipcode, $street, $self->billing_zipcode, $self->billing_email); + my @values = ($lastname, $company, $self->billing_zipcode, $street, $self->billing_zipcode, $self->billing_email); - my $customers = SL::DB::Manager::Customer->get_objects_from_sql( - sql => $fs_query, - args => \@values, - ); + $customers = SL::DB::Manager::Customer->get_objects_from_sql( + sql => $fs_query, + args => \@values, + ); + }else{ + # If trgm extension is not installed + $customers = SL::DB::Manager::Customer->get_all( + where => [ + or => [ + and => [ + or => [ 'name' => { ilike => $lastname }, + 'name' => { ilike => $company }, + ], + 'zipcode' => { ilike => $zipcode }, + ], + and => [ + and => [ 'street' => { ilike => $street_not_fuzzy }, + 'zipcode' => { ilike => $zipcode }, + ], + ], + or => [ 'email' => { ilike => $email } ], + ], + ], + ); + } return $customers; } @@ -208,6 +234,17 @@ sub compare_to { return $result || ($self->id <=> $other->id); } +sub check_trgm { + my ( $self ) = @_; + + my $dbh = $::form->get_standard_dbh(); + my $sql = "SELECT installed_version FROM pg_available_extensions WHERE name = 'pg_trgm'"; + my @version = selectall_hashref_query($::form, $dbh, $sql); + + return 1 if($version[0]->{installed_version}); + return 0; +} + 1; __END__ @@ -244,6 +281,10 @@ When it is 0 get customer creates a new customer object of the shop order billin =item C +=item C + +Checks if the postgresextension pg_trgm is installed and return 0 or 1. + =back =head1 TODO diff --git a/t/shop/shop_order.t b/t/shop/shop_order.t index 853322d0c..550423178 100644 --- a/t/shop/shop_order.t +++ b/t/shop/shop_order.t @@ -67,6 +67,8 @@ Support::TestSetup::login(); reset_state(); +my $trgm = SL::DB::ShopOrder->check_trgm; + my $shop_trans_id = 1; $shop_order = new_shop_order( @@ -76,9 +78,9 @@ $shop_order = new_shop_order( billing_lastname => 'Schmidt', billing_firstname => 'Sven', billing_company => 'Evil Inc', - billing_street => 'Evil Street', + billing_street => 'Evil Street 666', billing_zipcode => $customer->zipcode, - billing_email => $customer->email, + billing_email => 'email', ); my $shop_order_item = SL::DB::ShopOrderItem->new( @@ -107,15 +109,33 @@ my $customer_different = new_customer( $fuzzy_customers = $shop_order->check_for_existing_customers; is(scalar @{ $fuzzy_customers }, 1, 'still only found 1 matching customer (zipcode equal + street dissimilar'); -note('adding a similar customer'); +note('adding 2 similar customers and 1 dissimilar but same email'); my $customer_similar = new_customer( name => "Different Name", - street => 'Good Street', # difference not large enough from "Evil Street", street matches + street => 'Evil Street 666', # difference not large enough from "Evil Street", street matches zipcode => $customer->zipcode, email => "foo", )->save; +my $customer_similar_2 = new_customer( + name => "Different Name", + street => 'Evil Straet', # difference not large enough from "Evil Street", street matches + zipcode => $customer->zipcode, + email => "foofoo", +)->save; +my $customer_same_email = new_customer( + name => "Different Name", + street => 'Angel Way', # difference large enough from "Evil Street", street not matches , same email + zipcode => $customer->zipcode, + email => 'email', +)->save; +my $customers = SL::DB::Manager::Customer->get_all(); + $fuzzy_customers = $shop_order->check_for_existing_customers; -is(scalar @{ $fuzzy_customers }, 2, 'found 2 matching customers (zipcode equal + street similar)'); +if($trgm){ + is(scalar @{ $fuzzy_customers }, 4, 'found 4 matching customers (zipcode equal + street similar + same email) trgm_pg is installed'); +}else{ + is(scalar @{ $fuzzy_customers }, 3, 'found 3 matching customers (zipcode equal + %street% + same email) trgm_pg is not installed, could be 4 with trgm_pg'); +} is($shop->description , 'testshop' , 'shop description ok'); is($shop_order->shop_id , $shop->id , "shop_id ok"); -- 2.20.1