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

(-)a/Koha/REST/V1/Auth.pm (-4 / +4 lines)
Lines 254-260 sub check_object_ownership { Link Here
254
254
255
    my $parameters = {
255
    my $parameters = {
256
        accountlines_id => \&_object_ownership_by_accountlines_id,
256
        accountlines_id => \&_object_ownership_by_accountlines_id,
257
        borrowernumber  => \&_object_ownership_by_borrowernumber,
257
        patron_id       => \&_object_ownership_by_patron_id,
258
        checkout_id     => \&_object_ownership_by_checkout_id,
258
        checkout_id     => \&_object_ownership_by_checkout_id,
259
        reserve_id      => \&_object_ownership_by_reserve_id,
259
        reserve_id      => \&_object_ownership_by_reserve_id,
260
    };
260
    };
Lines 296-305 Compares C<$borrowernumber> to currently logged in C<$user>. Link Here
296
296
297
=cut
297
=cut
298
298
299
sub _object_ownership_by_borrowernumber {
299
sub _object_ownership_by_patron_id {
300
    my ($c, $user, $borrowernumber) = @_;
300
    my ($c, $user, $patron_id) = @_;
301
301
302
    return $user->borrowernumber == $borrowernumber;
302
    return $user->borrowernumber == $patron_id;
303
}
303
}
304
304
305
=head3 _object_ownership_by_checkout_id
305
=head3 _object_ownership_by_checkout_id
(-)a/Koha/REST/V1/Patrons.pm (-19 / +57 lines)
Lines 44-50 sub list { Link Here
44
44
45
    return try {
45
    return try {
46
        my $patrons_set = Koha::Patrons->new;
46
        my $patrons_set = Koha::Patrons->new;
47
        my @patrons = $c->objects->search( $patrons_set )->as_list;
47
        my @patrons = $c->objects->search( $patrons_set, \&_to_model )->as_list;
48
        @patrons = map { _to_api($_->TO_JSON) } @patrons;
48
        return $c->render( status => 200, openapi => \@patrons );
49
        return $c->render( status => 200, openapi => \@patrons );
49
    }
50
    }
50
    catch {
51
    catch {
Lines 70-83 Controller function that handles retrieving a single Koha::Patron object Link Here
70
sub get {
71
sub get {
71
    my $c = shift->openapi->valid_input or return;
72
    my $c = shift->openapi->valid_input or return;
72
73
73
    my $borrowernumber = $c->validation->param('borrowernumber');
74
    my $patron_id = $c->validation->param('patron_id');
74
    my $patron = Koha::Patrons->find($borrowernumber);
75
    my $patron    = Koha::Patrons->find($patron_id);
75
76
76
    unless ($patron) {
77
    unless ($patron) {
77
        return $c->render(status => 404, openapi => { error => "Patron not found." });
78
        return $c->render( status => 404, openapi => { error => "Patron not found." } );
78
    }
79
    }
79
80
80
    return $c->render(status => 200, openapi => $patron);
81
    return $c->render( status => 200, openapi => _to_api($patron->TO_JSON) );
81
}
82
}
82
83
83
=head3 add
84
=head3 add
Lines 92-102 sub add { Link Here
92
    return try {
93
    return try {
93
        my $body = $c->validation->param('body');
94
        my $body = $c->validation->param('body');
94
95
95
        Koha::Patron->new($body)->_validate;
96
        Koha::Patron->new(_to_model($body))->_validate;
96
97
97
        # TODO: Use AddMember until it has been moved to Koha-namespace
98
        # TODO: Use AddMember until it has been moved to Koha-namespace
98
        my $borrowernumber = AddMember(%$body);
99
        my $patron_id = AddMember( %{ _to_model($body) } );
99
        my $patron         = Koha::Patrons->find($borrowernumber);
100
        my $patron    = _to_api(Koha::Patrons->find( $patron_id )->TO_JSON);
100
101
101
        return $c->render( status => 201, openapi => $patron );
102
        return $c->render( status => 201, openapi => $patron );
102
    }
103
    }
Lines 118-130 sub add { Link Here
118
        elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
119
        elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
119
            return $c->render(
120
            return $c->render(
120
                status  => 400,
121
                status  => 400,
121
                openapi => { error => "Given branchcode does not exist" }
122
                openapi => { error => "Given library_id does not exist" }
122
            );
123
            );
123
        }
124
        }
124
        elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
125
        elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
125
            return $c->render(
126
            return $c->render(
126
                status  => 400,
127
                status  => 400,
127
                openapi => { error => "Given categorycode does not exist" }
128
                openapi => { error => "Given category_id does not exist" }
128
            );
129
            );
129
        }
130
        }
130
        else {
131
        else {
Lines 147-153 Controller function that handles updating a Koha::Patron object Link Here
147
sub update {
148
sub update {
148
    my $c = shift->openapi->valid_input or return;
149
    my $c = shift->openapi->valid_input or return;
149
150
150
    my $patron = Koha::Patrons->find( $c->validation->param('borrowernumber') );
151
    my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
151
152
152
    return try {
153
    return try {
153
        my $body = $c->validation->param('body');
154
        my $body = $c->validation->param('body');
Lines 155-161 sub update { Link Here
155
        $patron->set( _to_model($body) )->_validate;
156
        $patron->set( _to_model($body) )->_validate;
156
157
157
        # TODO: Use ModMember until it has been moved to Koha-namespace
158
        # TODO: Use ModMember until it has been moved to Koha-namespace
158
        if ( ModMember(%$body) ) {
159
        if ( ModMember( %{ _to_model($body) } ) ) {
159
            return $c->render( status => 200, openapi => $patron );
160
            return $c->render( status => 200, openapi => $patron );
160
        }
161
        }
161
        else {
162
        else {
Lines 191-203 sub update { Link Here
191
        elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
192
        elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
192
            return $c->render(
193
            return $c->render(
193
                status  => 400,
194
                status  => 400,
194
                openapi => { error => "Given branchcode does not exist" }
195
                openapi => { error => "Given library_id does not exist" }
195
            );
196
            );
196
        }
197
        }
197
        elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
198
        elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
198
            return $c->render(
199
            return $c->render(
199
                status  => 400,
200
                status  => 400,
200
                openapi => { error => "Given categorycode does not exist" }
201
                openapi => { error => "Given category_id does not exist" }
201
            );
202
            );
202
        }
203
        }
203
        elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
204
        elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
Lines 248-254 sub delete { Link Here
248
    my $patron;
249
    my $patron;
249
250
250
    return try {
251
    return try {
251
        $patron = Koha::Patrons->find( $c->validation->param('borrowernumber') );
252
        $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
252
253
253
        # check if loans, reservations, debarrment, etc. before deletion!
254
        # check if loans, reservations, debarrment, etc. before deletion!
254
        my $res = $patron->delete;
255
        my $res = $patron->delete;
Lines 273-278 sub delete { Link Here
273
    };
274
    };
274
}
275
}
275
276
277
=head3 _to_api
278
279
Helper function that maps Koha::Patron objects into REST api
280
attribute names.
281
282
=cut
283
284
sub _to_api {
285
    my $patron = shift;
286
287
    my $mapping = {
288
        borrowernumber => 'patron_id',
289
        branchcode     => 'library_id',
290
        categorycode   => 'category_id'
291
    };
292
293
    foreach my $key ( keys %{ $mapping } ) {
294
        if ( exists $patron->{ $key } ) {
295
            $patron->{ $mapping->{$key} } = delete $patron->{ $key };
296
        }
297
    }
298
299
    return $patron;
300
}
301
276
=head3 _to_model
302
=head3 _to_model
277
303
278
Helper function that maps REST api objects into Koha::Patron
304
Helper function that maps REST api objects into Koha::Patron
Lines 281-292 attribute names. Link Here
281
=cut
307
=cut
282
308
283
sub _to_model {
309
sub _to_model {
284
    my $params = shift;
310
    my $patron = shift;
311
312
    my $mapping = {
313
        category_id => 'categorycode',
314
        library_id  => 'branchcode',
315
        patron_id   => 'borrowernumber'
316
    };
317
318
    foreach my $key ( keys %{ $mapping } ) {
319
        if ( exists $patron->{ $key } ) {
320
            $patron->{ $mapping->{$key} } = delete $patron->{ $key };
321
        }
322
    }
285
323
286
    $params->{lost} = ($params->{lost}) ? 1 : 0;
324
    $patron->{lost} = ($patron->{lost}) ? 1 : 0;
287
    $params->{gonenoaddress} = ($params->{gonenoaddress}) ? 1 : 0;
325
    $patron->{gonenoaddress} = ($patron->{gonenoaddress}) ? 1 : 0;
288
326
289
    return $params;
327
    return $patron;
290
}
328
}
291
329
292
1;
330
1;
(-)a/api/v1/swagger/definitions/patron.json (-7 / +7 lines)
Lines 1-8 Link Here
1
{
1
{
2
  "type": "object",
2
  "type": "object",
3
  "properties": {
3
  "properties": {
4
    "borrowernumber": {
4
    "patron_id": {
5
      "$ref": "../x-primitives.json#/borrowernumber"
5
      "$ref": "../x-primitives.json#/patron_id"
6
    },
6
    },
7
    "cardnumber": {
7
    "cardnumber": {
8
      "$ref": "../x-primitives.json#/cardnumber"
8
      "$ref": "../x-primitives.json#/cardnumber"
Lines 124-136 Link Here
124
      "format": "date",
124
      "format": "date",
125
      "description": "patron's date of birth"
125
      "description": "patron's date of birth"
126
    },
126
    },
127
    "branchcode": {
127
    "library_id": {
128
      "type": "string",
128
      "type": "string",
129
      "description": "code of patron's home branch"
129
      "description": "Internal identifier for the patron's home library"
130
    },
130
    },
131
    "categorycode": {
131
    "category_id": {
132
      "type": "string",
132
      "type": "string",
133
      "description": "code of patron's category"
133
      "description": "Internal identifier for the patron's category"
134
    },
134
    },
135
    "dateenrolled": {
135
    "dateenrolled": {
136
      "type": ["string", "null"],
136
      "type": ["string", "null"],
Lines 299-303 Link Here
299
    }
299
    }
300
  },
300
  },
301
  "additionalProperties": false,
301
  "additionalProperties": false,
302
  "required": ["surname", "address", "city", "branchcode", "categorycode"]
302
  "required": ["surname", "address", "city", "library_id", "category_id"]
303
}
303
}
(-)a/api/v1/swagger/parameters.json (-4 / +4 lines)
Lines 1-9 Link Here
1
{
1
{
2
  "borrowernumberPathParam": {
2
  "patron_id_pp": {
3
    "$ref": "parameters/patron.json#/borrowernumberPathParam"
3
    "$ref": "parameters/patron.json#/patron_id_pp"
4
  },
4
  },
5
  "borrowernumberQueryParam": {
5
  "patron_id_qp": {
6
    "$ref": "parameters/patron.json#/borrowernumberQueryParam"
6
    "$ref": "parameters/patron.json#/patron_id_qp"
7
  },
7
  },
8
  "cityidPathParam": {
8
  "cityidPathParam": {
9
    "$ref": "parameters/city.json#/cityidPathParam"
9
    "$ref": "parameters/city.json#/cityidPathParam"
(-)a/api/v1/swagger/parameters/patron.json (-5 / +5 lines)
Lines 1-15 Link Here
1
{
1
{
2
  "borrowernumberPathParam": {
2
  "patron_id_pp": {
3
    "name": "borrowernumber",
3
    "name": "patron_id",
4
    "in": "path",
4
    "in": "path",
5
    "description": "Internal patron identifier",
5
    "description": "Internal patron identifier",
6
    "required": true,
6
    "required": true,
7
    "type": "integer"
7
    "type": "integer"
8
  },
8
  },
9
  "borrowernumberQueryParam": {
9
  "patron_id_qp": {
10
    "name": "borrowernumber",
10
    "name": "patron_id",
11
    "in": "query",
11
    "in": "query",
12
    "description": "Internal borrower identifier",
12
    "description": "Internal patron identifier",
13
    "type": "integer"
13
    "type": "integer"
14
  }
14
  }
15
}
15
}
(-)a/api/v1/swagger/paths.json (-2 / +2 lines)
Lines 20-27 Link Here
20
  "/patrons": {
20
  "/patrons": {
21
    "$ref": "paths/patrons.json#/~1patrons"
21
    "$ref": "paths/patrons.json#/~1patrons"
22
  },
22
  },
23
  "/patrons/{borrowernumber}": {
23
  "/patrons/{patron_id}": {
24
    "$ref": "paths/patrons.json#/~1patrons~1{borrowernumber}"
24
    "$ref": "paths/patrons.json#/~1patrons~1{patron_id}"
25
  },
25
  },
26
  "/illrequests": {
26
  "/illrequests": {
27
    "$ref": "paths/illrequests.json#/~1illrequests"
27
    "$ref": "paths/illrequests.json#/~1illrequests"
(-)a/api/v1/swagger/paths/patrons.json (-10 / +10 lines)
Lines 8-16 Link Here
8
          "application/json"
8
          "application/json"
9
      ],
9
      ],
10
      "parameters": [{
10
      "parameters": [{
11
        "name": "borrowernumber",
11
        "name": "patron_id",
12
        "in": "query",
12
        "in": "query",
13
        "description": "Case insensitive search on borrowernumber",
13
        "description": "Search on patron_id",
14
        "required": false,
14
        "required": false,
15
        "type": "string"
15
        "type": "string"
16
      }, {
16
      }, {
Lines 200-214 Link Here
200
        "required": false,
200
        "required": false,
201
        "type": "string"
201
        "type": "string"
202
      }, {
202
      }, {
203
        "name": "branchcode",
203
        "name": "library_id",
204
        "in": "query",
204
        "in": "query",
205
        "description": "Case insensitive search on branchcode",
205
        "description": "Case insensitive search on library_id",
206
        "required": false,
206
        "required": false,
207
        "type": "string"
207
        "type": "string"
208
      }, {
208
      }, {
209
        "name": "categorycode",
209
        "name": "category_id",
210
        "in": "query",
210
        "in": "query",
211
        "description": "Case insensitive search on categorycode",
211
        "description": "Case insensitive search on category_id",
212
        "required": false,
212
        "required": false,
213
        "type": "string"
213
        "type": "string"
214
      }, {
214
      }, {
Lines 563-575 Link Here
563
      }
563
      }
564
    }
564
    }
565
  },
565
  },
566
  "/patrons/{borrowernumber}": {
566
  "/patrons/{patron_id}": {
567
    "get": {
567
    "get": {
568
      "x-mojo-to": "Patrons#get",
568
      "x-mojo-to": "Patrons#get",
569
      "operationId": "getPatron",
569
      "operationId": "getPatron",
570
      "tags": ["patrons"],
570
      "tags": ["patrons"],
571
      "parameters": [{
571
      "parameters": [{
572
        "$ref": "../parameters.json#/borrowernumberPathParam"
572
          "$ref": "../parameters.json#/patron_id_pp"
573
      }],
573
      }],
574
      "produces": [
574
      "produces": [
575
        "application/json"
575
        "application/json"
Lines 626-632 Link Here
626
      "tags": ["patrons"],
626
      "tags": ["patrons"],
627
      "parameters": [
627
      "parameters": [
628
        {
628
        {
629
          "$ref": "../parameters.json#/borrowernumberPathParam"
629
          "$ref": "../parameters.json#/patron_id_pp"
630
        },
630
        },
631
        {
631
        {
632
          "name": "body",
632
          "name": "body",
Lines 703-709 Link Here
703
      "operationId": "deletePatron",
703
      "operationId": "deletePatron",
704
      "tags": ["patrons"],
704
      "tags": ["patrons"],
705
      "parameters": [{
705
      "parameters": [{
706
        "$ref": "../parameters.json#/borrowernumberPathParam"
706
        "$ref": "../parameters.json#/patron_id_pp"
707
      }],
707
      }],
708
      "produces": ["application/json"],
708
      "produces": ["application/json"],
709
      "responses": {
709
      "responses": {
(-)a/api/v1/swagger/x-primitives.json (-3 / +2 lines)
Lines 3-11 Link Here
3
    "type": "integer",
3
    "type": "integer",
4
    "description": "internally assigned biblio identifier"
4
    "description": "internally assigned biblio identifier"
5
  },
5
  },
6
  "borrowernumber": {
6
  "patron_id": {
7
    "type": "integer",
7
    "type": "integer",
8
    "description": "internally assigned user identifier"
8
    "description": "Internal patron identifier"
9
  },
9
  },
10
  "cardnumber": {
10
  "cardnumber": {
11
    "type": ["string", "null"],
11
    "type": ["string", "null"],
12
- 

Return to bug 19784