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

(-)a/Koha/REST/V1/Patrons/AccountLinks.pm (+141 lines)
Line 0 Link Here
1
package Koha::REST::V1::Patrons::AccountLinks;
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 <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Patrons;
23
24
use Scalar::Util qw( blessed );
25
use Try::Tiny    qw( catch try );
26
27
=head1 NAME
28
29
Koha::REST::V1::Patrons::AccountLinks
30
31
=head1 API
32
33
=head2 Methods
34
35
=head3 list
36
37
Controller function that handles listing Koha::Patron::AccountLink objects for the requested patron
38
39
=cut
40
41
sub list {
42
    my $c = shift->openapi->valid_input or return;
43
44
    my $patron = Koha::Patrons->find( $c->param('patron_id') );
45
46
    return $c->render_resource_not_found("Patron")
47
        unless $patron;
48
49
    return try {
50
        my $links_set = $patron->linked_account_links;
51
52
        return $c->render( status => 200, openapi => [] )
53
            unless $links_set;
54
55
        my $links = $c->objects->search($links_set);
56
        return $c->render( status => 200, openapi => $links );
57
    } catch {
58
        $c->unhandled_exception($_);
59
    };
60
}
61
62
=head3 add
63
64
Controller function that handles linking a patron to another account
65
66
=cut
67
68
sub add {
69
    my $c = shift->openapi->valid_input or return;
70
71
    my $patron = Koha::Patrons->find( $c->param('patron_id') );
72
73
    return $c->render_resource_not_found("Patron")
74
        unless $patron;
75
76
    my $body             = $c->req->json;
77
    my $linked_patron_id = $body->{linked_patron_id};
78
    my $linked_patron    = Koha::Patrons->find($linked_patron_id);
79
80
    return $c->render_resource_not_found("Linked patron")
81
        unless $linked_patron;
82
83
    return try {
84
        my $link = $patron->link_to_patron($linked_patron);
85
86
        $c->res->headers->location( $c->req->url->to_string . '/' . $link->id );
87
        return $c->render(
88
            status  => 201,
89
            openapi => $c->objects->to_api($link),
90
        );
91
    } catch {
92
        if ( blessed $_ ) {
93
            if ( $_->isa('Koha::Exceptions::PatronAccountLink::AlreadyLinked') ) {
94
                return $c->render(
95
                    status  => 409,
96
                    openapi => { error => $_->description, error_code => 'AlreadyLinked' }
97
                );
98
            } elsif ( $_->isa('Koha::Exceptions::PatronAccountLink::DifferentGroups') ) {
99
                return $c->render(
100
                    status  => 409,
101
                    openapi => { error => $_->description, error_code => 'DifferentGroups' }
102
                );
103
            }
104
        }
105
        $c->unhandled_exception($_);
106
    };
107
}
108
109
=head3 delete
110
111
Controller method that handles removing a patron account link
112
113
=cut
114
115
sub delete {
116
    my $c = shift->openapi->valid_input or return;
117
118
    my $patron = Koha::Patrons->find( $c->param('patron_id') );
119
120
    return $c->render_resource_not_found("Patron")
121
        unless $patron;
122
123
    return try {
124
        my $links_set = $patron->linked_account_links;
125
126
        return $c->render_resource_not_found("Account link")
127
            unless $links_set;
128
129
        my $link = $links_set->find( $c->param('account_link_id') );
130
131
        return $c->render_resource_not_found("Account link")
132
            unless $link;
133
134
        $link->delete;
135
        return $c->render_resource_deleted;
136
    } catch {
137
        $c->unhandled_exception($_);
138
    };
139
}
140
141
1;
(-)a/api/v1/swagger/definitions/patron_account_link.yaml (+19 lines)
Line 0 Link Here
1
---
2
type: object
3
properties:
4
  account_link_id:
5
    type: integer
6
    description: Internal link identifier
7
    readOnly: true
