Merge pull request #63 from Pear-Trading/TBSliver/Postcode-Location

Added lat/long to customers and organisations
This commit is contained in:
Tom Bloor 2017-09-28 14:42:57 +01:00 committed by GitHub
commit 005c4694ff
99 changed files with 7149 additions and 77 deletions

4
.gitignore vendored
View file

@ -8,3 +8,7 @@ hypnotoad.pid
cover_db/
schema.png
etc/code-point-open/codepo_gb/
pear-local_loop.production.conf

View file

@ -2,6 +2,9 @@
# Next Release
* **Feature:** lat/long locations on customers and organisations
* **Feature:** Suppliers map co-ords
# v0.9.2
* **Fix:** Leaderboard total calculations not mapped correctly

View file

@ -5,3 +5,20 @@
*Master:* [![Build Status](https://travis-ci.org/Pear-Trading/Foodloop-Server.svg?branch=master)](https://travis-ci.org/Pear-Trading/Foodloop-Server)
*Development:* [![Build Status](https://travis-ci.org/Pear-Trading/Foodloop-Server.svg?branch=development)](https://travis-ci.org/Pear-Trading/Foodloop-Server)
# Testing
To run the main test framework, first install all the dependencies, then run the tests:
```
cpanm --installdeps .
prove -lr
```
To run the main framework against a PostgreSQL backend, assuming you have postgres installed, you will need some extra dependencies first:
```
cpanm --installdeps . --with-feature postgres
PEAR_TEST_PG=1 prove -lr
```

View file

@ -21,8 +21,19 @@ requires 'MooX::Options::Actions';
requires 'Module::Runtime';
requires 'DBIx::Class::DeploymentHandler';
requires 'DBIx::Class::Fixtures';
requires 'GIS::Distance';
on 'schema-graph' => sub {
feature 'schema-graph', 'Draw diagrams of Schema' => sub {
requires 'GraphViz';
requires 'SQL::Translator';
};
feature 'postgres', 'PostgreSQL Support' => sub {
requires 'DBD::Pg';
requires 'Test::PostgreSQL';
};
feature 'codepoint-open', 'Code Point Open manipulation' => sub {
requires 'Geo::UK::Postcode::CodePointOpen';
};

View file

@ -0,0 +1,10 @@
ORDNANCE SURVEY DATA LICENCE
Your use of data is subject to terms at www.ordnancesurvey.co.uk/opendata/licence.
Contains Ordnance Survey data © Crown copyright and database right 2017.
Contains Royal Mail data © Royal Mail copyright and database right 2017.
Contains National Statistics data © Crown copyright and database right 2017.
August 2017

Binary file not shown.

View file

@ -148,6 +148,10 @@ sub startup {
my $api_v1 = $api->under('/v1');
my $api_v1_supplier = $api_v1->under('/supplier');
$api_v1_supplier->post('/location')->to('api-v1-supplier-location#index');
my $api_v1_org = $api_v1->under('/organisation')->to('api-v1-organisation#auth');
$api_v1_org->post('/graphs')->to('api-v1-organisation-graphs#index');

View file

@ -0,0 +1,64 @@
package Pear::LocalLoop::Command::codepoint_open;
use Mojo::Base 'Mojolicious::Command';
use Mojo::Util 'getopt';
use Geo::UK::Postcode::CodePointOpen;
has description => 'Manage Codepoint Open Data';
has usage => sub { shift->extract_usage };
sub run {
my ( $self, @args ) = @_;
getopt \@args,
'o|outcodes=s' => \my @outcodes,
'q|quiet' => \my $quiet_mode;
my $cpo_dir = $self->app->home->child('etc')->child('code-point-open');
my $zip_file = $cpo_dir->child('codepo_gb.zip')->realpath->to_string;
my $output_dir = $cpo_dir->child('codepo_gb')->realpath->to_string;
unless ( -d $output_dir ) {
print "Unzipping code-point-open data\n" unless $quiet_mode;
system( 'unzip', '-q', $zip_file, '-d', $output_dir );
}
my $cpo = Geo::UK::Postcode::CodePointOpen->new( path => $output_dir );
printf( "Importing data for %s outcode(s)\n", @outcodes ? join( ' ', @outcodes ) : 'all' )
unless $quiet_mode;
my $iter = $cpo->read_iterator(
outcodes => \@outcodes,
include_lat_long => 1,
split_postcode => 1,
);
my $pc_rs = $self->app->schema->resultset('GbPostcode');
while ( my $pc = $iter->() ) {
$pc_rs->find_or_create(
{
outcode => $pc->{Outcode},
incode => $pc->{Incode},
latitude => $pc->{Latitude},
longitude => $pc->{Longitude},
},
{ key => 'primary' },
);
}
}
=head1 SYNOPSIS
Usage: APPLICATION codepoint_open [OPTIONS]
Options:
-o|--outcodes <outcode> : limit to specified outcodes (can be defined
multiple times)
=cut
1;

View file

@ -0,0 +1,83 @@
package Pear::LocalLoop::Command::latlong_setup;
use Mojo::Base 'Mojolicious::Command';
use Mojo::Util 'getopt';
use Geo::UK::Postcode::Regex;
use GIS::Distance;
has description => 'Set lat/long data on customers and orgs';
has usage => sub { shift->extract_usage };
sub run {
my ( $self, @args ) = @_;
my $customer_rs = $self->app->schema->resultset('Customer');
my $org_rs = $self->app->schema->resultset('Organisation');
for my $result ( $customer_rs->all, $org_rs->all ) {
$self->_set_lat_long_for_result( $result );
}
my $transaction_rs = $self->app->schema->resultset('Transaction');
for my $result ( $transaction_rs->all ) {
my $distance = $self->_calculate_distance(
$result->buyer->${\$result->buyer->type},
$result->seller->${\$result->seller->type},
);
$result->update({ distance => $distance }) if defined $distance;
}
}
sub _set_lat_long_for_result {
my ( $self, $result ) = @_;
my $parsed_postcode = Geo::UK::Postcode::Regex->parse($result->postcode);
my $pc_rs = $self->app->schema->resultset('GbPostcode');
if ( $parsed_postcode->{valid} && !$parsed_postcode->{non_geographical} ) {
my $gb_pc = $pc_rs->find({
outcode => $parsed_postcode->{outcode},
incode => $parsed_postcode->{incode},
});
if ( $gb_pc ) {
$result->update({
latitude => $gb_pc->latitude,
longitude => $gb_pc->longitude,
});
}
}
}
sub _calculate_distance {
my ( $self, $buyer, $seller ) = @_;
my $gis = GIS::Distance->new();
my $buyer_lat = $buyer->latitude;
my $buyer_long = $buyer->longitude;
my $seller_lat = $seller->latitude;
my $seller_long = $seller->longitude;
if ( $buyer_lat && $buyer_long
&& $seller_lat && $seller_long ) {
return $gis->distance( $buyer_lat, $buyer_long => $seller_lat, $seller_long )->meters;
} else {
print STDERR "missing lat-long for: " . $buyer->name . " or " . $seller->name . "\n";
}
return;
}
=head1 SYNOPSIS
Usage: APPLICATION latlong_setup [OPTIONS]
Options:
none for now
=cut
1;

View file

@ -0,0 +1,109 @@
package Pear::LocalLoop::Controller::Api::V1::Supplier::Location;
use Mojo::Base 'Mojolicious::Controller';
has validation_data => sub {
my $children_errors = {
latitude => {
validation => [
{ required => {} },
{ number => { error_prefix => 'not_number' } },
{ in_range => { args => [ -90, 90 ], error_prefix => 'outside_range' } },
],
},
longitude => {
validation => [
{ required => {} },
{ number => { error_prefix => 'not_number' } },
{ in_range => { args => [ -180, 180 ], error_prefix => 'outside_range' } },
],
},
};
return {
index => {
north_east => {
validation => [
{ required => {} },
{ is_object => { error_prefix => 'not_object' } },
],
children => $children_errors,
},
south_west => {
validation => [
{ required => {} },
{ is_object => { error_prefix => 'not_object' } },
],
children => $children_errors,
},
}
}
};
sub index {
my $c = shift;
return if $c->validation_error('index');
my $json = $c->stash->{api_json};
# Extra custom error, because its funny
if ( $json->{north_east}->{latitude} < $json->{south_west}->{latitude} ) {
return $c->render(
json => {
success => Mojo::JSON->false,
errors => [ 'upside_down' ],
},
status => 400,
);
}
my $entity = $c->stash->{api_user}->entity;
my $entity_type_object = $entity->type_object;
# need: organisations only, with name, latitude, and longitude
my $org_rs = $entity->purchases->search_related('seller',
{
'seller.type' => 'organisation',
'organisation.latitude' => { -between => [
$json->{south_west}->{latitude},
$json->{north_east}->{latitude},
] },
'organisation.longitude' => { -between => [
$json->{south_west}->{longitude},
$json->{north_east}->{longitude},
] },
},
{
join => [ qw/ organisation / ],
columns => [
'organisation.name',
'organisation.latitude',
'organisation.longitude',
],
group_by => [ qw/ organisation.id / ],
},
);
$org_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
my $suppliers = [ map {
{
latitude => $_->{organisation}->{latitude} * 1,
longitude => $_->{organisation}->{longitude} * 1,
name => $_->{organisation}->{name},
}
} $org_rs->all ];
$c->render(
json => {
success => Mojo::JSON->true,
suppliers => $suppliers,
self => {
latitude => $entity_type_object->latitude,
longitude => $entity_type_object->longitude,
}
},
);
}
1;

View file

@ -64,6 +64,115 @@ sub register {
$value = $app->parse_iso_datetime( $value );
return defined $value ? undef : 1;
});
$app->validator->add_check( is_object => sub {
my ( $validation, $name, $value ) = @_;
return ref ( $value ) eq 'HASH' ? undef : 1;
});
$app->validator->add_check( in_range => sub {
my ( $validation, $name, $value, $low, $high ) = @_;
return $low < $value && $value < $high ? undef : 1;
});
$app->helper( validation_error => sub { _validation_error(@_) } );
}
=head2 validation_error
Returns undef if there is no validation error, returns true otherwise - having
set the errors up as required. Renders out the errors as an array, with status
400
=cut
sub _validation_error {
my ( $c, $sub_name ) = @_;
my $val_data = $c->validation_data->{ $sub_name };
return unless defined $val_data;
my $data = $c->stash->{api_json};
my @errors = _validate_set( $c, $val_data, $data );
if ( scalar @errors ) {
my @sorted_errors = sort @errors;
$c->render(
json => {
success => Mojo::JSON->false,
errors => \@sorted_errors,
},
status => 400,
);
return \@errors;
}
return;
}
sub _validate_set {
my ( $c, $val_data, $data, $parent_name ) = @_;
my @errors;
# MUST get a raw validation object
my $validation = $c->app->validator->validation;
$validation->input( $data );
for my $val_data_key ( keys %$val_data ) {
$validation->topic( $val_data_key );
my $val_set = $val_data->{$val_data_key};
my $custom_check_prefix = {};
for my $val_error ( @{$val_set->{validation}} ) {
my ( $val_validator ) = keys %$val_error;
unless (
$validation->validator->checks->{$val_validator}
|| $val_validator =~ /required|optional/
) {
$c->app->log->warn( 'Unknown Validator [' . $val_validator . ']' );
next;
}
if ( my $custom_prefix = $val_error->{ $val_validator }->{ error_prefix } ) {
$custom_check_prefix->{ $val_validator } = $custom_prefix;
}
my $val_args = $val_error->{ $val_validator }->{ args };
$validation->$val_validator(
( $val_validator =~ /required|optional/ ? $val_data_key : () ),
( defined $val_args ? @$val_args : () )
);
# stop bothering checking if failed, validation stops after first failure
last if $validation->has_error( $val_data_key );
}
if ( $validation->has_error( $val_data_key ) ) {
my ( $check ) = @{ $validation->error( $val_data_key ) };
my $error_prefix = defined $custom_check_prefix->{ $check }
? $custom_check_prefix->{ $check }
: $check;
my $error_string = join ('_',
$error_prefix,
( defined $parent_name ? $parent_name : () ),
$val_data_key,
);
push @errors, $error_string;
} elsif ( defined $val_set->{ children } ) {
push @errors, _validate_set(
$c,
$val_set->{ children },
$data->{ $val_data_key },
$val_data_key );
}
}
return @errors;
}
1;

View file

@ -6,7 +6,7 @@ use warnings;
use base 'DBIx::Class::Schema';
our $VERSION = 8;
our $VERSION = 11;
__PACKAGE__->load_namespaces;

View file

@ -37,6 +37,18 @@ __PACKAGE__->add_columns(
size => 16,
is_nullable => 0,
},
latitude => {
data_type => 'decimal',
size => [5,2],
is_nullable => 1,
default_value => undef,
},
longitude => {
data_type => 'decimal',
size => [5,2],
is_nullable => 1,
default_value => undef,
},
);
__PACKAGE__->set_primary_key("id");

View file

@ -63,4 +63,16 @@ sub name {
}
}
sub type_object {
my $self = shift;
if ( $self->type eq 'customer' ) {
return $self->customer;
} elsif ( $self->type eq 'organisation' ) {
return $self->organisation;
} else {
return;
}
}
1;

View file

@ -0,0 +1,38 @@
package Pear::LocalLoop::Schema::Result::GbPostcode;
use strict;
use warnings;
use base 'DBIx::Class::Core';
__PACKAGE__->table('gb_postcodes');
__PACKAGE__->add_columns(
outcode => {
data_type => 'char',
size => 4,
is_nullable => 0,
},
incode => {
data_type => 'char',
size => 3,
is_nullable => 0,
default_value => '',
},
latitude => {
data_type => 'decimal',
size => [7,5],
is_nullable => 1,
default_value => undef,
},
longitude => {
data_type => 'decimal',
size => [7,5],
is_nullable => 1,
default_value => undef,
},
);
__PACKAGE__->set_primary_key(qw/ outcode incode /);
1;

View file

@ -58,6 +58,18 @@ __PACKAGE__->add_columns(
data_type => 'integer',
is_nullable => 1,
},
latitude => {
data_type => 'decimal',
size => [8,5],
is_nullable => 1,
default_value => undef,
},
longitude => {
data_type => 'decimal',
size => [8,5],
is_nullable => 1,
default_value => undef,
},
);
__PACKAGE__->set_primary_key('id');

View file

@ -48,6 +48,11 @@ __PACKAGE__->add_columns(
is_nullable => 0,
set_on_create => 1,
},
distance => {
data_type => 'numeric',
size => [15],
is_nullable => 1,
},
);
__PACKAGE__->set_primary_key("id");

