<commit_before><?php

namespace Remp\MailerModule\Api\v1\Handlers\Users;

use Nette\Database\Table\ActiveRow;
use Remp\MailerModule\Repository\ListsRepository;
use Remp\MailerModule\Repository\UserSubscriptionsRepository;
use Remp\MailerModule\User\IUser;
use Tomaj\NetteApi\Handlers\BaseHandler;
use Tomaj\NetteApi\Params\InputParam;
use Tomaj\NetteApi\Response\JsonApiResponse;

class UserRegisteredHandler extends BaseHandler
{
    private $userSubscriptionsRepository;

    private $userProvider;

    private $listsRepository;

    public function __construct(
        UserSubscriptionsRepository $userSubscriptionsRepository,
        ListsRepository $listsRepository,
        IUser $userProvider
    ) {
        parent::__construct();
        $this->userSubscriptionsRepository = $userSubscriptionsRepository;
        $this->userProvider = $userProvider;
        $this->listsRepository = $listsRepository;
    }

    public function params()
    {
        return [
            new InputParam(InputParam::TYPE_POST, 'user_id', InputParam::REQUIRED),
        ];
    }

    public function handle($params)
    {
        $userId = $params['user_id'];

        $userList = $this->userProvider->list([$userId], 1);
        if (count($userList) === 0) {
            return new JsonApiResponse(404, ['status' => 'error', 'message' => 'Invalid user_id parameter']);
        }
        $userInfo = $userList[$userId];

        $lists = $this->listsRepository->all();

        /** @var ActiveRow $list */
        foreach ($lists as $list) {
            if ($list->auto_subscribe) {
                $this->userSubscriptionsRepository->subscribeUser($list, $userId, $userInfo['email']);
            } else {
                $this->userSubscriptionsRepository->unsubscribeUser($list, $userId, $userInfo['email']);
            }
        }

        return new JsonApiResponse(200, ['status' => 'ok']);
    }
}<commit_msg>Do not process user registered call twice in Mailer<commit_after><?php

namespace Remp\MailerModule\Api\v1\Handlers\Users;

use Nette\Database\Table\ActiveRow;
use Remp\MailerModule\Repository\ListsRepository;
use Remp\MailerModule\Repository\UserSubscriptionsRepository;
use Remp\MailerModule\User\IUser;
use Tomaj\NetteApi\Handlers\BaseHandler;
use Tomaj\NetteApi\Params\InputParam;
use Tomaj\NetteApi\Response\JsonApiResponse;

class UserRegisteredHandler extends BaseHandler
{
    private $userSubscriptionsRepository;

    private $userProvider;

    private $listsRepository;

    public function __construct(
        UserSubscriptionsRepository $userSubscriptionsRepository,
        ListsRepository $listsRepository,
        IUser $userProvider
    ) {
        parent::__construct();
        $this->userSubscriptionsRepository = $userSubscriptionsRepository;
        $this->userProvider = $userProvider;
        $this->listsRepository = $listsRepository;
    }

    public function params()
    {
        return [
            new InputParam(InputParam::TYPE_POST, 'user_id', InputParam::REQUIRED),
        ];
    }

    public function handle($params)
    {
        $userId = $params['user_id'];

        $userList = $this->userProvider->list([$userId], 1);
        if (count($userList) === 0) {
            return new JsonApiResponse(404, ['status' => 'error', 'message' => 'Invalid user_id parameter']);
        }
        $userInfo = $userList[$userId];

        $lists = $this->listsRepository->all();

        if (!empty($this->userSubscriptionsRepository->findByEmail($userInfo['email']))) {
            return new JsonApiResponse(400, ['status' => 'error', 'message' => 'User has already been registered.']);
        }

        /** @var ActiveRow $list */
        foreach ($lists as $list) {
            if ($list->auto_subscribe) {
                $this->userSubscriptionsRepository->subscribeUser($list, $userId, $userInfo['email']);
            } else {
                $this->userSubscriptionsRepository->unsubscribeUser($list, $userId, $userInfo['email']);
            }
        }

        return new JsonApiResponse(200, ['status' => 'ok']);
    }
}
{'fraction_non_alphanumeric': 0.1262873957822462, 'fraction_numerical': 0.005149583128984796, 'mean_word_length': 3.079, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4041187', 'n_tokens_mistral': 1147, 'n_tokens_neox': 1118, 'n_words': 213}