package LATMOS::Accounts::Task::Refreshexpired;

use strict;
use warnings;
use base qw(LATMOS::Accounts::Task);
use LATMOS::Accounts;
use Config::IniFiles;
use LATMOS::Accounts::Log;
use LATMOS::Accounts::Utils;

sub init {
    my ($self) = @_;
    my $LA = LATMOS::Accounts->new($self->{config});
    my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->default_base;
    $labase && $labase->load or die "Cannot load base";

    $self->{_la} = $LA;
    $self->{_base} = $labase;

    1;
}

sub run {
    my ($self) = @_;

    my $find = $self->{_base}->db->prepare(
        q{
        SELECT * FROM "user"
        WHERE expire < now() and "date" < expire;
        }
    );
    $find->execute();
    my $upd = $self->{_base}->db->prepare(
        q{
        UPDATE "user" SET date = now() where "name" = ?
        }
    );
    while ( my $res = $find->fetchrow_hashref ) {
        la_log(LA_NOTICE, "Refresh user %s to take expiry into account",
            $res->{name},
        );
        $upd->execute($res->{name});
    }

    $self->{_base}->commit;

    1;
}

1;
