2020class 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 }
0 commit comments