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

(-)a/Koha/BackgroundJob.pm (-1 / +25 lines)
Lines 469-474 sub plugin_types_to_classes { Link Here
469
    return $self->{_plugin_mapping};
469
    return $self->{_plugin_mapping};
470
}
470
}
471
471
472
=head3 to_api
473
474
    my $json = $job->to_api;
475
476
Overloaded method that returns a JSON representation of the Koha::BackgroundJob object,
477
suitable for API output.
478
479
=cut
480
481
sub to_api {
482
    my ( $self, $params ) = @_;
483
484
    my $json = $self->SUPER::to_api( $params );
485
486
    $json->{context} = $self->json->decode($self->context)
487
      if defined $self->context;
488
    $json->{data} = $self->decoded_data;
489
490
    return $json;
491
}
492
472
=head3 to_api_mapping
493
=head3 to_api_mapping
473
494
474
This method returns the mapping for representing a Koha::BackgroundJob object
495
This method returns the mapping for representing a Koha::BackgroundJob object
Lines 478-485 on the API. Link Here
478
499
479
sub to_api_mapping {
500
sub to_api_mapping {
480
    return {
501
    return {
481
        id             => 'background_job_id',
502
        id             => 'job_id',
482
        borrowernumber => 'patron_id',
503
        borrowernumber => 'patron_id',
504
        ended_on       => 'ended_date',
505
        enqueued_on    => 'enqueued_date',
506
        started_on     => 'started_date',
483
    };
507
    };
484
}
508
}
485
509
(-)a/Koha/REST/V1/BackgroundJobs.pm (-11 / +28 lines)
Lines 29-77 use Try::Tiny; Link Here
29
29
30
=head3 list
30
=head3 list
31
31
32
Controller function that handles listing Koha::BackgrounJob objects
33
32
=cut
34
=cut
33
35
34
sub list {
36
sub list {
35
    my $c = shift->openapi->valid_input or return;
37
    my $c = shift->openapi->valid_input or return;
36
38
37
    return try {
39
    return try {
38
        my $background_jobs_set = Koha::BackgroundJobs->new;
40
39
        my $background_jobs     = $c->objects->search($background_jobs_set);
41
        my $only_current = delete $c->validation->output->{only_current};
40
        return $c->render( status => 200, openapi => $background_jobs );
42
41
    }
43
        my $bj_rs = Koha::BackgroundJobs->new;
42
    catch {
44
45
        if ($only_current) {
46
            $bj_rs = $bj_rs->filter_by_current;
47
        }
48
49
        return $c->render(
50
            status  => 200,
51
            openapi => $c->objects->search($bj_rs)
52
        );
53
    } catch {
43
        $c->unhandled_exception($_);
54
        $c->unhandled_exception($_);
44
    };
55
    };
45
}
56
}
46
57
58
=head3 get
59
60
Controller function that handles retrieving a single Koha::BackgroundJob object
61
62
=cut
63
47
sub get {
64
sub get {
48
    my $c = shift->openapi->valid_input or return;
65
    my $c = shift->openapi->valid_input or return;
49
66
50
    return try {
67
    return try {
51
68
52
        my $background_job_id = $c->validation->param('background_job_id');
69
        my $job_id = $c->validation->param('job_id');
53
        my $patron            = $c->stash('koha.user');
70
        my $patron = $c->stash('koha.user');
54
71
55
        my $can_manage_background_jobs =
72
        my $can_manage_background_jobs =
56
          $patron->has_permission( { parameters => 'manage_background_jobs' } );
73
          $patron->has_permission( { parameters => 'manage_background_jobs' } );
57
74
58
        my $background_job = Koha::BackgroundJobs->find($background_job_id);
75
        my $job = Koha::BackgroundJobs->find($job_id);
59
76
60
        return $c->render(
77
        return $c->render(
61
            status  => 404,
78
            status  => 404,
62
            openapi => { error => "Object not found" }
79
            openapi => { error => "Object not found" }
63
        ) unless $background_job;
80
        ) unless $job;
64
81
65
        return $c->render(
82
        return $c->render(
66
            status  => 403,
83
            status  => 403,
67
            openapi => { error => "Cannot see background job info" }
84
            openapi => { error => "Cannot see background job info" }
68
          )
85
          )
69
          if !$can_manage_background_jobs
86
          if !$can_manage_background_jobs
70
          && $background_job->borrowernumber != $patron->borrowernumber;
87
          && $job->borrowernumber != $patron->borrowernumber;
71
88
72
        return $c->render(
89
        return $c->render(
73
            status  => 200,
90
            status  => 200,
74
            openapi => $background_job->to_api
91
            openapi => $job->to_api
75
        );
92
        );
76
    }
93
    }
77
    catch {
94
    catch {
(-)a/api/v1/swagger/definitions/background_job.yaml (-63 lines)
Lines 1-63 Link Here
1
---
2
type: object
3
properties:
4
  background_job_id:
5
    type: integer
6
    description: internally assigned background_job identifier
7
    readOnly: true
8
  status:
9
    description: background_job status
10
    type:
11
      - string
12
      - "null"
13
  progress:
14
    description: background_job progress
15
    type:
16
      - string
17
      - "null"
18
  size:
19
    description: background_job size
20
    type:
21
      - string
22
      - "null"
23
  patron_id:
24
    description: background_job enqueuer
25
    type:
26
      - string
27
      - "null"
28
  type:
29
    description: background_job type
30
    type:
31
      - string
32
      - "null"
33
  queue:
34
    description: background_job queue
35
    type:
36
      - string
37
      - "null"
38
  data:
39
    description: background_job data
40
    type:
41
      - string
42
      - "null"
43
  context:
44
    description: background_job context
45
    type:
46
      - string
47
      - "null"
48
  enqueued_on:
49
    description: background_job enqueue date
50
    type:
51
      - string
52
      - "null"
53
  started_on:
54
    description: background_job start date
55
    type:
56
      - string
57
      - "null"
58
  ended_on:
59
    description: background_job end date
60
    type:
61
      - string
62
      - "null"
63
additionalProperties: false
(-)a/api/v1/swagger/definitions/job.yaml (+54 lines)
Line 0 Link Here
1
---
2
type: object
3
properties:
4
  job_id:
5
    type: integer
6
    description: internally assigned job identifier
7
    readOnly: true
8
  status:
9
    description: job status
10
    type: string
11
  progress:
12
    description: job progress
13
    type:
14
      - string
15
      - "null"
16
  size:
17
    description: job size
18
    type:
19
      - string
20
      - "null"
21
  patron_id:
22
    description: job enqueuer
23
    type:
24
      - string
25
      - "null"
26
  type:
27
    description: job type
28
    type: string
29
  queue:
30
    description: job queue
31
    type: string
32
  data:
33
    description: job data
34
    type: object
35
  context:
36
    description: job context
37
    type: object
38
  enqueued_date:
39
    description: job enqueue date
40
    type: string
41
    format: date-time
42
  started_date:
43
    description: job start date
44
    type:
45
      - string
46
      - "null"
47
    format: date-time
48
  ended_date:
49
    description: job end date
50
    type:
51
      - string
52
      - "null"
53
    format: date-time
54
additionalProperties: false
(-)a/api/v1/swagger/paths/background_jobs.yaml (-61 / +16 lines)
Lines 1-64 Link Here
1
---
1
---
2
/background_jobs:
2
/jobs:
3
  get:
3
  get:
4
    x-mojo-to: BackgroundJobs#list
4
    x-mojo-to: BackgroundJobs#list
5
    operationId: listBackgroundJobs
5
    operationId: listJobs
6
    tags:
6
    tags:
7
      - background_jobs
7
      - jobs
8
    summary: List background jobs
8
    summary: List jobs
9
    produces:
9
    produces:
10
      - application/json
10
      - application/json
11
    parameters:
11
    parameters:
12
      - name: status
12
      - name: only_current
13
        in: query
13
        in: query
14
        description: Case insensative search on job status
15
        required: false
14
        required: false
16
        type: string
15
        type: boolean
17
      - name: progress
16
        description: If should filter out not current jobs
18
        in: query
19
        description: Case insensative search on job progress
20
        required: false
21
        type: string
22
      - name: size
23
        in: query
24
        description: Case insensative search on job size
25
        required: false
26
        type: string
27
      - name: patron_id
28
        in: query
29
        description: Case insensative search on job enqueuer id
30
        required: false
31
        type: string
32
      - name: type
33
        in: query
34
        description: Case insensative search on job type
35
        required: false
36
        type: string
37
      - name: queue
38
        in: query
39
        description: Case insensative search on job queue
40
        required: false
41
        type: string
42
      - name: context
43
        in: query
44
        description: Case insensative search on context
45
        required: false
46
        type: string
47
      - name: enqueued_on
48
        in: query
49
        description: Case insensative search on job enqueue date
50
        required: false
51
        type: string
52
      - name: started_on
53
        in: query
54
        description: Case insensative search on job start date
55
        required: false
56
        type: string
57
      - name: ended_on
58
        in: query
59
        description: Case insensative search on job end date
60
        required: false
61
        type: string
62
      - $ref: "../swagger.yaml#/parameters/match"
17
      - $ref: "../swagger.yaml#/parameters/match"
63
      - $ref: "../swagger.yaml#/parameters/order_by"
18
      - $ref: "../swagger.yaml#/parameters/order_by"
64
      - $ref: "../swagger.yaml#/parameters/page"
19
      - $ref: "../swagger.yaml#/parameters/page"
Lines 73-79 Link Here
73
        schema:
28
        schema:
74
          type: array
29
          type: array
75
          items:
30
          items:
76
            $ref: "../swagger.yaml#/definitions/background_job"
31
            $ref: "../swagger.yaml#/definitions/job"
77
      "403":
32
      "403":
78
        description: Access forbidden
33
        description: Access forbidden
79
        schema:
34
        schema:
Lines 92-119 Link Here
92
    x-koha-authorization:
47
    x-koha-authorization:
93
      permissions:
48
      permissions:
94
        catalogue: "1"
49
        catalogue: "1"
95
"/background_jobs/{background_job_id}":
50
"/jobs/{job_id}":
96
  get:
51
  get:
97
    x-mojo-to: BackgroundJobs#get
52
    x-mojo-to: BackgroundJobs#get
98
    operationId: getBackgroundJob
53
    operationId: getJob
99
    tags:
54
    tags:
100
      - background_job
55
      - jobs
101
    summary: Get background job
56
    summary: Get a job
102
    parameters:
57
    parameters:
103
      - $ref: "../swagger.yaml#/parameters/background_job_id_pp"
58
      - $ref: "../swagger.yaml#/parameters/job_id_pp"
104
    produces:
59
    produces:
105
      - application/json
60
      - application/json
106
    responses:
61
    responses:
107
      "200":
62
      "200":
108
        description: A background job
63
        description: A job
109
        schema:
64
        schema:
110
          $ref: "../swagger.yaml#/definitions/background_job"
65
          $ref: "../swagger.yaml#/definitions/job"
111
      "403":
66
      "403":
112
        description: Access forbidden
67
        description: Access forbidden
113
        schema:
68
        schema:
114
          $ref: "../swagger.yaml#/definitions/error"
69
          $ref: "../swagger.yaml#/definitions/error"
115
      "404":
70
      "404":
116
        description: Background job not found
71
        description: Job not found
117
        schema:
72
        schema:
118
          $ref: "../swagger.yaml#/definitions/error"
73
          $ref: "../swagger.yaml#/definitions/error"
119
      "500":
74
      "500":
(-)a/api/v1/swagger/swagger.yaml (-12 / +15 lines)
Lines 8-15 definitions: Link Here
8
    $ref: ./definitions/advancededitormacro.yaml
8
    $ref: ./definitions/advancededitormacro.yaml
9
  allows_renewal:
9
  allows_renewal:
10
    $ref: ./definitions/allows_renewal.yaml
10
    $ref: ./definitions/allows_renewal.yaml
11
  background_job:
12
    $ref: ./definitions/background_job.yaml
13
  basket:
11
  basket:
14
    $ref: ./definitions/basket.yaml
12
    $ref: ./definitions/basket.yaml
15
  bundle_link:
13
  bundle_link:
Lines 48-53 definitions: Link Here
48
    $ref: ./definitions/item.yaml
46
    $ref: ./definitions/item.yaml
49
  item_group:
47
  item_group:
50
    $ref: ./definitions/item_group.yaml
48
    $ref: ./definitions/item_group.yaml
49
  job:
50
    $ref: ./definitions/job.yaml
51
  library:
51
  library:
52
    $ref: ./definitions/library.yaml
52
    $ref: ./definitions/library.yaml
53
  order:
53
  order:
Lines 105-114 paths: Link Here
105
    $ref: "./paths/article_requests.yaml#/~1article_requests~1{article_request_id}"
105
    $ref: "./paths/article_requests.yaml#/~1article_requests~1{article_request_id}"
106
  /auth/otp/token_delivery:
106
  /auth/otp/token_delivery:
107
    $ref: paths/auth.yaml#/~1auth~1otp~1token_delivery
107
    $ref: paths/auth.yaml#/~1auth~1otp~1token_delivery
108
  /background_jobs:
109
    $ref: ./paths/background_jobs.yaml#/~1background_jobs
110
  "/background_jobs/{background_job_id}":
111
    $ref: "./paths/background_jobs.yaml#/~1background_jobs~1{background_job_id}"
112
  "/biblios/{biblio_id}":
108
  "/biblios/{biblio_id}":
113
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}"
109
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}"
114
  "/biblios/{biblio_id}/checkouts":
110
  "/biblios/{biblio_id}/checkouts":
Lines 185-190 paths: Link Here
185
    $ref: ./paths/items.yaml#/~1items~1{item_id}~1bundled_items~1{bundled_item_id}
181
    $ref: ./paths/items.yaml#/~1items~1{item_id}~1bundled_items~1{bundled_item_id}
186
  "/items/{item_id}/pickup_locations":
182
  "/items/{item_id}/pickup_locations":
187
    $ref: "./paths/items.yaml#/~1items~1{item_id}~1pickup_locations"
183
    $ref: "./paths/items.yaml#/~1items~1{item_id}~1pickup_locations"
184
  /jobs:
185
    $ref: ./paths/jobs.yaml#/~1jobs
186
  "/jobs/{job_id}":
187
    $ref: "./paths/jobs.yaml#/~1jobs~1{job_id}"
188
  /libraries:
188
  /libraries:
189
    $ref: ./paths/libraries.yaml#/~1libraries
189
    $ref: ./paths/libraries.yaml#/~1libraries
190
  "/libraries/{library_id}":
190
  "/libraries/{library_id}":
Lines 260-271 parameters: Link Here
260
    name: advancededitormacro_id
260
    name: advancededitormacro_id
261
    required: true
261
    required: true
262
    type: integer
262
    type: integer
263
  background_job_id_pp:
264
    description: Job internal identifier
265
    in: path
266
    name: background_job_id
267
    required: true
268
    type: integer
269
  biblio_id_pp:
263
  biblio_id_pp:
270
    description: Record internal identifier
264
    description: Record internal identifier
271
    in: path
265
    in: path
Lines 338-343 parameters: Link Here
338
    name: item_id
332
    name: item_id
339
    required: true
333
    required: true
340
    type: integer
334
    type: integer
335
  job_id_pp:
336
    description: Job internal identifier
337
    in: path
338
    name: job_id
339
    required: true
340
    type: integer
341
  library_id_pp:
341
  library_id_pp:
342
    description: Internal library identifier
342
    description: Internal library identifier
343
    in: path
343
    in: path
Lines 604-609 tags: Link Here
604
  - description: "Manage items\n"
604
  - description: "Manage items\n"
605
    name: items
605
    name: items
606
    x-displayName: Items
606
    x-displayName: Items
607
  - description: "Manage jobs\n"
608
    name: jobs
609
    x-displayName: Jobs
607
  - description: "Manage libraries\n"
610
  - description: "Manage libraries\n"
608
    name: libraries
611
    name: libraries
609
    x-displayName: Libraries
612
    x-displayName: Libraries
(-)a/t/db_dependent/api/v1/background_jobs.t (-21 / +21 lines)
Lines 65-75 my $patron = $builder->build_object( Link Here
65
$patron->set_password( { password => $password, skip_validation => 1 } );
65
$patron->set_password( { password => $password, skip_validation => 1 } );
66
my $patron_userid = $patron->userid;
66
my $patron_userid = $patron->userid;
67
67
68
$t->get_ok("//$librarian_userid:$password@/api/v1/background_jobs")
68
$t->get_ok("//$librarian_userid:$password@/api/v1/jobs")
69
  ->status_is(200)
69
  ->status_is(200)
70
  ->json_is( [] );
70
  ->json_is( [] );
71
71
72
my $background_job = $builder->build_object(
72
my $job = $builder->build_object(
73
    {
73
    {
74
        class => 'Koha::BackgroundJobs',
74
        class => 'Koha::BackgroundJobs',
75
        value => {
75
        value => {
Lines 86-124 my $background_job = $builder->build_object( Link Here
86
);
86
);
87
87
88
{
88
{
89
    $t->get_ok("//$superlibrarian_userid:$password@/api/v1/background_jobs")
89
    $t->get_ok("//$superlibrarian_userid:$password@/api/v1/jobs")
90
      ->status_is(200)->json_is( [ $background_job->to_api ] );
90
      ->status_is(200)->json_is( [ $job->to_api ] );
91
91
92
    $t->get_ok("//$librarian_userid:$password@/api/v1/background_jobs")
92
    $t->get_ok("//$librarian_userid:$password@/api/v1/jobs")
93
      ->status_is(200)->json_is( [] );
93
      ->status_is(200)->json_is( [] );
94
94
95
    $t->get_ok("//$patron_userid:$password@/api/v1/background_jobs")
95
    $t->get_ok("//$patron_userid:$password@/api/v1/jobs")
96
      ->status_is(403);
96
      ->status_is(403);
97
97
98
    $background_job->borrowernumber( $librarian->borrowernumber )->store;
98
    $job->borrowernumber( $librarian->borrowernumber )->store;
99
99
100
    $t->get_ok("//$librarian_userid:$password@/api/v1/background_jobs")
100
    $t->get_ok("//$librarian_userid:$password@/api/v1/jobs")
101
      ->status_is(200)->json_is( [ $background_job->to_api ] );
101
      ->status_is(200)->json_is( [ $job->to_api ] );
102
}
102
}
103
103
104
{
104
{
105
    $t->get_ok( "//$superlibrarian_userid:$password@/api/v1/background_jobs/"
105
    $t->get_ok( "//$superlibrarian_userid:$password@/api/v1/jobs/"
106
          . $background_job->id )->status_is(200)
106
          . $job->id )->status_is(200)
107
      ->json_is( $background_job->to_api );
107
      ->json_is( $job->to_api );
108
108
109
    $t->get_ok( "//$librarian_userid:$password@/api/v1/background_jobs/"
109
    $t->get_ok( "//$librarian_userid:$password@/api/v1/jobs/"
110
          . $background_job->id )->status_is(200)
110
          . $job->id )->status_is(200)
111
      ->json_is( $background_job->to_api );
111
      ->json_is( $job->to_api );
112
112
113
    $background_job->borrowernumber( $superlibrarian->borrowernumber )->store;
113
    $job->borrowernumber( $superlibrarian->borrowernumber )->store;
114
    $t->get_ok( "//$librarian_userid:$password@/api/v1/background_jobs/"
114
    $t->get_ok( "//$librarian_userid:$password@/api/v1/jobs/"
115
          . $background_job->id )->status_is(403);
115
          . $job->id )->status_is(403);
116
}
116
}
117
117
118
{
118
{
119
    $background_job->delete;
119
    $job->delete;
120
    $t->get_ok( "//$superlibrarian_userid:$password@/api/v1/background_jobs/"
120
    $t->get_ok( "//$superlibrarian_userid:$password@/api/v1/jobs/"
121
          . $background_job->id )->status_is(404)
121
          . $job->id )->status_is(404)
122
      ->json_is( '/error' => 'Object not found' );
122
      ->json_is( '/error' => 'Object not found' );
123
}
123
}
124
124
(-)a/t/lib/TestBuilder.pm (-1 / +3 lines)
Lines 558-563 sub _gen_blob { Link Here
558
sub _gen_default_values {
558
sub _gen_default_values {
559
    my ($self) = @_;
559
    my ($self) = @_;
560
    return {
560
    return {
561
        BackgroundJob => {
562
            context => '{}'
563
        },
561
        Borrower => {
564
        Borrower => {
562
            login_attempts => 0,
565
            login_attempts => 0,
563
            gonenoaddress  => undef,
566
            gonenoaddress  => undef,
564
- 

Return to bug 30982