App-Dochazka-REST
view release on metacpan or search on metacpan
lib/App/Dochazka/REST/Fillup.pm view on Meta::CPAN
'SUN' => 7,
);
my %num_to_dow = reverse %dow_to_num;
=head1 NAME
App::Dochazka::REST::Fillup - fillup routines
=head1 SYNOPSIS
use App::Dochazka::REST::Fillup;
...
=head1 METHODS
=head2 populate
Get the next TIID and store in the object
=cut
sub populate {
my $self = shift;
if ( ! exists( $self->{tiid} ) or ! defined( $self->{tiid} ) or $self->{tiid} == 0 ) {
my $ss = _next_tiid();
$log->info( "Got next TIID: $ss" );
$self->{tiid} = $ss;
}
return;
}
=head2 Accessors
Make accessors for all the attributes. Already done, above, in BEGIN block.
=cut
=head2 _vet_context
Performs various tests on the C<context> attribute. If the value of that
attribute is not what we're expecting, returns a non-OK status. Otherwise,
returns an OK status.
=cut
sub _vet_context {
my $self = shift;
my %ARGS = @_;
return $CELL->status_not_ok unless $ARGS{context};
return $CELL->status_not_ok unless $ARGS{context}->{dbix_conn};
return $CELL->status_not_ok unless $ARGS{context}->{dbix_conn}->isa('DBIx::Connector');
$self->context( $ARGS{context} );
$self->{'vetted'}->{'context'} = 1;
return $CELL->status_ok;
}
=head2 _vet_date_spec
The user can specify fillup dates either as a tsrange or as a list of
individual dates.
One or the other must be given, not neither and not both.
Returns a status object.
=cut
sub _vet_date_spec {
my $self = shift;
my %ARGS = @_;
$log->debug( "Entering " . __PACKAGE__ . "::_vet_date_spec to enforce date specification policy" );
if ( defined( $ARGS{date_list} ) and defined( $ARGS{tsrange} ) ) {
$log->debug( "date_spec is NOT OK" );
return $CELL->status_not_ok;
}
if ( ! defined( $ARGS{date_list} ) and ! defined( $ARGS{tsrange} ) ) {
$log->debug( "date_spec is NOT OK" );
return $CELL->status_not_ok;
}
$self->{'vetted'}->{'date_spec'} = 1;
$log->debug( "date_spec is OK" );
return $CELL->status_ok;
}
=head2 _vet_date_list
This function takes one named argument: date_list, the value of which must
be a reference to an array of dates, each in canonical YYYY-MM-DD form. For
example, this
[ '2016-01-13', '2016-01-27', '2016-01-14' ]
is a legal C<date_list> argument.
This function performs various checks on the date list, sorts it, and
populates the C<tsrange> and C<tsranges> attributes based on it. For the
sample date list given above, the tsrange will be something like
{ tsrange => "[\"2016-01-13 00:00:00+01\",\"2016-01-28 00:00:00+01\")" }
This is used to make sure the employee's schedule and priv level did not
change during the time period represented by the date list, as well as in
C<fillup_tempintvls> to generate the C<tempintvl> working set.
Returns a status object.
=cut
sub _vet_date_list {
my $self = shift;
my ( %ARGS ) = validate( @_, {
date_list => { type => ARRAYREF|UNDEF },
} );
$log->debug( "Entering " . __PACKAGE__ . "::_vet_date_list to vet/populate the date_list property" );
if ( $ARGS{'date_list'} ) {
$log->debug( "Date list is " . Dumper $ARGS{'date_list'} );
}
die "GOPHFQQ! tsrange property must not be populated in _vet_date_list()" if $self->tsrange;
return $CELL->status_ok if not defined( $ARGS{date_list} );
return $CELL->status_err( 'DOCHAZKA_EMPTY_DATE_LIST' ) if scalar( @{ $ARGS{date_list} } ) == 0;
# check that dates are valid and in canonical form
my @canonicalized_date_list = ();
foreach my $date ( @{ $ARGS{date_list} } ) {
my ( $y, $m, $d ) = canon_to_ymd( $date );
if ( ! check_date( $y, $m, $d ) ) {
return $CELL->status_err(
"DOCHAZKA_INVALID_DATE_IN_DATE_LIST",
args => [ $date ],
);
}
push @canonicalized_date_list, sprintf( "%04d-%02d-%02d", $y, $m, $d );
}
my @sorted_date_list = sort @canonicalized_date_list;
$self->date_list( \@sorted_date_list );
( run in 0.516 second using v1.01-cache-2.11-cpan-d7f47b0818f )