| 1 | package Daje::Workflow::Checks::Exists; | ||
| 2 | use Mojo::Base 'Daje::Workflow::Checks::Super::Base', -base, -signatures; | ||
| 3 | use v5.42; | ||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | # NAME | ||
| 8 | # ==== | ||
| 9 | # | ||
| 10 | # Daje::Workflow::Checks::Exists - Checks for mandatory fields | ||
| 11 | # | ||
| 12 | # SYNOPSIS | ||
| 13 | # ======== | ||
| 14 | # | ||
| 15 | # exists($self) | ||
| 16 | # | ||
| 17 | # Checks can either come from the workflow checks tag as a comma separated string of fields | ||
| 18 | # | ||
| 19 | # { | ||
| 20 | # "name": "Record already exists", | ||
| 21 | # "class": "Daje::Workflow::Checks::Exists", | ||
| 22 | # "checks": { | ||
| 23 | # "fields": "mail", | ||
| 24 | # "table": "users_users", | ||
| 25 | # "err_text": "This user already exists, maybe you should try to change password instead ?" | ||
| 26 | # } | ||
| 27 | # | ||
| 28 | # Assumed format of data $self->context->{context}->{payload}->{field(s) to be checked for existence) | ||
| 29 | # | ||
| 30 | # | ||
| 31 | # DESCRIPTION | ||
| 32 | # =========== | ||
| 33 | # | ||
| 34 | # Daje::Workflow::Checks::Mandatory is used to check and make sure | ||
| 35 | # mandatory fields are included in the context | ||
| 36 | # | ||
| 37 | # LICENSE | ||
| 38 | # ======= | ||
| 39 | # | ||
| 40 | # Copyright (C) janeskil1525. | ||
| 41 | # | ||
| 42 | # This library is free software; you can redistribute it and/or modify | ||
| 43 | # it under the same terms as Perl itself. | ||
| 44 | # | ||
| 45 | # AUTHOR | ||
| 46 | # ====== | ||
| 47 | # janeskil1525 E<lt>janeskil1525@gmail.comE<gt> | ||
| 48 | # | ||
| 49 | |||
| 50 | sub check($self) { | ||
| 51 | my $result = 1; | ||
| 52 | |||
| 53 | if (exists $self->checks->{fields}) { | ||
| 54 | my @fields = (); | ||
| 55 | my $condition = {}; | ||
| 56 | @fields = split(',', $self->checks()->{fields}); | ||
| 57 | my $table = $self->checks()->{table}; | ||
| 58 | |||
| 59 | my $length = scalar @fields; | ||
| 60 | my $temp = $self->context(); | ||
| 61 | for (my $i = 0; $i < $length; $i++) { | ||
| 62 | if (exists $self->context->{context}->{payload}->{$fields[$i]}) { | ||
| 63 | $condition->{$fields[$i]} = $self->context->{context}->{payload}->{$fields[$i]} | ||
| 64 | } | ||
| 65 | } | ||
| 66 | my $data; | ||
| 67 | my $load = $self->db->select($table, '*', $condition); | ||
| 68 | $data = $load->hash if $load and $load->rows > 0; | ||
| 69 | if(defined $data) { | ||
| 70 | $self->error->add_error($self->checks()->{err_text}); | ||
| 71 | $result = 0; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | return $result; | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; |