blib/lib/Daje/Workflow/Client/Activity.pm

filter: Statementstmt 100% 3 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 4 / 4 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Daje::Workflow::Client::Activity;
2 1 ▶use Mojo::Base 'Daje::Workflow::Common::Activity::Base', -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 use v5.40;
4  
5  
6 # NAME
7 # ====
8 #
9 # Daje::Client::Activity - Create a client
10 #
11 # SYNOPSIS
12 # ========
13 #
14 # use Daje::Client::Activity;
15 #
16 # DESCRIPTION
17 # ===========
18 #
19 # Daje::Client::Activity is ...
20 #
21 # LICENSE
22 # =======
23 #
24 # Copyright (C) janeskil1525.
25 #
26 # This library is free software; you can redistribute it and/or modify
27 # it under the same terms as Perl itself.
28 #
29 # AUTHOR
30 # ======
31 #
32 # janeskil1525 E<lt>janeskil1525@gmail.comE<gt>
33 #
34  
35 use Daje::Database::Model::Users;
36 use Daje::Database::Model::Companies;
37 use Daje::Database::Model::CompaniesUsers;
38 use Daje::Workflow::Database::Model::Workflow;
39 use Daje::Workflow::Database::Model::Context;
40  
41 our $VERSION = "0.03";
42  
43 sub create_client($self) {
44 try {
45 $self->model->insert_history("New client will be created", "Daje::Workflow::Client::Activity");
46 my $wfl = Daje::Workflow::Database::Model::Workflow->new(db => $self->db);
47 my $user_pkey = $self->add_user($wfl);
48 if ($self->error->has_error() ==0 ) {
49 my $companies_pkey = $self->add_company($wfl);
50 if ($self->error->has_error() ==0 ) {
51 $self->add_companies_users($companies_pkey, $user_pkey);
52 $self->model->insert_history("New client created", "Daje::Workflow::Client::Activity");
53 }
54 }
55 } catch ($e) {
56 $self->error->add_error("Adding client failed due to : '$e'");
57 };
58  
59 return 1;
60 }
61  
62 sub add_companies_users($self, $companies_pkey, $users_pkey) {
63 my $data->{companies_fkey} = $companies_pkey;
64 $data->{users_fkey} = $users_pkey;
65 my $result = Daje::Database::Model::CompaniesUsers->new(
66 db => $self->db
67 )->insert_companies_users(
68 $data
69 );
70 $self->error->add_error($result->{error})
71 if exists $result->{error} and $result->{result} == 0;
72 }
73  
74 sub add_company($self, $wfl) {
75 my $company = $self->context->{context}->{company};
76 my $workflow->{name} = "companies";
77 $workflow->{state} = "company_active";
78  
79 my $workflow_pkey = $wfl->save($workflow);
80 $company->{workflow_fkey} = $workflow_pkey;
81  
82 my $company_obj = Daje::Database::Model::Companies->new(
83 db => $self->db
84 );
85 my $result = $company_obj->insert_companies($company);
86 $self->error->add_error($result->{error})
87 if exists $result->{error} and $result->{result} == 0;
88 if ($self->error->has_error() == 0) {
89 $company->{companies_pkey} = $result->{data}->{companies_pkey};
90  
91 my $context->{context} = $company;
92 $context->{workflow_fkey} = $workflow_pkey;
93  
94 Daje::Workflow::Database::Model::Context->new(
95 db => $self->db,
96 workflow_pkey => $workflow_pkey
97 )->save(
98 $context
99 );
100 }
101  
102 return $result->{data}->{companies_pkey};
103 }
104  
105 sub add_user($self, $wfl) {
106 my $user = $self->context()->{context}->{user};
107  
108 my $user_obj = Daje::Database::Model::Users->new(
109 db => $self->db
110 );
111 my $existing_user = $user_obj->load_users_userid($user->{userid});
112  
113 return $existing_user->{data}->{users_pkey} # User exists
114 unless exists $existing_user->{data}->{users_pkey}
115 and $existing_user->{data}->{users_pkey} > 0;
116  
117 my $workflow->{name} = "users";
118 $workflow->{state} = "confirmationMail";
119  
120 my $workflow_pkey = $wfl->save($workflow);
121 $user->{workflow_fkey} = $workflow_pkey;
122  
123 my $result = $user_obj->insert_users($user);
124 $self->error->add_error($result->{error})
125 if exists $result->{error} and $result->{result} == 0;
126  
127 if ($self->error->has_error() == 0) {
128 $user->{users_pkey} = $result->{data}->{users_pkey};
129  
130 my $context->{context} = $user;
131 $context->{context}->{mail}->{to} = $user->{userid};
132 $context->{context}->{mail}->{template} = "ConfirmEmail";
133 $context->{context}->{mail}->{users_pkey} = $result->{data}->{users_pkey};
134 $context->{workflow_fkey} = $workflow_pkey;
135  
136 Daje::Workflow::Database::Model::Context->new(
137 db => $self->db,
138 workflow_pkey => $workflow_pkey,
139 )->save(
140 $context
141 );
142 }
143  
144 return $result->{data}->{users_pkey};
145 }
146  
147 1;
148 __END__
↑ Summary