View | Details | Raw Unified | Return to bug 19661
Collapse All | Expand All

(-)a/Koha/REST/V1/Acquisitions/Funds.pm (+105 lines)
Line 0 Link Here
1
package Koha::REST::V1::Acquisitions::Funds;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use C4::Budgets;
23
use JSON qw(to_json);
24
25
use Try::Tiny;
26
27
=head1 NAME
28
29
Koha::REST::V1::Acquisitions::Funds
30
31
=head1 API
32
33
=head2 Methods
34
35
=head3 list_funds
36
37
Controller function that handles listing Funds
38
39
=cut
40
41
sub list_funds {
42
    my $c = shift->openapi->valid_input or return;
43
44
    my $args = _to_model($c->req->params->to_hash);
45
    my $filter;
46
47
    for my $filter_param ( keys %$args ) {
48
        $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" }
49
            if $args->{$filter_param};
50
    }
51
52
    return try {
53
        my $funds = GetBudgets($filter);
54
        my @fundsArray = map { _to_api($_) } @$funds;
55
        return $c->render( status  => 200,
56
                           openapi =>  \@fundsArray);
57
    }
58
    catch {
59
        if ( $_->isa('DBIx::Class::Exception') ) {
60
            return $c->render( status  => 500,
61
                               openapi => { error => $_->{msg} } );
62
        }
63
        else {
64
            return $c->render( status  => 500,
65
                               openapi => { error => "Something went wrong, check the logs. $_ $filter" } );
66
        }
67
    };
68
}
69
70
=head3 _to_api
71
72
Helper function that maps a Fund  into
73
the attribute names the exposed REST api spec.
74
75
=cut
76
77
sub _to_api {
78
    my $fund = shift;
79
    my $returnfund;
80
    $returnfund->{id} = delete $fund->{budget_id};
81
    $returnfund->{code} = delete $fund->{budget_code};
82
    $returnfund->{name} = delete $fund->{budget_name};
83
84
    return $returnfund;
85
}
86
87
=head3 _to_model
88
89
Helper function that maps REST api objects into Fund
90
attribute names.
91
92
=cut
93
94
sub _to_model {
95
    my $fund = shift;
96
97
    # Rename back
98
    $fund->{budget_id}     = delete $fund->{id};
99
    $fund->{budget_code}   = delete $fund->{code};
100
    $fund->{budget_name}   = delete $fund->{name};
101
102
    return $fund;
103
}
104
105
1;
(-)a/api/v1/swagger/definitions.json (+3 lines)
Lines 28-32 Link Here
28
  },
28
  },
29
  "vendor": {
29
  "vendor": {
30
    "$ref": "definitions/vendor.json"
30
    "$ref": "definitions/vendor.json"
31
  },
32
  "fund": {
33
    "$ref": "definitions/fund.json"
31
  }
34
  }
32
}
35
}
(-)a/api/v1/swagger/definitions/fund.json (+24 lines)
Line 0 Link Here
1
{
2
  "type": "object",
3
  "properties": {
4
    "id": {
5
      "$ref": "../x-primitives.json#/fund_id"
6
    },
7
    "code": {
8
      "type": [
9
        "string",
10
        "null"
11
      ],
12
      "description": "Fund code"
13
    },
14
    "name": {
15
      "type": [
16
        "string",
17
        "null"
18
      ],
19
      "description": "Fund name"
20
    }
21
  },
22
  "additionalProperties": false,
23
  "required": ["name"]
24
}
(-)a/api/v1/swagger/parameters.json (-1 / +4 lines)
Lines 54-58 Link Here
54
    "required": false,
54
    "required": false,
55
    "description": "Page size, for paginated object listing",
55
    "description": "Page size, for paginated object listing",
56
    "type": "integer"
56
    "type": "integer"
57
   }
57
   },
58
  "fundidPathParam": {
59
    "$ref": "parameters/fund.json#/fundidPathParam"
60
  }
58
}
61
}
(-)a/api/v1/swagger/parameters/fund.json (+9 lines)
Line 0 Link Here
1
{
2
    "fundidPathParam": {
3
      "name": "fund_id",
4
      "in": "path",
5
      "description": "Fund id",
6
      "required": true,
7
      "type": "integer"
8
    }
9
}
(-)a/api/v1/swagger/paths.json (+3 lines)
Lines 8-13 Link Here
8
  "/acquisitions/vendors/{vendor_id}": {
8
  "/acquisitions/vendors/{vendor_id}": {
9
    "$ref": "paths/acquisitions_vendors.json#/~1acquisitions~1vendors~1{vendor_id}"
9
    "$ref": "paths/acquisitions_vendors.json#/~1acquisitions~1vendors~1{vendor_id}"
10
  },
10
  },
11
  "/acquisitions/funds": {
12
    "$ref": "paths/acquisitions_funds.json#/~1acquisitions~1funds"
13
  },
11
  "/cities": {
14
  "/cities": {
12
    "$ref": "paths/cities.json#/~1cities"
15
    "$ref": "paths/cities.json#/~1cities"
13
  },
16
  },
(-)a/api/v1/swagger/paths/acquisitions_funds.json (+73 lines)
Line 0 Link Here
1
{
2
  "/acquisitions/funds": {
3
    "get": {
4
      "x-mojo-to": "Acquisitions::Funds#list_funds",
5
      "operationId": "listFunds",
6
      "tags": ["acquisitions","funds"],
7
      "produces": [
8
        "application/json"
9
      ],
10
      "parameters": [{
11
        "name": "name",
12
        "in": "query",
13
        "description": "Case insensitive search on fund name",
14
        "required": false,
15
        "type": "string"
16
      },
17
      {
18
        "name": "budget_owner_id",
19
        "in": "query",
20
        "description": "Display only the funds that belongs to the given borrowernumber",
21
        "required": false,
22
        "type": "integer"
23
      }
24
      ],
25
      "responses": {
26
        "200": {
27
          "description": "A list of funds",
28
          "schema": {
29
            "type": "array",
30
            "items": {
31
              "$ref": "../definitions.json#/fund"
32
            }
33
          }
34
        },
35
        "401": {
36
          "description": "Authentication required",
37
          "schema": {
38
            "$ref": "../definitions.json#/error"
39
          }
40
        },
41
        "403": {
42
          "description": "Access forbidden",
43
          "schema": {
44
            "$ref": "../definitions.json#/error"
45
          }
46
        },
47
        "404": {
48
          "description": "Fund not found",
49
          "schema": {
50
            "$ref": "../definitions.json#/error"
51
          }
52
        },
53
        "500": {
54
          "description": "Internal server error",
55
          "schema": {
56
            "$ref": "../definitions.json#/error"
57
          }
58
        },
59
        "503": {
60
          "description": "Under maintenance",
61
          "schema": {
62
            "$ref": "../definitions.json#/error"
63
          }
64
        }
65
      },
66
      "x-koha-authorization": {
67
        "permissions": {
68
          "acquisition": "budget_manage_all"
69
        }
70
      }
71
    }
72
  }
73
}
(-)a/api/v1/swagger/x-primitives.json (+6 lines)
Lines 50-54 Link Here
50
    "type": "integer",
50
    "type": "integer",
51
    "description": "internally assigned vendor identifier",
51
    "description": "internally assigned vendor identifier",
52
    "readOnly": true
52
    "readOnly": true
53
  },
54
  "fund_id": {
55
    "type": "integer",
56
    "description": "internally assigned fund identifier",
57
    "readOnly": true
53
  }
58
  }
59
54
}
60
}
(-)a/t/db_dependent/api/v1/acquisitions_funds.t (-1 / +125 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Test::More tests => 14;
21
use Test::Mojo;
22
use t::lib::TestBuilder;
23
use t::lib::Mocks;
24
25
use C4::Auth;
26
use C4::Context;
27
use C4::Budgets;
28
29
use Koha::Database;
30
use Koha::Patron;
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new();
34
35
$schema->storage->txn_begin;
36
37
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
38
# this affects the other REST api tests
39
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
40
41
$ENV{REMOTE_ADDR} = '127.0.0.1';
42
my $t = Test::Mojo->new('Koha::REST::V1');
43
44
my $fund1 = {
45
    budget_code      => 'ABCD',
46
    budget_amount    => '123.132000',
47
    budget_name      => 'Periodiques',
48
    budget_notes     => 'This is a note',
49
};
50
my $budget_id = AddBudget($fund1);
51
isnt( $budget_id, undef, 'AddBudget does not returns undef' );
52
53
$t->get_ok('/api/v1/acquisitions/funds')
54
  ->status_is(401);
55
56
$t->get_ok('/api/v1/acquisitions/funds/?name=testFund')
57
  ->status_is(401);
58
59
my ( $borrowernumber, $session_id )
60
        #= create_user_and_session( { authorized => 1 } );
61
        = create_user_and_session(  );
62
63
my $tx = $t->ua->build_tx(GET => '/api/v1/acquisitions/funds');
64
$tx->req->cookies({name => 'CGISESSID', value => $session_id});
65
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
66
$t->request_ok($tx)
67
  ->status_is(403);
68
69
$tx = $t->ua->build_tx(GET => "/api/v1/acquisitions/funds/?name=" . $fund1->{ budget_name });
70
$tx->req->cookies({name => 'CGISESSID', value => $session_id});
71
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
72
$t->request_ok($tx)
73
  ->status_is(403);
74
75
( $borrowernumber, $session_id )
76
        = create_user_and_session( { authorized => 1 } );
77
78
$tx = $t->ua->build_tx(GET => '/api/v1/acquisitions/funds');
79
$tx->req->cookies({name => 'CGISESSID', value => $session_id});
80
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
81
$t->request_ok($tx)
82
  ->status_is(200);
83
84
$tx = $t->ua->build_tx(GET => "/api/v1/acquisitions/funds/?name=" . $fund1->{ budget_name });
85
$tx->req->cookies({name => 'CGISESSID', value => $session_id});
86
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
87
$t->request_ok($tx)
88
  ->status_is(200)
89
  ->json_like('/0/name' => qr/$fund1->{ budget_name }/);
90
91
$schema->storage->txn_rollback;
92
93
sub create_user_and_session {
94
95
    my $args = shift;
96
    my $flags = ( $args->{authorized} ) ? 2052 : 0;
97
98
    # my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0;
99
    my $dbh = C4::Context->dbh;
100
101
    my $user = $builder->build(
102
        {   source => 'Borrower',
103
            value  => { flags => $flags }
104
        }
105
    );
106
107
    # Create a session for the authorized user
108
    my $session = C4::Auth::get_session('');
109
    $session->param( 'number',   $user->{borrowernumber} );
110
    $session->param( 'id',       $user->{userid} );
111
    $session->param( 'ip',       '127.0.0.1' );
112
    $session->param( 'lasttime', time() );
113
    $session->flush;
114
115
    if ( $args->{authorized} ) {
116
        $dbh->do(
117
            q{
118
            INSERT INTO user_permissions (borrowernumber,module_bit,code)
119
            VALUES (?,11,'budget_manage_all')},
120
            undef, $user->{borrowernumber}
121
        );
122
    }
123
124
    return ( $user->{borrowernumber}, $session->id );
125
}

Return to bug 19661