include email_id in group membership

This commit is contained in:
Marc Koch 2025-03-31 17:58:01 +02:00
parent 8dab2377f5
commit 5a424771fd
Signed by: marc.koch
GPG key ID: 12406554CFB028B9
5 changed files with 86 additions and 13 deletions

View file

@ -128,7 +128,7 @@ class MailingListRecipient {
* @return MailingListRecipient
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
public static function getContactIdEmail(string $email): MailingListRecipient {
public static function getContactByEmail(string $email): MailingListRecipient {
try {
$recipientData = Contact::get()
->addSelect('id', 'first_name', 'last_name', 'email.email', 'Active_Directory.SID')
@ -163,6 +163,49 @@ class MailingListRecipient {
return $recipient;
}
/**
* Get a recipient by its id.
*
* @param int $id
*
* @return MailingListRecipient
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
public static function getContactById(int $id): MailingListRecipient {
try {
$recipientData = Contact::get()
->addSelect('id', 'first_name', 'last_name', 'email.email', 'Active_Directory.SID')
->addJoin('Email AS email', 'LEFT', ['id', '=', 'email.contact_id'])
->addWhere('id', '=', $id)
->addGroupBy('id')
->execute()
->first();
}
catch (\Exception $e) {
throw new MailinglistException(
"Could not get recipient by id '{$id}': {$e->getMessage()}",
MailinglistException::ERROR_CODE_GET_RECIPIENTS_FAILED
);
}
try {
$recipient = new MailingListRecipient(
sid: $recipientData['Active_Directory.SID'],
contact_id: $recipientData['id'],
first_name: $recipientData['first_name'],
last_name: $recipientData['last_name'],
email: $recipientData['email.email'],
);
} catch (\Exception $e) {
throw new MailinglistException(
"Could not create recipient object for contact with id '{$recipientData['id']}': {$e->getMessage()}",
MailinglistException::ERROR_CODE_GET_RECIPIENTS_FAILED
);
}
return $recipient;
}
/**
* Get a list of group mailing lists the contact is subscribed to.
*
@ -307,6 +350,32 @@ class MailingListRecipient {
return $this->email;
}
/**
* Get the email id.
*
* @param string $locationType
*
* @return int|NULL
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
*/
public function getEmailId(string $locationType): ?int {
try {
return \Civi\Api4\Email::get()
->addSelect('id')
->addWhere('contact_id', '=', $this->getContactId())
->addWhere('email', '=', $this->email)
->addWhere('location_type_id:name', '=', $locationType)
->execute()
->first()['id'] ?? NULL;
}
catch (\Exception $e) {
throw new MailinglistException(
"Failed to get email id: {$e->getMessage()}",
MailinglistException::ERROR_CODE_GET_EMAIL_ID_FAILED,
);
}
}
/**
* Get the SID of the contact.
*