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

(-)a/Koha/REST/V1/ItemTypes.pm (+49 lines)
Line 0 Link Here
1
package Koha::REST::V1::ItemTypes;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::ItemTypes;
23
24
use Try::Tiny;
25
26
=head1 API
27
28
=head2 Methods
29
30
=head3 list
31
32
=cut
33
34
sub list {
35
    my $c = shift->openapi->valid_input or return;
36
37
    return try {
38
        my $itemtypes_set = Koha::ItemTypes->new;
39
        my $itemtypes = $c->objects->search( $itemtypes_set, { embed => $c->stash('koha.embed') } );
40
41
        return $c->render( status => 200, openapi => $itemtypes );
42
    }
43
    catch {
44
        $c->unhandled_exception($_);
45
    };
46
47
}
48
49
1;
(-)a/api/v1/swagger/definitions/itemtype.yaml (+96 lines)
Line 0 Link Here
1
---
2
type: object
3
properties:
4
  itemtype:
5
    type: string
6
    description: Unique key, a code associated with the item type
7
    readOnly: true
8
  parent_type:
9
    description: Unique key, a code associated with the parent item type
10
    type:
11
      - string
12
      - "null"
13
  description:
14
    description: A plain text explanation of the item type
15
    type: string
16
  rentalcharge:
17
    description: The amount charged when this item is checked out/issued
18
    type:
19
      - number
20
      - "null"
21
  rentalcharge_daily:
22
    description: The amount charged for each day between checkout date and due date
23
    type:
24
      - number
25
      - "null"
26
  rentalcharge_daily_calendar:
27
    description: Controls if the daily rental fee is calculated directly or using finesCalendar
28
    type: boolean
29
  rentalcharge_hourly:
30
    description: The amount charged for each hour between checkout date and due date
31
    type:
32
      - number
33
      - "null"
34
  rentalcharge_hourly_calendar:
35
    description: Controls if the hourly rental fee is calculated directly or using finesCalendar
36
    type: boolean
37
  defaultreplacecost:
38
    description: Default replacement cost
39
    type:
40
      - number
41
      - "null"
42
  processfee:
43
    description: Default text be recorded in the column note when the processing fee is applied
44
    type:
45
      - number
46
      - "null"
47
  notforloan:
48
    description: Controls if the item is available for loan
49
    type:
50
      - boolean
51
      - "null"
52
  imageurl:
53
    description: URL for the item type icon
54
    type:
55
      - string
56
      - "null"
57
  summary:
58
    description: Information from the summary field, may include HTML
59
    type:
60
      - string
61
      - "null"
62
  checkinmsg:
63
    description: Message that is displayed when an item with the given item type is checked in
64
    type:
65
      - string
66
      - "null"
67
  checkinmsgtype:
68
    description: Type (CSS class) for the checkinmsg, can be 'alert' or 'message'
69
    type:
70
      - string
71
      - "null"
72
  sip_media_type:
73
    description: SIP2 protocol media type for this itemtype
74
    type:
75
      - string
76
      - "null"
77
  hideinopac:
78
    description: Hide the item type from the search options in OPAC
79
    type: boolean
80
  searchcategory:
81
    description: Group this item type with others with the same value on OPAC search options
82
    type:
83
      - string
84
      - "null"
85
  automatic_checkin:
86
    description: Controls if automatic checkin is enabled for items of this type
87
    type: boolean
88
  translated_descriptions:
89
    description: Translations of description plain text
90
    type: array
91
    items:
92
      $ref: "itemtype_translated_description.yaml"
93
94
additionalProperties: false
95
required:
96
  - itemtype
(-)a/api/v1/swagger/definitions/itemtype_translated_description.yaml (+13 lines)
Line 0 Link Here
1
---
2
type: object
3
properties:
4
  lang:
5
    description: Language identifier
6
    type: string
7
  translation:
8
    description: Translated plain text
9
    type: string
10
additionalProperties: false
11
required:
12
  - lang
13
  - translation
(-)a/api/v1/swagger/paths/itemtypes.yaml (+59 lines)
Line 0 Link Here
1
---
2
/itemtypes:
3
  get:
4
    x-mojo-to: ItemTypes#list
5
    operationId: listItemTypes
6
    tags:
7
      - itemtypes
8
    summary: List item types
9
    produces:
10
      - application/json
11
    parameters:
12
      - name: x-koha-embed
13
        in: header
14
        required: false
15
        description: Embed list sent as a request header
16
        type: array
17
        items:
18
          type: string
19
          enum:
20
            - translated_descriptions
21
        collectionFormat: csv
22
      - $ref: "../swagger.yaml#/parameters/match"
23
      - $ref: "../swagger.yaml#/parameters/order_by"
24
      - $ref: "../swagger.yaml#/parameters/page"
25
      - $ref: "../swagger.yaml#/parameters/per_page"
26
      - $ref: "../swagger.yaml#/parameters/q_param"
27
      - $ref: "../swagger.yaml#/parameters/q_body"
28
    responses:
29
      200:
30
        description: A list of itemtypes
31
        schema:
32
          items:
33
            $ref: "../swagger.yaml#/definitions/itemtype"
34
          type: array
35
      400:
36
        description: Bad request
37
        schema:
38
          $ref: "../swagger.yaml#/definitions/error"
39
      403:
40
        description: Access forbidden
41
        schema:
42
          $ref: "../swagger.yaml#/definitions/error"
43
      404:
44
        description: Resource not found
45
        schema:
46
          $ref: "../swagger.yaml#/definitions/error"
47
      500:
48
        description: |-
49
          Internal server error. Possible `error_code` attribute values:
50
          * `internal_server_error`
51
        schema:
52
          $ref: "../swagger.yaml#/definitions/error"
53
      503:
54
        description: Under maintenance
55
        schema:
56
          $ref: "../swagger.yaml#/definitions/error"
57
    x-koha-authorization:
58
      permissions:
59
        catalogue: 1
(-)a/api/v1/swagger/swagger.yaml (+7 lines)
Lines 76-81 definitions: Link Here
76
    $ref: ./definitions/item.yaml
76
    $ref: ./definitions/item.yaml
77
  item_group:
77
  item_group:
78
    $ref: ./definitions/item_group.yaml
78
    $ref: ./definitions/item_group.yaml
79
  itemtype:
80
    $ref: ./definitions/itemtype.yaml
79
  job:
81
  job:
80
    $ref: ./definitions/job.yaml
82
    $ref: ./definitions/job.yaml
81
  library:
83
  library:
Lines 275-280 paths: Link Here
275
    $ref: ./paths/import_batch_profiles.yaml#/~1import_batch_profiles
277
    $ref: ./paths/import_batch_profiles.yaml#/~1import_batch_profiles
276
  "/import_batch_profiles/{import_batch_profile_id}":
278
  "/import_batch_profiles/{import_batch_profile_id}":
277
    $ref: "./paths/import_batch_profiles.yaml#/~1import_batch_profiles~1{import_batch_profile_id}"
279
    $ref: "./paths/import_batch_profiles.yaml#/~1import_batch_profiles~1{import_batch_profile_id}"
280
  /itemtypes:
281
    $ref: ./paths/itemtypes.yaml#/~1itemtypes
278
  /items:
282
  /items:
279
    $ref: ./paths/items.yaml#/~1items
283
    $ref: ./paths/items.yaml#/~1items
280
  "/items/{item_id}":
284
  "/items/{item_id}":
Lines 863-868 tags: Link Here
863
  - description: "Manage items\n"
867
  - description: "Manage items\n"
864
    name: items
868
    name: items
865
    x-displayName: Items
869
    x-displayName: Items
870
  - description: "Manage item types\n"
871
    name: itemtypes
872
    x-displayName: Item Types
866
  - description: "Manage jobs\n"
873
  - description: "Manage jobs\n"
867
    name: jobs
874
    name: jobs
868
    x-displayName: Jobs
875
    x-displayName: Jobs
