Permalink
Browse files

Made basic messaging preferences API functional for GET

  • Loading branch information...
jirislav committed Apr 4, 2017
1 parent fc91954 commit 7bddf4f709fce467296ddce814b4612aeaa00088
View
@@ -584,6 +584,35 @@ sub holds {
return Koha::Holds->_new_from_dbic($holds_rs);
}
=head3 messaging_preferences
my $messaging = $category->messaging_preferences();
=cut
sub messaging_preferences {
my ( $self ) = @_;
my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
PREF: foreach my $option ( @$messaging_options ) {
my $pref = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $self->borrowernumber, message_name => $option->{'message_name'} } );
$option->{ $option->{'message_name'} } = 1;
# make a hashref of the days, selecting one.
if ( $option->{'takes_days'} ) {
my $days_in_advance = $pref->{'days_in_advance'} ? $pref->{'days_in_advance'} : 0;
$option->{days_in_advance} = $days_in_advance;
@{$option->{'select_days'}} = map { {
day => $_,
selected => $_ == $days_in_advance }
} ( 0..30 );
}
foreach my $transport ( keys %{$pref->{'transports'}} ) {
$option->{'transports_'.$transport} = 1;
}
$option->{'digest'} = 1 if $pref->{'wants_digest'};
}
return $messaging_options;
}
=head3 type
=cut
@@ -0,0 +1,121 @@
package Koha::REST::V1::MessagingPreferences;
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use Modern::Perl;
use Mojo::Base 'Mojolicious::Controller';
use JSON qw/to_json/;
use Koha::Patrons;
use C4::Context;
use C4::Auth qw( haspermission );
sub set {
#
# WORK IN PROGRESS
#
my ($c, $args, $cb) = @_;
my $user = $c->stash('koha.user');
if ( ! C4::Context->preference('EnhancedMessagingPreferences') ) {
return $c->$cb({error => "Enhanced messaging preferences are not enabled"}, 403);
}
if ( !
( C4::Context->preference('EnhancedMessagingPreferencesOPAC') ||
C4::Auth::haspermission($user->userid, 'borrowers') )
) {
return $c->$cb({error => "Patrons does not have access to enhanced messaging preferences"}, 403);
}
my $body = $c->req->json;
my $borrowernumber = $body->{borrowernumber};
my $biblionumber = $body->{biblionumber};
my $itemnumber = $body->{itemnumber};
my $branchcode = $body->{branchcode};
my $expirationdate = $body->{expirationdate};
my $borrower = Koha::Patrons->find($borrowernumber);
my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
my $prefs_set = 0;
OPTION: foreach my $option ( @$messaging_options ) {
my $updater = { borrowernumber => $user->borrowernumber,
message_attribute_id => $option->{'message_attribute_id'} };
# find the desired transports
@{$updater->{'message_transport_types'}} = $query->multi_param( $option->{'message_attribute_id'} );
next OPTION unless $updater->{'message_transport_types'};
if ( $option->{'has_digest'} ) {
if ( List::Util::first { $_ == $option->{'message_attribute_id'} } $query->multi_param( 'digest' ) ) {
$updater->{'wants_digest'} = 1;
}
}
if ( $option->{'takes_days'} ) {
# Here inserts number of days in advance ..
if ( defined $query->param( $option->{'message_attribute_id'} . '-DAYS' ) ) {
$updater->{'days_in_advance'} = $query->param( $option->{'message_attribute_id'} . '-DAYS' );
}
}
C4::Members::Messaging::SetMessagingPreference( $updater );
if ($query->param( $option->{'message_attribute_id'})){
$prefs_set = 1;
}
}
return $c->$cb($patrons, 200);
}
sub get {
my ($c, $args, $cb) = @_;
my $user = $c->stash('koha.user');
if ( ! C4::Context->preference('EnhancedMessagingPreferences') ) {
return $c->$cb({error => "Enhanced messaging preferences are not enabled"}, 403);
}
if ( !
( C4::Context->preference('EnhancedMessagingPreferencesOPAC') ||
C4::Auth::haspermission($user->userid, 'borrowers') )
) {
return $c->$cb({error => "Patrons does not have access to enhanced messaging preferences"}, 403);
}
my $patron = Koha::Patrons->find($args->{borrowernumber});
unless ($patron) {
return $c->$cb({error => "Patron not found"}, 404);
}
#return $c->$cb({error => to_json($patron->messaging_preferences)}, 403);
return $c->$cb($patron->messaging_preferences, 200);
}
1
@@ -11,6 +11,9 @@
"hold": {
"$ref": "definitions/hold.json"
},
"messaging_preferences": {
"$ref": "definitions/messaging_preferences.json"
},
"error": {
"$ref": "definitions/error.json"
}
@@ -0,0 +1,94 @@
{
"type": "array",
"items": {
"type": "object",
"properties": {
"message_name": {
"description": "Name qualifier of the message",
"type": "string"
},
"transport_email": {
"description": "Email to use for transport",
"type": "string"
},
"transport_sms": {
"description": "Sms to use for transport",
"type": "string"
},
"message_attribute_id": {
"description": "Id of the message attribute",
"type": "integer"
},
"takes_days": {
"description": "Number of days it takes",
"type": "string"
},
"days_in_advance": {
"description": "Number of days in advance to notify",
"type": ["integer", "string", "null"]
},
"select_days": {
"description": "Number of days it takes",
"type": ["array", "null"],
"items": {
"type": "object",
"properties": {
"selected": {
"description": "Is this number selected",
"type": ["integer", "string"]
},
"day": {
"description": "Day number",
"type": "integer"
}
}
}
},
"has_digest": {
"description": "Can be digest turned on",
"type": ["integer", "null"]
},
"digest": {
"description": "Is digest turned on",
"type": ["integer", "null"]
},
"transports_email": {
"description": "Is email transport turned on",
"type": ["integer", "null"]
},
"transports_sms": {
"description": "Is sms transport turned on",
"type": ["integer", "null"]
},
"transports_phone": {
"description": "Is phone transport turned on",
"type": ["integer", "null"]
},
"Hold_Filled": {
"description": "Notification qualifier for hold filled",
"type": ["integer", "null"]
},
"Item_Check_in": {
"description": "Notification qualifier for item check in",
"type": ["integer", "null"]
},
"Item_Checkout": {
"description": "Notification qualifier for item checkout",
"type": ["integer", "null"]
},
"Hold_Filled": {
"description": "Notification qualifier for hold filled",
"type": ["integer", "null"]
},
"Advance_Notice": {
"description": "Notification qualifier for advance notice",
"type": ["integer", "null"]
},
"Hold_Filled": {
"description": "Notification qualifier for hold filled",
"type": ["integer", "null"]
}
}
},
"additionalProperties": false
}
@@ -0,0 +1,32 @@
{
"type": "object",
"properties": {
"borrowernumber": {
"type": "string",
"description": "internally assigned user identifier"
},
"email": {
"type": ["string", "null"],
"description": "primary email address for patron's primary address"
},
"surname": {
"type": "string",
"description": "patron's last name"
},
"firstname": {
"type": ["string", "null"],
"description": "patron's first name"
},
"sessionid": {
"description": "Koha Session identifier",
"type": "string"
},
"permissions": {
"description": "patron's permissions",
"type": "array",
"items": {
"type": "string"
}
}
}
}
@@ -0,0 +1,10 @@
{
"type": "object",
"properties": {
"sessionid": {
"description": "The Koha sessionid",
"type": "string"
}
},
"required": ["sessionid"]
}
@@ -16,5 +16,8 @@
},
"/patrons/{borrowernumber}": {
"$ref": "paths/patrons.json#/~1patrons~1{borrowernumber}"
},
"/messaging_preferences/{borrowernumber}": {
"$ref": "paths/messaging_preferences.json#/~1messaging_preferences~1{borrowernumber}"
}
}
@@ -0,0 +1,83 @@
{
"/messaging_preferences/{borrowernumber}": {
"get": {
"operationId": "getMessagingPreferences",
"tags": ["messaging_preferences"],
"parameters": [{
"$ref": "../parameters.json#/borrowernumberPathParam"
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "A messaging preferences of a patron",
"schema": {
"$ref": "../definitions.json#/messaging_preferences"
}
},
"403": {
"description": "Access forbidden",
"schema": {
"$ref": "../definitions.json#/error"
}
},
"404": {
"description": "Patron not found",
"schema": {
"$ref": "../definitions.json#/error"
}
}
},
"x-koha-authorization": {
"allow-owner": true,
"allow-guarantor": true,
"permissions": {
"borrowers": "1"
}
}
},
"post": {
"operationId": "setMessagingPreferences",
"tags": ["messaging_preferences"],
"parameters": [{
"name": "body",
"in": "body",
"description": "A JSON object containing informations about the patron messaging preferences",
"required": true,
"schema": {
"$ref": "../definitions.json#/messaging_preferences"
}
}],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "City added",
"schema": {
"$ref": "../definitions.json#/messaging_preferences"
}
},
"403": {
"description": "Access forbidden",
"schema": {
"$ref": "../definitions.json#/error"
}
},
"500": {
"description": "Internal error",
"schema": {
"$ref": "../definitions.json#/error"
}
}
},
"x-koha-authorization": {
"permissions": {
"parameters": "parameters_remaining_permissions"
}
}
}
}
}

0 comments on commit 7bddf4f

Please sign in to comment.