Commit 09997460 authored by Emiliano Balbuena's avatar Emiliano Balbuena
Browse files

(fix): Synchronous queue save; update impression on revoke

parent 866a01b0
Loading
Loading
Loading
Loading
+22 −12
Original line number Original line Diff line number Diff line
@@ -23,6 +23,9 @@ class Manager
    /** @var ElasticRepository */
    /** @var ElasticRepository */
    protected $elasticRepository;
    protected $elasticRepository;


    /** @var Metrics */
    protected $metrics;

    /** @var PaymentsRepository */
    /** @var PaymentsRepository */
    protected $paymentsRepository;
    protected $paymentsRepository;


@@ -51,8 +54,9 @@ class Manager
     * Manager constructor.
     * Manager constructor.
     * @param Repository $repository
     * @param Repository $repository
     * @param ElasticRepository $elasticRepository
     * @param ElasticRepository $elasticRepository
     * @param Metrics $metrics
     * @param PaymentsRepository $paymentsRepository
     * @param PaymentsRepository $paymentsRepository
     * @param null $queueClient
     * @param QueueClientInterface $queueClient
     * @param Delegates\CampaignUrnDelegate $campaignUrnDelegate
     * @param Delegates\CampaignUrnDelegate $campaignUrnDelegate
     * @param Delegates\NormalizeDatesDelegate $normalizeDatesDelegate
     * @param Delegates\NormalizeDatesDelegate $normalizeDatesDelegate
     * @param Delegates\NormalizeEntityUrnsDelegate $normalizeEntityUrnsDelegate
     * @param Delegates\NormalizeEntityUrnsDelegate $normalizeEntityUrnsDelegate
