I'm trying to set up a way to add a list of registrants to the apostrophe-events module. I was able to add a new joinByMany field to the module, but I realized I also need to store a registration date and count on each registrant object. In order to accommodate this, I added the following field, consisting of an array of joinByOne user, registration date string, and registration count integer:
addFields: [
{
name: '_registrants',
label: 'Registrants',
type: 'array',
titleField: '_user.firstName',
schema: [
{
name: '_user',
withType: 'apostrophe-user',
type: 'joinByOne',
idField: 'userId',
filters: {
// Fetch just enough information
projection: {
id: 1,
username: 1,
firstName: 1,
lastName: 1,
email: 1
}
}
},
{
name: 'registrationDate',
label: 'Registration Date',
contextual: true,
type: 'string'
},
{
name: 'attendeeCount',
label: 'Total Attendees',
type: 'integer'
}
]
}
]
Now, the problem I'm running into is that I want to show either a 'Register' or 'Unregister' button on my apostrophe-events-pages show.html template, depending on whether or not the current logged in user is in the _registrants array. Previously, when I was using the joinByMany to set up a list of users, I could just look for the index of the current user's id in the idsField of the JoinByMany. Now, though, I have to somehow loop through _registrants and check each items _user.userId in order to check the same thing, and I have no idea how to do that. I'm guessing that it will be some type of logic in the template, but I'm not sure how to accomplish this in nunjucks.
Here is what I currently had to check for the user when I was using a joinByMany instead of an array of joinByOnes:
{% if data.piece.userIds.indexOf(data.user._id) < 0 %}
<div class="event-controls-item">
{{ buttons.major('Register for Field Trip', { action: 'register-event' }) }}
</div>
{% else %}
<div class="event-controls-item">
{{ buttons.danger('Unregister for Trip', { action: 'unregister-event' }) }}
</div>
{% endif %}
Thank you!