Skip to content

Commit

Permalink
Refs #99. Added support to email test invitations.
Browse files Browse the repository at this point in the history
  • Loading branch information
SBriere committed Dec 18, 2024
1 parent 374ef18 commit cc1cfde
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 25 deletions.
142 changes: 128 additions & 14 deletions client/src/dialogs/TestInvitationDialog.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "TestInvitationDialog.h"
#include "services/EmailService/EmailServiceWebAPI.h"
#include "ui_TestInvitationDialog.h"
#include "WebAPI.h"

Expand All @@ -11,11 +12,17 @@ TestInvitationDialog::TestInvitationDialog(ComManager *comMan, QWidget *parent)
setModal(true);

initUI();
connect(m_comManager, &ComManager::testInvitationsReceived, this, &TestInvitationDialog::processTestInvitationsReply);

m_emailComManager = new EmailComManager(m_comManager);
connect(m_emailComManager, &EmailComManager::postResultsOK, this, &TestInvitationDialog::emailSentSuccess);
connect(m_emailComManager, &EmailComManager::networkError, this, &TestInvitationDialog::emailSentError);
}

TestInvitationDialog::~TestInvitationDialog()
{
delete ui;
m_emailComManager->deleteLater();
}

void TestInvitationDialog::setTestTypes(const QList<TeraData> &test_types)
Expand All @@ -33,10 +40,12 @@ void TestInvitationDialog::setCurrentData(TeraData *data)
ui->lblCount->setVisible(m_data);
ui->numCount->setVisible(m_data);
ui->cmbTestType->setEnabled(!m_data);
ui->btnNext->setVisible(!m_data);
ui->btnPrevious->setVisible(!m_data);
// ui->btnNext->setVisible(!m_data);
// ui->btnPrevious->setVisible(!m_data);
ui->stackedPages->removeWidget(ui->pageTargets);
ui->chkInviteEmail->setText(m_data ? tr("Renvoyer l'invitation par courriel") : tr("Envoyer les invitations automatiquement par courriel"));
ui->btnOK->setText(m_data ? tr("OK") : tr("Inviter"));
ui->btnOK->setVisible(m_data);
/*ui->btnOK->setVisible(m_data);*/

if (m_data){
int test_index = ui->cmbTestType->findData(m_data->getFieldValue("id_test_type").toInt());
Expand All @@ -47,6 +56,19 @@ void TestInvitationDialog::setCurrentData(TeraData *data)

}

void TestInvitationDialog::setEnableEmail(const bool &enable_email)
{
m_enableEmails = enable_email;

if (!m_enableEmails){
ui->chkInviteEmail->setCheckState(Qt::Unchecked);
}else{
// Default value is on
ui->chkInviteEmail->setCheckState(Qt::Checked);
}
ui->chkInviteEmail->setVisible(m_enableEmails);
}

void TestInvitationDialog::setInvitableDevices(QHash<int, TeraData> *devices)
{
if (devices)
Expand All @@ -71,13 +93,50 @@ void TestInvitationDialog::setInvitableUsers(QHash<int, TeraData> *users)
ui->widgetInvitees->setAvailableUsers(QList<TeraData>());
}

void TestInvitationDialog::processTestInvitationsReply(QList<TeraData> invitations)
{
if (ui->chkInviteEmail->isChecked()){
// Create email queue
m_pendingInvitations = invitations;

// Start sending!
ui->progressEmails->setMaximum(m_pendingInvitations.count());
ui->progressEmails->setValue(0);
ui->btnOK->hide();
ui->frameMessage->hide();
ui->frameEmails->show();
processNextEmail();
}
}

void TestInvitationDialog::emailSentSuccess()
{
ui->txtEmailLogs->insertHtml("<font color='green'>" + tr("OK") + "</font><br>");
ui->progressEmails->setValue(ui->progressEmails->value()+1);
processNextEmail();
}

void TestInvitationDialog::emailSentError(QNetworkReply::NetworkError error, QString error_str, QNetworkAccessManager::Operation op, int status_code, QString path, QUrlQuery url_query)
{
ui->txtEmailLogs->insertHtml("<font color='red'>" + tr("Erreur") + ": " + error_str + "</font><br>");
ui->progressEmails->setValue(ui->progressEmails->value()+1);
processNextEmail();
}

void TestInvitationDialog::initUI()
{
ui->stackedPages->setCurrentIndex(0);
ui->lblCount->hide();
ui->numCount->hide();
ui->btnPrevious->setEnabled(false);
ui->btnOK->setVisible(false);
ui->btnDone->setVisible(false);
ui->txtMessage->setEmailTemplate(tr("Bonjour") + " $participant,<br><br>" + tr("Vous êtes invités à compléter un questionnaire.") +
"<br>" + tr("Veuillez") + " $join_link " + tr("pour compléter le questionnaire.") + "<br><br>" +
tr("Merci de votre participation") + "<br><br>$fullname");
ui->txtMessage->setVariable("$fullname", m_comManager->getCurrentUser().getName());
setEnableEmail(m_enableEmails);
ui->frameEmails->hide();

ui->dateExpiration->setDate(QDate::currentDate().addDays(30));

Expand All @@ -88,20 +147,49 @@ void TestInvitationDialog::initUI()

}

void TestInvitationDialog::processNextEmail()
{
if (m_pendingInvitations.isEmpty()){
// All done sending!
ui->btnCancel->hide();
ui->btnNext->hide();
ui->btnPrevious->hide();
ui->btnDone->setVisible(true);
setEnabled(true);
return;
}

QJsonDocument doc;
QJsonObject data_obj;
QString participant_name;
TeraData* invitation = &m_pendingInvitations.first();
if (invitation->hasFieldName("test_invitation_participant")){
QJsonObject obj = invitation->getFieldValue("test_invitation_participant").toJsonObject();
data_obj["participant_uuid"] = obj["participant_uuid"].toString();
participant_name = obj["participant_name"].toString();
ui->txtMessage->setVariable("$participant", participant_name);
}
ui->txtMessage->setVariable("$join_link", "<a href=\"" + invitation->getFieldValue("test_invitation_url").toString() + "\">" + tr("cliquez ici") + "</a>");

data_obj["body"] = ui->txtMessage->getPreview()->toHtml();
data_obj["subject"] = ui->txtSubject->text();
doc.setObject(data_obj);
ui->txtEmailLogs->insertHtml(tr("Envoi de l'invitation à") + " " + participant_name + "... ");
m_emailComManager->doPost(EMAIL_SEND_PATH, doc.toJson());

m_pendingInvitations.removeFirst();
}

void TestInvitationDialog::on_btnCancel_clicked()
{
reject();
}

void TestInvitationDialog::on_stackedPages_currentChanged(int current_index)
{
int last_page_offset = 0;
if (!ui->chkInviteEmail->isChecked())
last_page_offset = 1;

ui->btnPrevious->setEnabled(current_index>0);
ui->btnNext->setEnabled(current_index < ui->stackedPages->count() - 1 - last_page_offset);
ui->btnOK->setVisible(current_index == ui->stackedPages->count() - 1 - last_page_offset);
ui->btnNext->setEnabled(current_index < ui->stackedPages->count() - 1);
ui->btnOK->setVisible(current_index == ui->stackedPages->count() - 1);
}


Expand All @@ -114,11 +202,8 @@ void TestInvitationDialog::on_btnPrevious_clicked()

void TestInvitationDialog::on_btnNext_clicked()
{
int last_page_offset = 0;
if (!ui->chkInviteEmail->isChecked())
last_page_offset = 1;
if (ui->stackedPages->currentIndex() < ui->stackedPages->count() -1 - last_page_offset)
ui->stackedPages->setCurrentIndex(ui->stackedPages->currentIndex()+1);
if (ui->stackedPages->currentIndex() < ui->stackedPages->count() - 1)
ui->stackedPages->setCurrentIndex(ui->stackedPages->currentIndex() + 1);
}


Expand Down Expand Up @@ -178,6 +263,35 @@ void TestInvitationDialog::on_btnOK_clicked()

m_comManager->doPost(WEB_TESTINVITATION_PATH, doc.toJson());

setEnabled(false);
if (!ui->chkInviteEmail->isChecked()){
disconnect(m_comManager);
accept();
}
}


void TestInvitationDialog::on_btnDone_clicked()
{
accept();
}


void TestInvitationDialog::on_chkInviteEmail_checkStateChanged(const Qt::CheckState &state)
{
if (!ui->chkInviteEmail->isChecked()){
ui->stackedPages->removeWidget(ui->pageMessage);
}else{
bool has_widget = false;
for (int i=0; i<ui->stackedPages->count(); i++){
if (ui->stackedPages->widget(i) == ui->pageMessage){
has_widget = true;
break;
}
}
if (!has_widget)
ui->stackedPages->addWidget(ui->pageMessage);
}
on_stackedPages_currentChanged(ui->stackedPages->currentIndex());
}

17 changes: 14 additions & 3 deletions client/src/dialogs/TestInvitationDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QDialog>
#include "managers/ComManager.h"
#include "services/EmailService/EmailComManager.h"
#include "TeraData.h"

namespace Ui {
Expand All @@ -19,26 +20,36 @@ class TestInvitationDialog : public QDialog

void setTestTypes(const QList<TeraData>& test_types);
void setCurrentData(TeraData* data);
void setEnableEmail(const bool& enable_email);

void setInvitableDevices(QHash<int, TeraData>* devices);
void setInvitableParticipants(QHash<int, TeraData>* participants);
void setInvitableUsers(QHash<int, TeraData>* users);

private slots:
void on_btnCancel_clicked();
void processTestInvitationsReply(QList<TeraData> invitations);
void emailSentSuccess();
void emailSentError(QNetworkReply::NetworkError error, QString error_str, QNetworkAccessManager::Operation op, int status_code, QString path, QUrlQuery url_query);

void on_btnCancel_clicked();
void on_stackedPages_currentChanged(int current_index);

void on_btnPrevious_clicked();
void on_btnNext_clicked();
void on_btnOK_clicked();
void on_btnDone_clicked();
void on_chkInviteEmail_checkStateChanged(const Qt::CheckState &state);

private:
Ui::TestInvitationDialog *ui;
ComManager* m_comManager;
ComManager* m_comManager;
EmailComManager* m_emailComManager;
TeraData* m_data = nullptr;
bool m_enableEmails = false;

QList<TeraData> m_pendingInvitations;

void initUI();
void processNextEmail();
};

#endif // TESTINVITATIONDIALOG_H
Loading

0 comments on commit cc1cfde

Please sign in to comment.