(-)a/t/db_dependent/api/v1/itemtypes.t (-1 / +145 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
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 1;
21
use Test::Mojo;
22
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
25
26
use Koha::ItemTypes;
27
use Koha::Database;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
my $t = Test::Mojo->new('Koha::REST::V1');
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
35
use Data::Dumper;
36
37
subtest 'list_itemtypes() tests' => sub {
38
39
    plan tests => 12;
40
41
    $schema->storage->txn_begin;
42
43
    my $itemtype = $builder->build_object(
44
        {
45
            class => 'Koha::ItemTypes',
46
            value => {
47
                itemtype => 'TEST_IT',
48
                parent_type => undef,
49
                description => 'Test itemtype',
50
                rentalcharge => 100.0,
51
                rentalcharge_daily => 50.,
52
                rentalcharge_daily_calendar => 0,
53
                rentalcharge_hourly => 0.60,
54
                rentalcharge_hourly_calendar => 1,
55
                defaultreplacecost => 1000.0,
56
                processfee => 20.0,
57
                notforloan => 0,
58
                imageurl => 'https://upload.wikimedia.org/wikipedia/commons/1/1f/202208_test-tube-4.svg',
59
                summary => 'An itemtype for testing',
60
                checkinmsg => 'Checking in test',
61
                checkinmsgtype => 'message',
62
                sip_media_type => 'spt',
63
                hideinopac => 1,
64
                searchcategory => 'test search category',
65
                automatic_checkin => 0,
66
            }
67
        }
68
        );
69
70
    my $en = $builder->build_object(
71
        {
72
            class => 'Koha::Localizations',
73
            value => {
74
                entity => 'itemtypes',
75
                code => 'TEST_IT',
76
                lang => 'en',
77
                translation => 'English word "test"',
78
            }
79
        }
80
    );
81
    my $sv = $builder->build_object(
82
        {
83
            class => 'Koha::Localizations',
84
            value => {
85
                entity => 'itemtypes',
86
                code => 'TEST_IT',
87
                lang => 'sv_SE',
88
                translation => 'Swedish word "test"',
89
            }
90
        }
91
    );
92
93
    my $librarian = $builder->build_object(
94
        {
95
            class => 'Koha::Patrons',
96
            value => { flags => 2 ** 2 } # catalogue flag = 2
97
        }
98
    );
99
    my $password = 'thePassword123';
100
    $librarian->set_password( { password => $password, skip_validation => 1 } );
101
    my $userid = $librarian->userid;
102
103
    my $patron = $builder->build_object(
104
        {
105
            class => 'Koha::Patrons',
106
            value => { flags => 0 }
107
        }
108
    );
109
110
    $patron->set_password( { password => $password, skip_validation => 1 } );
111
    my $unauth_userid = $patron->userid;
112
113
    ## Authorized user tests
114
    # No category, 404 expected
115
    $t->get_ok("//$userid:$password@/api/v1/itemtypes")
116
      ->status_is(200)
117
      ->json_has('/0');
118
119
    for my $json (@{$t->tx->res->json}) {
120
        if ($json->{itemtype} eq 'TEST_IT') {
121
            is($json->{description}, 'Test itemtype');
122
            ok(!exists $json->{translated_descriptions});
123
        }
124
    }
125
126
    $t->get_ok("//$userid:$password@/api/v1/itemtypes" => { 'x-koha-embed' => 'translated_descriptions' } )
127
      ->status_is(200)
128
      ->json_has('/0');
129
130
    for my $json (@{$t->tx->res->json}) {
131
        if ($json->{itemtype} eq 'TEST_IT') {
132
            is($json->{description}, 'Test itemtype');
133
            is_deeply($json->{translated_descriptions}, [
134
                          { lang => 'en', translation => 'English word "test"' },
135
                          { lang => 'sv_SE', translation => 'Swedish word "test"' },
136
                      ]);
137
        }
138
    }
139
140
    # Unauthorized access
141
    $t->get_ok("//$unauth_userid:$password@/api/v1/itemtypes")
142
      ->status_is(403);
143
144
    $schema->storage->txn_rollback;
145
};

Return to bug 34008