@@ -63,6 +67,7 @@ class Manager
    public function __construct(
    public function __construct(
        $repository = null,
        $repository = null,
        $elasticRepository = null,
        $elasticRepository = null,
        $metrics = null,
        $paymentsRepository = null,
        $paymentsRepository = null,
        $queueClient = null,
        $queueClient = null,
        $campaignUrnDelegate = null,
        $campaignUrnDelegate = null,
@@ -74,6 +79,7 @@ class Manager
    {
    {
        $this->repository = $repository ?: new Repository();
        $this->repository = $repository ?: new Repository();
        $this->elasticRepository = $elasticRepository ?: new ElasticRepository();
        $this->elasticRepository = $elasticRepository ?: new ElasticRepository();
        $this->metrics = $metrics ?: new Metrics();
        $this->paymentsRepository = $paymentsRepository ?: new PaymentsRepository();
        $this->paymentsRepository = $paymentsRepository ?: new PaymentsRepository();
        $this->queueClient = $queueClient ?: QueueClient::build();
        $this->queueClient = $queueClient ?: QueueClient::build();


@@ -99,6 +105,7 @@ class Manager
    /**
    /**
     * @param array $opts
     * @param array $opts
     * @return Response
     * @return Response
     * @throws Exception
     */
     */
    public function getList(array $opts = [])
    public function getList(array $opts = [])
    {
    {
@@ -146,6 +153,7 @@ class Manager


    /**
    /**
     * @param $urn
     * @param $urn
     * @param array $opts
     * @return Campaign|null
     * @return Campaign|null
     * @throws Exception
     * @throws Exception
     */
     */
@@ -241,6 +249,7 @@ class Manager
     * @param mixed $paymentPayload
     * @param mixed $paymentPayload
     * @return Campaign
     * @return Campaign
     * @throws CampaignException
     * @throws CampaignException
     * @throws Exception
     */
     */
    public function update(Campaign $campaignRef, $paymentPayload = null)
    public function update(Campaign $campaignRef, $paymentPayload = null)
    {
    {
@@ -286,8 +295,7 @@ class Manager


        // Write
        // Write


        $this->repository->update($campaign);
        $this->sync($campaign);
        $this->elasticRepository->update($campaign);


        // Queue for lifecycle events
        // Queue for lifecycle events


@@ -309,7 +317,7 @@ class Manager
     */
     */
    public function sync(Campaign $campaign)
    public function sync(Campaign $campaign)
    {
    {
        $this->repository->update($campaign);
        $this->repository->update($campaign, true);
        $this->elasticRepository->update($campaign);
        $this->elasticRepository->update($campaign);


        return $campaign;
        return $campaign;
@@ -362,8 +370,7 @@ class Manager


        // Write
        // Write


        $this->repository->update($campaign);
        $this->sync($campaign);
        $this->elasticRepository->update($campaign);


        //
        //


@@ -398,6 +405,12 @@ class Manager
            throw new CampaignException('Campaign should be in [created] or [approved] state in order to cancel it');
            throw new CampaignException('Campaign should be in [created] or [approved] state in order to cancel it');
        }
        }


        // Sync impressions

        $campaign = $this->metrics
            ->setCampaign($campaign)
            ->syncImpressionsMet();

        // Update
        // Update


        $campaign
        $campaign
@@ -409,8 +422,7 @@ class Manager


        // Write
        // Write


        $this->repository->update($campaign);
        $this->sync($campaign);
        $this->elasticRepository->update($campaign);


        //
        //


@@ -454,8 +466,7 @@ class Manager


        // Write
        // Write


        $this->repository->update($campaign);
        $this->sync($campaign);
        $this->elasticRepository->update($campaign);


        //
        //


@@ -499,8 +510,7 @@ class Manager


        // Write
        // Write


        $this->repository->update($campaign);
        $this->sync($campaign);
        $this->elasticRepository->update($campaign);


        return $campaign;
        return $campaign;
    }
    }
+7 −7
Original line number Original line Diff line number Diff line
@@ -99,7 +99,7 @@ class Repository
                        ->setImpressionsMet($data['impressions_met'])
                        ->setImpressionsMet($data['impressions_met'])
                        ->setRating($data['rating'])
                        ->setRating($data['rating'])
                        ->setQuality($data['quality'])
                        ->setQuality($data['quality'])
                        ->setCreatedTimestamp(((int) $data['timestamp_timestamp']) ?: null)
                        ->setCreatedTimestamp(((int) $data['created_timestamp']) ?: null)
                        ->setReviewedTimestamp(((int) $data['reviewed_timestamp']) ?: null)
                        ->setReviewedTimestamp(((int) $data['reviewed_timestamp']) ?: null)
                        ->setRejectedTimestamp(((int) $data['rejected_timestamp']) ?: null)
                        ->setRejectedTimestamp(((int) $data['rejected_timestamp']) ?: null)
                        ->setRevokedTimestamp(((int) $data['revoked_timestamp']) ?: null)
                        ->setRevokedTimestamp(((int) $data['revoked_timestamp']) ?: null)
@@ -120,10 +120,10 @@ class Repository


    /**
    /**
     * @param Campaign $campaign
     * @param Campaign $campaign
     * @param bool $async
     * @return bool
     * @return bool
     * @throws Exception
     */
     */
    public function add(Campaign $campaign)
    public function add(Campaign $campaign, $async = true)
    {
    {
        $cql = "INSERT INTO boost_campaigns (type, guid, owner_guid, json_data, delivery_status) VALUES (?, ?, ?, ?, ?)";
        $cql = "INSERT INTO boost_campaigns (type, guid, owner_guid, json_data, delivery_status) VALUES (?, ?, ?, ?, ?)";
        $values = [
        $values = [
@@ -159,17 +159,17 @@ class Repository
        $prepared = new Custom();
        $prepared = new Custom();
        $prepared->query($cql, $values);
        $prepared->query($cql, $values);


        return (bool) $this->db->request($prepared, true);
        return (bool) $this->db->request($prepared, $async);
    }
    }


    /**
    /**
     * @param Campaign $campaign
     * @param Campaign $campaign
     * @param bool $async
     * @return bool
     * @return bool
     * @throws Exception
     */
     */
    public function update(Campaign $campaign)
    public function update(Campaign $campaign, $async = true)
    {
    {
        return $this->add($campaign);
        return $this->add($campaign, $async);
    }
    }


    /**
    /**