8
  link_group_id:
9
    type: integer
10
    description: Group identifier linking accounts together
11
  patron_id:
12
    type: integer
13
    description: Patron this link belongs to
14
  created_on:
15
    type: string
16
    format: date-time
17
    description: When the link was created
18
    readOnly: true
19
additionalProperties: false
(-)a/api/v1/swagger/paths/patrons_account_links.yaml (+162 lines)
Line 0 Link Here
1
---
2
"/patrons/{patron_id}/account_links":
3
  get:
4
    x-mojo-to: Patrons::AccountLinks#list
5
    operationId: listPatronAccountLinks
6
    tags:
7
      - patrons
8
    summary: List linked accounts for a patron
9
    produces:
10
      - application/json
11
    parameters:
12
      - $ref: "../swagger.yaml#/parameters/patron_id_pp"
13
      - $ref: "../swagger.yaml#/parameters/match"
14
      - $ref: "../swagger.yaml#/parameters/order_by"
15
      - $ref: "../swagger.yaml#/parameters/page"
16
      - $ref: "../swagger.yaml#/parameters/per_page"
17
      - $ref: "../swagger.yaml#/parameters/q_param"
18
      - $ref: "../swagger.yaml#/parameters/q_body"
19
      - $ref: "../swagger.yaml#/parameters/request_id_header"
20
    responses:
21
      "200":
22
        description: A list of linked patron accounts
23
        schema:
24
          type: array
25
          items:
26
            $ref: "../swagger.yaml#/definitions/patron_account_link"
27
      "400":
28
        description: |
29
          Bad request. Possible `error_code` attribute values:
30
31
            * `invalid_query`
32
        schema:
33
          $ref: "../swagger.yaml#/definitions/error"
34
      "401":
35
        description: Authentication required
36
        schema:
37
          $ref: "../swagger.yaml#/definitions/error"
38
      "403":
39
        description: Access forbidden
40
        schema:
41
          $ref: "../swagger.yaml#/definitions/error"
42
      "404":
43
        description: Patron not found
44
        schema:
45
          $ref: "../swagger.yaml#/definitions/error"
46
      "500":
47
        description: |
48
          Internal server error. Possible `error_code` attribute values:
49
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
        borrowers: edit_borrowers
60
  post:
61
    x-mojo-to: Patrons::AccountLinks#add
62
    operationId: addPatronAccountLink
63
    tags:
64
      - patrons
65
    summary: Link patron to another account
66
    parameters:
67
      - $ref: "../swagger.yaml#/parameters/patron_id_pp"
68
      - name: body
69
        in: body
70
        description: A JSON object containing the patron_id to link to
71
        required: true
72
        schema:
73
          type: object
74
          properties:
75
            linked_patron_id:
76
              type: integer
77
              description: The patron_id to link this patron to
78
          required:
79
            - linked_patron_id
80
    produces:
81
      - application/json
82
    responses:
83
      "201":
84
        description: Link created
85
        schema:
86
          $ref: "../swagger.yaml#/definitions/patron_account_link"
87
      "400":
88
        description: Bad request
89
        schema:
90
          $ref: "../swagger.yaml#/definitions/error"
91
      "401":
92
        description: Authentication required
93
        schema:
94
          $ref: "../swagger.yaml#/definitions/error"
95
      "403":
96
        description: Access forbidden
97
        schema:
98
          $ref: "../swagger.yaml#/definitions/error"
99
      "404":
100
        description: Patron not found
101
        schema:
102
          $ref: "../swagger.yaml#/definitions/error"
103
      "409":
104
        description: Conflict - patron already linked
105
        schema:
106
          $ref: "../swagger.yaml#/definitions/error"
107
      "500":
108
        description: |
109
          Internal server error. Possible `error_code` attribute values:
110
111
          * `internal_server_error`
112
        schema:
113
          $ref: "../swagger.yaml#/definitions/error"
