verbb / formie

The most user-friendly forms plugin for Craft CMS.
Other
93 stars 69 forks source link

Ability to Query Notifications #1768

Closed medoingthings closed 3 months ago

medoingthings commented 3 months ago

What are you trying to do?

I’ve built a system that allows users to manage inhouse trainings. At two points a Formie form is used to send out invitation and follow-up emails to participants.

Sometimes those notifications aren’t sent by the SMTP server and I can see that in the „Sent Notifications” area (red indicator next to notification), which is great. But I would like to be able to show the training editors whether the invitation/follow-up mails were sent successfully or not.

Right now I only query the submissions by doing this in my Craft module:

$invitationSubmission = \verbb\formie\elements\Submission::find()
      ->form('inhouseInvitation')
      ->notificationType('invitation')
      ->relatedTo($trainingDate->id)
      ->one();

So I know the form was sent successfully, but that’s not enough, because the notification sending can go wrong and I want to act on it, if it did go wrong…

What's your proposed solution?

I would like to be able to query not only for submissions, but for notifications as well. E.g.:

    $invitationNotification = \verbb\formie\elements\Notification::find()
      ->relatedTo($invitationSubmission->id)
      ->one();

What do you think? Have you already considered that at some point?

Additional context

No response

engram-design commented 3 months ago

Notifications cannot be queried, as they aren't elements. There are no plans to make them elements, as this adds needless overhead.

You can fetch the notifications via the form, which can be fetched from the submission.

$invitationSubmission->getForm()->getNotifications();

You can query SentNotification elements, which might be more useful if you're trying to re-send failed notifications.

medoingthings commented 3 months ago

Yes, I’m after the SentNotification – great news that I can already query them! But I’m stuck in how to do it. This doesn’t work:

      // attempt #1
      $invitationNotification = \verbb\formie\elements\SentNotification::find()
      ->relatedTo($invitationSubmission->id)
      ->all();

      // attempt #2
      $invitationNotification = \verbb\formie\elements\SentNotification::find()
      ->submissionId($invitationSubmission->id)
      ->all();

Any hint in the right direction would be much appreciated. Is there anything in the documentation about this? Couldn’t find it.

engram-design commented 3 months ago

So you can't use relatedTo as they're not hooked up to relations. I've just added submissionId and notificationId params to query by. To get this early, run composer require verbb/formie:"dev-craft-4 as 2.1.6".

medoingthings commented 3 months ago

Love the fast pace and reaction time. Thank you so much!