Refactored customer table and fixed all tests form that

This commit is contained in:
Tom Bloor 2017-04-21 21:20:47 +01:00
parent d0cbb215d8
commit f69b06bda2
13 changed files with 67 additions and 232 deletions

View file

@ -79,7 +79,7 @@ sub post_register{
if ( $usertype eq 'customer' ) { if ( $usertype eq 'customer' ) {
my $age_rs = $c->schema->resultset('AgeRange'); my $age_rs = $c->schema->resultset('AgeRange');
$validation->required('age')->in_resultset('agerangestring', $age_rs); $validation->required('age')->in_resultset('id', $age_rs);
} elsif ( $usertype eq 'organisation' ) { } elsif ( $usertype eq 'organisation' ) {
@ -103,8 +103,6 @@ sub post_register{
} }
if ($usertype eq 'customer'){ if ($usertype eq 'customer'){
# TODO replace with actually using the value on the post request
my $ageForeignKey = $c->get_age_foreign_key( $validation->param('age') );
$c->schema->txn_do( sub { $c->schema->txn_do( sub {
$c->schema->resultset('AccountToken')->find({ $c->schema->resultset('AccountToken')->find({
@ -114,7 +112,7 @@ sub post_register{
$c->schema->resultset('User')->create({ $c->schema->resultset('User')->create({
customer => { customer => {
username => $validation->param('username'), username => $validation->param('username'),
agerange_fk => $ageForeignKey, age_range_id => $validation->param('age'),
postcode => $validation->param('postcode'), postcode => $validation->param('postcode'),
}, },
email => $validation->param('email'), email => $validation->param('email'),

View file

@ -38,7 +38,7 @@ sub register {
$validation->required('token')->in_resultset('accounttokenname', $token_rs); $validation->required('token')->in_resultset('accounttokenname', $token_rs);
my $age_rs = $c->schema->resultset('AgeRange'); my $age_rs = $c->schema->resultset('AgeRange');
$validation->required('agerange')->in_resultset('agerangeid', $age_rs); $validation->required('agerange')->in_resultset('id', $age_rs);
my @error_messages; my @error_messages;
if ( $validation->has_error ) { if ( $validation->has_error ) {
@ -51,7 +51,7 @@ sub register {
customer => { customer => {
username => $validation->param('name'), username => $validation->param('name'),
postcode => $validation->param('postcode'), postcode => $validation->param('postcode'),
agerange_fk => $validation->param('agerange'), age_range_id => $validation->param('agerange'),
}, },
}); });
if ( $new_user->in_storage ) { if ( $new_user->in_storage ) {

View file

@ -1,107 +1,29 @@
use utf8; use utf8;
package Pear::LocalLoop::Schema::Result::AgeRange; package Pear::LocalLoop::Schema::Result::AgeRange;
# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE
=head1 NAME
Pear::LocalLoop::Schema::Result::AgeRange
=cut
use strict; use strict;
use warnings; use warnings;
use base 'DBIx::Class::Core'; use base 'DBIx::Class::Core';
=head1 COMPONENTS LOADED __PACKAGE__->table("age_ranges");
=over 4
=item * L<DBIx::Class::InflateColumn::DateTime>
=back
=cut
__PACKAGE__->load_components("InflateColumn::DateTime");
=head1 TABLE: C<AgeRanges>
=cut
__PACKAGE__->table("AgeRanges");
=head1 ACCESSORS
=head2 agerangeid
data_type: 'integer'
is_auto_increment: 1
is_nullable: 0
=head2 agerangestring
data_type: 'text'
is_nullable: 0
=cut
__PACKAGE__->add_columns( __PACKAGE__->add_columns(
"agerangeid", "id",
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
"agerangestring", "string",
{ data_type => "text", is_nullable => 0 }, { data_type => "text", is_nullable => 0 },
); );
=head1 PRIMARY KEY __PACKAGE__->set_primary_key("id");
=over 4 __PACKAGE__->add_unique_constraint(["string"]);
=item * L</agerangeid>
=back
=cut
__PACKAGE__->set_primary_key("agerangeid");
=head1 UNIQUE CONSTRAINTS
=head2 C<agerangestring_unique>
=over 4
=item * L</agerangestring>
=back
=cut
__PACKAGE__->add_unique_constraint("agerangestring_unique", ["agerangestring"]);
=head1 RELATIONS
=head2 customers
Type: has_many
Related object: L<Pear::LocalLoop::Schema::Result::Customer>
=cut
__PACKAGE__->has_many( __PACKAGE__->has_many(
"customers", "customers",
"Pear::LocalLoop::Schema::Result::Customer", "Pear::LocalLoop::Schema::Result::Customer",
{ "foreign.agerange_fk" => "self.agerangeid" }, { "foreign.age_range_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 }, { cascade_copy => 0, cascade_delete => 0 },
); );
# Created by DBIx::Class::Schema::Loader v0.07046 @ 2017-02-24 17:32:21
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:4zGNm0RlwptF9hlj9oErVA
# You can replace this text with custom code or comments, and it will be preserved on regeneration
1; 1;

View file

@ -1,137 +1,52 @@
use utf8; use utf8;
package Pear::LocalLoop::Schema::Result::Customer; package Pear::LocalLoop::Schema::Result::Customer;
# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE
=head1 NAME
Pear::LocalLoop::Schema::Result::Customer
=cut
use strict; use strict;
use warnings; use warnings;
use base 'DBIx::Class::Core'; use base 'DBIx::Class::Core';
=head1 COMPONENTS LOADED __PACKAGE__->table("customers");
=over 4
=item * L<DBIx::Class::InflateColumn::DateTime>
=back
=cut
__PACKAGE__->load_components("InflateColumn::DateTime");
=head1 TABLE: C<Customers>
=cut
__PACKAGE__->table("Customers");
=head1 ACCESSORS
=head2 customerid
data_type: 'integer'
is_auto_increment: 1
is_nullable: 0
=head2 username
data_type: 'text'
is_nullable: 0
=head2 agerange_fk
data_type: 'integer'
is_foreign_key: 1
is_nullable: 0
=head2 postcode
data_type: 'text'
is_nullable: 0
=cut
__PACKAGE__->add_columns( __PACKAGE__->add_columns(
"customerid", "id" => {
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, data_type => "integer",
"username", is_auto_increment => 1,
{ data_type => "text", is_nullable => 0 }, is_nullable => 0,
"agerange_fk", },
{ data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, "username" => {
"postcode", data_type => "varchar",
{ data_type => "text", is_nullable => 0 }, size => 255,
is_nullable => 0,
},
"age_range_id" => {
data_type => "integer",
is_foreign_key => 1,
is_nullable => 0,
},
"postcode" => {
data_type => "varchar",
size => 16,
is_nullable => 0,
},
); );
=head1 PRIMARY KEY __PACKAGE__->set_primary_key("id");
=over 4 __PACKAGE__->add_unique_constraint(["username"]);
=item * L</customerid>
=back
=cut
__PACKAGE__->set_primary_key("customerid");
=head1 UNIQUE CONSTRAINTS
=head2 C<username_unique>
=over 4
=item * L</username>
=back
=cut
__PACKAGE__->add_unique_constraint("username_unique", ["username"]);
=head1 RELATIONS
=head2 agerange_fk
Type: belongs_to
Related object: L<Pear::LocalLoop::Schema::Result::AgeRange>
=cut
__PACKAGE__->belongs_to( __PACKAGE__->belongs_to(
"agerange_fk", "age_range",
"Pear::LocalLoop::Schema::Result::AgeRange", "Pear::LocalLoop::Schema::Result::AgeRange",
{ agerangeid => "agerange_fk" }, { id => "age_range_id" },
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
); );
=head2 user
Type: might_have
Related object: L<Pear::LocalLoop::Schema::Result::User>
=cut
__PACKAGE__->might_have( __PACKAGE__->might_have(
"user", "user",
"Pear::LocalLoop::Schema::Result::User", "Pear::LocalLoop::Schema::Result::User",
{ "foreign.customer_id" => "self.customerid" }, { "foreign.customer_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 }, { cascade_copy => 0, cascade_delete => 0 },
); );
# Created by DBIx::Class::Schema::Loader v0.07046 @ 2017-02-24 17:32:21
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ocoCGZYvw9O9wxzr14okiQ
# You can replace this text with custom code or comments, and it will be preserved on regeneration
1; 1;

View file

@ -70,7 +70,7 @@ __PACKAGE__->might_have(
__PACKAGE__->belongs_to( __PACKAGE__->belongs_to(
"customer", "customer",
"Pear::LocalLoop::Schema::Result::Customer", "Pear::LocalLoop::Schema::Result::Customer",
{ customerid => "customer_id" }, { id => "customer_id" },
{ {
is_deferrable => 0, is_deferrable => 0,
join_type => "LEFT", join_type => "LEFT",

View file

@ -29,7 +29,7 @@ has framework => sub {
$schema->deploy; $schema->deploy;
$schema->resultset('AgeRange')->populate([ $schema->resultset('AgeRange')->populate([
[ qw/ agerangestring / ], [ qw/ string / ],
[ '20-35' ], [ '20-35' ],
[ '35-50' ], [ '35-50' ],
[ '50+' ], [ '50+' ],

View file

@ -29,10 +29,10 @@ my $testJson = {
'email' => $emailReno, 'email' => $emailReno,
'postcode' => 'E1 MP01', 'postcode' => 'E1 MP01',
'password' => $passwordReno, 'password' => $passwordReno,
'age' => '20-35' 'age' => 1
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)->or($dump_error)
->json_is('/success', Mojo::JSON->true); ->json_is('/success', Mojo::JSON->true);
print "test 2 - Create organisation user account (Choco Billy)\n"; print "test 2 - Create organisation user account (Choco Billy)\n";
@ -62,7 +62,7 @@ $testJson = {
'email' => $emailAdmin, 'email' => $emailAdmin,
'postcode' => 'NW1 W01', 'postcode' => 'NW1 W01',
'password' => $passwordAdmin, 'password' => $passwordAdmin,
'age' => '35-50' 'age' => 2
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)

View file

@ -29,7 +29,7 @@ my $testJson = {
'email' => $emailReno, 'email' => $emailReno,
'postcode' => 'E1 MP01', 'postcode' => 'E1 MP01',
'password' => $passwordReno, 'password' => $passwordReno,
'age' => '20-35' 'age' => 1
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)
@ -63,7 +63,7 @@ $testJson = {
'email' => $emailAdmin, 'email' => $emailAdmin,
'postcode' => 'NW1 W01', 'postcode' => 'NW1 W01',
'password' => $passwordAdmin, 'password' => $passwordAdmin,
'age' => '35-50' 'age' => 2
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)

View file

@ -23,7 +23,7 @@ my $test_json = {
'email' => $email, 'email' => $email,
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => $password, 'password' => $password,
'age' => '20-35' 'age' => 1
}; };
$t->post_ok('/api/register' => json => $test_json) $t->post_ok('/api/register' => json => $test_json)
->status_is(200) ->status_is(200)

View file

@ -36,7 +36,7 @@ $testJson = {
'email' => shift(@emails), 'email' => shift(@emails),
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '50+' 'age' => 3
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -52,7 +52,7 @@ $testJson = {
'email' => shift(@emails), 'email' => shift(@emails),
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '50+' 'age' => 3
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(401) ->status_is(401)
@ -67,7 +67,7 @@ $testJson = {
'email' => shift(@emails), 'email' => shift(@emails),
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '50+' 'age' => 3
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -83,7 +83,7 @@ $testJson = {
'email' => shift(@emails), 'email' => shift(@emails),
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '50+' 'age' => 3
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -99,7 +99,7 @@ $testJson = {
'email' => shift(@emails), 'email' => shift(@emails),
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '50+' 'age' => 3
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -117,7 +117,7 @@ $testJson = {
'email' => $emailToReuse, 'email' => $emailToReuse,
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '50+' 'age' => 3
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)
@ -131,7 +131,7 @@ $testJson = {
'email' => shift(@emails), 'email' => shift(@emails),
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '35-50' 'age' => 2
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)
@ -145,7 +145,7 @@ $testJson = {
'email' => shift(@emails), 'email' => shift(@emails),
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '20-35' 'age' => 1
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)
@ -159,7 +159,7 @@ $testJson = {
'email' => shift(@emails), 'email' => shift(@emails),
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '50+' 'age' => 3
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(403) ->status_is(403)
@ -173,7 +173,7 @@ $testJson = {
'username' => shift(@names), 'username' => shift(@names),
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '50+' 'age' => 3
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -188,7 +188,7 @@ $testJson = {
'email' => 'dfsd@.com', 'email' => 'dfsd@.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '35-50' 'age' => 2
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -204,7 +204,7 @@ $testJson = {
'email' => 'dfsd@com', 'email' => 'dfsd@com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '35-50' 'age' => 2
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -220,7 +220,7 @@ $testJson = {
'email' => $emailToReuse, 'email' => $emailToReuse,
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '35-50' 'age' => 2
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(403) ->status_is(403)
@ -235,7 +235,7 @@ $testJson = {
'username' => shift(@names), 'username' => shift(@names),
'email' => shift(@emails), 'email' => shift(@emails),
'password' => 'Meh', 'password' => 'Meh',
'age' => '50+' 'age' => 3
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -251,7 +251,7 @@ $testJson = {
'username' => shift(@names), 'username' => shift(@names),
'email' => shift(@emails), 'email' => shift(@emails),
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'age' => '50+' 'age' => 3
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -267,7 +267,7 @@ $testJson = {
'email' => shift(@emails), 'email' => shift(@emails),
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age' => '50+' 'age' => 3
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)

View file

@ -34,7 +34,7 @@ my $testJson = {
'email' => $emailRufus, 'email' => $emailRufus,
'postcode' => 'LA1 1CF', 'postcode' => 'LA1 1CF',
'password' => $passwordRufus, 'password' => $passwordRufus,
'age' => '20-35' 'age' => 1
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)

View file

@ -42,7 +42,7 @@ my $testJson = {
'email' => $emailRufus, 'email' => $emailRufus,
'postcode' => 'E1 MP01', 'postcode' => 'E1 MP01',
'password' => $passwordRufus, 'password' => $passwordRufus,
'age' => '20-35' 'age' => 1
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)
@ -58,7 +58,7 @@ $testJson = {
'email' => $emailHojo, 'email' => $emailHojo,
'postcode' => 'E1 MP01', 'postcode' => 'E1 MP01',
'password' => $passwordHojo, 'password' => $passwordHojo,
'age' => '35-50' 'age' => 1
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)

View file

@ -73,8 +73,8 @@ body {
<label for="register-age">Age Range</label> <label for="register-age">Age Range</label>
<select id="register-age" class="form-control" name="agerange" required> <select id="register-age" class="form-control" name="agerange" required>
% for my $agerange ( @$ageranges ) { % for my $agerange ( @$ageranges ) {
<option value="<%= $agerange->{agerangeid} %>"<%= defined $form_data->{agerange} && $form_data->{agerange} eq $agerange->{agerangeid} ? ' selected' : '' %>> <option value="<%= $agerange->{id} %>"<%= defined $form_data->{agerange} && $form_data->{agerange} eq $agerange->{id} ? ' selected' : '' %>>
<%= $agerange->{agerangestring} %> <%= $agerange->{string} %>
</option> </option>
% } % }
</select> </select>