Commit f28726e8 authored by Hermann's avatar Hermann
Browse files

fix: relationlistcontroller crashed when relation was not defined

This caused eventsignups to crash in case of email signups.
Additionally, constructor arguments are now set as object making them better readable.
parent 2e52258a
......@@ -74,7 +74,13 @@ class DuoLangProperty {
// waiting list.
class ParticipantsTable {
constructor({ attrs: { where } }) {
this.ctrl = new RelationlistController('eventsignups', 'users', { where }, ['email']);
this.ctrl = new RelationlistController({
primary: 'eventsignups',
secondary: 'users',
query: { where },
searchKeys: ['email'],
includeWithoutRelation: true,
});
}
getItemData(data) {
......
......@@ -23,7 +23,9 @@ class MembersTable {
constructor({ attrs: { group, hasPatchRights } }) {
this.group_id = group;
this.hasPatchRights = hasPatchRights;
this.ctrl = new RelationlistController('groupmemberships', 'users', { where: { group } });
this.ctrl = new RelationlistController({
primary: 'groupmemberships', secondary: 'users', query: { where: { group } },
});
// true while in the modus of adding a member
this.addmode = false;
this.userHandler = new ResourceHandler('users');
......
......@@ -8,15 +8,19 @@ export default class RelationlistController {
* Controller for a list of data embedding a relationship.
* The secondary api endpoint is embedded into the list items of the primary endpoint results.
* Searches are applied to both resources, queries and filters need to be specified for each.
*
* @param {bool} includeWithoutRelation - Specifies what to do in case the relation is undefined.
* By default, such items are excluded, if true they will be included into the list.
*/
constructor(
constructor({
primary,
secondary,
query = {},
searchKeys = false,
secondaryQuery = {},
secondarySearchKeys = false,
) {
includeWithoutRelation = false,
}) {
this.handler = new ResourceHandler(primary, searchKeys);
this.handler2 = new ResourceHandler(secondary, secondarySearchKeys);
this.secondaryKey = secondary.slice(0, -1);
......@@ -24,6 +28,7 @@ export default class RelationlistController {
this.query2 = secondaryQuery || {};
this.filter = null;
this.filter2 = null;
this.includeWithoutRelation = includeWithoutRelation;
// state pointer that is counted up every time the table is refreshed so
// we can tell infinite scroll that the data-version has changed.
this.stateCounter = Stream(0);
......@@ -67,9 +72,12 @@ export default class RelationlistController {
console.log(data._items.map(item => item._id));
const itemsWithoutRelation = data._items.filter(item => !(this.secondaryKey in item));
const itemsWithRelation = data._items.filter(item => (this.secondaryKey in item));
const query2 = Object.assign({}, this.query2);
query2.where = {
_id: { $in: data._items.map(item => item[this.secondaryKey]) },
_id: { $in: itemsWithRelation.map(item => item[this.secondaryKey]) },
...this.filter2,
...this.query2.where,
};
......@@ -78,15 +86,18 @@ export default class RelationlistController {
const secondaryIds = secondaryData._items.map(item => item._id);
// filter the primary list to only include those items that have a relation to
// the queried secondary IDs
const filteredPrimaries = data._items.filter(item =>
const filteredPrimaries = itemsWithRelation.filter(item =>
secondaryIds.includes(item[this.secondaryKey]));
// now return the list of filteredPrimaries with the secondary data embedded
resolve(filteredPrimaries.map((item) => {
// embed the secondary data
const embeddedList = filteredPrimaries.map((item) => {
const itemCopy = Object.assign({}, item);
itemCopy[this.secondaryKey] = secondaryData._items.find(relItem =>
relItem._id === item[this.secondaryKey]);
return itemCopy;
}));
});
// now return the list of filteredPrimaries with the secondary data embedded
if (this.includeWithoutRelation) resolve([...embeddedList, ...itemsWithoutRelation]);
else resolve(embeddedList);
});
});
});
......
......@@ -12,12 +12,12 @@ export default class UserView extends ItemView {
constructor(vnode) {
super(vnode);
// a controller to handle the groupmemberships of this user
this.groupmemberships = new RelationlistController('groupmemberships', 'groups', {
where: { user: this.data._id },
this.groupmemberships = new RelationlistController({
primary: 'groupmemberships', secondary: 'groups', query: { where: { user: this.data._id } },
});
// a controller to handle the eventsignups of this user
this.eventsignups = new RelationlistController('eventsignups', 'events', {
where: { user: this.data._id },
this.eventsignups = new RelationlistController({
primary: 'eventsignups', secondary: 'events', query: { where: { user: this.data._id } },
});
// initially, don't display the choice field for a new group
// (this will be displayed once the user clicks on 'new')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment