From 4ca89cfad40c967dd94f397a8203ad15121469e1 Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Sun, 20 Dec 2015 12:10:27 -0800 Subject: [PATCH] _getOrCreateRequestConfiguration: Fix various bugs There was some parameter confusion (using ids instead of the actual types/descriptions) on some of the public facing methods. Also, lookup of existing ids was buggy; just use public facing methods. --- Supporters/lib/Supporters.pm | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/Supporters/lib/Supporters.pm b/Supporters/lib/Supporters.pm index a8a286e..03d94e9 100644 --- a/Supporters/lib/Supporters.pm +++ b/Supporters/lib/Supporters.pm @@ -635,20 +635,33 @@ sub _getOrCreateRequestConfiguration($$) { die "_getOrCreateRequestConfiguration: requestTypeId is required" unless defined $params->{requestTypeId}; my $requestTypeId = $params->{requestTypeId}; die "_getOrCreateRequestConfiguration: requestTypeId must be a number" unless looks_like_number($requestTypeId); - die "_getOrCreateRequestConfiguration: requestTypeId is unknown" unless $self->_verifyRequestTypeId($requestTypeId); + + my $val = $self->dbh()->selectall_hashref("SELECT id, type FROM request_type WHERE id = " . + $self->dbh->quote($requestTypeId, 'SQL_INTEGER'), 'id'); + die "_getOrCreateRequestConfiguration: unknown requestTypeId, \"$requestTypeId\"" + unless (defined $val and defined $val->{$requestTypeId} and defined $val->{$requestTypeId}{type}); + my $requestType = $val->{$requestTypeId}{type}; + + my $existingRequestConfig = $self->getRequestConfigurations($requestType); + + die "_getOrCreateRequestConfiguration: requestTypeId is unknown" unless (keys(%$existingRequestConfig) == 1); if (not defined $params->{requestConfigurationId}) { - $params->{requestConfigurationId} = $self->addRequestType($params->{requestConfiguration}); + die "_getOrCreateRequestConfiguration: requestConfiguration is not defined" unless defined $params->{requestConfiguration}; + if (defined $existingRequestConfig->{$requestTypeId}{$params->{requestConfiguration}}) { + $params->{requestConfigurationId} = $existingRequestConfig->{$requestTypeId}{$params->{requestConfiguration}}; + } else { + $existingRequestConfig = $self->addRequestConfigurations($requestType, [ $params->{requestConfiguration} ]); + $params->{requestConfigurationId} = $existingRequestConfig->{$requestTypeId}{$params->{requestConfiguration}}; + } } else { my $id = $params->{requestConfigurationId}; die "_getOrCreateRequestConfiguration(): called with a non-numeric requestConfigurationId, \"$id\"" unless defined $id and looks_like_number($id); - - my $val = $self->dbh()->selectall_hashref("SELECT id FROM request_configuration WHERE id = " . - $self->dbh->quote($id, 'SQL_INTEGER'), 'id'); - + my $found = 0; + foreach my $foundId (values %{$existingRequestConfig->{$requestTypeId}}) { if ($foundId == $id) { $found = 1; last; } } die "_getOrCreateRequestType(): given requestConfigurationId, \"$id\", is invalid" - unless (defined $val and defined $val->{$id}); + unless defined $found; } delete $params->{requestConfiguration}; return $params->{requestConfigurationId};