Skip to content

Commit b3eaf3d

Browse files
committed
Send push notifications about user's orders
1 parent 14b7ad3 commit b3eaf3d

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/controller/ShoppingListController.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
class ShoppingListController extends BaseController {
2121
private $shoppingListDao;
2222
private $productDao;
23+
private $fcmService;
2324

24-
public function __construct(ShoppingListDao $shoppingListDao, ProductDao $productDao) {
25+
public function __construct(ShoppingListDao $shoppingListDao, ProductDao $productDao, FcmService $fcmService) {
2526
$this->shoppingListDao = $shoppingListDao;
2627
$this->productDao = $productDao;
28+
$this->fcmService = $fcmService;
2729
$this->registerRoute('/users/me/lists', 'GET', 'USER', 'getMyLists');
2830
$this->registerRoute('/shops/me/lists', 'GET', 'OWNER', 'getMyShopLists');
2931
$this->registerRoute('/lists', 'POST', 'USER', 'addList');
@@ -91,15 +93,21 @@ public function deleteList(int $listId) {
9193
$userRole = $authContext['role'];
9294
$userShopId = $authContext['shopId'];
9395

94-
$shoppingList = $this->shoppingListDao->getListById($listId);
95-
if ($shoppingList == null)
96+
$entity = $this->shoppingListDao->getListById($listId);
97+
if ($entity == null)
9698
throw new AppHttpException(HTTP_NOT_FOUND);
99+
$shoppingList = new ShoppingList($entity);
97100

98-
if ($shoppingList[0]['userId'] == $userId) {
101+
if ($shoppingList->userId == $userId) {
99102
$this->shoppingListDao->deleteShoppingList($listId);
100-
} elseif ($userRole == 'OWNER' && $shoppingList[0]['shopId'] == $userShopId) {
101-
if (!$shoppingList[0]['isReady']) {
102-
// TODO: push notification
103+
} elseif ($userRole == 'OWNER' && $shoppingList->shop->id == $userShopId) {
104+
if (!$shoppingList->isReady) {
105+
// Send push notification
106+
$this->fcmService->sendPayloadToUser(
107+
$shoppingList->userId,
108+
FCM_TYPE_ORDER_CANCELLED,
109+
$shoppingList
110+
);
103111
}
104112
$this->shoppingListDao->deleteShoppingList($listId);
105113
} else {
@@ -120,7 +128,16 @@ public function prepareList(int $listId) {
120128
if ($entity == null)
121129
throw new AppHttpException(HTTP_NOT_FOUND);
122130

123-
// TODO: Push notification
131+
$shoppingList = new ShoppingList($entity);
132+
if ($shoppingList->shop->id != AuthService::getAuthContext()['shopId'])
133+
throw new AppHttpException(HTTP_NOT_AUTHORIZED);
134+
135+
// Send push notification
136+
$this->fcmService->sendPayloadToUser(
137+
$shoppingList->userId,
138+
FCM_TYPE_ORDER_READY,
139+
$shoppingList
140+
);
124141

125142
return new ShoppingList($entity);
126143
}

src/service/FcmService.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
define('FCM_TYPE_BOOKING_CANCELLED', 'booking-cancelled');
2121
define('FCM_TYPE_QUEUE_NOTICE', 'queue-notice');
22+
define('FCM_TYPE_ORDER_READY', 'order-ready');
23+
define('FCM_TYPE_ORDER_CANCELLED', 'order-cancelled');
2224

2325
class FcmService {
2426
private $fcmDao;
@@ -48,13 +50,13 @@ public function sendPayloadToUser(int $userId, string $messageType, $messageData
4850
* @param string $messageType The type of the message (e.g.: booking-cancelled)
4951
* @param mixed $messageData Data to send
5052
*/
51-
public function sendPayloadOrDeleteToken(string $token, string $messageType, $messageData) {
53+
private function sendPayloadOrDeleteToken(string $token, string $messageType, $messageData) {
5254
if (FCM_SERVER_KEY === '') {
5355
throw new RuntimeException('FCM server key not found in env variables');
5456
}
5557

5658
$url = 'https://fcm.googleapis.com/fcm/send';
57-
$headers = [ 'Authorization: key=' . FCM_SERVER_KEY ];
59+
$headers = ['Authorization: key=' . FCM_SERVER_KEY];
5860
$body = [
5961
'to' => $token,
6062
'data' => [

0 commit comments

Comments
 (0)