Skip to content

Commit

Permalink
Ajout d'une synchronisation automatique des membres vers mailchimp
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Leune committed Mar 28, 2017
1 parent d0fec14 commit b1e744d
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 15 deletions.
3 changes: 3 additions & 0 deletions app/config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ parameters:
twitter_oauth_access_token_secret: ""
twitter_consumer_key: ""
twitter_consumer_secret: ""

mailchimp_api_key: ""
mailchimp_members_list:
9 changes: 9 additions & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,12 @@ services:
app.twitter_api:
class: TwitterAPIExchange
arguments: ["%twitter_api_settings%"]

app.mailchimp_client:
class: Mailchimp\Mailchimp
arguments: ["%mailchimp_api_key%"]
public: false

app.mailchimp_api:
class: AppBundle\Mailchimp\Mailchimp
arguments: ["@app.mailchimp_client"]
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"algolia/algoliasearch-client-php": "^1.12",
"cocur/slugify": "^2.3",
"j7mbo/twitter-api-php": "^1.0",
"nojimage/twitter-text-php": "^1.1"
"nojimage/twitter-text-php": "^1.1",
"pacely/mailchimp-apiv3": "^1.0"
},
"scripts": {
"post-install-cmd": [
Expand Down
153 changes: 152 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 76 additions & 12 deletions sources/AppBundle/Association/Model/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace AppBundle\Association\Model\Repository;

use AppBundle\Association\Model\User;
use Aura\SqlQuery\Common\SelectInterface;
use CCMBenchmark\Ting\Repository\HydratorSingleObject;
use CCMBenchmark\Ting\Repository\Metadata;
use CCMBenchmark\Ting\Repository\MetadataInitializer;
Expand All @@ -16,6 +17,10 @@

class UserRepository extends Repository implements MetadataInitializer, UserProviderInterface
{
const USER_TYPE_PHYSICAL = 0;
const USER_TYPE_COMPANY = 1;
const USER_TYPE_ALL = 2;

public function loadUserByUsername($username)
{
$user = $this->getOneBy(['username' => $username]);
Expand All @@ -26,30 +31,89 @@ public function loadUserByUsername($username)
}

/**
* Retrieve all "physical" users by the date of end of membership.
* @return SelectInterface
*/
private function getQueryBuilderWithSubscriptions()
{
/**
* @var $queryBuilder SelectInterface
*/
$queryBuilder = $this->getQueryBuilder(self::QUERY_SELECT);
$queryBuilder
->cols(['app.`id`', 'app.`login`', 'app.`prenom`', 'app.`nom`', 'app.`email`'])
->from('afup_personnes_physiques app')
->join('LEFT', 'afup_personnes_morales apm', 'apm.id = app.id_personne_morale')
->join('LEFT', 'afup_cotisations ac', 'ac.type_personne = IF(apm.id IS NULL, 0, 1) AND ac.id_personne = IFNULL(apm.id, app.id)')
->groupBy(['app.`id`'])
;

return $queryBuilder;
}

/**
* Retrieve all users by the date of end of membership.
*
* @param int $userType one of self::USER_TYPE_*
* @return \CCMBenchmark\Ting\Repository\CollectionInterface
*/
public function getActiveMembers($userType = self::USER_TYPE_PHYSICAL)
{
$today = new \DateTimeImmutable();
$queryBuilder = $this->getQueryBuilderWithSubscriptions();
$queryBuilder
->where('app.`etat` = :status')
->having('MAX(ac.`date_fin`) > :start ')
;

if ($userType === self::USER_TYPE_PHYSICAL) {
$queryBuilder->where('id_personne_morale = 0');
} elseif ($userType === self::USER_TYPE_COMPANY) {
$queryBuilder->where('id_personne_morale <> 0');
} elseif ($userType !== self::USER_TYPE_ALL) {
throw new \UnexpectedValueException(sprintf('Unknown user type "%s"', $userType));
}

return $this
->getPreparedQuery($queryBuilder->getStatement())
->setParams([
'start' => $today->format('U'),
'status' => User::STATUS_ACTIVE
])
->query($this->getCollection(new HydratorSingleObject()));
}

/**
* Retrieve all users by the date of end of membership.
*
* @param \DateTimeImmutable $endOfSubscription
* @param int $userType one of self::USER_TYPE_*
* @return \CCMBenchmark\Ting\Repository\CollectionInterface
*/
public function getUsersByEndOfMembership(\DateTimeImmutable $endOfSubscription)
public function getUsersByEndOfMembership(\DateTimeImmutable $endOfSubscription, $userType = self::USER_TYPE_PHYSICAL)
{
$startOfDay = $endOfSubscription->setTime(0, 0, 0);
$endOfDay = $endOfSubscription->setTime(23, 59, 59);

$queryBuilder = $this->getQueryBuilderWithSubscriptions();
$queryBuilder
->where('app.`etat` = :status')
->having('MAX(ac.`date_fin`) BETWEEN :start AND :end')
;

if ($userType === self::USER_TYPE_PHYSICAL) {
$queryBuilder->where('id_personne_morale = 0');
} elseif ($userType === self::USER_TYPE_COMPANY) {
$queryBuilder->where('id_personne_morale <> 0');
} elseif ($userType !== self::USER_TYPE_ALL) {
throw new \UnexpectedValueException(sprintf('Unknown user type "%s"', $userType));
}

return $this
->getQuery(<<<SQL
SELECT app.`id`, app.`login`, app.`prenom`, app.`nom`, app.`email`
FROM `afup_personnes_physiques` app
LEFT JOIN `afup_cotisations` ac ON ac.type_personne = :type AND ac.id_personne = app.id
WHERE id_personne_morale = 0
GROUP BY app.`id`
HAVING MAX(ac.`date_fin`) BETWEEN :start AND :end
SQL
)
->getPreparedQuery($queryBuilder->getStatement())
->setParams([
'start' => $startOfDay->format('U'),
'end' => $endOfDay->format('U'),
'type' => 0
'status' => User::STATUS_ACTIVE
])
->query($this->getCollection(new HydratorSingleObject()));
}
Expand Down
2 changes: 1 addition & 1 deletion sources/AppBundle/Command/SubscriptionReminderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($reminders as $name => $details) {
$reminder = $factory->getReminder($details['class']);

$users = $repository->getUsersByEndOfMembership($details['date']);
$users = $repository->getUsersByEndOfMembership($details['date'], UserRepository::USER_TYPE_PHYSICAL);
$output->writeln(sprintf('%s (%s)', $name, $details['date']->format('d/m/Y')));
$output->writeln(sprintf('<info>%s membres</info>', $users->count()));
foreach ($users as $user) {
Expand Down
Loading

0 comments on commit b1e744d

Please sign in to comment.