diff --git a/Supporters/lib/Supporters.pm b/Supporters/lib/Supporters.pm index dd26155..906350b 100644 --- a/Supporters/lib/Supporters.pm +++ b/Supporters/lib/Supporters.pm @@ -1447,7 +1447,72 @@ sub holdRequest($$) { } return $holdRecord->{$requestId}{id}; } +###################################################################### +=begin releaseRequestHold + +Arguments: + +=item $parmas + +A hash reference, the following keys are considered: + +=over + +=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 / requestTypeId + + If one or both of these parameters is defined, they are used as arguments + to C method. die()'s if neither is defined. + +=back + +Returns: + +If the release has been successful, returns the id of the hold request that +is now released. Otherwise, undef is returned. + +Note that the release can also be "unsuccessful" if the request wasn't on +hold in the first place. + +=cut + + +sub releaseRequestHold($$) { + my($self, $params) = @_; + die "holdRequest: undefined donorId" unless defined $params->{donorId}; + my $donorId = $params->{donorId}; + die "holdRequest: donorId, \"$donorId\" not found in supporter database" + unless $self->_verifyId($donorId); + die "holdRequest: requestType and requestTypeId are all undefined" + unless defined $params->{requestType} or defined $params->{requestTypeId}; + + my $req = $self->getRequest($params); + return undef if not defined $req; + my $requestId = $req->{requestId}; + return undef if not defined $requestId; + + my $holdLookupSql = "SELECT id, request_id, release_date FROM request_hold WHERE request_id = " . + $self->dbh->quote($requestId, 'SQL_INTEGER'); + + my $holdRecord = $self->dbh()->selectall_hashref($holdLookupSql, "request_id"); + return undef if (not defined $holdRecord or not defined $holdRecord->{$requestId}); + + # If this has already been released, just return the release id again. + return $holdRecord->{$requestId}{id} if defined $holdRecord->{$requestId}{release_date} and + $holdRecord->{$requestId}{release_date} le $TODAY; + $self->_beginWork; + my $sth = $self->dbh->prepare("UPDATE request_hold SET release_date = date('now') WHERE id = ?"); + + $sth->execute($holdRecord->{$requestId}{id}); + $sth->finish; + $self->_commit; + return $holdRecord->{$requestId}{id}; +} ###################################################################### =begin findDonor diff --git a/Supporters/t/Supporters.t b/Supporters/t/Supporters.t index 78899da..1cfd5d2 100644 --- a/Supporters/t/Supporters.t +++ b/Supporters/t/Supporters.t @@ -8,7 +8,7 @@ use strict; use warnings; -use Test::More tests => 334; +use Test::More tests => 341; use Test::Exception; use Sub::Override; use File::Temp qw/tempfile/; @@ -762,9 +762,19 @@ is($tt->{notes}, undef, "getRequest: notes are undef when null in database.") my $releasedHoldId; -lives_ok { $releasedHoldId = $sp->releaseRequestHold(donorId => $drapperId, requestType => 't-shirt-0'); } +lives_ok { $releasedHoldId = $sp->releaseRequestHold({ donorId => $drapperId, requestType => 't-shirt-0' }); } "releaseRequestHold: release of a known held request succeeds..."; is($releasedHoldId, $drapperTShirt0HoldId, "releaseRequestHold: ... & returns same hold id as holdRequest() call did"); +lives_ok { $req = $sp->getRequest({ donorId => $drapperId, requestType => 't-shirt-0'}) } + "releaseRequestHold: lookup of request after release succeeds...."; +is($req->{holdReleaseDate}, $today, "... and the release date is today."); + +lives_ok { $releasedHoldId = $sp->releaseRequestHold({ donorId => $drapperId, requestType => 't-shirt-0' }); } + "releaseRequestHold: release again of the same a hold request also succeeds..."; +is($releasedHoldId, $drapperTShirt0HoldId, "releaseRequestHold: ... & also returns same hold id as holdRequest() call did"); +lives_ok { $req = $sp->getRequest({ donorId => $drapperId, requestType => 't-shirt-0'}) } + "releaseRequestHold: lookup of request after second release succeeds...."; +is($req->{holdReleaseDate}, $today, "... and the release date is still set to today."); lives_ok { $newFRID = $sp->fulfillRequest( { donorId => $drapperId,