[FIX] event: Include unregistered attendee emails in total sent count

Before this commit the Email Sent Count (located in the Communication
tab of the event view) wasn't including emails sent to the attendees who
weren't confirmed.

[This commit changes]
Event mail scheduler will include seats_uncofirmed into calculation of
the mail_count_done which is the count of emails sent. This is
reflecting the actual number of emails sent.

[Reproduce]
- Install event_mail,event_sms
- Create event E starting in time T (Events/Events)
- Add an "Unconfirmed" attendee (Modify state via database write)
- Add an E-Mail in the Communication tab of the event E
	- Set Interval time > T,
	- Template: "Email Reminder"
	- Trigger "Before the event"
- Run scheduled Action sending email (Scheduled Actions / Event: Mail Scheduler)
- BUG: email sent, but not included in the sent count in the communication tab of the event E

opw-3693626

closes odoo/odoo#157229

Signed-off-by: Andrzej Pietrusiak (pian) <pian@odoo.com>
This commit is contained in:
Andrzej(pian)
2024-03-13 09:14:09 +00:00
parent 3e169c0c69
commit 78aea4c0d6
3 changed files with 85 additions and 2 deletions
+2 -1
View File
@@ -185,9 +185,10 @@ class EventMailScheduler(models.Model):
# do not send emails if the mailing was scheduled before the event but the event is over # do not send emails if the mailing was scheduled before the event but the event is over
if scheduler.scheduled_date <= now and (scheduler.interval_type != 'before_event' or scheduler.event_id.date_end > now): if scheduler.scheduled_date <= now and (scheduler.interval_type != 'before_event' or scheduler.event_id.date_end > now):
scheduler.event_id.mail_attendees(scheduler.template_ref.id) scheduler.event_id.mail_attendees(scheduler.template_ref.id)
# Mail is sent to all attendees (unconfirmed as well), so count all attendees
scheduler.update({ scheduler.update({
'mail_done': True, 'mail_done': True,
'mail_count_done': scheduler.event_id.seats_reserved + scheduler.event_id.seats_used, 'mail_count_done': len(scheduler.event_id.registration_ids.filtered(lambda r: r.state != 'cancel'))
}) })
return True return True
+1 -1
View File
@@ -58,7 +58,7 @@ class EventMailScheduler(models.Model):
) )
scheduler.update({ scheduler.update({
'mail_done': True, 'mail_done': True,
'mail_count_done': scheduler.event_id.seats_reserved + scheduler.event_id.seats_used, 'mail_count_done': len(scheduler.event_id.registration_ids.filtered(lambda r: r.state != 'cancel'))
}) })
return super(EventMailScheduler, self).execute() return super(EventMailScheduler, self).execute()
@@ -1,6 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details. # Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import datetime, timedelta
from freezegun import freeze_time
from odoo.addons.mail.tests.common import MockEmail
from odoo.addons.sms.tests.common import MockSMS
from odoo.addons.test_event_full.tests.common import TestWEventCommon from odoo.addons.test_event_full.tests.common import TestWEventCommon
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
from odoo.tests import tagged from odoo.tests import tagged
@@ -43,3 +48,80 @@ class TestTemplateRefModel(TestWEventCommon):
self.assertEqual(str(cm.exception), 'The template which is referenced should be coming from mail.template model.') self.assertEqual(str(cm.exception), 'The template which is referenced should be coming from mail.template model.')
else: else:
self.assertEqual(str(cm.exception), 'The template which is referenced should be coming from sms.template model.') self.assertEqual(str(cm.exception), 'The template which is referenced should be coming from sms.template model.')
class TestEventSmsMailSchedule(TestWEventCommon, MockEmail, MockSMS):
@freeze_time('2020-07-06 12:00:00')
def test_event_mail_before_trigger_sent_count(self):
""" Emails are sent to both confirmed and unconfirmed attendees.
This test checks that the count of sent emails includes the emails sent to unconfirmed ones
Time in the test is frozen to simulate the following state:
NOW Event Start Event End
12:00 13:00 14:00
| | |
──────────────────────────────────────►
| | time
◄─────────────────►
3 hours
Trigger before event
"""
self.sms_template_rem = self.env['sms.template'].create({
'name': 'Test reminder',
'model_id': self.env.ref('event.model_event_registration').id,
'body': '{{ object.event_id.organizer_id.name }} reminder',
'lang': '{{ object.partner_id.lang }}'
})
test_event = self.env['event.event'].create({
'name': 'TestEventMail',
# 'user_id': self.env.ref('base.user_admin').id,
'date_begin': datetime.now() + timedelta(hours=1),
'date_end': datetime.now() + timedelta(hours=2),
'event_mail_ids': [
(0, 0, { # email 3 hours before event
'interval_nbr': 3,
'interval_unit': 'hours',
'interval_type': 'before_event',
'template_ref': 'mail.template,%i' % self.env['ir.model.data']._xmlid_to_res_id('event.event_reminder')}),
(0, 0, { # sms 3 hours before event
'interval_nbr': 3,
'interval_unit': 'hours',
'interval_type': 'before_event',
'notification_type': 'sms',
'template_ref': 'sms.template,%i' % self.sms_template_rem.id}),
]
})
mail_scheduler = test_event.event_mail_ids
self.assertEqual(len(mail_scheduler), 2, 'There should be two mail schedulers. One for mail one for sms. Cannot perform test')
# Add registrations
self.env['event.registration'].create([{
'event_id': test_event.id,
'name': 'RegistrationUnconfirmed',
'email': 'Registration@Unconfirmed.com',
'state': 'draft',
}, {
'event_id': test_event.id,
'name': 'RegistrationCanceled',
'email': 'Registration@Canceled.com',
'state': 'cancel',
}, {
'event_id': test_event.id,
'name': 'RegistrationConfirmed',
'email': 'Registration@Confirmed.com',
'state': 'open',
}])
with self.mock_mail_gateway(), self.mockSMSGateway():
mail_scheduler.execute()
self.assertEqual(len(self._new_mails), 2, 'Mails were not created')
self.assertEqual(len(self._new_sms), 2, 'SMS were not created')
self.assertEqual(test_event.seats_taken, 1, 'Wrong number of seats_taken')
self.assertEqual(mail_scheduler.filtered(lambda r: r.notification_type == 'mail').mail_count_done, 2,
'Wrong Emails Sent Count! Probably emails sent to unconfirmed attendees were not included into the Sent Count')
self.assertEqual(mail_scheduler.filtered(lambda r: r.notification_type == 'sms').mail_count_done, 2,
'Wrong SMS Sent Count! Probably SMS sent to unconfirmed attendees were not included into the Sent Count')