statiegeld_tokens: log creation, use, and expiry of tokens
This commit is contained in:
parent
9045eb7ff4
commit
1696028ce3
2 changed files with 35 additions and 6 deletions
plugins
|
@ -52,3 +52,7 @@ sub hook_log_warning($class, $message, @) {
|
||||||
sub hook_log_error($class, $message, @) {
|
sub hook_log_error($class, $message, @) {
|
||||||
_log(ERROR => $message);
|
_log(ERROR => $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub hook_log_info($class, $message, @) {
|
||||||
|
_log(INFO => $message);
|
||||||
|
}
|
||||||
|
|
|
@ -72,12 +72,24 @@ sub _expire_tokens($line, $time) {
|
||||||
defined $line or return $line;
|
defined $line or return $line;
|
||||||
$line =~ / / or return $line;
|
$line =~ / / or return $line;
|
||||||
|
|
||||||
# Rewrite line with only non-tokens, invalid tokens, and non-expired tokens
|
my ($username, @tokens) = split " ", $line;
|
||||||
return join(" ", grep {
|
|
||||||
my ($type, undef, $expiry) = split /,/;
|
|
||||||
|
|
||||||
!defined($expiry) or $expiry < 0 or $expiry > $time
|
# Rewrite line with only non-tokens, invalid tokens, and non-expired tokens
|
||||||
} split " ", $line) . "\n";
|
my @keep;
|
||||||
|
my @expired;
|
||||||
|
for my $token (@tokens) {
|
||||||
|
my ($type, undef, $expiry) = split /,/, $token;
|
||||||
|
|
||||||
|
my $expired = defined($expiry) && $expiry > 0 && $expiry < $time;
|
||||||
|
push @{ $expired ? \@expired : \@keep }, $token;
|
||||||
|
}
|
||||||
|
|
||||||
|
call_hooks(
|
||||||
|
"log_info",
|
||||||
|
"statiegeld_tokens: ${\scalar @expired} expired for $username: @expired"
|
||||||
|
) if @expired;
|
||||||
|
|
||||||
|
return join(" ", $username, @keep) . "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
sub _write($username, $tokens_by_type, $create) {
|
sub _write($username, $tokens_by_type, $create) {
|
||||||
|
@ -149,6 +161,8 @@ sub hook_checkout_prepare($class, $cart, $username, $transaction_id, @) {
|
||||||
my $time_is_reliable = _time_is_reliable;
|
my $time_is_reliable = _time_is_reliable;
|
||||||
|
|
||||||
my $tokens_changed = 0;
|
my $tokens_changed = 0;
|
||||||
|
my @created;
|
||||||
|
my @used;
|
||||||
|
|
||||||
# Products bought: add tokens
|
# Products bought: add tokens
|
||||||
my $seq = 0;
|
my $seq = 0;
|
||||||
|
@ -171,6 +185,7 @@ sub hook_checkout_prepare($class, $cart, $username, $transaction_id, @) {
|
||||||
$transaction_id,
|
$transaction_id,
|
||||||
$seq++,
|
$seq++,
|
||||||
);
|
);
|
||||||
|
push @created, $token;
|
||||||
push @{ $tokens_by_type->{$addon->{id}} }, $token;
|
push @{ $tokens_by_type->{$addon->{id}} }, $token;
|
||||||
}
|
}
|
||||||
$tokens_changed++;
|
$tokens_changed++;
|
||||||
|
@ -201,7 +216,7 @@ sub hook_checkout_prepare($class, $cart, $username, $transaction_id, @) {
|
||||||
$cart->delete($entry);
|
$cart->delete($entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
splice @{ $tokens_by_type->{$type} }, 0, $entry->quantity;
|
push @used, splice @{ $tokens_by_type->{$type} }, 0, $entry->quantity;
|
||||||
$tokens_changed++;
|
$tokens_changed++;
|
||||||
}
|
}
|
||||||
for my $type (keys %warnings_by_type) {
|
for my $type (keys %warnings_by_type) {
|
||||||
|
@ -218,6 +233,16 @@ sub hook_checkout_prepare($class, $cart, $username, $transaction_id, @) {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Store data
|
# Store data
|
||||||
|
call_hooks(
|
||||||
|
"log_info",
|
||||||
|
"statiegeld_tokens: ${\scalar @created } created for $username: @created"
|
||||||
|
) if @created;
|
||||||
|
|
||||||
|
call_hooks(
|
||||||
|
"log_info",
|
||||||
|
"statiegeld_tokens: ${\scalar @used } used by $username: @used"
|
||||||
|
) if @used;
|
||||||
|
|
||||||
_write $username, $tokens_by_type, $is_new if $tokens_changed;
|
_write $username, $tokens_by_type, $is_new if $tokens_changed;
|
||||||
|
|
||||||
return ABORT if %warnings_by_type and not $cart->size;
|
return ABORT if %warnings_by_type and not $cart->size;
|
||||||
|
|
Loading…
Add table
Reference in a new issue