-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #588 from bakaphp/unsubscribe-0.2
[0.2] Unsubscribe
- Loading branch information
Showing
8 changed files
with
279 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Canvas\Models; | ||
|
||
use Phalcon\Di; | ||
|
||
class NotificationsUnsubscribe extends AbstractModel | ||
{ | ||
public ?int $users_id = 0; | ||
public ?int $companies_id = 0; | ||
public ?int $apps_id = 0; | ||
public ?int $system_modules_id = 0; | ||
public ?int $notification_type_id = 0; | ||
public ?string $email = null; | ||
|
||
/** | ||
* Initialize method for model. | ||
*/ | ||
public function initialize() | ||
{ | ||
$this->setSource('notifications_unsubscribe'); | ||
|
||
$this->belongsTo( | ||
'users_id', | ||
'Canvas\Models\Users', | ||
'id', | ||
['alias' => 'user'] | ||
); | ||
|
||
$this->belongsTo( | ||
'notification_type_id', | ||
'Canvas\Models\NotificationType', | ||
'id', | ||
['alias' => 'type'] | ||
); | ||
} | ||
|
||
/** | ||
* get NotificationsUnsubscribe by NotificationType | ||
* @param Users $user | ||
* @param int $notificationTypeId | ||
* @return NotificationsUnsubscribe | ||
*/ | ||
public static function getByNotificationType(Users $user, int $notificationTypeId) : ?NotificationsUnsubscribe | ||
{ | ||
return NotificationsUnsubscribe::findFirst([ | ||
'conditions' => 'users_id = ?0 AND companies_id = ?1 AND apps_id = ?2 AND \notification_type_id = ?3 AND is_deleted = 0', | ||
'bind' => [ | ||
0 => $user->getId(), | ||
1 => $user->currentCompanyId(), | ||
2 => Di::getDefault()->getApp()->getId(), | ||
3 => $notificationTypeId | ||
] | ||
]); | ||
} | ||
|
||
/** | ||
* Verify that the user is unsubscribed | ||
* @param Users $user | ||
* @param int $notificationType | ||
* @return bool | ||
*/ | ||
public static function isUnsubscribe(Users $user, int $notificationTypeId) : bool | ||
{ | ||
//-1 means it is out of all lists | ||
$userNotification = NotificationsUnsubscribe::getByNotificationType($user, -1); | ||
|
||
if (!$userNotification) { | ||
$userNotification = NotificationsUnsubscribe::getByNotificationType($user, $notificationTypeId); | ||
} | ||
|
||
return $userNotification ? true : false; | ||
} | ||
|
||
/** | ||
* unsubscribe user for NotificationType | ||
* @param Users $user | ||
* @param int $notificationTypeId | ||
* @param int $systemModulesId | ||
* @return NotificationsUnsubscribe | ||
*/ | ||
public static function unsubscribe(Users $user, int $notificationTypeId, int $systemModulesId) : NotificationsUnsubscribe | ||
{ | ||
$userNotification = NotificationsUnsubscribe::getByNotificationType($user, $notificationTypeId); | ||
|
||
if (!$userNotification) { | ||
$userNotification = new NotificationsUnsubscribe(); | ||
$userNotification->users_id = $user->getId(); | ||
$userNotification->companies_id = $user->currentCompanyId(); | ||
$userNotification->apps_id = Di::getDefault()->getApp()->getId(); | ||
$userNotification->notification_type_id = $notificationTypeId; | ||
$userNotification->system_modules_id = $systemModulesId; | ||
$userNotification->email = $user->getEmail(); | ||
} | ||
|
||
$userNotification->is_deleted = 0; | ||
$userNotification->saveOrFail(); | ||
return $userNotification; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
storage/db/migrations/20210114193743_notifications_unsubscribe.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
use Phinx\Db\Adapter\MysqlAdapter; | ||
|
||
class NotificationsUnsubscribe extends Phinx\Migration\AbstractMigration | ||
{ | ||
public function change() | ||
{ | ||
$this->table('notifications_unsubscribe', [ | ||
'id' => false, | ||
'primary_key' => ['id'], | ||
'engine' => 'InnoDB', | ||
'encoding' => 'latin1', | ||
'collation' => 'latin1_swedish_ci', | ||
'comment' => '', | ||
'row_format' => 'DYNAMIC', | ||
]) | ||
->addColumn('id', 'integer', [ | ||
'null' => false, | ||
'limit' => MysqlAdapter::INT_REGULAR, | ||
'identity' => 'enable', | ||
]) | ||
->addColumn('users_id', 'integer', [ | ||
'null' => false, | ||
'limit' => MysqlAdapter::INT_REGULAR, | ||
'after' => 'id', | ||
]) | ||
->addColumn('companies_id', 'integer', [ | ||
'null' => false, | ||
'limit' => MysqlAdapter::INT_REGULAR, | ||
'after' => 'users_id', | ||
]) | ||
->addColumn('apps_id', 'integer', [ | ||
'null' => false, | ||
'limit' => MysqlAdapter::INT_REGULAR, | ||
'after' => 'companies_id', | ||
]) | ||
->addColumn('notification_type_id', 'integer', [ | ||
'null' => false, | ||
'limit' => MysqlAdapter::INT_REGULAR, | ||
'after' => 'apps_id', | ||
]) | ||
->addColumn('email', 'string', [ | ||
'null' => false, | ||
'limit' => 255, | ||
'collation' => 'latin1_swedish_ci', | ||
'encoding' => 'latin1', | ||
'after' => 'notification_type_id', | ||
]) | ||
->addColumn('created_at', 'datetime', [ | ||
'null' => false, | ||
'after' => 'email', | ||
]) | ||
->addColumn('updated_at', 'datetime', [ | ||
'null' => true, | ||
'default' => null, | ||
'after' => 'created_at', | ||
]) | ||
->addColumn('is_deleted', 'integer', [ | ||
'null' => false, | ||
'limit' => MysqlAdapter::INT_REGULAR, | ||
'after' => 'updated_at', | ||
]) | ||
->addColumn('system_modules_id', 'integer', [ | ||
'null' => false, | ||
'limit' => MysqlAdapter::INT_REGULAR, | ||
'after' => 'is_deleted', | ||
]) | ||
->create(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Canvas\Tests\api; | ||
|
||
use ApiTester; | ||
|
||
class UsersCest | ||
{ | ||
/** | ||
* unsubscribe from notification | ||
* | ||
* @param ApiTester | ||
* | ||
* @return void | ||
*/ | ||
public function unsubscribe(ApiTester $I) : void | ||
{ | ||
$userData = $I->apiLogin(); | ||
$userName = $I->faker()->firstname; | ||
|
||
$I->haveHttpHeader('Authorization', $userData->token); | ||
$I->sendPost('/v1/users/0/unsubscribe', [ | ||
'notification_types' => [ | ||
-1 | ||
] | ||
]); | ||
|
||
$I->seeResponseIsSuccessful(); | ||
$response = $I->grabResponse(); | ||
$data = json_decode($response, true); | ||
|
||
$I->assertTrue(isset($data[0]['notification_type_id'])); | ||
$I->assertTrue($data[0]['notification_type_id'] == -1); | ||
} | ||
} |