|
#<<< |
|
use utf8; |
|
package Nutrinet::Schema::Result::User; |
|
|
|
# Created by DBIx::Class::Schema::Loader |
|
# DO NOT MODIFY THE FIRST PART OF THIS FILE |
|
|
|
use strict; |
|
use warnings; |
|
|
|
use Moose; |
|
use MooseX::NonMoose; |
|
use MooseX::MarkAsMethods autoclean => 1; |
|
extends 'DBIx::Class::Core'; |
|
__PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp"); |
|
__PACKAGE__->table("user"); |
|
__PACKAGE__->add_columns( |
|
"id", |
|
{ |
|
data_type => "integer", |
|
is_auto_increment => 1, |
|
is_nullable => 0, |
|
sequence => "nutrinet.user_id_seq", |
|
}, |
|
"email", |
|
{ |
|
data_type => "text", |
|
is_nullable => 0, |
|
original => { data_type => "varchar" }, |
|
}, |
|
"name", |
|
{ |
|
data_type => "text", |
|
is_nullable => 0, |
|
original => { data_type => "varchar" }, |
|
}, |
|
"gender", |
|
{ |
|
data_type => "text", |
|
is_nullable => 0, |
|
original => { data_type => "varchar" }, |
|
}, |
|
"birthdate", |
|
{ data_type => "date", is_nullable => 0 }, |
|
"created_at", |
|
{ |
|
data_type => "timestamp with time zone", |
|
default_value => \"current_timestamp", |
|
is_nullable => 0, |
|
original => { default_value => \"now()" }, |
|
}, |
|
"terms_checked_at", |
|
{ data_type => "timestamp with time zone", is_nullable => 0 }, |
|
"survey_start_date", |
|
{ data_type => "timestamp with time zone", is_nullable => 1 }, |
|
"email_validated_at", |
|
{ data_type => "timestamp with time zone", is_nullable => 1 }, |
|
"optout", |
|
{ data_type => "boolean", is_nullable => 0 }, |
|
"active", |
|
{ data_type => "boolean", default_value => \"true", is_nullable => 0 }, |
|
); |
|
__PACKAGE__->set_primary_key("id"); |
|
__PACKAGE__->has_many( |
|
"user_logs", |
|
"Nutrinet::Schema::Result::UserLog", |
|
{ "foreign.user_id" => "self.id" }, |
|
{ cascade_copy => 0, cascade_delete => 0 }, |
|
); |
|
__PACKAGE__->has_many( |
|
"user_one_time_passwords", |
|
"Nutrinet::Schema::Result::UserOneTimePassword", |
|
{ "foreign.user_id" => "self.id" }, |
|
{ cascade_copy => 0, cascade_delete => 0 }, |
|
); |
|
__PACKAGE__->has_many( |
|
"user_sessions", |
|
"Nutrinet::Schema::Result::UserSession", |
|
{ "foreign.user_id" => "self.id" }, |
|
{ cascade_copy => 0, cascade_delete => 0 }, |
|
); |
|
__PACKAGE__->has_many( |
|
"user_surveys", |
|
"Nutrinet::Schema::Result::UserSurvey", |
|
{ "foreign.user_id" => "self.id" }, |
|
{ cascade_copy => 0, cascade_delete => 0 }, |
|
); |
|
#>>> |
|
|
|
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2018-11-05 16:46:56 |
|
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:MKTTUeCmi4+mf55gtgO+jw |
|
|
|
with 'Nutrinet::Role::Verification'; |
|
with 'Nutrinet::Role::Verification::TransactionalActions::DBIC'; |
|
|
|
use JSON; |
|
use Data::Verifier; |
|
use MooseX::Types::Email qw/EmailAddress/; |
|
use MooseX::Types::Common::Numeric qw/PositiveInt/; |
|
use Nutrinet::Types qw/DateStr/; |
|
|
|
sub verifiers_specs { |
|
my $self = shift; |
|
return { |
|
update => Data::Verifier->new( |
|
filters => [qw(trim)], |
|
profile => { |
|
|
|
name => { |
|
required => 0, |
|
max_length => 128, |
|
type => 'Str', |
|
}, |
|
|
|
log_data => { |
|
required => 1, |
|
max_length => 2048, |
|
type => 'Str', |
|
}, |
|
|
|
birthdate => { |
|
required => 0, |
|
type => DateStr |
|
}, |
|
|
|
gender => { |
|
required => 0, |
|
max_length => 10, |
|
type => 'Str', |
|
post_check => sub { |
|
my $r = shift; |
|
return 0 unless $r->get_value('gender') =~ /^(male|female)$/; |
|
return 1; |
|
} |
|
}, |
|
|
|
email => { |
|
required => 0, |
|
max_length => 64, |
|
type => EmailAddress, |
|
post_check => sub { |
|
my $r = shift; |
|
die \[ 'email', 'already-in-use' ] |
|
if ( $self->find( { active => 1, email => lc $r->get_value('email') } ) ); |
|
return 1; |
|
} |
|
}, |
|
|
|
}, |
|
), |
|
|
|
}; |
|
} |
|
|
|
sub action_specs { |
|
my $self = shift; |
|
return { |
|
|
|
update => sub { |
|
my %values = shift->valid_values; |
|
|
|
not defined $values{$_} and delete $values{$_} for keys %values; |
|
|
|
my $log_data = delete $values{log_data}; |
|
|
|
return $self unless keys %values; |
|
|
|
my $user = $self->update( \%values ); |
|
|
|
$user->log( 'user.update', $log_data ); |
|
$user->discard_changes; |
|
return $user; |
|
} |
|
|
|
}; |
|
} |
|
|
|
sub log { |
|
my ( $self, $context, $data ) = @_; |
|
|
|
$data = from_json($data) if ref $data eq ''; |
|
|
|
my $ip = delete $data->{remote_addr}; |
|
|
|
$self->user_logs->create( |
|
{ |
|
context => $context, |
|
data => to_json($data), |
|
ip_address => $ip, |
|
} |
|
); |
|
|
|
} |
|
|
|
sub new_session { |
|
my ($self) = @_; |
|
|
|
my $session = $self->user_sessions->create( |
|
{ |
|
api_key => \'uuid_generate_v4()' |
|
} |
|
); |
|
$session->discard_changes; |
|
|
|
return { |
|
jti => $session->api_key, |
|
exp => int( $session->valid_until->epoch ), |
|
typ => 'usr', |
|
uid => $self->id, |
|
}; |
|
} |
|
|
|
sub as_row { |
|
my ($self, %opts) = @_; |
|
|
|
my $surveys; |
|
|
|
if ($opts{include_survey}){ |
|
|
|
$surveys = [ map { $_->as_row } $self->list_surveys ]; |
|
|
|
} |
|
|
|
|
|
return { |
|
user => { |
|
id => 0 + $self->id, |
|
( map { $_ => $self->$_ || '' } qw/name email gender birthdate/ ), |
|
}, |
|
|
|
($surveys ? (surveys => $surveys) : ()), |
|
|
|
}; |
|
} |
|
|
|
sub add_pending_surveys { |
|
my ($self ) = @_; |
|
|
|
# todo: begin database, call obtain_lock, insert pending surveys.. |
|
|
|
}; |
|
|
|
sub obtain_lock { |
|
my ( $self ) = @_; |
|
|
|
## right here, that's where I stopped! |
|
log_info( 'obtain_lock start ' . $self->id ); |
|
|
|
my ($lock_myself) = |
|
$self->result_source->schema->resultset('User') |
|
->search( { 'me.id' => $self->id }, { for => 'UPDATE', columns => ['id'] } )->next; |
|
|
|
log_info( 'obtain_lock end ' . $self->id ); |
|
|
|
return $lock_myself; |
|
} |
|
|
|
|
|
sub list_surveys { |
|
my ($self ) = @_; |
|
|
|
my $user = $self->schema->resultset('User')->search({ |
|
'me.id' => $self->id, |
|
}, { |
|
prefetch => { 'user_surveys' => 'survey_config'}, |
|
order_by => 'user_surveys.visible_after' |
|
})->next; |
|
|
|
return map { $_ } $user->user_surveys; |
|
}; |
|
|
|
# You can replace this text with custom code or comments, and it will be preserved on regeneration |
|
__PACKAGE__->meta->make_immutable; |
|
1; |
at controller, $c->stash->{user_rs}->next returns a Nutrinet::Schema::Result::User.
I forget to add the signup controller: