Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

395
Views
How to make a flow in directus with the following function

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 enter image description here


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;
  });
};

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!