114
      "503":
115
        description: Under maintenance
116
        schema:
117
          $ref: "../swagger.yaml#/definitions/error"
118
    x-koha-authorization:
119
      permissions:
120
        borrowers: edit_borrowers
121
"/patrons/{patron_id}/account_links/{account_link_id}":
122
  delete:
123
    x-mojo-to: Patrons::AccountLinks#delete
124
    operationId: deletePatronAccountLink
125
    tags:
126
      - patrons
127
    summary: Remove a patron account link
128
    parameters:
129
      - $ref: "../swagger.yaml#/parameters/patron_id_pp"
130
      - name: account_link_id
131
        in: path
132
        description: Internal account link identifier
133
        required: true
134
        type: integer
135
    produces:
136
      - application/json
137
    responses:
138
      "204":
139
        description: Link deleted
140
      "401":
141
        description: Authentication required
142
        schema:
143
          $ref: "../swagger.yaml#/definitions/error"
144
      "403":
145
        description: Access forbidden
146
        schema:
147
          $ref: "../swagger.yaml#/definitions/error"
148
      "404":
149
        description: Link not found
150
        schema:
151
          $ref: "../swagger.yaml#/definitions/error"
152
      "500":
153
        description: Internal error
154
        schema:
155
          $ref: "../swagger.yaml#/definitions/error"
156
      "503":
157
        description: Under maintenance
158
        schema:
159
          $ref: "../swagger.yaml#/definitions/error"
160
    x-koha-authorization:
161
      permissions:
162
        borrowers: edit_borrowers
(-)a/api/v1/swagger/swagger.yaml (-1 / +6 lines)
Lines 150-155 definitions: Link Here
150
    $ref: ./definitions/patron.yaml
150
    $ref: ./definitions/patron.yaml
151
  patron_account_credit:
151
  patron_account_credit:
152
    $ref: ./definitions/patron_account_credit.yaml
152
    $ref: ./definitions/patron_account_credit.yaml
153
  patron_account_link:
154
    $ref: ./definitions/patron_account_link.yaml
153
  patron_balance:
155
  patron_balance:
154
    $ref: ./definitions/patron_balance.yaml
156
    $ref: ./definitions/patron_balance.yaml
155
  patron_category:
157
  patron_category:
Lines 503-508 paths: Link Here
503
      $ref: "./paths/patrons_account.yaml#/~1patrons~1{patron_id}~1account~1debits~1{debit_id}"
505
      $ref: "./paths/patrons_account.yaml#/~1patrons~1{patron_id}~1account~1debits~1{debit_id}"
504
  "/patrons/{patron_id}/account/debits":
506
  "/patrons/{patron_id}/account/debits":
505
    $ref: "./paths/patrons_account.yaml#/~1patrons~1{patron_id}~1account~1debits"
507
    $ref: "./paths/patrons_account.yaml#/~1patrons~1{patron_id}~1account~1debits"
508
  "/patrons/{patron_id}/account_links":
509
    $ref: "./paths/patrons_account_links.yaml#/~1patrons~1{patron_id}~1account_links"
510
  "/patrons/{patron_id}/account_links/{account_link_id}":
511
    $ref: "./paths/patrons_account_links.yaml#/~1patrons~1{patron_id}~1account_links~1{account_link_id}"
506
  "/patrons/{patron_id}/extended_attributes":
512
  "/patrons/{patron_id}/extended_attributes":
507
    $ref: "./paths/patrons_extended_attributes.yaml#/~1patrons~1{patron_id}~1extended_attributes"
513
    $ref: "./paths/patrons_extended_attributes.yaml#/~1patrons~1{patron_id}~1extended_attributes"
508
  "/patrons/{patron_id}/extended_attributes/{extended_attribute_id}":
514
  "/patrons/{patron_id}/extended_attributes/{extended_attribute_id}":
509
- 

Return to bug 39658