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

(-)a/Koha/REST/V1/ItemTypes.pm (+51 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
use Data::Dumper;
35
36
sub list {
37
    my $c = shift->openapi->valid_input or return;
38
39
    return try {
40
        my $itemtypes_set = Koha::ItemTypes->new;
41
        my $itemtypes = $c->objects->search( $itemtypes_set, { embed => $c->stash('koha.embed') } );
42
43
        return $c->render( status => 200, openapi => $itemtypes );
44
    }
45
    catch {
46
        $c->unhandled_exception($_);
47
    };
48
49
}
50
51
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 (+60 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
      - $ref: "../swagger.yaml#/parameters/q_header"
29
    responses:
30
      200:
31
        description: A list of itemtypes
32
        schema:
33
          items:
34
            $ref: "../swagger.yaml#/definitions/itemtype"
35
          type: array
36
      400:
37
        description: Bad request
38
        schema:
39
          $ref: "../swagger.yaml#/definitions/error"
40
      403:
41
        description: Access forbidden
42
        schema:
43
          $ref: "../swagger.yaml#/definitions/error"
44
      404:
45
        description: Resource not found
46
        schema:
47
          $ref: "../swagger.yaml#/definitions/error"
48
      500:
49
        description: |-
50
          Internal server error. Possible `error_code` attribute values:
51
          * `internal_server_error`
52
        schema:
53
          $ref: "../swagger.yaml#/definitions/error"
54
      503:
55
        description: Under maintenance
56
        schema:
57
          $ref: "../swagger.yaml#/definitions/error"
58
    x-koha-authorization:
59
      permissions:
60
        catalogue: 1
(-)a/api/v1/swagger/swagger.yaml (+7 lines)
Lines 72-77 definitions: Link Here
72
    $ref: ./definitions/item.yaml
72
    $ref: ./definitions/item.yaml
73
  item_group:
73
  item_group:
74
    $ref: ./definitions/item_group.yaml
74
    $ref: ./definitions/item_group.yaml
75
  itemtype:
76
    $ref: ./definitions/itemtype.yaml
75
  job:
77
  job:
76
    $ref: ./definitions/job.yaml
78
    $ref: ./definitions/job.yaml
77
  library:
79
  library:
Lines 263-268 paths: Link Here
263
    $ref: ./paths/import_batch_profiles.yaml#/~1import_batch_profiles
265
    $ref: ./paths/import_batch_profiles.yaml#/~1import_batch_profiles
264
  "/import_batch_profiles/{import_batch_profile_id}":
266
  "/import_batch_profiles/{import_batch_profile_id}":
265
    $ref: "./paths/import_batch_profiles.yaml#/~1import_batch_profiles~1{import_batch_profile_id}"
267
    $ref: "./paths/import_batch_profiles.yaml#/~1import_batch_profiles~1{import_batch_profile_id}"
268
  /itemtypes:
269
    $ref: ./paths/itemtypes.yaml#/~1itemtypes
266
  /items:
270
  /items:
267
    $ref: ./paths/items.yaml#/~1items
271
    $ref: ./paths/items.yaml#/~1items
268
  "/items/{item_id}":
272
  "/items/{item_id}":
Lines 846-851 tags: Link Here
846
  - description: "Manage items\n"
850
  - description: "Manage items\n"
847
    name: items
851
    name: items
848
    x-displayName: Items
852
    x-displayName: Items
853
  - description: "Manage item types\n"
854
    name: itemtypes
855
    x-displayName: Item Types
849
  - description: "Manage jobs\n"
856
  - description: "Manage jobs\n"
850
    name: jobs
857
    name: jobs
851
    x-displayName: Jobs
858
    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