2017-04-06 21:43:27 +00:00
|
|
|
package Pear::LocalLoop::Controller::Api::Upload;
|
2017-02-24 19:27:43 +00:00
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
use Data::Dumper;
|
|
|
|
|
2017-04-04 21:22:22 +00:00
|
|
|
=head2 post_upload
|
|
|
|
|
2017-04-20 11:33:56 +00:00
|
|
|
Takes a file upload, with a file key of 'file', and a json string under the
|
2017-04-04 21:22:22 +00:00
|
|
|
'json' key.
|
|
|
|
|
|
|
|
The json string should be an object, with the following keys:
|
|
|
|
|
|
|
|
=over
|
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
=item * transaction_value
|
2017-04-04 21:22:22 +00:00
|
|
|
|
|
|
|
The value of the transaction
|
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
=item * transaction_type
|
2017-04-04 21:22:22 +00:00
|
|
|
|
|
|
|
Is a value of 1, 2, or 3 - depending on the type of transaction.
|
|
|
|
|
2017-08-14 10:15:33 +00:00
|
|
|
=item * purchase_time
|
|
|
|
|
|
|
|
Is a DateTime value submitted for when the receipt is from.
|
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
=item * organisation_id
|
2017-04-04 21:22:22 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
An ID of a valid organisation. used when transaction_type is 1 or 2.
|
2017-04-04 21:22:22 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
=item * organisation_name
|
2017-04-04 21:22:22 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
The name of an organisation. Used when transaction_type is 3.
|
2017-04-04 21:22:22 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
=item * street_name
|
2017-04-04 21:22:22 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
The street of an organisation, optional key. Used when transaction_type is 3.
|
2017-04-04 21:22:22 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
=item * town
|
2017-04-04 21:22:22 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
The village/town/city of an organisation. Used when transaction_type is 3.
|
2017-04-04 21:22:22 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
=item * postcode
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
The postcode of an organisation, optional key. Used when transaction_Type is 3.
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
=back
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
=cut
|
|
|
|
|
|
|
|
has error_messages => sub {
|
|
|
|
return {
|
2017-04-20 11:33:56 +00:00
|
|
|
transaction_type => {
|
|
|
|
required => { message => 'transaction_type is missing.', status => 400 },
|
|
|
|
in => { message => 'transaction_type is not a valid value.', status => 400 },
|
2017-02-24 19:27:43 +00:00
|
|
|
},
|
2017-04-20 11:33:56 +00:00
|
|
|
transaction_value => {
|
|
|
|
required => { message => 'transaction_value is missing', status => 400 },
|
|
|
|
number => { message => 'transaction_value does not look like a number', status => 400 },
|
|
|
|
gt_num => { message => 'transaction_value cannot be equal to or less than zero', status => 400 },
|
2017-02-24 19:27:43 +00:00
|
|
|
},
|
2017-08-14 10:15:33 +00:00
|
|
|
purchase_time => {
|
|
|
|
required => { message => 'purchase_time is missing', status => 400 },
|
2017-08-17 15:34:43 +00:00
|
|
|
is_full_iso_datetime => { message => 'purchase_time is in incorrect format', status => 400 },
|
2017-08-14 10:15:33 +00:00
|
|
|
},
|
2017-04-20 11:33:56 +00:00
|
|
|
file => {
|
2017-04-20 00:27:18 +00:00
|
|
|
required => { message => 'No file uploaded', status => 400 },
|
2017-04-20 13:02:30 +00:00
|
|
|
upload => { message => 'file key does not contain a file', status => 400 },
|
2017-04-20 12:58:45 +00:00
|
|
|
filetype => { message => 'File must be of type image/jpeg', status => 400 },
|
2017-02-24 19:27:43 +00:00
|
|
|
},
|
2017-04-20 11:33:56 +00:00
|
|
|
organisation_id => {
|
|
|
|
required => { message => 'organisation_id is missing', status => 400 },
|
2017-04-20 00:27:18 +00:00
|
|
|
number => { message => 'organisation_id is not a number', status => 400 },
|
2017-04-20 11:33:56 +00:00
|
|
|
in_resultset => { message => 'organisation_id does not exist in the database', status => 400 },
|
2017-02-24 19:27:43 +00:00
|
|
|
},
|
2017-04-20 11:33:56 +00:00
|
|
|
organisation_name => {
|
|
|
|
required => { message => 'organisation_name is missing', status => 400 },
|
2017-04-20 00:27:18 +00:00
|
|
|
},
|
2017-04-21 16:23:41 +00:00
|
|
|
search_name => {
|
|
|
|
required => { message => 'search_name is missing', status => 400 },
|
|
|
|
},
|
2017-05-15 21:43:31 +00:00
|
|
|
postcode => {
|
|
|
|
required => { message => 'postcode is missing', status => 400 },
|
|
|
|
postcode => { message => 'postcode must be valid', status => 400 },
|
|
|
|
},
|
2017-04-20 00:27:18 +00:00
|
|
|
};
|
|
|
|
};
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
sub post_upload {
|
|
|
|
my $c = shift;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
my $user = $c->stash->{api_user};
|
|
|
|
|
|
|
|
my $validation = $c->validation;
|
|
|
|
|
|
|
|
# Test for file before loading the JSON in to the validator
|
2017-04-20 12:58:45 +00:00
|
|
|
$validation->required('file')->upload->filetype('image/jpeg');
|
2017-04-20 00:27:18 +00:00
|
|
|
|
|
|
|
$validation->input( $c->stash->{api_json} );
|
|
|
|
|
2017-04-20 11:33:56 +00:00
|
|
|
$validation->required('transaction_value')->number->gt_num(0);
|
|
|
|
$validation->required('transaction_type')->in( 1, 2, 3 );
|
2017-04-20 00:27:18 +00:00
|
|
|
|
2017-08-14 14:35:23 +00:00
|
|
|
#Check a proper purchase time was submitted
|
2017-08-14 15:21:06 +00:00
|
|
|
$validation->optional('purchase_time')->is_full_iso_datetime;
|
2017-08-14 10:15:33 +00:00
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
# First pass of required items
|
|
|
|
return $c->api_validation_error if $validation->has_error;
|
|
|
|
|
2017-04-20 11:33:56 +00:00
|
|
|
my $type = $validation->param('transaction_type');
|
2017-04-20 00:27:18 +00:00
|
|
|
|
2017-04-21 21:12:53 +00:00
|
|
|
my $organisation;
|
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
if ( $type == 1 ) {
|
|
|
|
# Validated Organisation
|
|
|
|
my $valid_org_rs = $c->schema->resultset('Organisation');
|
2017-04-20 21:17:13 +00:00
|
|
|
$validation->required('organisation_id')->number->in_resultset( 'id', $valid_org_rs );
|
2017-04-21 21:12:53 +00:00
|
|
|
|
|
|
|
return $c->api_validation_error if $validation->has_error;
|
|
|
|
|
|
|
|
$organisation = $valid_org_rs->find( $validation->param('organisation_id') );
|
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
} elsif ( $type == 2 ) {
|
|
|
|
# Unvalidated Organisation
|
2017-04-21 13:14:51 +00:00
|
|
|
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 );
|
2017-08-14 10:15:33 +00:00
|
|
|
|
2017-04-21 21:12:53 +00:00
|
|
|
return $c->api_validation_error if $validation->has_error;
|
|
|
|
|
|
|
|
$organisation = $valid_org_rs->find( $validation->param('organisation_id') );
|
|
|
|
|
2017-04-20 00:27:18 +00:00
|
|
|
} elsif ( $type == 3 ) {
|
|
|
|
# Unknown Organisation
|
2017-04-20 11:33:56 +00:00
|
|
|
$validation->required('organisation_name');
|
|
|
|
$validation->optional('street_name');
|
2017-04-20 00:27:18 +00:00
|
|
|
$validation->optional('town');
|
|
|
|
$validation->optional('postcode')->postcode;
|
2017-08-14 10:15:33 +00:00
|
|
|
|
2017-04-21 21:12:53 +00:00
|
|
|
return $c->api_validation_error if $validation->has_error;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-21 21:12:53 +00:00
|
|
|
$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'),
|
|
|
|
});
|
|
|
|
}
|
2017-04-20 00:27:18 +00:00
|
|
|
|
2017-08-17 14:38:08 +00:00
|
|
|
unless ( defined $organisation ) {
|
2017-08-17 14:45:26 +00:00
|
|
|
return $c->render(
|
|
|
|
json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => 'Error Finding Organisation',
|
|
|
|
error => 'organisation_error',
|
|
|
|
},
|
|
|
|
status => 400,
|
|
|
|
);
|
2017-08-17 14:38:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 11:33:56 +00:00
|
|
|
my $transaction_value = $validation->param('transaction_value');
|
2017-04-21 18:54:28 +00:00
|
|
|
my $upload = $validation->param('file');
|
2017-08-14 15:21:06 +00:00
|
|
|
my $purchase_time = $c->parse_iso_datetime($validation->param('purchase_time') || '');
|
|
|
|
$purchase_time ||= DateTime->now();
|
2017-04-21 18:54:28 +00:00
|
|
|
my $file = $c->store_file_from_upload( $upload );
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-08-17 14:38:08 +00:00
|
|
|
my $new_transaction = $organisation->create_related(
|
2017-04-21 21:12:53 +00:00
|
|
|
'transactions',
|
|
|
|
{
|
|
|
|
buyer => $user,
|
|
|
|
value => $transaction_value,
|
2017-04-21 18:54:28 +00:00
|
|
|
proof_image => $file,
|
2017-08-14 11:45:54 +00:00
|
|
|
purchase_time => $c->format_db_datetime($purchase_time),
|
2017-04-21 21:12:53 +00:00
|
|
|
}
|
|
|
|
);
|
2017-04-20 00:27:18 +00:00
|
|
|
|
2017-08-17 14:38:08 +00:00
|
|
|
unless ( defined $new_transaction ) {
|
2017-08-17 14:45:26 +00:00
|
|
|
return $c->render(
|
|
|
|
json => {
|
|
|
|
success => Mojo::JSON->false,
|
|
|
|
message => 'Error Adding Transaction',
|
|
|
|
error => 'transaction_error',
|
|
|
|
},
|
|
|
|
status => 400,
|
|
|
|
);
|
2017-08-17 14:38:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 21:12:53 +00:00
|
|
|
return $c->render( json => {
|
2017-04-20 00:27:18 +00:00
|
|
|
success => Mojo::JSON->true,
|
|
|
|
message => 'Upload Successful',
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 16:23:41 +00:00
|
|
|
# TODO Limit search results, possibly paginate them?
|
|
|
|
# TODO Search by location as well
|
2017-02-24 19:27:43 +00:00
|
|
|
sub post_search {
|
2017-04-21 16:23:41 +00:00
|
|
|
my $c = shift;
|
|
|
|
my $self = $c;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-21 16:23:41 +00:00
|
|
|
my $validation = $c->validation;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-21 16:23:41 +00:00
|
|
|
$validation->input( $c->stash->{api_json} );
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-21 16:23:41 +00:00
|
|
|
$validation->required('search_name');
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-21 16:23:41 +00:00
|
|
|
return $c->api_validation_error if $validation->has_error;
|
2017-02-24 19:27:43 +00:00
|
|
|
|
2017-04-21 16:23:41 +00:00
|
|
|
my $search_name = $validation->param('search_name');
|
|
|
|
|
2017-07-25 13:14:30 +00:00
|
|
|
my $search_stmt = [ 'LOWER("name") LIKE ?', '%' . lc $search_name . '%' ];
|
|
|
|
|
2017-04-21 16:23:41 +00:00
|
|
|
my $valid_orgs_rs = $c->schema->resultset('Organisation')->search(
|
2017-07-25 13:14:30 +00:00
|
|
|
\$search_stmt,
|
2017-04-21 16:23:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
my $pending_orgs_rs = $c->stash->{api_user}->pending_organisations->search(
|
2017-07-25 13:14:30 +00:00
|
|
|
\$search_stmt,
|
2017-04-21 16:23:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
my @valid_orgs = (
|
|
|
|
map {{
|
|
|
|
id => $_->id,
|
|
|
|
name => $_->name,
|
|
|
|
street_name => $_->street_name,
|
|
|
|
town => $_->town,
|
|
|
|
postcode => $_->postcode,
|
|
|
|
}} $valid_orgs_rs->all
|
|
|
|
);
|
|
|
|
|
|
|
|
my @pending_orgs = (
|
|
|
|
map {{
|
|
|
|
id => $_->id,
|
|
|
|
name => $_->name,
|
|
|
|
street_name => $_->street_name,
|
|
|
|
town => $_->town,
|
|
|
|
postcode => $_->postcode,
|
|
|
|
}} $pending_orgs_rs->all
|
|
|
|
);
|
2017-02-24 19:27:43 +00:00
|
|
|
|
|
|
|
return $self->render( json => {
|
|
|
|
success => Mojo::JSON->true,
|
2017-04-21 16:23:41 +00:00
|
|
|
validated => \@valid_orgs,
|
|
|
|
unvalidated => \@pending_orgs,
|
|
|
|
});
|
2017-02-24 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|