View file

@ -8,3 +8,9 @@ body {
-moz-box-shadow: 0px 2px 4px 0px rgba(0,0,0,0.2);
box-shadow: 0px 2px 4px 0px rgba(0,0,0,0.2);
}
#map {
width: 100%;
height: 400px;
background-color: grey;
}

View file

@ -0,0 +1,18 @@
--
-- Created by SQL::Translator::Producer::PostgreSQL
-- Created on Thu Sep 21 17:01:35 2017
--
;
--
-- Table: dbix_class_deploymenthandler_versions
--
CREATE TABLE "dbix_class_deploymenthandler_versions" (
"id" serial NOT NULL,
"version" character varying(50) NOT NULL,
"ddl" text,
"upgrade_sql" text,
PRIMARY KEY ("id"),
CONSTRAINT "dbix_class_deploymenthandler_versions_version" UNIQUE ("version")
);
;

View file

@ -0,0 +1,251 @@
--
-- Created by SQL::Translator::Producer::PostgreSQL
-- Created on Thu Sep 21 17:01:35 2017
--
;
--
-- Table: account_tokens
--
CREATE TABLE "account_tokens" (
"id" serial NOT NULL,
"name" text NOT NULL,
"used" integer DEFAULT 0 NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "account_tokens_name" UNIQUE ("name")
);
;
--
-- Table: entities
--
CREATE TABLE "entities" (
"id" serial NOT NULL,
"type" character varying(255) NOT NULL,
PRIMARY KEY ("id")
);
;
--
-- Table: gb_postcodes
--
CREATE TABLE "gb_postcodes" (
"outcode" character(4) NOT NULL,
"incode" character(3) DEFAULT '' NOT NULL,
"latitude" numeric(7,5),
"longitude" numeric(7,5),
PRIMARY KEY ("outcode", "incode")
);
;
--
-- Table: leaderboards
--
CREATE TABLE "leaderboards" (
"id" serial NOT NULL,
"name" character varying(255) NOT NULL,
"type" character varying(255) NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "leaderboards_type" UNIQUE ("type")
);
;
--
-- Table: customers
--
CREATE TABLE "customers" (
"id" serial NOT NULL,
"entity_id" integer NOT NULL,
"display_name" character varying(255) NOT NULL,
"full_name" character varying(255) NOT NULL,
"year_of_birth" integer NOT NULL,
"postcode" character varying(16) NOT NULL,
"latitude" numeric(5,2),
"longitude" numeric(5,2),
PRIMARY KEY ("id")
);
CREATE INDEX "customers_idx_entity_id" on "customers" ("entity_id");
;
--
-- Table: leaderboard_sets
--
CREATE TABLE "leaderboard_sets" (
"id" serial NOT NULL,
"leaderboard_id" integer NOT NULL,
"date" timestamp NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "leaderboard_sets_idx_leaderboard_id" on "leaderboard_sets" ("leaderboard_id");
;
--
-- Table: organisations
--
CREATE TABLE "organisations" (
"id" serial NOT NULL,
"entity_id" integer NOT NULL,
"name" character varying(255) NOT NULL,
"street_name" text,
"town" character varying(255) NOT NULL,
"postcode" character varying(16),
"country" character varying(255),
"sector" character varying(1),
"pending" boolean DEFAULT false NOT NULL,
"submitted_by_id" integer,
"latitude" numeric(8,5),
"longitude" numeric(8,5),
PRIMARY KEY ("id")
);
CREATE INDEX "organisations_idx_entity_id" on "organisations" ("entity_id");
;
--
-- Table: transactions
--
CREATE TABLE "transactions" (
"id" serial NOT NULL,
"buyer_id" integer NOT NULL,
"seller_id" integer NOT NULL,
"value" numeric(100,0) NOT NULL,
"proof_image" text,
"submitted_at" timestamp NOT NULL,
"purchase_time" timestamp NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "transactions_idx_buyer_id" on "transactions" ("buyer_id");
CREATE INDEX "transactions_idx_seller_id" on "transactions" ("seller_id");
;
--
-- Table: users
--
CREATE TABLE "users" (
"id" serial NOT NULL,
"entity_id" integer NOT NULL,
"email" text NOT NULL,
"join_date" timestamp NOT NULL,
"password" character varying(100) NOT NULL,
"is_admin" boolean DEFAULT false NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "users_email" UNIQUE ("email")
);
CREATE INDEX "users_idx_entity_id" on "users" ("entity_id");
;
--
-- Table: feedback
--
CREATE TABLE "feedback" (
"id" serial NOT NULL,
"user_id" integer NOT NULL,
"submitted_at" timestamp NOT NULL,
"feedbacktext" text NOT NULL,
"app_name" character varying(255) NOT NULL,
"package_name" character varying(255) NOT NULL,
"version_code" character varying(255) NOT NULL,
"version_number" character varying(255) NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "feedback_idx_user_id" on "feedback" ("user_id");
;
--
-- Table: organisation_payroll
--
CREATE TABLE "organisation_payroll" (
"id" serial NOT NULL,
"org_id" integer NOT NULL,
"submitted_at" timestamp NOT NULL,
"entry_period" timestamp NOT NULL,
"employee_amount" integer NOT NULL,
"local_employee_amount" integer NOT NULL,
"gross_payroll" numeric(100,0) NOT NULL,
"payroll_income_tax" numeric(100,0) NOT NULL,
"payroll_employee_ni" numeric(100,0) NOT NULL,
"payroll_employer_ni" numeric(100,0) NOT NULL,
"payroll_total_pension" numeric(100,0) NOT NULL,
"payroll_other_benefit" numeric(100,0) NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "organisation_payroll_idx_org_id" on "organisation_payroll" ("org_id");
;
--
-- Table: session_tokens
--
CREATE TABLE "session_tokens" (
"id" serial NOT NULL,
"token" character varying(255) NOT NULL,
"user_id" integer NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "session_tokens_token" UNIQUE ("token")
);
CREATE INDEX "session_tokens_idx_user_id" on "session_tokens" ("user_id");
;
--
-- Table: leaderboard_values
--
CREATE TABLE "leaderboard_values" (
"id" serial NOT NULL,
"entity_id" integer NOT NULL,
"set_id" integer NOT NULL,
"position" integer NOT NULL,
"value" numeric(100,0) NOT NULL,
"trend" integer DEFAULT 0 NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "leaderboard_values_entity_id_set_id" UNIQUE ("entity_id", "set_id")
);
CREATE INDEX "leaderboard_values_idx_entity_id" on "leaderboard_values" ("entity_id");
CREATE INDEX "leaderboard_values_idx_set_id" on "leaderboard_values" ("set_id");
;
--
-- Foreign Key Definitions
--
;
ALTER TABLE "customers" ADD CONSTRAINT "customers_fk_entity_id" FOREIGN KEY ("entity_id")
REFERENCES "entities" ("id") ON DELETE CASCADE DEFERRABLE;
;
ALTER TABLE "leaderboard_sets" ADD CONSTRAINT "leaderboard_sets_fk_leaderboard_id" FOREIGN KEY ("leaderboard_id")
REFERENCES "leaderboards" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "organisations" ADD CONSTRAINT "organisations_fk_entity_id" FOREIGN KEY ("entity_id")
REFERENCES "entities" ("id") ON DELETE CASCADE DEFERRABLE;
;
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_fk_buyer_id" FOREIGN KEY ("buyer_id")
REFERENCES "entities" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_fk_seller_id" FOREIGN KEY ("seller_id")
REFERENCES "entities" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "users" ADD CONSTRAINT "users_fk_entity_id" FOREIGN KEY ("entity_id")
REFERENCES "entities" ("id") ON DELETE CASCADE DEFERRABLE;
;
ALTER TABLE "feedback" ADD CONSTRAINT "feedback_fk_user_id" FOREIGN KEY ("user_id")
REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "organisation_payroll" ADD CONSTRAINT "organisation_payroll_fk_org_id" FOREIGN KEY ("org_id")
REFERENCES "organisations" ("id") DEFERRABLE;
;
ALTER TABLE "session_tokens" ADD CONSTRAINT "session_tokens_fk_user_id" FOREIGN KEY ("user_id")
REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "leaderboard_values" ADD CONSTRAINT "leaderboard_values_fk_entity_id" FOREIGN KEY ("entity_id")
REFERENCES "entities" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "leaderboard_values" ADD CONSTRAINT "leaderboard_values_fk_set_id" FOREIGN KEY ("set_id")
REFERENCES "leaderboard_sets" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;

View file

@ -0,0 +1,18 @@
--
-- Created by SQL::Translator::Producer::PostgreSQL
-- Created on Thu Sep 21 17:28:47 2017
--
;
--
-- Table: dbix_class_deploymenthandler_versions
--
CREATE TABLE "dbix_class_deploymenthandler_versions" (
"id" serial NOT NULL,
"version" character varying(50) NOT NULL,
"ddl" text,
"upgrade_sql" text,
PRIMARY KEY ("id"),
CONSTRAINT "dbix_class_deploymenthandler_versions_version" UNIQUE ("version")
);
;

View file

@ -0,0 +1,252 @@
--
-- Created by SQL::Translator::Producer::PostgreSQL
-- Created on Thu Sep 21 17:28:46 2017
--
;
--
-- Table: account_tokens
--
CREATE TABLE "account_tokens" (
"id" serial NOT NULL,
"name" text NOT NULL,
"used" integer DEFAULT 0 NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "account_tokens_name" UNIQUE ("name")
);
;
--
-- Table: entities
--
CREATE TABLE "entities" (
"id" serial NOT NULL,
"type" character varying(255) NOT NULL,
PRIMARY KEY ("id")
);
;
--
-- Table: gb_postcodes
--
CREATE TABLE "gb_postcodes" (
"outcode" character(4) NOT NULL,
"incode" character(3) DEFAULT '' NOT NULL,
"latitude" numeric(7,5),
"longitude" numeric(7,5),
PRIMARY KEY ("outcode", "incode")
);
;
--
-- Table: leaderboards
--
CREATE TABLE "leaderboards" (
"id" serial NOT NULL,
"name" character varying(255) NOT NULL,
"type" character varying(255) NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "leaderboards_type" UNIQUE ("type")
);
;
--
-- Table: customers
--
CREATE TABLE "customers" (
"id" serial NOT NULL,
"entity_id" integer NOT NULL,
"display_name" character varying(255) NOT NULL,
"full_name" character varying(255) NOT NULL,
"year_of_birth" integer NOT NULL,
"postcode" character varying(16) NOT NULL,
"latitude" numeric(5,2),
"longitude" numeric(5,2),
PRIMARY KEY ("id")
);
CREATE INDEX "customers_idx_entity_id" on "customers" ("entity_id");
;
--
-- Table: leaderboard_sets
--
CREATE TABLE "leaderboard_sets" (
"id" serial NOT NULL,
"leaderboard_id" integer NOT NULL,
"date" timestamp NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "leaderboard_sets_idx_leaderboard_id" on "leaderboard_sets" ("leaderboard_id");
;
--
-- Table: organisations
--
CREATE TABLE "organisations" (
"id" serial NOT NULL,
"entity_id" integer NOT NULL,
"name" character varying(255) NOT NULL,
"street_name" text,
"town" character varying(255) NOT NULL,
"postcode" character varying(16),
"country" character varying(255),
"sector" character varying(1),
"pending" boolean DEFAULT false NOT NULL,
"submitted_by_id" integer,
"latitude" numeric(8,5),
"longitude" numeric(8,5),
PRIMARY KEY ("id")
);
CREATE INDEX "organisations_idx_entity_id" on "organisations" ("entity_id");
;
--
-- Table: transactions
--
CREATE TABLE "transactions" (
"id" serial NOT NULL,
"buyer_id" integer NOT NULL,
"seller_id" integer NOT NULL,
"value" numeric(100,0) NOT NULL,
"proof_image" text,
"submitted_at" timestamp NOT NULL,
"purchase_time" timestamp NOT NULL,
"distance" numeric(15),
PRIMARY KEY ("id")
);
CREATE INDEX "transactions_idx_buyer_id" on "transactions" ("buyer_id");
CREATE INDEX "transactions_idx_seller_id" on "transactions" ("seller_id");
;
--
-- Table: users
--
CREATE TABLE "users" (
"id" serial NOT NULL,
"entity_id" integer NOT NULL,
"email" text NOT NULL,
"join_date" timestamp NOT NULL,
"password" character varying(100) NOT NULL,
"is_admin" boolean DEFAULT false NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "users_email" UNIQUE ("email")
);
CREATE INDEX "users_idx_entity_id" on "users" ("entity_id");
;
--
-- Table: feedback
--
CREATE TABLE "feedback" (
"id" serial NOT NULL,
"user_id" integer NOT NULL,
"submitted_at" timestamp NOT NULL,
"feedbacktext" text NOT NULL,
"app_name" character varying(255) NOT NULL,
"package_name" character varying(255) NOT NULL,
"version_code" character varying(255) NOT NULL,
"version_number" character varying(255) NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "feedback_idx_user_id" on "feedback" ("user_id");
;
--
-- Table: organisation_payroll
--
CREATE TABLE "organisation_payroll" (
"id" serial NOT NULL,
"org_id" integer NOT NULL,
"submitted_at" timestamp NOT NULL,
"entry_period" timestamp NOT NULL,
"employee_amount" integer NOT NULL,
"local_employee_amount" integer NOT NULL,
"gross_payroll" numeric(100,0) NOT NULL,
"payroll_income_tax" numeric(100,0) NOT NULL,
"payroll_employee_ni" numeric(100,0) NOT NULL,
"payroll_employer_ni" numeric(100,0) NOT NULL,
"payroll_total_pension" numeric(100,0) NOT NULL,
"payroll_other_benefit" numeric(100,0) NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "organisation_payroll_idx_org_id" on "organisation_payroll" ("org_id");
;
--
-- Table: session_tokens
--
CREATE TABLE "session_tokens" (
"id" serial NOT NULL,
"token" character varying(255) NOT NULL,
"user_id" integer NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "session_tokens_token" UNIQUE ("token")
);
CREATE INDEX "session_tokens_idx_user_id" on "session_tokens" ("user_id");
;
--
-- Table: leaderboard_values
--
CREATE TABLE "leaderboard_values" (
"id" serial NOT NULL,
"entity_id" integer NOT NULL,
"set_id" integer NOT NULL,
"position" integer NOT NULL,
"value" numeric(100,0) NOT NULL,
"trend" integer DEFAULT 0 NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "leaderboard_values_entity_id_set_id" UNIQUE ("entity_id", "set_id")
);
CREATE INDEX "leaderboard_values_idx_entity_id" on "leaderboard_values" ("entity_id");
CREATE INDEX "leaderboard_values_idx_set_id" on "leaderboard_values" ("set_id");
;
--
-- Foreign Key Definitions
--
;
ALTER TABLE "customers" ADD CONSTRAINT "customers_fk_entity_id" FOREIGN KEY ("entity_id")
REFERENCES "entities" ("id") ON DELETE CASCADE DEFERRABLE;
;
ALTER TABLE "leaderboard_sets" ADD CONSTRAINT "leaderboard_sets_fk_leaderboard_id" FOREIGN KEY ("leaderboard_id")
REFERENCES "leaderboards" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "organisations" ADD CONSTRAINT "organisations_fk_entity_id" FOREIGN KEY ("entity_id")
REFERENCES "entities" ("id") ON DELETE CASCADE DEFERRABLE;
;
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_fk_buyer_id" FOREIGN KEY ("buyer_id")
REFERENCES "entities" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_fk_seller_id" FOREIGN KEY ("seller_id")
REFERENCES "entities" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "users" ADD CONSTRAINT "users_fk_entity_id" FOREIGN KEY ("entity_id")
REFERENCES "entities" ("id") ON DELETE CASCADE DEFERRABLE;
;
ALTER TABLE "feedback" ADD CONSTRAINT "feedback_fk_user_id" FOREIGN KEY ("user_id")
REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "organisation_payroll" ADD CONSTRAINT "organisation_payroll_fk_org_id" FOREIGN KEY ("org_id")
REFERENCES "organisations" ("id") DEFERRABLE;
;
ALTER TABLE "session_tokens" ADD CONSTRAINT "session_tokens_fk_user_id" FOREIGN KEY ("user_id")
REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "leaderboard_values" ADD CONSTRAINT "leaderboard_values_fk_entity_id" FOREIGN KEY ("entity_id")
REFERENCES "entities" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "leaderboard_values" ADD CONSTRAINT "leaderboard_values_fk_set_id" FOREIGN KEY ("set_id")
REFERENCES "leaderboard_sets" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;

View file

@ -0,0 +1,18 @@
--
-- Created by SQL::Translator::Producer::PostgreSQL
-- Created on Thu Sep 21 15:53:07 2017
--
;
--
-- Table: dbix_class_deploymenthandler_versions
--
CREATE TABLE "dbix_class_deploymenthandler_versions" (
"id" serial NOT NULL,
"version" character varying(50) NOT NULL,
"ddl" text,
"upgrade_sql" text,
PRIMARY KEY ("id"),
CONSTRAINT "dbix_class_deploymenthandler_versions_version" UNIQUE ("version")
);
;

View file

@ -0,0 +1,247 @@
--
-- Created by SQL::Translator::Producer::PostgreSQL
-- Created on Thu Sep 21 15:53:07 2017
--
;
--
-- Table: account_tokens
--
CREATE TABLE "account_tokens" (
"id" serial NOT NULL,
"name" text NOT NULL,
"used" integer DEFAULT 0 NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "account_tokens_name" UNIQUE ("name")
);
;
--
-- Table: entities
--
CREATE TABLE "entities" (
"id" serial NOT NULL,
"type" character varying(255) NOT NULL,
PRIMARY KEY ("id")
);
;
--
-- Table: gb_postcodes
--
CREATE TABLE "gb_postcodes" (
"outcode" character(4) NOT NULL,
"incode" character(3) DEFAULT '' NOT NULL,
"latitude" numeric(7,5),
"longitude" numeric(7,5),
PRIMARY KEY ("outcode", "incode")
);
;
--
-- Table: leaderboards
--
CREATE TABLE "leaderboards" (
"id" serial NOT NULL,
"name" character varying(255) NOT NULL,
"type" character varying(255) NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "leaderboards_type" UNIQUE ("type")
);
;
--
-- Table: customers
--
CREATE TABLE "customers" (
"id" serial NOT NULL,
"entity_id" integer NOT NULL,
"display_name" character varying(255) NOT NULL,
"full_name" character varying(255) NOT NULL,
"year_of_birth" integer NOT NULL,
"postcode" character varying(16) NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "customers_idx_entity_id" on "customers" ("entity_id");
;
--
-- Table: leaderboard_sets
--
CREATE TABLE "leaderboard_sets" (
"id" serial NOT NULL,
"leaderboard_id" integer NOT NULL,
"date" timestamp NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "leaderboard_sets_idx_leaderboard_id" on "leaderboard_sets" ("leaderboard_id");
;
--
-- Table: organisations
--
CREATE TABLE "organisations" (
"id" serial NOT NULL,
"entity_id" integer NOT NULL,
"name" character varying(255) NOT NULL,
"street_name" text,
"town" character varying(255) NOT NULL,
"postcode" character varying(16),
"country" character varying(255),
"sector" character varying(1),
"pending" boolean DEFAULT false NOT NULL,
"submitted_by_id" integer,
PRIMARY KEY ("id")
);
CREATE INDEX "organisations_idx_entity_id" on "organisations" ("entity_id");
;
--
-- Table: transactions
--
CREATE TABLE "transactions" (
"id" serial NOT NULL,
"buyer_id" integer NOT NULL,
"seller_id" integer NOT NULL,
"value" numeric(100,0) NOT NULL,
"proof_image" text,
"submitted_at" timestamp NOT NULL,
"purchase_time" timestamp NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "transactions_idx_buyer_id" on "transactions" ("buyer_id");
CREATE INDEX "transactions_idx_seller_id" on "transactions" ("seller_id");
;
--
-- Table: users
--
CREATE TABLE "users" (
"id" serial NOT NULL,
"entity_id" integer NOT NULL,
"email" text NOT NULL,
"join_date" timestamp NOT NULL,
"password" character varying(100) NOT NULL,
"is_admin" boolean DEFAULT false NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "users_email" UNIQUE ("email")
);
CREATE INDEX "users_idx_entity_id" on "users" ("entity_id");
;
--
-- Table: feedback
--
CREATE TABLE "feedback" (
"id" serial NOT NULL,
"user_id" integer NOT NULL,
"submitted_at" timestamp NOT NULL,
"feedbacktext" text NOT NULL,
"app_name" character varying(255) NOT NULL,
"package_name" character varying(255) NOT NULL,
"version_code" character varying(255) NOT NULL,
"version_number" character varying(255) NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "feedback_idx_user_id" on "feedback" ("user_id");
;
--
-- Table: organisation_payroll
--
CREATE TABLE "organisation_payroll" (
"id" serial NOT NULL,
"org_id" integer NOT NULL,
"submitted_at" timestamp NOT NULL,
"entry_period" timestamp NOT NULL,
"employee_amount" integer NOT NULL,
"local_employee_amount" integer NOT NULL,
"gross_payroll" numeric(100,0) NOT NULL,
"payroll_income_tax" numeric(100,0) NOT NULL,
"payroll_employee_ni" numeric(100,0) NOT NULL,
"payroll_employer_ni" numeric(100,0) NOT NULL,
"payroll_total_pension" numeric(100,0) NOT NULL,
"payroll_other_benefit" numeric(100,0) NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "organisation_payroll_idx_org_id" on "organisation_payroll" ("org_id");
;
--
-- Table: session_tokens
--
CREATE TABLE "session_tokens" (
"id" serial NOT NULL,
"token" character varying(255) NOT NULL,
"user_id" integer NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "session_tokens_token" UNIQUE ("token")
);
CREATE INDEX "session_tokens_idx_user_id" on "session_tokens" ("user_id");
;
--
-- Table: leaderboard_values
--
CREATE TABLE "leaderboard_values" (
"id" serial NOT NULL,
"entity_id" integer NOT NULL,
"set_id" integer NOT NULL,
"position" integer NOT NULL,
"value" numeric(100,0) NOT NULL,
"trend" integer DEFAULT 0 NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "leaderboard_values_entity_id_set_id" UNIQUE ("entity_id", "set_id")
);
CREATE INDEX "leaderboard_values_idx_entity_id" on "leaderboard_values" ("entity_id");
CREATE INDEX "leaderboard_values_idx_set_id" on "leaderboard_values" ("set_id");
;
--
-- Foreign Key Definitions
--
;
ALTER TABLE "customers" ADD CONSTRAINT "customers_fk_entity_id" FOREIGN KEY ("entity_id")
REFERENCES "entities" ("id") ON DELETE CASCADE DEFERRABLE;
;
ALTER TABLE "leaderboard_sets" ADD CONSTRAINT "leaderboard_sets_fk_leaderboard_id" FOREIGN KEY ("leaderboard_id")
REFERENCES "leaderboards" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "organisations" ADD CONSTRAINT "organisations_fk_entity_id" FOREIGN KEY ("entity_id")
REFERENCES "entities" ("id") ON DELETE CASCADE DEFERRABLE;
;
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_fk_buyer_id" FOREIGN KEY ("buyer_id")
REFERENCES "entities" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_fk_seller_id" FOREIGN KEY ("seller_id")
REFERENCES "entities" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "users" ADD CONSTRAINT "users_fk_entity_id" FOREIGN KEY ("entity_id")
REFERENCES "entities" ("id") ON DELETE CASCADE DEFERRABLE;
;
ALTER TABLE "feedback" ADD CONSTRAINT "feedback_fk_user_id" FOREIGN KEY ("user_id")
REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "organisation_payroll" ADD CONSTRAINT "organisation_payroll_fk_org_id" FOREIGN KEY ("org_id")
REFERENCES "organisations" ("id") DEFERRABLE;
;
ALTER TABLE "session_tokens" ADD CONSTRAINT "session_tokens_fk_user_id" FOREIGN KEY ("user_id")
REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "leaderboard_values" ADD CONSTRAINT "leaderboard_values_fk_entity_id" FOREIGN KEY ("entity_id")
REFERENCES "entities" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "leaderboard_values" ADD CONSTRAINT "leaderboard_values_fk_set_id" FOREIGN KEY ("set_id")
REFERENCES "leaderboard_sets" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;

View file

@ -0,0 +1,12 @@
-- Convert schema 'share/ddl/_source/deploy/10/001-auto.yml' to 'share/ddl/_source/deploy/11/001-auto.yml':;
;
BEGIN;
;
ALTER TABLE transactions ADD COLUMN distance numeric(15);
;
COMMIT;

View file

@ -0,0 +1,18 @@
-- Convert schema 'share/ddl/_source/deploy/8/001-auto.yml' to 'share/ddl/_source/deploy/9/001-auto.yml':;
;
BEGIN;
;
CREATE TABLE "gb_postcodes" (
"outcode" character(4) NOT NULL,
"incode" character(3) DEFAULT '' NOT NULL,
"latitude" numeric(7,5),
"longitude" numeric(7,5),
PRIMARY KEY ("outcode", "incode")
);
;
COMMIT;

View file

@ -0,0 +1,21 @@
-- Convert schema 'share/ddl/_source/deploy/9/001-auto.yml' to 'share/ddl/_source/deploy/10/001-auto.yml':;
;
BEGIN;
;
ALTER TABLE customers ADD COLUMN latitude numeric(5,2);
;
ALTER TABLE customers ADD COLUMN longitude numeric(5,2);
;
ALTER TABLE organisations ADD COLUMN latitude numeric(8,5);
;
ALTER TABLE organisations ADD COLUMN longitude numeric(8,5);
;
COMMIT;

View file

@ -0,0 +1,18 @@
--
-- Created by SQL::Translator::Producer::SQLite
-- Created on Thu Sep 21 17:01:35 2017
--
;
BEGIN TRANSACTION;
--
-- Table: dbix_class_deploymenthandler_versions
--
CREATE TABLE dbix_class_deploymenthandler_versions (
id INTEGER PRIMARY KEY NOT NULL,
version varchar(50) NOT NULL,
ddl text,
upgrade_sql text
);
CREATE UNIQUE INDEX dbix_class_deploymenthandler_versions_version ON dbix_class_deploymenthandler_versions (version);
COMMIT;

View file

@ -0,0 +1,178 @@
--
-- Created by SQL::Translator::Producer::SQLite
-- Created on Thu Sep 21 17:01:35 2017
--
;
BEGIN TRANSACTION;
--
-- Table: account_tokens
--
CREATE TABLE account_tokens (
id INTEGER PRIMARY KEY NOT NULL,
name text NOT NULL,
used integer NOT NULL DEFAULT 0
);
CREATE UNIQUE INDEX account_tokens_name ON account_tokens (name);
--
-- Table: entities
--
CREATE TABLE entities (
id INTEGER PRIMARY KEY NOT NULL,
type varchar(255) NOT NULL
);
--
-- Table: gb_postcodes
--
CREATE TABLE gb_postcodes (
outcode char(4) NOT NULL,
incode char(3) NOT NULL DEFAULT '',
latitude decimal(7,5),
longitude decimal(7,5),
PRIMARY KEY (outcode, incode)
);
--
-- Table: leaderboards
--
CREATE TABLE leaderboards (
id INTEGER PRIMARY KEY NOT NULL,
name varchar(255) NOT NULL,
type varchar(255) NOT NULL
);
CREATE UNIQUE INDEX leaderboards_type ON leaderboards (type);
--
-- Table: customers
--
CREATE TABLE customers (
id INTEGER PRIMARY KEY NOT NULL,
entity_id integer NOT NULL,
display_name varchar(255) NOT NULL,
full_name varchar(255) NOT NULL,
year_of_birth integer NOT NULL,
postcode varchar(16) NOT NULL,
latitude decimal(5,2),
longitude decimal(5,2),
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
);
CREATE INDEX customers_idx_entity_id ON customers (entity_id);
--
-- Table: leaderboard_sets
--
CREATE TABLE leaderboard_sets (
id INTEGER PRIMARY KEY NOT NULL,
leaderboard_id integer NOT NULL,
date datetime NOT NULL,
FOREIGN KEY (leaderboard_id) REFERENCES leaderboards(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX leaderboard_sets_idx_leaderboard_id ON leaderboard_sets (leaderboard_id);
--
-- Table: organisations
--
CREATE TABLE organisations (
id INTEGER PRIMARY KEY NOT NULL,
entity_id integer NOT NULL,
name varchar(255) NOT NULL,
street_name text,
town varchar(255) NOT NULL,
postcode varchar(16),
country varchar(255),
sector varchar(1),
pending boolean NOT NULL DEFAULT false,
submitted_by_id integer,
latitude decimal(8,5),
longitude decimal(8,5),
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
);
CREATE INDEX organisations_idx_entity_id ON organisations (entity_id);
--
-- Table: transactions
--
CREATE TABLE transactions (
id INTEGER PRIMARY KEY NOT NULL,
buyer_id integer NOT NULL,
seller_id integer NOT NULL,
value numeric(100,0) NOT NULL,
proof_image text,
submitted_at datetime NOT NULL,
purchase_time datetime NOT NULL,
FOREIGN KEY (buyer_id) REFERENCES entities(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (seller_id) REFERENCES entities(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX transactions_idx_buyer_id ON transactions (buyer_id);
CREATE INDEX transactions_idx_seller_id ON transactions (seller_id);
--
-- Table: users
--
CREATE TABLE users (
id INTEGER PRIMARY KEY NOT NULL,
entity_id integer NOT NULL,
email text NOT NULL,
join_date datetime NOT NULL,
password varchar(100) NOT NULL,
is_admin boolean NOT NULL DEFAULT false,
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
);
CREATE INDEX users_idx_entity_id ON users (entity_id);
CREATE UNIQUE INDEX users_email ON users (email);
--
-- Table: feedback
--
CREATE TABLE feedback (
id INTEGER PRIMARY KEY NOT NULL,
user_id integer NOT NULL,
submitted_at datetime NOT NULL,
feedbacktext text NOT NULL,
app_name varchar(255) NOT NULL,
package_name varchar(255) NOT NULL,
version_code varchar(255) NOT NULL,
version_number varchar(255) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX feedback_idx_user_id ON feedback (user_id);
--
-- Table: organisation_payroll
--
CREATE TABLE organisation_payroll (
id INTEGER PRIMARY KEY NOT NULL,
org_id integer NOT NULL,
submitted_at datetime NOT NULL,
entry_period datetime NOT NULL,
employee_amount integer NOT NULL,
local_employee_amount integer NOT NULL,
gross_payroll numeric(100,0) NOT NULL,
payroll_income_tax numeric(100,0) NOT NULL,
payroll_employee_ni numeric(100,0) NOT NULL,
payroll_employer_ni numeric(100,0) NOT NULL,
payroll_total_pension numeric(100,0) NOT NULL,
payroll_other_benefit numeric(100,0) NOT NULL,
FOREIGN KEY (org_id) REFERENCES organisations(id)
);
CREATE INDEX organisation_payroll_idx_org_id ON organisation_payroll (org_id);
--
-- Table: session_tokens
--
CREATE TABLE session_tokens (
id INTEGER PRIMARY KEY NOT NULL,
token varchar(255) NOT NULL,
user_id integer NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX session_tokens_idx_user_id ON session_tokens (user_id);
CREATE UNIQUE INDEX session_tokens_token ON session_tokens (token);
--
-- Table: leaderboard_values
--
CREATE TABLE leaderboard_values (
id INTEGER PRIMARY KEY NOT NULL,
entity_id integer NOT NULL,
set_id integer NOT NULL,
position integer NOT NULL,
value numeric(100,0) NOT NULL,
trend integer NOT NULL DEFAULT 0,
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (set_id) REFERENCES leaderboard_sets(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX leaderboard_values_idx_entity_id ON leaderboard_values (entity_id);
CREATE INDEX leaderboard_values_idx_set_id ON leaderboard_values (set_id);
CREATE UNIQUE INDEX leaderboard_values_entity_id_set_id ON leaderboard_values (entity_id, set_id);
COMMIT;

View file

@ -0,0 +1,18 @@
--
-- Created by SQL::Translator::Producer::SQLite
-- Created on Thu Sep 21 17:28:47 2017
--
;
BEGIN TRANSACTION;
--
-- Table: dbix_class_deploymenthandler_versions
--
CREATE TABLE dbix_class_deploymenthandler_versions (
id INTEGER PRIMARY KEY NOT NULL,
version varchar(50) NOT NULL,
ddl text,
upgrade_sql text
);
CREATE UNIQUE INDEX dbix_class_deploymenthandler_versions_version ON dbix_class_deploymenthandler_versions (version);
COMMIT;

View file

@ -0,0 +1,179 @@
--
-- Created by SQL::Translator::Producer::SQLite
-- Created on Thu Sep 21 17:28:46 2017
--
;
BEGIN TRANSACTION;
--
-- Table: account_tokens
--
CREATE TABLE account_tokens (
id INTEGER PRIMARY KEY NOT NULL,
name text NOT NULL,
used integer NOT NULL DEFAULT 0
);
CREATE UNIQUE INDEX account_tokens_name ON account_tokens (name);
--
-- Table: entities
--
CREATE TABLE entities (
id INTEGER PRIMARY KEY NOT NULL,
type varchar(255) NOT NULL
);
--
-- Table: gb_postcodes
--
CREATE TABLE gb_postcodes (
outcode char(4) NOT NULL,
incode char(3) NOT NULL DEFAULT '',
latitude decimal(7,5),
longitude decimal(7,5),
PRIMARY KEY (outcode, incode)
);
--
-- Table: leaderboards
--
CREATE TABLE leaderboards (
id INTEGER PRIMARY KEY NOT NULL,
name varchar(255) NOT NULL,
type varchar(255) NOT NULL
);
CREATE UNIQUE INDEX leaderboards_type ON leaderboards (type);
--
-- Table: customers
--
CREATE TABLE customers (
id INTEGER PRIMARY KEY NOT NULL,
entity_id integer NOT NULL,
display_name varchar(255) NOT NULL,
full_name varchar(255) NOT NULL,
year_of_birth integer NOT NULL,
postcode varchar(16) NOT NULL,
latitude decimal(5,2),
longitude decimal(5,2),
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
);
CREATE INDEX customers_idx_entity_id ON customers (entity_id);
--
-- Table: leaderboard_sets
--
CREATE TABLE leaderboard_sets (
id INTEGER PRIMARY KEY NOT NULL,
leaderboard_id integer NOT NULL,
date datetime NOT NULL,
FOREIGN KEY (leaderboard_id) REFERENCES leaderboards(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX leaderboard_sets_idx_leaderboard_id ON leaderboard_sets (leaderboard_id);
--
-- Table: organisations
--
CREATE TABLE organisations (
id INTEGER PRIMARY KEY NOT NULL,
entity_id integer NOT NULL,
name varchar(255) NOT NULL,
street_name text,
town varchar(255) NOT NULL,
postcode varchar(16),
country varchar(255),
sector varchar(1),
pending boolean NOT NULL DEFAULT false,
submitted_by_id integer,
latitude decimal(8,5),
longitude decimal(8,5),
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
);
CREATE INDEX organisations_idx_entity_id ON organisations (entity_id);
--
-- Table: transactions
--
CREATE TABLE transactions (
id INTEGER PRIMARY KEY NOT NULL,
buyer_id integer NOT NULL,
seller_id integer NOT NULL,
value numeric(100,0) NOT NULL,
proof_image text,
submitted_at datetime NOT NULL,
purchase_time datetime NOT NULL,
distance numeric(15),
FOREIGN KEY (buyer_id) REFERENCES entities(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (seller_id) REFERENCES entities(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX transactions_idx_buyer_id ON transactions (buyer_id);
CREATE INDEX transactions_idx_seller_id ON transactions (seller_id);
--
-- Table: users
--
CREATE TABLE users (
id INTEGER PRIMARY KEY NOT NULL,
entity_id integer NOT NULL,
email text NOT NULL,
join_date datetime NOT NULL,
password varchar(100) NOT NULL,
is_admin boolean NOT NULL DEFAULT false,
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
);
CREATE INDEX users_idx_entity_id ON users (entity_id);
CREATE UNIQUE INDEX users_email ON users (email);
--
-- Table: feedback
--
CREATE TABLE feedback (
id INTEGER PRIMARY KEY NOT NULL,
user_id integer NOT NULL,
submitted_at datetime NOT NULL,
feedbacktext text NOT NULL,
app_name varchar(255) NOT NULL,
package_name varchar(255) NOT NULL,
version_code varchar(255) NOT NULL,
version_number varchar(255) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX feedback_idx_user_id ON feedback (user_id);
--
-- Table: organisation_payroll
--
CREATE TABLE organisation_payroll (
id INTEGER PRIMARY KEY NOT NULL,
org_id integer NOT NULL,
submitted_at datetime NOT NULL,
entry_period datetime NOT NULL,
employee_amount integer NOT NULL,
local_employee_amount integer NOT NULL,
gross_payroll numeric(100,0) NOT NULL,
payroll_income_tax numeric(100,0) NOT NULL,
payroll_employee_ni numeric(100,0) NOT NULL,
payroll_employer_ni numeric(100,0) NOT NULL,
payroll_total_pension numeric(100,0) NOT NULL,
payroll_other_benefit numeric(100,0) NOT NULL,
FOREIGN KEY (org_id) REFERENCES organisations(id)
);
CREATE INDEX organisation_payroll_idx_org_id ON organisation_payroll (org_id);
--
-- Table: session_tokens
--
CREATE TABLE session_tokens (
id INTEGER PRIMARY KEY NOT NULL,
token varchar(255) NOT NULL,
user_id integer NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX session_tokens_idx_user_id ON session_tokens (user_id);
CREATE UNIQUE INDEX session_tokens_token ON session_tokens (token);
--
-- Table: leaderboard_values
--
CREATE TABLE leaderboard_values (
id INTEGER PRIMARY KEY NOT NULL,
entity_id integer NOT NULL,
set_id integer NOT NULL,
position integer NOT NULL,
value numeric(100,0) NOT NULL,
trend integer NOT NULL DEFAULT 0,
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (set_id) REFERENCES leaderboard_sets(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX leaderboard_values_idx_entity_id ON leaderboard_values (entity_id);
CREATE INDEX leaderboard_values_idx_set_id ON leaderboard_values (set_id);
CREATE UNIQUE INDEX leaderboard_values_entity_id_set_id ON leaderboard_values (entity_id, set_id);
COMMIT;

View file

@ -0,0 +1,18 @@
--
-- Created by SQL::Translator::Producer::SQLite
-- Created on Thu Sep 21 15:53:07 2017
--
;
BEGIN TRANSACTION;
--
-- Table: dbix_class_deploymenthandler_versions
--
CREATE TABLE dbix_class_deploymenthandler_versions (
id INTEGER PRIMARY KEY NOT NULL,
version varchar(50) NOT NULL,
ddl text,
upgrade_sql text
);
CREATE UNIQUE INDEX dbix_class_deploymenthandler_versions_version ON dbix_class_deploymenthandler_versions (version);
COMMIT;

View file

@ -0,0 +1,174 @@
--
-- Created by SQL::Translator::Producer::SQLite
-- Created on Thu Sep 21 15:53:07 2017
--
;
BEGIN TRANSACTION;
--
-- Table: account_tokens
--
CREATE TABLE account_tokens (
id INTEGER PRIMARY KEY NOT NULL,
name text NOT NULL,
used integer NOT NULL DEFAULT 0
);
CREATE UNIQUE INDEX account_tokens_name ON account_tokens (name);
--
-- Table: entities
--
CREATE TABLE entities (
id INTEGER PRIMARY KEY NOT NULL,
type varchar(255) NOT NULL
);
--
-- Table: gb_postcodes
--
CREATE TABLE gb_postcodes (
outcode char(4) NOT NULL,
incode char(3) NOT NULL DEFAULT '',
latitude decimal(7,5),
longitude decimal(7,5),
PRIMARY KEY (outcode, incode)
);
--
-- Table: leaderboards
--
CREATE TABLE leaderboards (
id INTEGER PRIMARY KEY NOT NULL,
name varchar(255) NOT NULL,
type varchar(255) NOT NULL
);
CREATE UNIQUE INDEX leaderboards_type ON leaderboards (type);
--
-- Table: customers
--
CREATE TABLE customers (
id INTEGER PRIMARY KEY NOT NULL,
entity_id integer NOT NULL,
display_name varchar(255) NOT NULL,
full_name varchar(255) NOT NULL,
year_of_birth integer NOT NULL,
postcode varchar(16) NOT NULL,
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
);
CREATE INDEX customers_idx_entity_id ON customers (entity_id);
--
-- Table: leaderboard_sets
--
CREATE TABLE leaderboard_sets (
id INTEGER PRIMARY KEY NOT NULL,
leaderboard_id integer NOT NULL,
date datetime NOT NULL,
FOREIGN KEY (leaderboard_id) REFERENCES leaderboards(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX leaderboard_sets_idx_leaderboard_id ON leaderboard_sets (leaderboard_id);
--
-- Table: organisations
--
CREATE TABLE organisations (
id INTEGER PRIMARY KEY NOT NULL,
entity_id integer NOT NULL,
name varchar(255) NOT NULL,
street_name text,
town varchar(255) NOT NULL,
postcode varchar(16),
country varchar(255),
sector varchar(1),
pending boolean NOT NULL DEFAULT false,
submitted_by_id integer,
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
);
CREATE INDEX organisations_idx_entity_id ON organisations (entity_id);
--
-- Table: transactions
--
CREATE TABLE transactions (
id INTEGER PRIMARY KEY NOT NULL,
buyer_id integer NOT NULL,
seller_id integer NOT NULL,
value numeric(100,0) NOT NULL,
proof_image text,
submitted_at datetime NOT NULL,
purchase_time datetime NOT NULL,
FOREIGN KEY (buyer_id) REFERENCES entities(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (seller_id) REFERENCES entities(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX transactions_idx_buyer_id ON transactions (buyer_id);
CREATE INDEX transactions_idx_seller_id ON transactions (seller_id);
--
-- Table: users
--
CREATE TABLE users (
id INTEGER PRIMARY KEY NOT NULL,
entity_id integer NOT NULL,
email text NOT NULL,
join_date datetime NOT NULL,
password varchar(100) NOT NULL,
is_admin boolean NOT NULL DEFAULT false,
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
);
CREATE INDEX users_idx_entity_id ON users (entity_id);
CREATE UNIQUE INDEX users_email ON users (email);
--
-- Table: feedback
--
CREATE TABLE feedback (
id INTEGER PRIMARY KEY NOT NULL,
user_id integer NOT NULL,
submitted_at datetime NOT NULL,
feedbacktext text NOT NULL,
app_name varchar(255) NOT NULL,
package_name varchar(255) NOT NULL,
version_code varchar(255) NOT NULL,
version_number varchar(255) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX feedback_idx_user_id ON feedback (user_id);
--
-- Table: organisation_payroll
--
CREATE TABLE organisation_payroll (
id INTEGER PRIMARY KEY NOT NULL,
org_id integer NOT NULL,
submitted_at datetime NOT NULL,
entry_period datetime NOT NULL,
employee_amount integer NOT NULL,
local_employee_amount integer NOT NULL,
gross_payroll numeric(100,0) NOT NULL,
payroll_income_tax numeric(100,0) NOT NULL,
payroll_employee_ni numeric(100,0) NOT NULL,
payroll_employer_ni numeric(100,0) NOT NULL,
payroll_total_pension numeric(100,0) NOT NULL,
payroll_other_benefit numeric(100,0) NOT NULL,
FOREIGN KEY (org_id) REFERENCES organisations(id)
);
CREATE INDEX organisation_payroll_idx_org_id ON organisation_payroll (org_id);
--
-- Table: session_tokens
--
CREATE TABLE session_tokens (
id INTEGER PRIMARY KEY NOT NULL,
token varchar(255) NOT NULL,
user_id integer NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX session_tokens_idx_user_id ON session_tokens (user_id);
CREATE UNIQUE INDEX session_tokens_token ON session_tokens (token);
--
-- Table: leaderboard_values
--
CREATE TABLE leaderboard_values (
id INTEGER PRIMARY KEY NOT NULL,
entity_id integer NOT NULL,
set_id integer NOT NULL,
position integer NOT NULL,
value numeric(100,0) NOT NULL,
trend integer NOT NULL DEFAULT 0,
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (set_id) REFERENCES leaderboard_sets(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX leaderboard_values_idx_entity_id ON leaderboard_values (entity_id);
CREATE INDEX leaderboard_values_idx_set_id ON leaderboard_values (set_id);
CREATE UNIQUE INDEX leaderboard_values_entity_id_set_id ON leaderboard_values (entity_id, set_id);
COMMIT;

View file

@ -0,0 +1,12 @@
-- Convert schema 'share/ddl/_source/deploy/10/001-auto.yml' to 'share/ddl/_source/deploy/11/001-auto.yml':;
;
BEGIN;
;
ALTER TABLE transactions ADD COLUMN distance numeric(15);
;
COMMIT;

View file

@ -0,0 +1,18 @@
-- Convert schema 'share/ddl/_source/deploy/8/001-auto.yml' to 'share/ddl/_source/deploy/9/001-auto.yml':;
;
BEGIN;
;
CREATE TABLE gb_postcodes (
outcode char(4) NOT NULL,
incode char(3) NOT NULL DEFAULT '',
latitude decimal(7,5),
longitude decimal(7,5),
PRIMARY KEY (outcode, incode)
);
;
COMMIT;

View file

@ -0,0 +1,21 @@
-- Convert schema 'share/ddl/_source/deploy/9/001-auto.yml' to 'share/ddl/_source/deploy/10/001-auto.yml':;
;
BEGIN;
;
ALTER TABLE customers ADD COLUMN latitude decimal(5,2);
;
ALTER TABLE customers ADD COLUMN longitude decimal(5,2);
;
ALTER TABLE organisations ADD COLUMN latitude decimal(8,5);
;
ALTER TABLE organisations ADD COLUMN longitude decimal(8,5);
;
COMMIT;

View file

@ -0,0 +1,91 @@
---
schema:
procedures: {}
tables:
dbix_class_deploymenthandler_versions:
constraints:
- deferrable: 1
expression: ''
fields:
- id
match_type: ''
name: ''
on_delete: ''
on_update: ''
options: []
reference_fields: []
reference_table: ''
type: PRIMARY KEY
- deferrable: 1
expression: ''
fields:
- version
match_type: ''
name: dbix_class_deploymenthandler_versions_version
on_delete: ''
on_update: ''
options: []
reference_fields: []
reference_table: ''
type: UNIQUE
fields:
ddl:
data_type: text
default_value: ~
is_nullable: 1
is_primary_key: 0
is_unique: 0
name: ddl
order: 3
size:
- 0
id:
data_type: int
default_value: ~
is_auto_increment: 1
is_nullable: 0
is_primary_key: 1
is_unique: 0
name: id
order: 1
size:
- 0
upgrade_sql:
data_type: text
default_value: ~
is_nullable: 1
is_primary_key: 0
is_unique: 0
name: upgrade_sql
order: 4
size:
- 0
version:
data_type: varchar
default_value: ~
is_nullable: 0
is_primary_key: 0
is_unique: 1
name: version
order: 2
size:
- 50
indices: []
name: dbix_class_deploymenthandler_versions
options: []
order: 1
triggers: {}
views: {}
translator:
add_drop_table: 0
filename: ~
no_comments: 0
parser_args:
sources:
- __VERSION
parser_type: SQL::Translator::Parser::DBIx::Class
producer_args: {}
producer_type: SQL::Translator::Producer::YAML
show_warnings: 0
trace: 0
version: 0.11021

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,91 @@
---
schema:
procedures: {}
tables:
dbix_class_deploymenthandler_versions:
constraints:
- deferrable: 1
expression: ''
fields:
- id
match_type: ''
name: ''
on_delete: ''
on_update: ''
options: []
reference_fields: []
reference_table: ''
type: PRIMARY KEY
- deferrable: 1
expression: ''
fields:
- version
match_type: ''
name: dbix_class_deploymenthandler_versions_version
on_delete: ''
on_update: ''
options: []
reference_fields: []
reference_table: ''
type: UNIQUE
fields:
ddl:
data_type: text
default_value: ~
is_nullable: 1
is_primary_key: 0
is_unique: 0
name: ddl
order: 3
size:
- 0
id:
data_type: int
default_value: ~
is_auto_increment: 1
is_nullable: 0
is_primary_key: 1
is_unique: 0
name: id
order: 1
size:
- 0
upgrade_sql:
data_type: text
default_value: ~
is_nullable: 1
is_primary_key: 0
is_unique: 0
name: upgrade_sql
order: 4
size:
- 0
version:
data_type: varchar
default_value: ~
is_nullable: 0
is_primary_key: 0
is_unique: 1
name: version
order: 2
size:
- 50
indices: []
name: dbix_class_deploymenthandler_versions
options: []
order: 1
triggers: {}
views: {}
translator:
add_drop_table: 0
filename: ~
no_comments: 0
parser_args:
sources:
- __VERSION
parser_type: SQL::Translator::Parser::DBIx::Class
producer_args: {}
producer_type: SQL::Translator::Producer::YAML
show_warnings: 0
trace: 0
version: 0.11021

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,91 @@
---
schema:
procedures: {}
tables:
dbix_class_deploymenthandler_versions:
constraints:
- deferrable: 1
expression: ''
fields:
- id
match_type: ''
name: ''
on_delete: ''
on_update: ''
options: []
reference_fields: []
reference_table: ''
type: PRIMARY KEY
- deferrable: 1
expression: ''
fields:
- version
match_type: ''
name: dbix_class_deploymenthandler_versions_version
on_delete: ''
on_update: ''
options: []
reference_fields: []
reference_table: ''
type: UNIQUE
fields:
ddl:
data_type: text
default_value: ~
is_nullable: 1
is_primary_key: 0
is_unique: 0
name: ddl
order: 3
size:
- 0
id:
data_type: int
default_value: ~
is_auto_increment: 1
is_nullable: 0
is_primary_key: 1
is_unique: 0
name: id
order: 1
size:
- 0
upgrade_sql:
data_type: text
default_value: ~
is_nullable: 1
is_primary_key: 0
is_unique: 0
name: upgrade_sql
order: 4
size:
- 0
version:
data_type: varchar
default_value: ~
is_nullable: 0
is_primary_key: 0
is_unique: 1
name: version
order: 2
size:
- 50
indices: []
name: dbix_class_deploymenthandler_versions
options: []
order: 1
triggers: {}
views: {}
translator:
add_drop_table: 0
filename: ~
no_comments: 0
parser_args:
sources:
- __VERSION
parser_type: SQL::Translator::Parser::DBIx::Class
producer_args: {}
producer_type: SQL::Translator::Producer::YAML
show_warnings: 0
trace: 0
version: 0.11021

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,59 @@
use Mojo::Base -strict;
use FindBin qw/ $Bin /;
use Test::More;
use Mojo::JSON;
use Test::Pear::LocalLoop;
use DateTime;
my $framework = Test::Pear::LocalLoop->new(
etc_dir => "$Bin/../../../etc",
);
$framework->install_fixtures('full');
my $t = $framework->framework;
my $schema = $t->app->schema;
my $session_key = $framework->login({
email => 'org1@example.com',
password => 'abc123',
});
$t->post_ok('/api/upload' => json => {
transaction_value => 10,
transaction_type => 1,
purchase_time => "2017-08-14T11:29:07.965+01:00",
organisation_id => 2,
session_key => $session_key,
})
->status_is(200)
->json_is('/success', Mojo::JSON->true);
# Rough area around Lancaster
$t->post_ok('/api/v1/supplier/location' => json => {
session_key => $session_key,
north_east => {
latitude => 54.077665,
longitude => -2.761860,
},
south_west => {
latitude => 54.013361,
longitude => -2.857647,
},
})
->status_is(200)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->true)
->json_is('/suppliers', [
{
name => 'Test Org 2',
latitude => 54.04679,
longitude => -2.7963,
},
])
->json_is('/self', {
latitude => 54.04725,
longitude => -2.79611,
});
done_testing;

View file

@ -0,0 +1,117 @@
use Mojo::Base -strict;
use FindBin qw/ $Bin /;
use Test::More;
use Mojo::JSON;
use Test::Pear::LocalLoop;
use DateTime;
my $framework = Test::Pear::LocalLoop->new(
etc_dir => "$Bin/../../../etc",
);
$framework->install_fixtures('full');
my $t = $framework->framework;
my $schema = $t->app->schema;
my $session_key = $framework->login({
email => 'org1@example.com',
password => 'abc123',
});
$t->post_ok('/api/v1/supplier/location' => json => {
session_key => $session_key,
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_is('/errors', [
'required_north_east',
'required_south_west',
]);
$t->post_ok('/api/v1/supplier/location' => json => {
session_key => $session_key,
north_east => 'banana',
south_west => 'apple',
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_is('/errors', [
'not_object_north_east',
'not_object_south_west',
]);
$t->post_ok('/api/v1/supplier/location' => json => {
session_key => $session_key,
north_east => {},
south_west => {},
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_is('/errors', [
'required_north_east_latitude',
'required_north_east_longitude',
'required_south_west_latitude',
'required_south_west_longitude',
]);
$t->post_ok('/api/v1/supplier/location' => json => {
session_key => $session_key,
north_east => {
latitude => 'banana',
longitude => 'apple',
},
south_west => {
latitude => 'grapefruit',
longitude => 'orange',
},
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_is('/errors', [
'not_number_north_east_latitude',
'not_number_north_east_longitude',
'not_number_south_west_latitude',
'not_number_south_west_longitude',
]);
$t->post_ok('/api/v1/supplier/location' => json => {
session_key => $session_key,
north_east => {
latitude => 90.00001,
longitude => 180.00001,
},
south_west => {
latitude => -90.00001,
longitude => -180.00001,
},
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_is('/errors', [
'outside_range_north_east_latitude',
'outside_range_north_east_longitude',
'outside_range_south_west_latitude',
'outside_range_south_west_longitude',
]);
# upside down when NeLat < SwLat
$t->post_ok('/api/v1/supplier/location' => json => {
session_key => $session_key,
north_east => {
latitude => -89,
longitude => 170,
},
south_west => {
latitude => 89,
longitude => -170,
},
})
->status_is(400)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->false)
->json_is('/errors', [
'upside_down',
])->or($framework->dump_error);
done_testing;

View file

@ -0,0 +1,210 @@
#! /usr/bin/env perl
use strict;
use warnings;
use DBIx::Class::Fixtures;
use FindBin qw/ $Bin /;
use lib "$Bin/../../../../lib";
use Pear::LocalLoop::Schema;
use DateTime;
my $fixtures = DBIx::Class::Fixtures->new({
config_dir => "$Bin",
});
my $schema = Pear::LocalLoop::Schema->connect('dbi:SQLite::memory:');
$schema->deploy;
$schema->resultset('Leaderboard')->populate([
[ qw/ name type / ],
[ 'Daily Total', 'daily_total' ],
[ 'Daily Count', 'daily_count' ],
[ 'Weekly Total', 'weekly_total' ],
[ 'Weekly Count', 'weekly_count' ],
[ 'Monthly Total', 'monthly_total' ],
[ 'Monthly Count', 'monthly_count' ],
[ 'All Time Total', 'all_time_total' ],
[ 'All Time Count', 'all_time_count' ],
]);
my $entity1 = {
customer => {
full_name => 'Test User1',
display_name => 'Test User1',
postcode => 'LA1 1AA',
year_of_birth => 2006,
latitude => 54.04,
longitude => -2.80,
},
user => {
email => 'test1@example.com',
password => 'abc123',
},
type => "customer",
};
my $entity2 = {
customer => {
full_name => 'Test User2',
display_name => 'Test User2',
postcode => 'LA1 1AB',
year_of_birth => 2006,
latitude => 54.04,
longitude => -2.80,
},
user => {
email => 'test2@example.com',
password => 'abc123',
},
type => "customer",
};
my $entity3 = {
customer => {
full_name => 'Test User3',
display_name => 'Test User3',
postcode => 'LA1 1AD',
year_of_birth => 2006,
latitude => 54.05,
longitude => -2.80,
},
user => {
email => 'test3@example.com',
password => 'abc123',
},
type => "customer",
};
my $entity4 = {
customer => {
full_name => 'Test User4',
display_name => 'Test User4',
postcode => 'LA1 1AE',
year_of_birth => 2006,
latitude => 54.04,
longitude => -2.80,
},
user => {
email => 'test4@example.com',
password => 'abc123',
},
type => "customer",
};
my $org1 = {
organisation => {
name => 'Test Org',
street_name => 'Test Street',
town => 'Lancaster',
postcode => 'LA1 1AF',
latitude => 54.04725,
longitude => -2.79611,
},
user => {
email => 'org1@example.com',
password => 'abc123',
},
type => "organisation",
};
my $org2 = {
organisation => {
name => 'Test Org 2',
street_name => 'Test Street',
town => 'Lancaster',
postcode => 'LA1 1AG',
latitude => 54.04679,
longitude => -2.7963,
},
user => {
email => 'org2@example.com',
password => 'abc123',
},
type => "organisation",
};
my $admin = {
customer => {
full_name => 'Test Admin',
display_name => 'Test Admin',
postcode => 'LA1 1AH',
year_of_birth => 2006,
latitude => 54.05,
longitude => -2.80,
},
user => {
email => 'admin@example.com',
password => 'abc123',
is_admin => \"1",
},
type => "customer",
};
$schema->resultset('Entity')->create( $_ )
for (
$entity1,
$entity2,
$entity3,
$entity4,
$org1,
$org2,
$admin,
);
use Geo::UK::Postcode::CodePointOpen;
my $output_dir = 'etc/code-point-open/codepo_gb';
my $cpo = Geo::UK::Postcode::CodePointOpen->new( path => $output_dir );
my $iter = $cpo->read_iterator(
outcodes => ['LA1'],
include_lat_long => 1,
split_postcode => 1,
);
my $pc_rs = $schema->resultset('GbPostcode');
my $i = 1;
while ( my $pc = $iter->() ) {
$pc_rs->find_or_create(
{
outcode => $pc->{Outcode},
incode => $pc->{Incode},
latitude => $pc->{Latitude},
longitude => $pc->{Longitude},
},
{ key => 'primary' },
);
last if $i++ > 10
}
$iter = $cpo->read_iterator(
outcodes => ['LA2'],
include_lat_long => 1,
split_postcode => 1,
);
$i = 1;
while ( my $pc = $iter->() ) {
$pc_rs->find_or_create(
{
outcode => $pc->{Outcode},
incode => $pc->{Incode},
latitude => $pc->{Latitude},
longitude => $pc->{Longitude},
},
{ key => 'primary' },
);
last if $i++ > 10
}
my $data_set = 'full';
$fixtures->dump({
all => 1,
schema => $schema,
directory => "$Bin/../data/" . $data_set,
});

View file

@ -0,0 +1,65 @@
$VAR1 = {
'has_many' => {
'fetch' => 0
},
'sets' => [
{
'class' => 'OrganisationPayroll',
'quantity' => 'all'
},
{
'quantity' => 'all',
'class' => 'User'
},
{
'quantity' => 'all',
'class' => 'Customer'
},
{
'class' => 'Entity',
'quantity' => 'all'
},
{
'quantity' => 'all',
'class' => 'LeaderboardValue'
},
{
'quantity' => 'all',
'class' => 'Feedback'
},
{
'class' => 'Organisation',
'quantity' => 'all'
},
{
'class' => 'GbPostcode',
'quantity' => 'all'
},
{
'quantity' => 'all',
'class' => 'LeaderboardSet'
},
{
'quantity' => 'all',
'class' => 'AccountToken'
},
{
'quantity' => 'all',
'class' => 'SessionToken'
},
{
'quantity' => 'all',
'class' => 'Transaction'
},
{
'class' => 'Leaderboard',
'quantity' => 'all'
}
],
'belongs_to' => {
'fetch' => 0
},
'might_have' => {
'fetch' => 0
}
};

View file

@ -0,0 +1 @@
1.001039

View file

@ -0,0 +1,10 @@
$HASH1 = {
display_name => 'Test User1',
entity_id => 1,
full_name => 'Test User1',
id => 1,
latitude => 54.04,
longitude => -2.8,
postcode => 'LA1 1AA',
year_of_birth => 2006
};

View file

@ -0,0 +1,10 @@
$HASH1 = {
display_name => 'Test User2',
entity_id => 2,
full_name => 'Test User2',
id => 2,
latitude => 54.04,
longitude => -2.8,
postcode => 'LA1 1AB',
year_of_birth => 2006
};

View file

@ -0,0 +1,10 @@
$HASH1 = {
display_name => 'Test User3',
entity_id => 3,
full_name => 'Test User3',
id => 3,
latitude => 54.05,
longitude => -2.8,
postcode => 'LA1 1AD',
year_of_birth => 2006
};

View file

@ -0,0 +1,10 @@
$HASH1 = {
display_name => 'Test User4',
entity_id => 4,
full_name => 'Test User4',
id => 4,
latitude => 54.04,
longitude => -2.8,
postcode => 'LA1 1AE',
year_of_birth => 2006
};

View file

@ -0,0 +1,10 @@
$HASH1 = {
display_name => 'Test Admin',
entity_id => 7,
full_name => 'Test Admin',
id => 5,
latitude => 54.05,
longitude => -2.8,
postcode => 'LA1 1AH',
year_of_birth => 2006
};

View file

@ -0,0 +1,4 @@
$HASH1 = {
id => 1,
type => 'customer'
};

View file

@ -0,0 +1,4 @@
$HASH1 = {
id => 2,
type => 'customer'
};

View file

@ -0,0 +1,4 @@
$HASH1 = {
id => 3,
type => 'customer'
};

View file

@ -0,0 +1,4 @@
$HASH1 = {
id => 4,
type => 'customer'
};

View file

@ -0,0 +1,4 @@
$HASH1 = {
id => 5,
type => 'organisation'
};

View file

@ -0,0 +1,4 @@
$HASH1 = {
id => 6,
type => 'organisation'
};

View file

@ -0,0 +1,4 @@
$HASH1 = {
id => 7,
type => 'customer'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '1AA',
latitude => 54.04816,
longitude => -2.80382,
outcode => 'LA1'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '1AB',
latitude => 54.04795,
longitude => -2.80423,
outcode => 'LA1'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '1AD',
latitude => 54.05425,
longitude => -2.8071,
outcode => 'LA1'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '1AE',
latitude => 54.04697,
longitude => -2.80403,
outcode => 'LA1'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '1AF',
latitude => 54.04725,
longitude => -2.79611,
outcode => 'LA1'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '1AG',
latitude => 54.04679,
longitude => -2.7963,
outcode => 'LA1'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '1AH',
latitude => 54.05155,
longitude => -2.80299,
outcode => 'LA1'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '1AJ',
latitude => 54.04835,
longitude => -2.80416,
outcode => 'LA1'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '1AL',
latitude => 54.04807,
longitude => -2.80116,
outcode => 'LA1'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '1AN',
latitude => 54.04531,
longitude => -2.80106,
outcode => 'LA1'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '1AP',
latitude => 54.05399,
longitude => -2.80818,
outcode => 'LA1'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '0AA',
latitude => 54.02493,
longitude => -2.80717,
outcode => 'LA2'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '0AB',
latitude => 54.02412,
longitude => -2.81217,
outcode => 'LA2'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '0AD',
latitude => 54.02432,
longitude => -2.80635,
outcode => 'LA2'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '0AE',
latitude => 54.02851,
longitude => -2.82025,
outcode => 'LA2'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '0AG',
latitude => 54.02071,
longitude => -2.81721,
outcode => 'LA2'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '0AH',
latitude => 54.01159,
longitude => -2.81195,
outcode => 'LA2'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '0AJ',
latitude => 54.00774,
longitude => -2.82477,
outcode => 'LA2'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '0AN',
latitude => 53.99747,
longitude => -2.82742,
outcode => 'LA2'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '0AP',
latitude => 54.0107,
longitude => -2.79779,
outcode => 'LA2'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '0AQ',
latitude => 54.02042,
longitude => -2.80675,
outcode => 'LA2'
};

View file

@ -0,0 +1,6 @@
$HASH1 = {
incode => '0AR',
latitude => 53.99469,
longitude => -2.84932,
outcode => 'LA2'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 1,
name => 'Daily Total',
type => 'daily_total'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 2,
name => 'Daily Count',
type => 'daily_count'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 3,
name => 'Weekly Total',
type => 'weekly_total'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 4,
name => 'Weekly Count',
type => 'weekly_count'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 5,
name => 'Monthly Total',
type => 'monthly_total'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 6,
name => 'Monthly Count',
type => 'monthly_count'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 7,
name => 'All Time Total',
type => 'all_time_total'
};

View file

@ -0,0 +1,5 @@
$HASH1 = {
id => 8,
name => 'All Time Count',
type => 'all_time_count'
};

View file

@ -0,0 +1,18 @@
$HASH1 = {
country => undef,
entity_id
=> 5,
id => 1,
latitude => 54.04725,
longitude
=> -2.79611,
name => 'Test Org',
pending => 0,
postcode => 'LA1 1AF',
sector => undef,
street_name
=> 'Test Street',
submitted_by_id
=> undef,
town => 'Lancaster'
};

View file

@ -0,0 +1,18 @@
$HASH1 = {
country => undef,
entity_id
=> 6,
id => 2,
latitude => 54.04679,
longitude
=> -2.7963,
name => 'Test Org 2',
pending => 0,
postcode => 'LA1 1AG',
sector => undef,
street_name
=> 'Test Street',
submitted_by_id
=> undef,
town => 'Lancaster'
};

View file

@ -0,0 +1,8 @@
$HASH1 = {
email => 'test1@example.com',
entity_id => 1,
id => 1,
is_admin => 0,
join_date => '2017-09-26 16:46:07',
password => '$2a$08$6CPa/BnjeTVEiYKxIyc2VeDn.5Feer6vSUZ1GnR7mMK.NOLulxeK6'
};

View file

@ -0,0 +1,8 @@
$HASH1 = {
email => 'test2@example.com',
entity_id => 2,
id => 2,
is_admin => 0,
join_date => '2017-09-26 16:46:07',
password => '$2a$08$vYQVGmZ3JHnRhHG63l/o2Op/iCvzE0C16lNGeuOoMuY5NOvFbKABC'
};

View file

@ -0,0 +1,8 @@
$HASH1 = {
email => 'test3@example.com',
entity_id => 3,
id => 3,
is_admin => 0,
join_date => '2017-09-26 16:46:07',
password => '$2a$08$TwGM55wGoR3RiiurPVhIYem4i4dm84ADWK1NHjDELApXSw2n9lloe'
};

View file

@ -0,0 +1,8 @@
$HASH1 = {
email => 'test4@example.com',
entity_id => 4,
id => 4,
is_admin => 0,
join_date => '2017-09-26 16:46:07',
password => '$2a$08$2dp0fMs/CWTQglOEOTCSduyXWrknO7XrfUmmUty1Uoy9ljCv6rR8e'
};

View file

@ -0,0 +1,8 @@
$HASH1 = {
email => 'org1@example.com',
entity_id => 5,
id => 5,
is_admin => 0,
join_date => '2017-09-26 16:46:07',
password => '$2a$08$f1OC7odxIXTtPN5EfXFdrO5/7QqzwVbqLpN6UOiV0nPW4tvETQEE2'
};

View file

@ -0,0 +1,8 @@
$HASH1 = {
email => 'org2@example.com',
entity_id => 6,
id => 6,
is_admin => 0,
join_date => '2017-09-26 16:46:07',
password => '$2a$08$PwazoBpY7oXR5lXgNL4Mv.givt1J6vuclDtxltJKlBHJL1xkOjXMy'
};

View file

@ -0,0 +1,8 @@
$HASH1 = {
email => 'admin@example.com',
entity_id => 7,
id => 7,
is_admin => 1,
join_date => '2017-09-26 16:46:07',
password => '$2a$08$PKmst2sVN6N1Qrp9i98GYuWW.mwVrOAktQYCP.ibu7eLDYVP3yja2'
};

View file

@ -1,6 +1,25 @@
% layout 'admin';
% title 'Organisations';
% content_for javascript => begin
<script>
function initMap() {
var org_latlng = {
lat: <%= $valid_org->latitude %>,
lng: <%= $valid_org->longitude %>
};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 18,
center: org_latlng
});
var marker = new google.maps.Marker({
position: org_latlng,
map: map
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=<%= $c->config->{api_keys}->{google_maps_js_api} %>&callback=initMap">
</script>
% end
% if ( my $error = flash 'error' ) {
<div class="alert alert-danger" role="alert">
@ -11,6 +30,8 @@
<strong>Success!</strong> <%= $success %>
</div>
% }
<div class="row">
<div class="col-sm-12 col-md-6">
<div class="card mb-3">
<h3 class="card-header">
%= $valid_org->name
@ -18,51 +39,69 @@
<div class="card-block">
<form action="<%= url_for %>" method="post">
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Organisation Name</label>
<div class="col-sm-10">
<label for="name" class="col-md-4 col-form-label">Organisation Name</label>
<div class="col-md-8">
<input id="name" type="text" class="form-control" placeholder="Organisation Name" name="name" value="<%= $valid_org->name %>">
</div>
</div>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Street Name</label>
<div class="col-sm-10">
<label for="name" class="col-md-4 col-form-label">Street Name</label>
<div class="col-md-8">
<input id="street_name" type="text" class="form-control" placeholder="Street Name" name="street_name" value="<%= $valid_org->street_name %>">
</div>
</div>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Town/City</label>
<div class="col-sm-10">
<label for="name" class="col-md-4 col-form-label">Town/City</label>
<div class="col-md-8">
<input id="town" type="text" class="form-control" placeholder="Town" name="town" value="<%= $valid_org->town %>">
</div>
</div>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Sector</label>
<div class="col-sm-10">
<label for="name" class="col-md-4 col-form-label">Sector</label>
<div class="col-md-8">
<select class="form-control" name="sector">
%= include 'partials/sector_options', selected_sector => $valid_org->sector || '';
</select>
</div>
</div>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Postcode</label>
<div class="col-sm-10">
<label for="name" class="col-md-4 col-form-label">Postcode</label>
<div class="col-md-8">
<input id="postcode" type="text" class="form-control" placeholder="Postcode" name="postcode" value="<%= $valid_org->postcode %>">
</div>
</div>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Validated</label>
<div class="col-sm-10">
<label for="name" class="col-md-4 col-form-label">Validated</label>
<div class="col-md-8">
<input id="pending" type="checkbox" name="pending" value="0"<%= $valid_org->pending ? '' : ' checked' %>>
</div>
</div>
<div class="form-group row">
<div class="ml-auto col-sm-10">
<button class="btn btn-success" type="submit">Save Changes To Organisation</button>
<div class="ml-auto col-md-8">
<button class="btn btn-success btn-block" type="submit">Save Changes To Organisation</button>
</div>
</div>
</form>
</div>
</div>
</div>
<div class="col-sm-12 col-md-6">
<div class="card mb-3">
<div class="card-header" role="tab" id="mapHeader">
<h3 class="mb-0">
Location
</h3>
</div>
<div id="mapBody" role="tabpanel">
<div class="card-block">
<!-- Yes this is nasty. no i dont care. --!>
<style> #map { width: 100%; height: 400px; background-color: grey; } </style>
<div id="map"></div>
</div>
</div>
</div>
</div>
<div class="col-12">
<div class="card mb-3">
<h3 class="card-header">
Transactions
@ -90,3 +129,5 @@
</li>
</ul>
</div>
</div>
</div>