Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8f598172 authored by theronakpatel's avatar theronakpatel
Browse files

refresh segment added

parent 6a91a7e5
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -29,5 +29,6 @@
        <command>OCA\EcloudAccounts\Command\MigrateWebmailAddressbooks</command>
        <command>OCA\EcloudAccounts\Command\MapActiveAttributetoLDAP</command>
        <command>OCA\EcloudAccounts\Command\DeleteSendgridContact</command>
        <command>OCA\EcloudAccounts\Command\RefreshSendGridSegment</command>
    </commands>
</info>
+65 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace OCA\EcloudAccounts\Command;

use Exception;
use OCA\EcloudAccounts\Service\SendGridService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class RefreshSendGridSegment extends Command {
	private SendGridService $sendGridService;
	public function __construct(SendGridService $sendGridService) {
		$this->sendGridService = $sendGridService;
		parent::__construct();
	}

	protected function configure(): void {
		$this
			->setName('ecloud-accounts:refresh-sendgrid-segment')
			->setDescription('Refreshes the SendGrid segment')
			->addOption(
				'segment-id',
				null,
				InputOption::VALUE_REQUIRED,
				'The ID of the SendGrid segment'
			)
			->addOption(
				'refresh',
				null,
				InputOption::VALUE_NONE,
				'Trigger the refresh of the SendGrid segment'
			);
	}

	protected function execute(InputInterface $input, OutputInterface $output): int {

		if ($input->getOption('refresh')) {
			$segmentId = $input->getOption('segment-id');

			if (!$segmentId) {
				$output->writeln('<error>option --segment-id is required.</error>');
				return Command::FAILURE;
			}
			try {
				$success = $this->sendGridService->refreshSendGridSegment($segmentId);
				if ($success) {
					$output->writeln('<info>Segment refreshed successfully.</info>');
					return Command::SUCCESS;
				}
			} catch (Exception $e) {
				$output->writeln('<error>Error: ' . $e->getMessage() . '</error>');
				return Command::FAILURE;
			}
			return 0;
		}

		$output->writeln('No action taken.');
		return 0;
	}

}
+25 −0
Original line number Diff line number Diff line
@@ -12,10 +12,12 @@ use OCP\IConfig;
class SendGridService {
	private \SendGrid $sendGridClient;
	private $config;
	private $apiKey;

	public function __construct(IConfig $config) {
		$this->config = $config;
		$apiKey = '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';
		$this->apiKey = $apiKey;
		$this->sendGridClient = new \SendGrid($apiKey);
	}

@@ -103,4 +105,27 @@ class SendGridService {
			throw new Exception("Failed to delete contacts: " . $response->body());
		}
	}

	public function refreshSendGridSegment($segmentId) {
		$data = [
			'user_time_zone' => 'America/Chicago'
		];

		try {
			$response = $this->sendGridClient->client
				->marketing()
				->segments()
				->_($segmentId)
				->refresh()
				->post($data);

			if ($response->statusCode() === 202) {
				return true;
			} else {
				throw new Exception("Failed to delete contacts: " . $response->body());
			}
		} catch (Exception $e) {
			throw new Exception('Caught exception: ' . $e->getMessage());
		}
	}
}