Finish major rework of schema
This commit is contained in:
parent
dd7862d93d
commit
3be6289fc3
15 changed files with 157 additions and 318 deletions
|
@ -105,15 +105,15 @@ sub post_admin_merge {
|
|||
sub copy_transactions_and_delete {
|
||||
my ( $c, $from_org, $to_org ) = @_;
|
||||
|
||||
my $from_org_transaction_rs = $from_org->pending_transactions;
|
||||
my $from_org_transaction_rs = $from_org->transactions;
|
||||
|
||||
while ( my $from_org_transaction = $from_org_transaction_rs->next ) {
|
||||
$to_org->create_related(
|
||||
'transactions', {
|
||||
buyeruserid_fk => $from_org_transaction->buyeruserid_fk,
|
||||
valuemicrocurrency => $from_org_transaction->valuemicrocurrency,
|
||||
proof_image => $from_org_transaction->proof_image,
|
||||
timedatesubmitted => $from_org_transaction->timedatesubmitted,
|
||||
buyer_id => $from_org_transaction->buyer_id,
|
||||
value => $from_org_transaction->value,
|
||||
proof_image => $from_org_transaction->proof_image,
|
||||
submitted_at => $from_org_transaction->submitted_at,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ sub auth {
|
|||
my $session_key = $c->stash->{api_json}->{session_key};
|
||||
|
||||
if ( defined $session_key ) {
|
||||
my $session_result = $c->schema->resultset('SessionToken')->find({ sessiontokenname => $session_key });
|
||||
my $session_result = $c->schema->resultset('SessionToken')->find({ token => $session_key });
|
||||
|
||||
if ( defined $session_result ) {
|
||||
$c->stash( api_user => $session_result->user );
|
||||
|
@ -78,7 +78,7 @@ sub post_login {
|
|||
|
||||
if ( defined $user_result ) {
|
||||
if ( $user_result->check_password($password) ) {
|
||||
my $session_key = $c->generate_session( $user_result->id );
|
||||
my $session_key = $user_result->generate_session;
|
||||
|
||||
return $c->render( json => {
|
||||
success => Mojo::JSON->true,
|
||||
|
@ -101,7 +101,7 @@ sub post_logout {
|
|||
|
||||
my $session_key = $c->req->json( '/session_key' );
|
||||
|
||||
my $session_result = $c->schema->resultset('SessionToken')->find({ sessiontokenname => $session_key });
|
||||
my $session_result = $c->schema->resultset('SessionToken')->find({ token => $session_key });
|
||||
|
||||
if ( defined $session_result ) {
|
||||
$session_result->delete;
|
||||
|
|
|
@ -30,6 +30,7 @@ has error_messages => sub {
|
|||
},
|
||||
age => {
|
||||
required => { message => 'No age sent.', status => 400 },
|
||||
number => { message => 'Age range is invalid', status => 400 },
|
||||
in_resultset => { message => 'Age range is invalid.', status => 400 },
|
||||
},
|
||||
street_name => {
|
||||
|
@ -79,7 +80,7 @@ sub post_register{
|
|||
if ( $usertype eq 'customer' ) {
|
||||
|
||||
my $age_rs = $c->schema->resultset('AgeRange');
|
||||
$validation->required('age')->in_resultset('id', $age_rs);
|
||||
$validation->required('age')->number->in_resultset('id', $age_rs);
|
||||
|
||||
} elsif ( $usertype eq 'organisation' ) {
|
||||
|
||||
|
|
|
@ -75,7 +75,6 @@ has error_messages => sub {
|
|||
|
||||
sub post_upload {
|
||||
my $c = shift;
|
||||
my $self = $c;
|
||||
|
||||
my $user = $c->stash->{api_user};
|
||||
|
||||
|
@ -94,75 +93,62 @@ sub post_upload {
|
|||
|
||||
my $type = $validation->param('transaction_type');
|
||||
|
||||
my $organisation;
|
||||
|
||||
if ( $type == 1 ) {
|
||||
# Validated Organisation
|
||||
my $valid_org_rs = $c->schema->resultset('Organisation');
|
||||
$validation->required('organisation_id')->number->in_resultset( 'id', $valid_org_rs );
|
||||
|
||||
return $c->api_validation_error if $validation->has_error;
|
||||
|
||||
$organisation = $valid_org_rs->find( $validation->param('organisation_id') );
|
||||
|
||||
} elsif ( $type == 2 ) {
|
||||
# Unvalidated Organisation
|
||||
my $valid_org_rs = $c->schema->resultset('PendingOrganisation')->search({ submitted_by_id => $user->id });
|
||||
$validation->required('organisation_id')->number->in_resultset( 'id', $valid_org_rs );
|
||||
|
||||
return $c->api_validation_error if $validation->has_error;
|
||||
|
||||
$organisation = $valid_org_rs->find( $validation->param('organisation_id') );
|
||||
|
||||
} elsif ( $type == 3 ) {
|
||||
# Unknown Organisation
|
||||
$validation->required('organisation_name');
|
||||
$validation->optional('street_name');
|
||||
$validation->optional('town');
|
||||
$validation->optional('postcode')->postcode;
|
||||
}
|
||||
|
||||
return $c->api_validation_error if $validation->has_error;
|
||||
|
||||
return $c->api_validation_error if $validation->has_error;
|
||||
$organisation = $c->schema->resultset('PendingOrganisation')->create({
|
||||
submitted_by => $user,
|
||||
submitted_at => DateTime->now,
|
||||
name => $validation->param('organisation_name'),
|
||||
street_name => $validation->param('street_name'),
|
||||
town => $validation->param('town'),
|
||||
postcode => $validation->param('postcode'),
|
||||
});
|
||||
}
|
||||
|
||||
my $transaction_value = $validation->param('transaction_value');
|
||||
my $upload = $validation->param('file');
|
||||
|
||||
my $file = $c->store_file_from_upload( $upload );
|
||||
|
||||
if ( $type == 1 ) {
|
||||
# Validated organisation
|
||||
$c->schema->resultset('Transaction')->create({
|
||||
buyeruserid_fk => $user->id,
|
||||
sellerorganisationid_fk => $validation->param('organisation_id'),
|
||||
valuemicrocurrency => $transaction_value,
|
||||
$organisation->create_related(
|
||||
'transactions',
|
||||
{
|
||||
buyer => $user,
|
||||
value => $transaction_value,
|
||||
proof_image => $file,
|
||||
timedatesubmitted => DateTime->now,
|
||||
});
|
||||
} elsif ( $type == 2 ) {
|
||||
# Unvalidated Organisation
|
||||
$c->schema->resultset('PendingTransaction')->create({
|
||||
buyeruserid_fk => $user->id,
|
||||
pendingsellerorganisationid_fk => $validation->param('organisation_id'),
|
||||
valuemicrocurrency => $transaction_value,
|
||||
proof_image => $file,
|
||||
timedatesubmitted => DateTime->now,
|
||||
});
|
||||
} elsif ( $type == 3 ) {
|
||||
my $organisation_name = $validation->param('organisation_name');
|
||||
my $street_name = $validation->param('street_name');
|
||||
my $town = $validation->param('town');
|
||||
my $postcode = $validation->param('postcode');
|
||||
}
|
||||
);
|
||||
|
||||
my $pending_org = $c->schema->resultset('PendingOrganisation')->create({
|
||||
submitted_by => $user,
|
||||
submitted_at => DateTime->now,
|
||||
name => $organisation_name,
|
||||
street_name => $street_name,
|
||||
town => $town,
|
||||
postcode => $postcode,
|
||||
});
|
||||
|
||||
$c->schema->resultset('PendingTransaction')->create({
|
||||
buyeruserid_fk => $user->id,
|
||||
pendingsellerorganisationid_fk => $pending_org->id,
|
||||
valuemicrocurrency => $transaction_value,
|
||||
proof_image => $file,
|
||||
timedatesubmitted => DateTime->now,
|
||||
});
|
||||
}
|
||||
return $self->render( json => {
|
||||
return $c->render( json => {
|
||||
success => Mojo::JSON->true,
|
||||
message => 'Upload Successful',
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
Reference in a new issue