Skip to content

Commit

Permalink
Add update_extra_metadata call.
Browse files Browse the repository at this point in the history
This can be used for an instant update of specific keys of the extra
column, reducing the chance of overwriting other data in there.
  • Loading branch information
dracos committed Nov 9, 2023
1 parent 365bd10 commit c057e79
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
26 changes: 26 additions & 0 deletions perllib/FixMyStreet/Roles/Extra.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package FixMyStreet::Roles::Extra;
use Moo::Role;
use JSON::MaybeXS;

=head1 NAME
Expand Down Expand Up @@ -83,6 +84,31 @@ sub unset_extra_metadata {
$self->extra($extra);
};

=head2 update_extra_metadata
This calls set_extra_metadata, plus immediately updates the database with the
new data (so update in the DBIx::Class sense, not like update_extra_field),
using the PostgreSQL || operator, to not affect anything else in the column.
If you are using this, you probably don't want to then call update and
potentially overwrite other things in the extra column.
=cut

sub update_extra_metadata {
my ($self, %new) = @_;

$self->set_extra_metadata(%new);
my $db = $self->result_source->schema->storage;
my $table = $self->result_source->name;
$db->dbh_do(sub {
my ($storage, $dbh, $json, $id) = @_;
$dbh->do("UPDATE $table SET extra = extra || ? WHERE id=?", undef, $json, $id);
},
encode_json(\%new),
$self->id
);
}

=head2 get_extra_metadata
my $metadata = $problem->get_extra_metadata;
Expand Down
7 changes: 2 additions & 5 deletions perllib/FixMyStreet/Roles/SCP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ sub waste_cc_get_redirect_url {
my $admin_fee = $p->get_extra_field_value('admin_fee');

my $redirect_id = mySociety::AuthToken::random_token();
$p->set_extra_metadata('redirect_id', $redirect_id);
$p->update;
$p->update_extra_metadata(redirect_id => $redirect_id);

my $fund_code = $payment->config->{scp_fund_code};
my $customer_ref = $payment->config->{customer_ref};
Expand Down Expand Up @@ -93,10 +92,8 @@ sub waste_cc_get_redirect_url {
if ( $result->{transactionState} eq 'IN_PROGRESS' &&
$result->{invokeResult}->{status} eq 'SUCCESS' ) {

$p->set_extra_metadata('scpReference', $result->{scpReference});
$p->update;
$p->update_extra_metadata(scpReference => $result->{scpReference});

# need to save scpReference against request here
my $redirect = $result->{invokeResult}->{redirectUrl};
return $redirect;
} else {
Expand Down

0 comments on commit c057e79

Please sign in to comment.