| 1 | package Concierge::Users::File v0.9.4; | ||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use Carp qw/ croak /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Text::CSV; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use File::Path qw/ make_path /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use parent qw/ Concierge::Users::Meta /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: File backend (CSV/TSV) for Concierge::Users | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # ============================================================================== | ||||||||||||||||||||||||||||
| 12 | # Configure Class Method - One-time setup (called by Users->setup) | ||||||||||||||||||||||||||||
| 13 | # ============================================================================== | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 20 | ▶ | sub configure { | ||||||||||||||||||||||||||
|
Subroutine
configure: called CC=14 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 20 | my ($class, $setup_config) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Extract parameters | ||||||||||||||||||||||||||||
| 19 | 20 | my $storage_dir = $setup_config->{storage_dir}; | |||||||||||||||||||||||||||
| 20 | 20 | ▶ | my $format = lc($setup_config->{file_format} || 'tsv'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$setup_config->{'file_format'} || 'tsv'
MC/DC100%1/1
$setup_config->{'file_format'} || 'tsv'
$setup_config->{'file_format'}
Truth table100%2/2
A
A= $setup_config->{'file_format'}
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Validate format | ||||||||||||||||||||||||||||
| 23 | 20 | ▶ | unless ($format =~ /^(csv|tsv)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | return { | ||||||||||||||||||||||||||||
| 25 | 2 | success => 0, | |||||||||||||||||||||||||||
| 26 | message => "Invalid file_format: '$format' (must be 'csv' or 'tsv')", | ||||||||||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Build file name | ||||||||||||||||||||||||||||
| 31 | 18 | my $file_name = "users.$format"; | |||||||||||||||||||||||||||
| 32 | 18 | my $file_full_path = "$storage_dir/$file_name"; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Initialize CSV parser | ||||||||||||||||||||||||||||
| 35 | 18 | ▶ | my $sep_char = ($format eq 'csv') ? ',' : "\t"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 18 | my $csv = Text::CSV->new({ | |||||||||||||||||||||||||||
| 37 | sep_char => $sep_char, | ||||||||||||||||||||||||||||
| 38 | binary => 1, | ||||||||||||||||||||||||||||
| 39 | auto_diag => 1, | ||||||||||||||||||||||||||||
| 40 | }); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 18 | ▶ | unless ($csv) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | return { | ||||||||||||||||||||||||||||
| 44 | 0 | success => 0, | |||||||||||||||||||||||||||
| 45 | message => "Failed to initialize CSV parser for format: $format", | ||||||||||||||||||||||||||||
| 46 | }; | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Create temporary object for ensure_storage | ||||||||||||||||||||||||||||
| 50 | my $temp_backend = bless { | ||||||||||||||||||||||||||||
| 51 | storage_dir => $storage_dir, | ||||||||||||||||||||||||||||
| 52 | format => $format, | ||||||||||||||||||||||||||||
| 53 | csv => $csv, | ||||||||||||||||||||||||||||
| 54 | fields => $setup_config->{fields} || [], | ||||||||||||||||||||||||||||
| 55 | field_definitions => $setup_config->{field_definitions}, | ||||||||||||||||||||||||||||
| 56 | 18 | ▶ | }, $class; | ||||||||||||||||||||||||||
|
Condition50%1/2
$setup_config->{'fields'} || []
MC/DC0%0/1
$setup_config->{'fields'} || []
$setup_config->{'fields'}
Truth table50%1/2
A
A= $setup_config->{'fields'}
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Check for existing file with data and archive if present | ||||||||||||||||||||||||||||
| 59 | 18 | my $user_file = "$storage_dir/users.$format"; | |||||||||||||||||||||||||||
| 60 | 18 | ▶ | if (-f $user_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | # Check if file has data rows (after header) | ||||||||||||||||||||||||||||
| 62 | 1 | open my $fh, '<:encoding(UTF-8)', $user_file; | |||||||||||||||||||||||||||
| 63 | 1 | ▶ | if ($fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1 | my $header = <$fh>; # Skip header | |||||||||||||||||||||||||||
| 65 | 1 | my $data_count = 0; | |||||||||||||||||||||||||||
| 66 | 1 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 2 | chomp $line; | |||||||||||||||||||||||||||
| 68 | 2 | ▶ | $data_count++ if $line =~ /\S/; # Count non-empty lines | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 1 | close $fh; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Archive if file has data | ||||||||||||||||||||||||||||
| 73 | 1 | ▶ | if ($data_count > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | my $archive_result = $temp_backend->_archive_user_data(); | |||||||||||||||||||||||||||
| 75 | 1 | ▶ | unless ($archive_result->{success}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | return { | ||||||||||||||||||||||||||||
| 77 | success => 0, | ||||||||||||||||||||||||||||
| 78 | message => $archive_result->{message}, | ||||||||||||||||||||||||||||
| 79 | 0 | }; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | # If empty, just let ensure_storage() overwrite it | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Ensure storage (file) exists | ||||||||||||||||||||||||||||
| 87 | 18 | my $storage_ok = $temp_backend->ensure_storage(); | |||||||||||||||||||||||||||
| 88 | 18 | ▶ | unless ($storage_ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | return { | ||||||||||||||||||||||||||||
| 90 | 0 | success => 0, | |||||||||||||||||||||||||||
| 91 | message => "Failed to initialize storage for file backend", | ||||||||||||||||||||||||||||
| 92 | }; | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Return success with config | ||||||||||||||||||||||||||||
| 96 | return { | ||||||||||||||||||||||||||||
| 97 | success => 1, | ||||||||||||||||||||||||||||
| 98 | message => "File backend configured successfully", | ||||||||||||||||||||||||||||
| 99 | config => { | ||||||||||||||||||||||||||||
| 100 | storage_dir => $storage_dir, | ||||||||||||||||||||||||||||
| 101 | file_format => $format, | ||||||||||||||||||||||||||||
| 102 | file_name => $file_name, | ||||||||||||||||||||||||||||
| 103 | file_full_path => $file_full_path, | ||||||||||||||||||||||||||||
| 104 | fields => $setup_config->{fields} || [], | ||||||||||||||||||||||||||||
| 105 | field_definitions => $setup_config->{field_definitions}, | ||||||||||||||||||||||||||||
| 106 | }, | ||||||||||||||||||||||||||||
| 107 | 18 | ▶ | }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$setup_config->{'fields'} || []
MC/DC0%0/1
$setup_config->{'fields'} || []
$setup_config->{'fields'}
Truth table50%1/2
A
A= $setup_config->{'fields'}
| |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # ============================================================================== | ||||||||||||||||||||||||||||
| 111 | # Constructor - Runtime instantiation (called by Users->new) | ||||||||||||||||||||||||||||
| 112 | # ============================================================================== | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 20 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 20 | my ($class, $runtime_config) = @_; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # Extract parameters from saved config (no validation needed) | ||||||||||||||||||||||||||||
| 118 | 20 | my $storage_dir = $runtime_config->{storage_dir}; | |||||||||||||||||||||||||||
| 119 | 20 | my $format = $runtime_config->{file_format}; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Initialize CSV parser | ||||||||||||||||||||||||||||
| 122 | 20 | ▶ | my $sep_char = ($format eq 'csv') ? ',' : "\t"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 20 | my $csv = Text::CSV->new({ | |||||||||||||||||||||||||||
| 124 | sep_char => $sep_char, | ||||||||||||||||||||||||||||
| 125 | binary => 1, | ||||||||||||||||||||||||||||
| 126 | auto_diag => 1, | ||||||||||||||||||||||||||||
| 127 | }); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 20 | ▶ | croak "Failed to initialize CSV parser for format: $format" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | unless $csv; | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | return bless { | ||||||||||||||||||||||||||||
| 133 | storage_dir => $storage_dir, | ||||||||||||||||||||||||||||
| 134 | format => $format, | ||||||||||||||||||||||||||||
| 135 | csv => $csv, | ||||||||||||||||||||||||||||
| 136 | fields => $runtime_config->{fields} || [], | ||||||||||||||||||||||||||||
| 137 | field_definitions => $runtime_config->{field_definitions} || {}, | ||||||||||||||||||||||||||||
| 138 | 20 | ▶ | }, $class; | ||||||||||||||||||||||||||
|
Condition50%2/4
$runtime_config->{'fields'} || []
$runtime_config->{'field_definitions'} || {}
MC/DC0%0/1
$runtime_config->{'fields'} || []
$runtime_config->{'fields'}
MC/DC0%0/1
$runtime_config->{'field_definitions'} || {}
$runtime_config->{'field_definitions'}
Truth table50%1/2
A
A= $runtime_config->{'fields'}
Truth table50%1/2
A
A= $runtime_config->{'field_definitions'}
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Report backend configuration (for debugging/info) | ||||||||||||||||||||||||||||
| 142 | 1 | ▶ | sub config { | ||||||||||||||||||||||||||
|
Subroutine
config: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 1 | my $file_name = "users.$self->{format}"; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | return { | ||||||||||||||||||||||||||||
| 148 | storage_dir => $self->{storage_dir}, | ||||||||||||||||||||||||||||
| 149 | file_format => $self->{format}, | ||||||||||||||||||||||||||||
| 150 | file_name => $file_name, | ||||||||||||||||||||||||||||
| 151 | file_full_path => "$self->{storage_dir}/$file_name", | ||||||||||||||||||||||||||||
| 152 | fields => $self->{fields}, | ||||||||||||||||||||||||||||
| 153 | field_definitions => $self->{field_definitions}, | ||||||||||||||||||||||||||||
| 154 | 1 | }; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # Ensure storage (file) exists | ||||||||||||||||||||||||||||
| 158 | 18 | ▶ | sub ensure_storage { | ||||||||||||||||||||||||||
|
Subroutine
ensure_storage: called CC=4 SCAR=14.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 159 | 18 | my ($self) = @_; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 18 | my $user_file = $self->_get_user_file(); | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 18 | ▶ | return 1 if -f $user_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # Create file with header | ||||||||||||||||||||||||||||
| 166 | 18 | ▶ | open my $fh, '>:encoding(UTF-8)', $user_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | or croak "File backend initialization failed: Cannot create user file '$user_file': $!"; | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 18 | my @headers = @{$self->{fields}}; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 18 | ▶ | if ($self->{csv}->print($fh, \@headers)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 18 | print $fh "\n"; | |||||||||||||||||||||||||||
| 173 | 18 | close $fh; | |||||||||||||||||||||||||||
| 174 | 18 | return 1; | |||||||||||||||||||||||||||
| 175 | } else { | ||||||||||||||||||||||||||||
| 176 | 0 | close $fh; | |||||||||||||||||||||||||||
| 177 | 0 | croak "File backend initialization failed: Cannot write header to file '$user_file': " . $self->{csv}->error_diag(); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # Archive existing user data (internal method, called by configure) | ||||||||||||||||||||||||||||
| 182 | 1 | ▶ | sub _archive_user_data { | ||||||||||||||||||||||||||
|
Subroutine
_archive_user_data: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 183 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 1 | my $user_file = $self->_get_user_file(); | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # Generate timestamp for archive filename | ||||||||||||||||||||||||||||
| 188 | 1 | my $timestamp = $self->archive_timestamp(); | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # Build archive filename: users_YYYYMMDD_HHMMSS.csv (or .tsv) | ||||||||||||||||||||||||||||
| 191 | 1 | my ($base, $ext) = $user_file =~ /^(.+)\.([^.]+)$/; | |||||||||||||||||||||||||||
| 192 | 1 | my $archive_file = "${base}_${timestamp}.${ext}"; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # Rename file | ||||||||||||||||||||||||||||
| 195 | 1 | ▶ | unless (rename $user_file, $archive_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | return { | ||||||||||||||||||||||||||||
| 197 | 0 | success => 0, | |||||||||||||||||||||||||||
| 198 | message => "Failed to archive existing user file: $!" | ||||||||||||||||||||||||||||
| 199 | }; | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 1 | return { success => 1 }; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # Get user file path | ||||||||||||||||||||||||||||
| 206 | 106 | ▶ | sub _get_user_file { | ||||||||||||||||||||||||||
|
Subroutine
_get_user_file: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 207 | 106 | my ($self) = @_; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 106 | return File::Spec->catfile($self->{storage_dir}, "users.$self->{format}"); | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # Add bare record with user_id and null_values | ||||||||||||||||||||||||||||
| 213 | 18 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=6 SCAR=19.4
Pod
documented
| |||||||||||||||||||||||||||||
| 214 | 18 | my ($self, $user_id, $initial_record) = @_; | |||||||||||||||||||||||||||
| 215 | 18 | ▶ | return { success => 0, message => "Add Record failed: missing user_id" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | unless $user_id; | ||||||||||||||||||||||||||||
| 217 | 18 | ▶ | return { success => 0, message => "Add Record failed: missing initial record" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | unless $initial_record; | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 18 | my %record = $initial_record->%*; | |||||||||||||||||||||||||||
| 221 | 18 | $record{created_date} = $self->current_timestamp(); | |||||||||||||||||||||||||||
| 222 | # Add last_mod_date timestamp | ||||||||||||||||||||||||||||
| 223 | 18 | $record{last_mod_date} = $self->current_timestamp(); | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 18 | my $user_file = $self->_get_user_file(); | |||||||||||||||||||||||||||
| 226 | # Check if file exists, write header if not | ||||||||||||||||||||||||||||
| 227 | 18 | my $write_header = ! -f $user_file; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # Open file in append mode | ||||||||||||||||||||||||||||
| 230 | 18 | ▶ | open my $fh, '>>:encoding(UTF-8)', $user_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | or return { success => 0, message => "Failed to open user file: $!" }; | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # Write header if file is new | ||||||||||||||||||||||||||||
| 234 | 18 | ▶ | if ($write_header) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 0 | $self->{csv}->print($fh, $self->{fields}); | |||||||||||||||||||||||||||
| 236 | 0 | print $fh "\n"; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | # Put row field values in order | ||||||||||||||||||||||||||||
| 240 | 18 | my @row = map { $record{$_} } $self->{fields}->@*; | |||||||||||||||||||||||||||
| 241 | # Write row | ||||||||||||||||||||||||||||
| 242 | 18 | ▶ | if ($self->{csv}->print($fh, \@row)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 18 | print $fh "\n"; | |||||||||||||||||||||||||||
| 244 | 18 | close $fh; | |||||||||||||||||||||||||||
| 245 | 18 | return { success => 1, message => "User '$user_id' created" }; | |||||||||||||||||||||||||||
| 246 | } else { | ||||||||||||||||||||||||||||
| 247 | 0 | close $fh; | |||||||||||||||||||||||||||
| 248 | 0 | return { success => 0, message => "Failed to create initial user record: " . $self->{csv}->error_diag() }; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | # Fetch user by ID | ||||||||||||||||||||||||||||
| 253 | 41 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 254 | 41 | my ($self, $user_id) = @_; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 41 | my $user_file = $self->_get_user_file(); | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 41 | ▶ | open my $fh, '<:encoding(UTF-8)', $user_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | or return { | ||||||||||||||||||||||||||||
| 260 | success => 0, | ||||||||||||||||||||||||||||
| 261 | data => '', | ||||||||||||||||||||||||||||
| 262 | message => "Cannot open user file: $!" | ||||||||||||||||||||||||||||
| 263 | }; | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | # Skip header | ||||||||||||||||||||||||||||
| 266 | 41 | my $header = <$fh>; | |||||||||||||||||||||||||||
| 267 | 41 | chomp $header; | |||||||||||||||||||||||||||
| 268 | 41 | ▶ | my @fields = $self->{csv}->parse($header) ? $self->{csv}->fields() : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | # Search for user | ||||||||||||||||||||||||||||
| 271 | 41 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 73 | chomp $line; | |||||||||||||||||||||||||||
| 273 | 73 | ▶ | next unless $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 45 | $self->{csv}->parse($line); | |||||||||||||||||||||||||||
| 276 | 45 | my @values = $self->{csv}->fields(); | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 45 | my %user; | |||||||||||||||||||||||||||
| 279 | 45 | @user{@fields} = @values; | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 45 | ▶ | if ($user{user_id} eq $user_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 19 | close $fh; | |||||||||||||||||||||||||||
| 283 | return { | ||||||||||||||||||||||||||||
| 284 | 19 | success => 1, | |||||||||||||||||||||||||||
| 285 | data => \%user, | ||||||||||||||||||||||||||||
| 286 | message => '' | ||||||||||||||||||||||||||||
| 287 | }; | ||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 22 | close $fh; | |||||||||||||||||||||||||||
| 292 | return { | ||||||||||||||||||||||||||||
| 293 | 22 | success => 0, | |||||||||||||||||||||||||||
| 294 | data => '', | ||||||||||||||||||||||||||||
| 295 | message => "User '$user_id' not found" | ||||||||||||||||||||||||||||
| 296 | }; | ||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # Update user | ||||||||||||||||||||||||||||
| 300 | 21 | ▶ | sub update { | ||||||||||||||||||||||||||
|
Subroutine
update: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||
| 301 | 21 | my ($self, $user_id, $updates) = @_; | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | # Remove readonly fields from updates | ||||||||||||||||||||||||||||
| 304 | 21 | my %readonly = map { $_ => 1 } qw(user_id created_date last_mod_date); | |||||||||||||||||||||||||||
| 305 | 21 | delete $updates->{$_} for keys %readonly; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | # Add last_mod_date timestamp | ||||||||||||||||||||||||||||
| 308 | 21 | $updates->{last_mod_date} = $self->current_timestamp(); | |||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 21 | my $user_file = $self->_get_user_file(); | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | # Read entire file | ||||||||||||||||||||||||||||
| 313 | 21 | ▶ | open my $fh_in, '<:encoding(UTF-8)', $user_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | or return { success => 0, message => "Failed to read user file: $!" }; | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 21 | my @lines; | |||||||||||||||||||||||||||
| 317 | 21 | my $header = <$fh_in>; | |||||||||||||||||||||||||||
| 318 | 21 | push @lines, $header; | |||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 21 | chomp $header; | |||||||||||||||||||||||||||
| 321 | 21 | $self->{csv}->parse($header); | |||||||||||||||||||||||||||
| 322 | 21 | my @fields = $self->{csv}->fields(); | |||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 21 | my $found = 0; | |||||||||||||||||||||||||||
| 325 | 21 | ▶ | while (my $line = <$fh_in>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 42 | chomp $line; | |||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 42 | $self->{csv}->parse($line); | |||||||||||||||||||||||||||
| 329 | 42 | my @values = $self->{csv}->fields(); | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 42 | my %user; | |||||||||||||||||||||||||||
| 332 | 42 | @user{@fields} = @values; | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 42 | ▶ | if ($user{user_id} eq $user_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | # Update user with provided data | ||||||||||||||||||||||||||||
| 336 | 21 | foreach my $field (keys %$updates) { | |||||||||||||||||||||||||||
| 337 | 46 | $user{$field} = $updates->{$field}; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | 21 | $found = 1; | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | # Write back user data | ||||||||||||||||||||||||||||
| 343 | 42 | my @row; | |||||||||||||||||||||||||||
| 344 | 42 | foreach my $field (@fields) { | |||||||||||||||||||||||||||
| 345 | 410 | ▶ | push @row, $user{$field} || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$user{$field} || ''
MC/DC100%1/1
$user{$field} || ''
$user{$field}
Truth table100%2/2
A
A= $user{$field}
| |||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 42 | my $output; | |||||||||||||||||||||||||||
| 349 | 42 | ▶ | if ($self->{csv}->combine(@row)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 42 | $output = $self->{csv}->string(); | |||||||||||||||||||||||||||
| 351 | 42 | push @lines, $output; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 21 | close $fh_in; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 21 | ▶ | return { success => 0, message => "User '$user_id' not found" } unless $found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | # Write entire file back | ||||||||||||||||||||||||||||
| 360 | 21 | ▶ | open my $fh_out, '>:encoding(UTF-8)', $user_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | or return { success => 0, message => "Failed to write user file: $!" }; | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 21 | foreach my $line (@lines) { | |||||||||||||||||||||||||||
| 364 | 63 | print $fh_out $line, "\n"; | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 21 | close $fh_out; | |||||||||||||||||||||||||||
| 368 | 21 | return { success => 1, message => "User '$user_id' updated" }; | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | # List users with filters | ||||||||||||||||||||||||||||
| 372 | 5 | ▶ | sub list { | ||||||||||||||||||||||||||
|
Subroutine
list: called CC=23 SCAR=36.6
Pod
documented
| |||||||||||||||||||||||||||||
| 373 | 5 | my ($self, $filters, $options) = @_; | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 5 | my $user_file = $self->_get_user_file(); | |||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 5 | ▶ | open my $fh, '<:encoding(UTF-8)', $user_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | or return { data => [], total_count => 0 }; | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | # Skip header | ||||||||||||||||||||||||||||
| 381 | 5 | my $header = <$fh>; | |||||||||||||||||||||||||||
| 382 | 5 | chomp $header; | |||||||||||||||||||||||||||
| 383 | 5 | $self->{csv}->parse($header); | |||||||||||||||||||||||||||
| 384 | 5 | my @fields = $self->{csv}->fields(); | |||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | 5 | my @users; | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 5 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | 25 | chomp $line; | |||||||||||||||||||||||||||
| 390 | 25 | ▶ | next unless $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 20 | $self->{csv}->parse($line); | |||||||||||||||||||||||||||
| 393 | 20 | my @values = $self->{csv}->fields(); | |||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 20 | my %user; | |||||||||||||||||||||||||||
| 396 | 20 | @user{@fields} = @values; | |||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | # Skip rows where user_id is empty/undefined | ||||||||||||||||||||||||||||
| 399 | 20 | ▶ | next unless $user{user_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | # Apply DSL filters | ||||||||||||||||||||||||||||
| 402 | 11 | my $match = 1; | |||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 11 | ▶ | if (ref $filters eq 'HASH' && exists $filters->{or_groups}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $filters eq "HASH" and exists $filters->{"or_groups"}
MC/DC50%1/2
ref $filters eq "HASH" and exists $filters->{"or_groups"}
ref $filters eq "HASH"
exists $filters->{"or_groups"}
Truth table66%2/3
A and B
A= ref $filters eq "HASH"B= exists $filters->{"or_groups"}
| |||||||||||||||||||||||||||||
| 405 | 3 | $match = 0; # Start with no match, need at least one OR group to match | |||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 3 | foreach my $and_group (@{$filters->{or_groups}}) { | |||||||||||||||||||||||||||
| 408 | 3 | my $group_match = 1; # All conditions in this AND group must match | |||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 3 | foreach my $condition (@$and_group) { | |||||||||||||||||||||||||||
| 411 | 3 | my ($field, $op, $value) = ($condition->{field}, $condition->{op}, $condition->{value}); | |||||||||||||||||||||||||||
| 412 | 3 | ▶ | my $user_value = $user{$field} || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$user{$field} || ''
MC/DC0%0/1
$user{$field} || ''
$user{$field}
Truth table50%1/2
A
A= $user{$field}
| |||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 3 | ▶ | if ($op eq '=') { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 415 | 3 | ▶ | $group_match = 0 unless $user_value eq $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 416 | } elsif ($op eq ':') { | ||||||||||||||||||||||||||||
| 417 | 0 | ▶ | $group_match = 0 unless $user_value =~ /\Q$value\E/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 418 | } elsif ($op eq '!') { | ||||||||||||||||||||||||||||
| 419 | 0 | ▶ | $group_match = 0 if $user_value =~ /\Q$value\E/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | } elsif ($op eq '>') { | ||||||||||||||||||||||||||||
| 421 | 0 | ▶ | $group_match = 0 unless $user_value gt $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 422 | } elsif ($op eq '<') { | ||||||||||||||||||||||||||||
| 423 | 0 | ▶ | $group_match = 0 unless $user_value lt $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 3 | ▶ | $match = 1 if $group_match; # At least one OR group matched | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 428 | 3 | ▶ | last if $match; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | 11 | ▶ | push @users, \%user if $match; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 5 | close $fh; | |||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | return { | ||||||||||||||||||||||||||||
| 438 | 5 | data => \@users, | |||||||||||||||||||||||||||
| 439 | total_count => scalar @users, | ||||||||||||||||||||||||||||
| 440 | }; | ||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | # Delete user | ||||||||||||||||||||||||||||
| 444 | 2 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 445 | 2 | my ($self, $user_id) = @_; | |||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 2 | my $user_file = $self->_get_user_file(); | |||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | # Read entire file | ||||||||||||||||||||||||||||
| 450 | 2 | ▶ | open my $fh_in, '<:encoding(UTF-8)', $user_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 451 | or return { success => 0, message => "Failed to read user file: $!" }; | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 2 | my @lines; | |||||||||||||||||||||||||||
| 454 | 2 | my $header = <$fh_in>; | |||||||||||||||||||||||||||
| 455 | 2 | push @lines, $header; | |||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 2 | chomp $header; | |||||||||||||||||||||||||||
| 458 | 2 | $self->{csv}->parse($header); | |||||||||||||||||||||||||||
| 459 | 2 | my @fields = $self->{csv}->fields(); | |||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 2 | my $found = 0; | |||||||||||||||||||||||||||
| 462 | 2 | ▶ | while (my $line = <$fh_in>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 463 | 9 | chomp $line; | |||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | 9 | $self->{csv}->parse($line); | |||||||||||||||||||||||||||
| 466 | 9 | my @values = $self->{csv}->fields(); | |||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 9 | my %user; | |||||||||||||||||||||||||||
| 469 | 9 | @user{@fields} = @values; | |||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | 9 | ▶ | if ($user{user_id} ne $user_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 472 | 7 | push @lines, $line; | |||||||||||||||||||||||||||
| 473 | } else { | ||||||||||||||||||||||||||||
| 474 | 2 | $found = 1; | |||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 2 | close $fh_in; | |||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 2 | ▶ | return { success => 0, message => "User '$user_id' not found" } unless $found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | # Write entire file back | ||||||||||||||||||||||||||||
| 483 | 2 | ▶ | open my $fh_out, '>:encoding(UTF-8)', $user_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 484 | or return { success => 0, message => "Failed to write user file: $!" }; | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | 2 | foreach my $line (@lines) { | |||||||||||||||||||||||||||
| 487 | 9 | print $fh_out $line, "\n"; | |||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | 2 | close $fh_out; | |||||||||||||||||||||||||||
| 491 | 2 | return { success => 1, message => "User '$user_id' deleted" }; | |||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | # Cleanup | ||||||||||||||||||||||||||||
| 495 | 20 | ▶ | sub disconnect { | ||||||||||||||||||||||||||
|
Subroutine
disconnect: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 496 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 497 | # No resources to clean up for file backend | ||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 6 | 1; | |||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | __END__ | ||||||||||||||||||||||||||||