From 1760f6075953176c30a5ccc303bb515b2480a02f Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Wed, 30 Dec 2015 03:58:48 -0800 Subject: [PATCH] getRequest: re-implement to take hash argument Instead of a bunch of serial arguments, reimplement getRequest() to take a hash of parameters. In the process, add support for requestTypeId to be included. --- Supporters/lib/Supporters.pm | 50 ++++++++++++++++++++++++++++-------- Supporters/t/Supporters.t | 14 +++++----- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/Supporters/lib/Supporters.pm b/Supporters/lib/Supporters.pm index 1eb1a8e..8582415 100644 --- a/Supporters/lib/Supporters.pm +++ b/Supporters/lib/Supporters.pm @@ -564,18 +564,30 @@ sub addRequestConfigurations($$$) { Arguments: +=item $parmas + +A hash reference, the following keys are considered: + =over -=item $donorId +=item donorId Valid donor_id number currently in the database. die() will occur if the id number is not in the database already as a supporter id. -=item $requestType +=item requestTypeId - String for the requestType sought. + Numeric id of a request_type entry. This must be a valid id in the + request_type table, otherwise the method L()s. -=item $ignoreFulfilledRequests + requestType is ignored if this parameter is set. + +=item requestType + + If requestTypeId is not given, requestType will be used. The type is + added to the request_type table if it is not present, so be careful. + +=item ignoreFulfilledRequests Optional boolean argument. If true, a request that is found will not be returned if the request has already been fulfilled. In other words, it @@ -583,6 +595,8 @@ Arguments: =back +=back + Returns: =over @@ -645,21 +659,37 @@ If the request has been fufilled, the following keys will also ahve values. =cut sub getRequest($$;$) { - my($self, $donorId, $requestType, $ignoreFulfilledRequests) = @_; + my($self, $params) = @_; + my($donorId, $requestType, $requestTypeId, $ignoreFulfilledRequests) = + ($params->{donorId}, $params->{requestType}, $params->{requestTypeId}, $params->{ignoreFulfilledRequests}); die "getRequest: undefined donorId" unless defined $donorId; die "getRequest: donorId, \"$donorId\" not found in supporter database" unless $self->_verifyId($donorId); - die "getRequest: undefined requestType" unless defined $requestType; - + my $requestTypeClause = ""; + if (defined $requestTypeId) { + $requestType = $self->_lookupRequestTypeById($requestTypeId); + die "getRequest: invalid requestTypeId, \"$requestTypeId\"" unless defined $requestType; + $requestTypeClause = " AND rt.id = " . $self->dbh->quote($requestTypeId, 'SQL_INTEGER'); + } elsif (defined $requestType) { + $requestTypeClause = " AND rt.type = " . $self->dbh->quote($requestType); + } else { + die "getRequest: undefined requestType" unless defined $requestType; + } my $req = $self->dbh()->selectall_hashref("SELECT r.id, r.request_type_id, r.request_configuration_id, r.date_requested, r.notes, rt.type " . "FROM request r, request_type rt WHERE r.request_type_id = rt.id AND " . "r.donor_id = " . $self->dbh->quote($donorId, 'SQL_INTEGER') . - " AND rt.type = " . $self->dbh->quote($requestType), + $requestTypeClause, 'type'); + if (defined $requestTypeId) { + die "getRequest: given requestTypeId, \"$requestTypeId\" was not the one found in the database $req->{$requestType}{'request_type_id'}" + unless $req->{$requestType}{'request_type_id'} == $requestTypeId; + } else { + $requestTypeId = $req->{$requestType}{'request_type_id'}; + } return undef unless (defined $req and defined $req->{$requestType} and defined $req->{$requestType}{'id'}); - my $requestTypeId = $req->{$requestType}{'request_type_id'}; + my $requestId = $req->{$requestType}{'id'}; my $rsp = { requestType => $requestType, @@ -822,7 +852,7 @@ sub fulfillRequest($$) { die "fulfillRequest: undefined who" unless defined $params->{who}; die "fulfillRequest: undefined requestType" unless defined $params->{requestType}; - my $req = $self->getRequest($donorId, $params->{requestType}); + my $req = $self->getRequest($params); return undef if not defined $req; my $requestId = $req->{requestId}; return undef if not defined $requestId; diff --git a/Supporters/t/Supporters.t b/Supporters/t/Supporters.t index 7b2dd50..9a920e4 100644 --- a/Supporters/t/Supporters.t +++ b/Supporters/t/Supporters.t @@ -364,20 +364,20 @@ is($lookedUpFulfillmentId, $fulfillRequestId, =cut -dies_ok { $sp->getRequest(undef, undef); } "getRequest: dies if donorId not specified."; +dies_ok { $sp->getRequest({} ); } "getRequest: dies if donorId not specified."; -dies_ok { $sp->getRequest(0, "t-shirt-small-only"); } "getRequest: dies if donorId invalid."; +dies_ok { $sp->getRequest({ donorId => 0, requestType => "t-shirt-small-only" }); } "getRequest: dies if donorId invalid."; -dies_ok { $sp->getRequest($drapperId, undef); } +dies_ok { $sp->getRequest({ donorId => $drapperId, requestType => undef}); } "getRequest: dies if requestType not specified."; my $tt; -lives_ok { $tt = $sp->getRequest($drapperId, 'this-one-is-not-there'); } +lives_ok { $tt = $sp->getRequest({ donorId => $drapperId, requestType => 'this-one-is-not-there' }); } "getRequest: returns normally with non-existent request."; is($tt, undef, "getRequest: returns undef for valid supporter and on-existent request."); -lives_ok { $tt = $sp->getRequest($drapperId, 't-shirt-small-only'); } +lives_ok { $tt = $sp->getRequest({donorId => $drapperId, requestType => 't-shirt-small-only' }); } "getRequest: succeeds with valid parameters."; is($tt->{requestType}, 't-shirt-small-only', "getRequest: requestType is correct."); @@ -387,7 +387,7 @@ is($tt->{requestConfiguration}, 'Small', "getRequest: configuration is correct." is($tt->{notes}, 'he probably needs a larger size but this shirt has none', "getRequest: notes are correct."); -lives_ok { $tt = $sp->getRequest($drapperId, 't-shirt-0'); } +lives_ok { $tt = $sp->getRequest({donorId => $drapperId, requestType => 't-shirt-0' } ); } "getRequest: succeeds with valid parameters."; is($tt->{requestType}, 't-shirt-0', "getRequest: requestType is correct."); @@ -395,7 +395,7 @@ is($tt->{requestDate}, $today, "getRequest: request date is today."); is($tt->{requestConfiguration}, 'MenL', "getRequest: configuration is correct."); is($tt->{notes}, undef, "getRequest: notes are undef when null in database."); -lives_ok { $tt = $sp->getRequest($drapperId, "join-announce-email-list"); } +lives_ok { $tt = $sp->getRequest({ donorId => $drapperId, requestType => "join-announce-email-list" }); } "getRequest: succeeds with valid parameters."; is($tt->{requestType}, "join-announce-email-list", "getRequest: requestType is correct.");