I have a custom (coded) hook that works like:
- a collection is updated
- is it the collection "events"?
- is one of the updated fields one of these: time_show_start, time_show_end, ...
- get all contacts from a collection who want to recieve mails
- send a mail to these contacts with some info about the event (icluding old and new times)
it should look something like this, but i just can't get it to work
module.exports = function registerHook({ action }, { services, exceptions }) {
const { MailService, ItemsService } = services;
const { ServiceUnavailableException, ForbiddenException } = exceptions;
// Send mail to admin when event time is updated
action('items.update', async ({ keys, collection, payload }, { schema, accountability }) => {
if (collection !== 'events') return;
if (
!payload.time_get_in &&
!payload.time_setup_start &&
!payload.time_setup_end &&
!payload.time_soundcheck_start &&
!payload.time_soundcheck_end &&
!payload.time_dinner &&
!payload.time_doors &&
!payload.time_show_start &&
!payload.time_show_end &&
!payload.time_curfew_live_music &&
!payload.time_curfew_guests
) {
return;
}
const eventsService = new ItemsService('events', { accountability: accountability, schema: schema });
const contactsService = new ItemsService('contacts', { accountability: accountability, schema: schema });
const mailService = new MailService({ schema });
const contacts = await contactsService.readByQuery({ filter: { getsmailon_eventtimechange: { _eq: true } } });
contacts.forEach(async (contact) => {
keys.forEach(async (el) => {
// get event
let event = null;
try {
event = await eventsService.readOne(el, { fields: ['*'] });
} catch (error) {
console.error(error);
throw new ServiceUnavailableException(error);
}
// send mail
try {
await mailService.send({
to: contact.email,
subject: 'Zeit der Veranstaltung ' + event.name + ' geändert',
text: 'Zeit der Veranstaltung <b>' + event.name + '</b> geändert',
template: {
name: 'event-time-update',
data: {
collection: collection,
event: event,
payload: payload,
},
},
});
console.log('mail sent for event', event.id);
} catch (error) {
console.error(error);
throw new ServiceUnavailableException(error);
}
});
});
return;
});
};