partial renaming of person_id parameter

This commit is contained in:
Davide Alberani 2016-07-02 13:46:39 +02:00
parent fee4458be1
commit 9642bfa3b7
3 changed files with 73 additions and 70 deletions

View file

@ -99,8 +99,8 @@ eventManControllers.controller('EventsListCtrl', ['$scope', 'Event', '$uibModal'
); );
eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', 'Person', 'EventUpdates', '$stateParams', 'Setting', '$log', '$translate', '$rootScope', 'easyFormSteWayConfig', '$uibModal', eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', 'Person', 'EventUpdates', '$stateParams', 'Setting', '$log', '$translate', '$rootScope', '$uibModal',
function ($scope, $state, Event, Person, EventUpdates, $stateParams, Setting, $log, $translate, $rootScope, easyFormSteWayConfig, $uibModal) { function ($scope, $state, Event, Person, EventUpdates, $stateParams, Setting, $log, $translate, $rootScope, $uibModal) {
$scope.personsOrder = ["name", "surname"]; $scope.personsOrder = ["name", "surname"];
$scope.countAttendees = 0; $scope.countAttendees = 0;
$scope.message = {}; $scope.message = {};
@ -146,12 +146,13 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
$scope.event.persons = []; $scope.event.persons = [];
} }
var person_idx = $scope.event.persons.findIndex(function(el, idx, array) { var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
return data.person_id == el.person_id; return data._id == el._id;
}); });
$log.debug(data);
if (person_idx != -1) { if (person_idx != -1) {
$log.debug('person_id ' + data.person_id + ' found'); $log.debug('_id ' + data._id + ' found');
} else { } else {
$log.debug('person_id ' + data.person_id + ' not found'); $log.debug('_id ' + data._id + ' not found');
} }
if (data.action == 'update' && person_idx != -1 && $scope.event.persons[person_idx] != data.person) { if (data.action == 'update' && person_idx != -1 && $scope.event.persons[person_idx] != data.person) {
@ -159,7 +160,7 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
} else if (data.action == 'add' && person_idx == -1) { } else if (data.action == 'add' && person_idx == -1) {
$scope._localAddAttendee(data.person, true); $scope._localAddAttendee(data.person, true);
} else if (data.action == 'delete' && person_idx != -1) { } else if (data.action == 'delete' && person_idx != -1) {
$scope._localRemoveAttendee({person_id: data.person_id}); $scope._localRemoveAttendee({_id: data._id});
} }
} }
); );
@ -214,12 +215,12 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
}; };
/* Stuff to do when an attendee is added locally. */ /* Stuff to do when an attendee is added locally. */
$scope._localAddAttendee = function(person, hideMessage) { $scope._localAddAttendee = function(person) {
if (!$scope.event.persons) { if (!$scope.event.persons) {
$scope.event.persons = []; $scope.event.persons = [];
} }
var person_idx = $scope.event.persons.findIndex(function(el, idx, array) { var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
return person.person_id == el.person_id; return person._id == el._id;
}); });
if (person_idx != -1) { if (person_idx != -1) {
$log.debug('person already present: not added'); $log.debug('person already present: not added');
@ -228,8 +229,25 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
$scope.event.persons.push(person); $scope.event.persons.push(person);
}; };
$rootScope.$on('event.ticket.new', function(evt, ticket) {
$scope._localAddAttendee(ticket);
});
$rootScope.$on('event.ticket.update', function(evt, ticket) {
if (!$scope.event.persons) {
$scope.event.persons = [];
}
var ticket_idx = $scope.event.persons.findIndex(function(el, idx, array) {
return ticket._id == el._id;
});
if (ticket_idx == -1) {
$log.debug('person not present: not updated');
return false;
}
$scope.event.persons[ticket_idx] = ticket;
});
$scope._addAttendee = function(person) { $scope._addAttendee = function(person) {
person.person_id = person._id;
person._id = $stateParams.id; // that's the id of the event, not the person. person._id = $stateParams.id; // that's the id of the event, not the person.
Event.addPerson(person, function() { Event.addPerson(person, function() {
$scope._localAddAttendee(person); $scope._localAddAttendee(person);
@ -241,7 +259,7 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
$scope._setAttended = function(person) { $scope._setAttended = function(person) {
$scope.setPersonAttribute(person, 'attended', true, function() { $scope.setPersonAttribute(person, 'attended', true, function() {
var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) { var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
return person.person_id == el.person_id; return person._id == el._id;
}); });
if (all_person_idx != -1) { if (all_person_idx != -1) {
$scope.allPersons.splice(all_person_idx, 1); $scope.allPersons.splice(all_person_idx, 1);
@ -249,20 +267,11 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
}, true); }, true);
}; };
$scope.fastAddAttendee = function(person, isNew) { $scope.fastAddAttendee = function(person) {
$log.debug('EventDetailsCtrl.fastAddAttendee.person:'); $log.debug('EventDetailsCtrl.fastAddAttendee.person:');
$log.debug(person); $log.debug(person);
if (isNew) {
var personObj = new Person(person);
personObj.$save(function(p) {
person = $scope._addAttendee(angular.copy(p));
$scope._setAttended(person);
$scope.newPerson = {};
});
} else {
person = $scope._addAttendee(angular.copy(person)); person = $scope._addAttendee(angular.copy(person));
$scope._setAttended(person); $scope._setAttended(person);
}
}; };
$scope.openQuickAddTicket = function(_id) { $scope.openQuickAddTicket = function(_id) {
@ -271,7 +280,6 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
controller: 'EventTicketsCtrl' controller: 'EventTicketsCtrl'
}); });
modalInstance.result.then(function(x) { modalInstance.result.then(function(x) {
$scope.event = Event.get($stateParams);
}); });
}; };
@ -281,9 +289,9 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
$scope.setPersonAttribute = function(person, key, value, callback, hideMessage) { $scope.setPersonAttribute = function(person, key, value, callback, hideMessage) {
$log.debug('EventDetailsCtrl.setPersonAttribute.event_id: ' + $stateParams.id); $log.debug('EventDetailsCtrl.setPersonAttribute.event_id: ' + $stateParams.id);
$log.debug('EventDetailsCtrl.setPersonAttribute.person_id: ' + person.person_id); $log.debug('EventDetailsCtrl.setPersonAttribute._id: ' + person._id);
$log.debug('EventDetailsCtrl.setPersonAttribute.key: ' + key + ' value: ' + value); $log.debug('EventDetailsCtrl.setPersonAttribute.key: ' + key + ' value: ' + value);
var data = {_id: $stateParams.id, person_id: person.person_id}; var data = {_id: $stateParams.id, ticket_id: person._id};
data[key] = value; data[key] = value;
Event.updatePerson(data, Event.updatePerson(data,
function(data) { function(data) {
@ -291,10 +299,10 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
return; return;
} }
var person_idx = $scope.event.persons.findIndex(function(el, idx, array) { var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
return data.person_id == el.person_id; return data._id == el._id;
}); });
if (person_idx == -1) { if (person_idx == -1) {
$log.warn('unable to find person_id ' + person_id); $log.warn('unable to find _id ' + _id);
return; return;
} }
if ($scope.event.persons[person_idx] != data.person) { if ($scope.event.persons[person_idx] != data.person) {
@ -325,14 +333,14 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
$scope._localRemoveAttendee = function(person) { $scope._localRemoveAttendee = function(person) {
$log.debug('_localRemoveAttendee'); $log.debug('_localRemoveAttendee');
$log.debug(person); $log.debug(person);
if (!(person && person.person_id && $scope.event.persons)) { if (!(person && person._id && $scope.event.persons)) {
return; return;
} }
var person_idx = $scope.event.persons.findIndex(function(el, idx, array) { var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
return person.person_id == el.person_id; return person._id == el._id;
}); });
if (person_idx == -1) { if (person_idx == -1) {
$log.warn('unable to find and delete person_id ' + person.person_id); $log.warn('unable to find and delete _id ' + person._id);
return; return;
} }
var removed_person = $scope.event.persons.splice(person_idx, 1); var removed_person = $scope.event.persons.splice(person_idx, 1);
@ -343,7 +351,7 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) { var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
return person.person_id == el._id; return person.person_id == el._id;
}); });
if (all_person_idx == -1 && person.person_id) { if (all_person_idx == -1 && person._id) {
$scope.allPersons.push(person); $scope.allPersons.push(person);
} }
}; };
@ -351,7 +359,7 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
$scope.removeAttendee = function(person) { $scope.removeAttendee = function(person) {
Event.deletePerson({ Event.deletePerson({
_id: $stateParams.id, _id: $stateParams.id,
person_id: person.person_id ticket_id: person._id
}, function() { }, function() {
$scope._localRemoveAttendee(person); $scope._localRemoveAttendee(person);
}); });
@ -429,26 +437,22 @@ eventManControllers.controller('EventTicketsCtrl', ['$scope', '$state', 'Event',
}; };
$scope.addTicket = function(person) { $scope.addTicket = function(person) {
var personObj = new Person(person); person.event_id = $state.params.id;
personObj.$save(function(p) {
person.person_id = p._id;
person._id = $state.params.id; // that's the id of the event, not the person.
EventTicket.add(person, function(ticket) { EventTicket.add(person, function(ticket) {
$log.debug(ticket); $log.debug(ticket);
$rootScope.$emit('event.ticket.new', ticket);
if (!$state.is('event.tickets')) { if (!$state.is('event.tickets')) {
$state.go('event.ticket.edit', {id: $scope.event._id, ticket_id: ticket._id}); $state.go('event.ticket.edit', {id: $scope.event._id, ticket_id: ticket._id});
} else if ($scope.$close) { } else if ($scope.$close) {
$scope.$close(); $scope.$close();
} }
}); });
});
}; };
$scope.updateTicket = function(ticket, cb) { $scope.updateTicket = function(ticket, cb) {
var data = angular.copy(ticket); ticket.event_id = $state.params.id;
data.ticket_id = data._id; EventTicket.update(ticket, function(t) {
data._id = $state.params.id; $rootScope.$emit('event.ticket.update', ticket);
EventTicket.update(data, function(t) {
if (cb) { if (cb) {
cb(t); cb(t);
} }

View file

@ -18,7 +18,7 @@ function convert_dates(obj) {
eventManServices.factory('Event', ['$resource', '$rootScope', eventManServices.factory('Event', ['$resource', '$rootScope',
function($resource, $rootScope) { function($resource, $rootScope) {
return $resource('events/:id', {id: '@_id', person_id: '@person_id'}, { return $resource('events/:id', {id: '@_id', ticket_id: '@_id'}, {
all: { all: {
method: 'GET', method: 'GET',
@ -64,7 +64,7 @@ eventManServices.factory('Event', ['$resource', '$rootScope',
method: 'PUT', method: 'PUT',
interceptor : {responseError: $rootScope.errorHandler}, interceptor : {responseError: $rootScope.errorHandler},
isArray: false, isArray: false,
url: 'events/:id/persons/:person_id', url: 'events/:id/tickets/:ticket_id',
params: {uuid: $rootScope.app_uuid}, params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) { transformResponse: function(data, headers) {
return angular.fromJson(data); return angular.fromJson(data);
@ -75,7 +75,7 @@ eventManServices.factory('Event', ['$resource', '$rootScope',
method: 'POST', method: 'POST',
interceptor : {responseError: $rootScope.errorHandler}, interceptor : {responseError: $rootScope.errorHandler},
isArray: false, isArray: false,
url: 'events/:id/persons/:person_id', url: 'events/:id/tickets',
params: {uuid: $rootScope.app_uuid}, params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) { transformResponse: function(data, headers) {
return angular.fromJson(data); return angular.fromJson(data);
@ -86,7 +86,7 @@ eventManServices.factory('Event', ['$resource', '$rootScope',
method: 'DELETE', method: 'DELETE',
interceptor : {responseError: $rootScope.errorHandler}, interceptor : {responseError: $rootScope.errorHandler},
isArray: false, isArray: false,
url: 'events/:_id/persons/:person_id', url: 'events/:_id/persons/:ticket_id',
params: {uuid: $rootScope.app_uuid}, params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) { transformResponse: function(data, headers) {
return angular.fromJson(data); return angular.fromJson(data);
@ -99,7 +99,7 @@ eventManServices.factory('Event', ['$resource', '$rootScope',
eventManServices.factory('EventTicket', ['$resource', '$rootScope', eventManServices.factory('EventTicket', ['$resource', '$rootScope',
function($resource, $rootScope) { function($resource, $rootScope) {
return $resource('events/:id/tickets', {id: '@_id', ticket_id: '@ticket_id'}, { return $resource('events/:id/tickets', {event_id: '@event_id', ticket_id: '@_id'}, {
get: { get: {
method: 'GET', method: 'GET',
@ -115,7 +115,7 @@ eventManServices.factory('EventTicket', ['$resource', '$rootScope',
method: 'POST', method: 'POST',
interceptor : {responseError: $rootScope.errorHandler}, interceptor : {responseError: $rootScope.errorHandler},
isArray: false, isArray: false,
url: 'events/:id/tickets/:ticket_id', url: 'events/:event_id/tickets',
params: {uuid: $rootScope.app_uuid}, params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) { transformResponse: function(data, headers) {
data = angular.fromJson(data); data = angular.fromJson(data);
@ -123,11 +123,11 @@ eventManServices.factory('EventTicket', ['$resource', '$rootScope',
} }
}, },
update: { updateTicket: {
method: 'PUT', method: 'PUT',
interceptor : {responseError: $rootScope.errorHandler}, interceptor : {responseError: $rootScope.errorHandler},
isArray: false, isArray: false,
url: 'events/:id/tickets/:ticket_id', url: 'events/:event_id/tickets/:ticket_id',
params: {uuid: $rootScope.app_uuid}, params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) { transformResponse: function(data, headers) {
return angular.fromJson(data); return angular.fromJson(data);
@ -138,7 +138,7 @@ eventManServices.factory('EventTicket', ['$resource', '$rootScope',
method: 'DELETE', method: 'DELETE',
interceptor : {responseError: $rootScope.errorHandler}, interceptor : {responseError: $rootScope.errorHandler},
isArray: false, isArray: false,
url: 'events/:_id/tickets/:ticket_id', url: 'events/:event_id/tickets/:ticket_id',
params: {uuid: $rootScope.app_uuid}, params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) { transformResponse: function(data, headers) {
return angular.fromJson(data); return angular.fromJson(data);
@ -158,6 +158,8 @@ eventManServices.factory('Person', ['$resource', '$rootScope',
interceptor : {responseError: $rootScope.errorHandler}, interceptor : {responseError: $rootScope.errorHandler},
isArray: true, isArray: true,
transformResponse: function(data, headers) { transformResponse: function(data, headers) {
// TODO: REMOVE!
return [];
data = angular.fromJson(data); data = angular.fromJson(data);
if (data.error) { if (data.error) {
return data; return data;

View file

@ -592,7 +592,7 @@ class EventsHandler(CollectionHandler):
if all(person.get(k) == v for k, v in person_id_or_query.iteritems()): if all(person.get(k) == v for k, v in person_id_or_query.iteritems()):
return person return person
else: else:
if str(person.get('person_id')) == person_id_or_query: if str(person.get('_id')) == person_id_or_query:
return person return person
return {} return {}
@ -622,8 +622,8 @@ class EventsHandler(CollectionHandler):
if person_id is None: if person_id is None:
doc = {} doc = {}
else: else:
doc = self.db.query('events', {'_id': id_, 'persons.person_id': person_id}) doc = self.db.query('events', {'_id': id_, 'persons._id': person_id})
ret = {'action': 'add', 'person_id': person_id, 'person': data, 'uuid': uuid} ret = {'action': 'add', '_id': person_id, 'person': data, 'uuid': uuid}
if '_id' in data: if '_id' in data:
del data['_id'] del data['_id']
self.send_ws_message('event/%s/tickets/updates' % id_, json.dumps(ret)) self.send_ws_message('event/%s/tickets/updates' % id_, json.dumps(ret))
@ -644,12 +644,9 @@ class EventsHandler(CollectionHandler):
uuid, arguments = self.uuid_arguments uuid, arguments = self.uuid_arguments
query = dict([('persons.%s' % k, v) for k, v in arguments.iteritems()]) query = dict([('persons.%s' % k, v) for k, v in arguments.iteritems()])
query['_id'] = id_ query['_id'] = id_
if ticket: if person_id is not None:
query['persons._id'] = person_id query['persons._id'] = person_id
person_query = {'_id': person_id} person_query = {'_id': person_id}
elif person_id is not None:
query['persons.person_id'] = person_id
person_query = person_id
else: else:
person_query = self.arguments person_query = self.arguments
old_person_data = {} old_person_data = {}
@ -667,8 +664,8 @@ class EventsHandler(CollectionHandler):
env = self._dict2env(new_person_data) env = self._dict2env(new_person_data)
# always takes the person_id from the new person (it may have # always takes the person_id from the new person (it may have
# been a ticket_id). # been a ticket_id).
person_id = str(new_person_data.get('person_id')) ticket_id = str(new_person_data.get('_id'))
env.update({'PERSON_ID': person_id, 'EVENT_ID': id_, env.update({'PERSON_ID': ticket_id, 'TICKED_ID': ticket_id, 'EVENT_ID': id_,
'EVENT_TITLE': doc.get('title', ''), 'WEB_USER': self.current_user, 'EVENT_TITLE': doc.get('title', ''), 'WEB_USER': self.current_user,
'WEB_REMOTE_IP': self.request.remote_ip}) 'WEB_REMOTE_IP': self.request.remote_ip})
stdin_data = {'old': old_person_data, stdin_data = {'old': old_person_data,
@ -681,7 +678,7 @@ class EventsHandler(CollectionHandler):
if new_person_data.get('attended'): if new_person_data.get('attended'):
self.run_triggers('attends', stdin_data=stdin_data, env=env) self.run_triggers('attends', stdin_data=stdin_data, env=env)
ret = {'action': 'update', 'person_id': person_id, 'person': new_person_data, 'uuid': uuid} ret = {'action': 'update', '_id': ticket_id, 'person': new_person_data, 'uuid': uuid}
if old_person_data != new_person_data: if old_person_data != new_person_data:
self.send_ws_message('event/%s/tickets/updates' % id_, json.dumps(ret)) self.send_ws_message('event/%s/tickets/updates' % id_, json.dumps(ret))
return ret return ret
@ -693,12 +690,12 @@ class EventsHandler(CollectionHandler):
# Remove a specific person from the list of persons registered at this event. # Remove a specific person from the list of persons registered at this event.
uuid, arguments = self.uuid_arguments uuid, arguments = self.uuid_arguments
doc = self.db.query('events', doc = self.db.query('events',
{'_id': id_, 'persons.person_id': person_id}) {'_id': id_, 'persons._id': person_id})
ret = {'action': 'delete', 'person_id': person_id, 'uuid': uuid} ret = {'action': 'delete', '_id': person_id, 'uuid': uuid}
if doc: if doc:
merged, doc = self.db.update('events', merged, doc = self.db.update('events',
{'_id': id_}, {'_id': id_},
{'persons': {'person_id': person_id}}, {'persons': {'_id': person_id}},
operation='delete', operation='delete',
create=False) create=False)
self.send_ws_message('event/%s/tickets/updates' % id_, json.dumps(ret)) self.send_ws_message('event/%s/tickets/updates' % id_, json.dumps(ret))