Bug 22785: Allow option to choose which record match is applied during import
[koha.git] / Koha / REST / V1 / ImportRecordMatches.pm
1 package Koha::REST::V1::ImportRecordMatches;
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::Import::Record::Matches;
23
24 use Try::Tiny;
25
26 =head1 API
27
28 =head2 Methods
29
30 =cut
31
32 =head3 unset_chosen
33
34 Method that handles unselecting all chosen matches for an import record
35
36 DELETE /api/v1/import/{import_batch_id}/records/{import_record_id}/matches/chosen
37
38 =cut
39
40 sub unset_chosen {
41     my $c = shift->openapi->valid_input or return;
42
43     my $import_record_id = $c->validation->param('import_record_id');
44     my $matches = Koha::Import::Record::Matches->search({
45         import_record_id => $import_record_id,
46     });
47     unless ($matches) {
48         return $c->render(
49             status  => 404,
50             openapi => { error => "No matches not found" }
51         );
52     }
53     return try {
54         $matches->update({ chosen => 0 });
55         return $c->render( status => 204, openapi => $matches );
56     }
57     catch {
58         $c->unhandled_exception($_);
59     };
60 }
61
62 =head3 set_chosen
63
64 Method that handles modifying if a Koha::Import::Record::Match object has been chosen for overlay
65
66 PUT /api/v1/import/{import_batch_id}/records/{import_record_id}/matches/chosen
67
68 Body should contain the condidate_match_id to chose
69
70 =cut
71
72 sub set_chosen {
73     my $c = shift->openapi->valid_input or return;
74
75     my $import_record_id = $c->validation->param('import_record_id');
76     my $body    = $c->validation->param('body');
77     my $candidate_match_id = $body->{'candidate_match_id'};
78
79     my $match = Koha::Import::Record::Matches->find({
80         import_record_id => $import_record_id,
81         candidate_match_id => $candidate_match_id
82     });
83
84     unless ($match) {
85         return $c->render(
86             status  => 404,
87             openapi => { error => "Match not found" }
88         );
89     }
90
91     return try {
92         my $matches = Koha::Import::Record::Matches->search({
93             import_record_id => $import_record_id,
94             chosen => 1
95         });
96         $matches->update({ chosen => 0}) if $matches;
97         $match->set_from_api({ chosen => JSON::true });
98         $match->store;
99         return $c->render( status => 200, openapi => $match );
100     }
101     catch {
102         $c->unhandled_exception($_);
103     };
104 }
105
106 1;