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

(-)a/Koha/AdditionalField.pm (-2 / +27 lines)
Lines 42-47 sub effective_authorised_value_category { Link Here
42
    return $category;
42
    return $category;
43
}
43
}
44
44
45
=head3 to_api
46
47
    my $json = $additional_field_type->to_api;
48
49
Overloaded method that returns a JSON representation of the Koha::AdditionalField
50
object, suitable for API output.
51
52
=cut
53
54
sub to_api {
55
    my ( $self, $params ) = @_;
56
57
    my $table_to_resource = {
58
        'aqbasket'   => 'basket',
59
        'aqinvoices' => 'invoice',
60
        'aqorders'   => 'order',
61
    };
62
63
    my $json = $self->SUPER::to_api($params);
64
65
    $json->{resource_type} = $table_to_resource->{ $self->tablename };
66
67
    return $json;
68
}
69
45
=head3 to_api_mapping
70
=head3 to_api_mapping
46
71
47
This method returns the mapping for representing an AdditionalField object
72
This method returns the mapping for representing an AdditionalField object
Lines 51-58 on the API. Link Here
51
76
52
sub to_api_mapping {
77
sub to_api_mapping {
53
    return {
78
    return {
54
        id                        => 'additional_field_id',
79
        id                        => 'extended_attribute_type_id',
55
        tablename                 => 'table_name',
80
        tablename                 => 'resource_type',
56
        authorised_value_category => 'authorised_value_category_name',
81
        authorised_value_category => 'authorised_value_category_name',
57
        marcfield                 => 'marc_field',
82
        marcfield                 => 'marc_field',
58
        marcfield_mode            => 'marc_field_mode'
83
        marcfield_mode            => 'marc_field_mode'
(-)a/Koha/REST/V1/AdditionalFields.pm (-7 / +17 lines)
Lines 1-4 Link Here
1
package Koha::REST::V1::AdditionalFields;
1
package Koha::REST::V1::ExtendedAttributeTypes;
2
2
3
# This file is part of Koha.
3
# This file is part of Koha.
4
#
4
#
Lines 34-52 use Try::Tiny qw( catch try ); Link Here
34
sub list {
34
sub list {
35
    my $c = shift->openapi->valid_input or return;
35
    my $c = shift->openapi->valid_input or return;
36
36
37
    my $tablename = $c->param('table_name');
37
    my $resource_type = $c->param('resource_type');
38
39
    # FIXME: Maybe not the best place for this mapping
40
    my $resource_to_table = {
41
        basket  => 'aqbasket',
42
        invoice => 'aqinvoices',
43
        order   => 'aqorders',
44
    };
38
45
39
    return try {
46
    return try {
40
        my $additional_fields_set = Koha::AdditionalFields->new;
47
        my $additional_fields_set = Koha::AdditionalFields->new;
41
        if ($tablename) {
48
        if ($resource_type) {
42
            $additional_fields_set = $additional_fields_set->search( { tablename => $tablename } );
49
            $additional_fields_set =
50
                $additional_fields_set->search( { tablename => $resource_to_table->{$resource_type} } );
43
        }
51
        }
44
        my $additional_fields = $c->objects->search($additional_fields_set);
52
45
        return $c->render( status => 200, openapi => $additional_fields );
53
        return $c->render(
54
            status  => 200,
55
            openapi => $c->objects->search($additional_fields_set)
56
        );
46
    } catch {
57
    } catch {
47
        $c->unhandled_exception($_);
58
        $c->unhandled_exception($_);
48
    };
59
    };
49
50
}
60
}
51
61
52
1;
62
1;
(-)a/api/v1/swagger/definitions/additional_field.yaml (-35 lines)
Lines 1-35 Link Here
1
---
2
type: object
3
properties:
4
  additional_field_id:
5
    type: integer
6
    description: internally assigned additional field identifier
7
    readOnly: true
8
  table_name:
9
    description: name of the table this additional field corresponds to
10
    type: string
11
  name:
12
    description: name of the additional field
13
    type: string
14
  authorised_value_category_name:
15
    description: authorised value category of the additional field
16
    type:
17
      - string
18
      - "null"
19
  marc_field:
20
    description: marc field of the additional field
21
    type: string
22
  marc_field_mode:
23
    description: marc field mode of the additional field
24
    type: string
25
    enum:
26
      - get
27
      - set
28
  searchable:
29
    description: is the additional field searchable
30
    type: boolean
31
32
additionalProperties: false
33
required:
34
  - additional_field_id
35
  - table_name
(-)a/api/v1/swagger/definitions/extended_attribute_type.yaml (+35 lines)
Line 0 Link Here
1
---
2
type: object
3
properties:
4
  extended_attribute_type_id:
5
    type: integer
6
    description: internally assigned extended attribute type identifier
7
    readOnly: true
8
  resource_type:
9
    description: name of the resource type this extended attribute type corresponds to
10
    type: string
11
  name:
12
    description: name of the extended attribute type
13
    type: string
14
  authorised_value_category_name:
15
    description: authorised value category of the extended attribute type values
16
    type:
17
      - string
18
      - "null"
19
  marc_field:
20
    description: marc field of the extended attribute type
21
    type: string
22
  marc_field_mode:
23
    description: marc field mode of the extended attribute type
24
    type: string
25
    enum:
26
      - get
27
      - set
28
  searchable:
29
    description: is the extended attribute type searchable
30
    type: boolean
31
32
additionalProperties: false
33
required:
34
  - extended_attribute_type_id
35
  - resource_type
(-)a/api/v1/swagger/paths/additional_fields.yaml (-8 / +12 lines)
Lines 1-18 Link Here
1
---
1
---
2
/additional_fields:
2
/extended_attribute_types:
3
  get:
3
  get:
4
    x-mojo-to: AdditionalFields#list
4
    x-mojo-to: ExtendedAttributeTypes#list
5
    operationId: listAdditionalFields
5
    operationId: listAdditionalFields
6
    tags:
6
    tags:
7
      - additional_fields
7
      - additional_fields
8
    summary: List additional fields
8
    summary: List extended attribute types
9
    produces:
9
    produces:
10
      - application/json
10
      - application/json
11
    parameters:
11
    parameters:
12
      - description: filter by table name
12
      - description: filter by resource type
13
        in: query
13
        in: query
14
        name: table_name
14
        name: resource_type
15
        type: string
15
        type: string
16
        enum:
17
          - basket
18
          - invoice
19
          - order
16
      - $ref: "../swagger.yaml#/parameters/match"
20
      - $ref: "../swagger.yaml#/parameters/match"
17
      - $ref: "../swagger.yaml#/parameters/order_by"
21
      - $ref: "../swagger.yaml#/parameters/order_by"
18
      - $ref: "../swagger.yaml#/parameters/page"
22
      - $ref: "../swagger.yaml#/parameters/page"
Lines 21-30 Link Here
21
      - $ref: "../swagger.yaml#/parameters/q_body"
25
      - $ref: "../swagger.yaml#/parameters/q_body"
22
    responses:
26
    responses:
23
      200:
27
      200:
24
        description: A list of additional_fields
28
        description: A list of extended attribute types
25
        schema:
29
        schema:
26
          items:
30
          items:
27
            $ref: "../swagger.yaml#/definitions/additional_field"
31
            $ref: "../swagger.yaml#/definitions/extended_attribute_type"
28
          type: array
32
          type: array
29
      400:
33
      400:
30
        description: Bad request
34
        description: Bad request
Lines 46-49 Link Here
46
          $ref: "../swagger.yaml#/definitions/error"
50
          $ref: "../swagger.yaml#/definitions/error"
47
    x-koha-authorization:
51
    x-koha-authorization:
48
      permissions:
52
      permissions:
49
        parameters: manage_additional_fields
53
        parameters: manage_additional_fields
(-)a/api/v1/swagger/swagger.yaml (-4 / +4 lines)
Lines 4-11 basePath: /api/v1 Link Here
4
definitions:
4
definitions:
5
  account_line:
5
  account_line:
6
    $ref: ./definitions/account_line.yaml
6
    $ref: ./definitions/account_line.yaml
7
  additional_field:
8
    $ref: ./definitions/additional_field.yaml
9
  advancededitormacro:
7
  advancededitormacro:
10
    $ref: ./definitions/advancededitormacro.yaml
8
    $ref: ./definitions/advancededitormacro.yaml
11
  allows_renewal:
9
  allows_renewal:
Lines 86-91 definitions: Link Here
86
    $ref: ./definitions/erm_usage_yus.yaml
84
    $ref: ./definitions/erm_usage_yus.yaml
87
  error:
85
  error:
88
    $ref: ./definitions/error.yaml
86
    $ref: ./definitions/error.yaml
87
  extended_attribute_type:
88
    $ref: ./definitions/extended_attribute_type.yaml
89
  fund:
89
  fund:
90
    $ref: ./definitions/fund.yaml
90
    $ref: ./definitions/fund.yaml
91
  hold:
91
  hold:
Lines 199-206 paths: Link Here
199
    $ref: "./paths/acquisitions_vendors.yaml#/~1acquisitions~1vendors~1{vendor_id}"
199
    $ref: "./paths/acquisitions_vendors.yaml#/~1acquisitions~1vendors~1{vendor_id}"
200
  "/acquisitions/vendors/{vendor_id}/issues":
200
  "/acquisitions/vendors/{vendor_id}/issues":
201
    $ref: "./paths/acquisitions_vendor_issues.yaml#/~1acquisitions~1vendors~1{vendor_id}~1issues"
201
    $ref: "./paths/acquisitions_vendor_issues.yaml#/~1acquisitions~1vendors~1{vendor_id}~1issues"
202
  /additional_fields:
203
    $ref: ./paths/additional_fields.yaml#/~1additional_fields
204
  /advanced_editor/macros:
202
  /advanced_editor/macros:
205
    $ref: ./paths/advancededitormacros.yaml#/~1advanced_editor~1macros
203
    $ref: ./paths/advancededitormacros.yaml#/~1advanced_editor~1macros
206
  /advanced_editor/macros/shared:
204
  /advanced_editor/macros/shared:
Lines 375-380 paths: Link Here
375
    $ref: ./paths/erm_usage_titles.yaml#/~1erm~1usage_titles
373
    $ref: ./paths/erm_usage_titles.yaml#/~1erm~1usage_titles
376
  /erm/users:
374
  /erm/users:
377
    $ref: ./paths/erm_users.yaml#/~1erm~1users
375
    $ref: ./paths/erm_users.yaml#/~1erm~1users
376
  /extended_attribute_types:
377
    $ref: ./paths/extended_attribute_types.yaml#/~1extended_attribute_types
378
  /holds:
378
  /holds:
379
    $ref: ./paths/holds.yaml#/~1holds
379
    $ref: ./paths/holds.yaml#/~1holds
380
  "/holds/{hold_id}":
380
  "/holds/{hold_id}":
(-)a/t/db_dependent/api/v1/additional_fields.t (-8 / +7 lines)
Lines 62-68 subtest 'list() tests' => sub { Link Here
62
62
63
    ## Authorized user tests
63
    ## Authorized user tests
64
    # No additional fields, so empty array should be returned
64
    # No additional fields, so empty array should be returned
65
    $t->get_ok("//$userid:$password@/api/v1/additional_fields")->status_is(200)->json_is( [] );
65
    $t->get_ok("//$userid:$password@/api/v1/extended_attribute_types")->status_is(200)->json_is( [] );
66
66
67
    my $additional_field = $builder->build_object(
67
    my $additional_field = $builder->build_object(
68
        {
68
        {
Lines 72-78 subtest 'list() tests' => sub { Link Here
72
    );
72
    );
73
73
74
    # One additional_field created, should get returned
74
    # One additional_field created, should get returned
75
    $t->get_ok("//$userid:$password@/api/v1/additional_fields")->status_is(200)
75
    $t->get_ok("//$userid:$password@/api/v1/extended_attribute_types")->status_is(200)
76
        ->json_is( [ $additional_field->to_api ] );
76
        ->json_is( [ $additional_field->to_api ] );
77
77
78
    my $another_additional_field = $builder->build_object(
78
    my $another_additional_field = $builder->build_object(
Lines 90-96 subtest 'list() tests' => sub { Link Here
90
    );
90
    );
91
91
92
    # Three additional fields created, they should both be returned
92
    # Three additional fields created, they should both be returned
93
    $t->get_ok("//$userid:$password@/api/v1/additional_fields")->status_is(200)->json_is(
93
    $t->get_ok("//$userid:$password@/api/v1/extended_attribute_types")->status_is(200)->json_is(
94
        [
94
        [
95
            $additional_field->to_api,
95
            $additional_field->to_api,
96
            $another_additional_field->to_api,
96
            $another_additional_field->to_api,
Lines 99-113 subtest 'list() tests' => sub { Link Here
99
    );
99
    );
100
100
101
    # Filtering works, two existing additional fields returned for the queried table name
101
    # Filtering works, two existing additional fields returned for the queried table name
102
    $t->get_ok( "//$userid:$password@/api/v1/additional_fields?table_name=" . $additional_field->tablename )
102
    $t->get_ok("//$userid:$password@/api/v1/extended_attribute_types?resource_type=invoice")->status_is(200)
103
        ->status_is(200)->json_is( [ $additional_field->to_api, $another_additional_field->to_api ] );
103
        ->json_is( [ $additional_field->to_api, $another_additional_field->to_api ] );
104
104
105
    # Warn on unsupported query parameter
105
    # Warn on unsupported query parameter
106
    $t->get_ok("//$userid:$password@/api/v1/additional_fields?blah=blah")->status_is(400)
106
    $t->get_ok("//$userid:$password@/api/v1/extended_attribute_types?blah=blah")->status_is(400)
107
        ->json_is( [ { path => '/query/blah', message => 'Malformed query string' } ] );
107
        ->json_is( [ { path => '/query/blah', message => 'Malformed query string' } ] );
108
108
109
    # Unauthorized access
109
    # Unauthorized access
110
    $t->get_ok("//$unauth_userid:$password@/api/v1/additional_fields")->status_is(403);
110
    $t->get_ok("//$unauth_userid:$password@/api/v1/extended_attribute_types")->status_is(403);
111
111
112
    $schema->storage->txn_rollback;
112
    $schema->storage->txn_rollback;
113
};
113
};
114
- 

Return to bug 35197