1+ <?php
2+ /**
3+ * Copyright 2020 Simone Sestito
4+ * This file is part of Shops Queue.
5+ *
6+ * Shops Queue is free software: you can redistribute it and/or modify
7+ * it under the terms of the GNU Affero General Public License as published by
8+ * the Free Software Foundation, either version 3 of the License, or
9+ * (at your option) any later version.
10+ *
11+ * Shops Queue is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+ * GNU Affero General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU Affero General Public License
17+ * along with Shops Queue. If not, see <http://www.gnu.org/licenses/>.
18+ */
19+
20+ class ShoppingListController extends BaseController {
21+ private $ shoppingListDao ;
22+ private $ productDao ;
23+ private $ fcmService ;
24+
25+ public function __construct (ShoppingListDao $ shoppingListDao , ProductDao $ productDao , FcmService $ fcmService ) {
26+ $ this ->shoppingListDao = $ shoppingListDao ;
27+ $ this ->productDao = $ productDao ;
28+ $ this ->fcmService = $ fcmService ;
29+ $ this ->registerRoute ('/users/me/lists ' , 'GET ' , 'USER ' , 'getMyLists ' );
30+ $ this ->registerRoute ('/shops/me/lists ' , 'GET ' , 'OWNER ' , 'getMyShopLists ' );
31+ $ this ->registerRoute ('/lists ' , 'POST ' , 'USER ' , 'addList ' );
32+ $ this ->registerRoute ('/lists/:id ' , 'DELETE ' , '* ' , 'deleteList ' );
33+ $ this ->registerRoute ('/lists/:id ' , 'POST ' , 'OWNER ' , 'prepareList ' );
34+ }
35+
36+ /**
37+ * Get the lists of the current user
38+ * @return ShoppingList[]
39+ */
40+ public function getMyLists () {
41+ $ userId = AuthService::getAuthContext ()['id ' ];
42+ $ lists = $ this ->shoppingListDao ->getListsByUserId ($ userId );
43+ return array_map (function ($ list ) {
44+ return new ShoppingList ($ list );
45+ }, $ lists );
46+ }
47+
48+ /**
49+ * Get the lists of the current shop owner
50+ * @return ShoppingList[]
51+ */
52+ public function getMyShopLists () {
53+ $ shopId = AuthService::getAuthContext ()['shopId ' ];
54+ $ lists = $ this ->shoppingListDao ->getListsByShopId ($ shopId );
55+ return array_map (function ($ list ) {
56+ return new ShoppingList ($ list );
57+ }, $ lists );
58+ }
59+
60+ /**
61+ * Add a new list
62+ * @param NewShoppingList $newShoppingList
63+ * @return ShoppingList
64+ * @throws AppHttpException
65+ */
66+ public function addList (NewShoppingList $ newShoppingList ) {
67+ // Check that every product is sold by the same shop
68+ $ products = $ this ->productDao ->getProductsByIds ($ newShoppingList ->productIds );
69+ if (count ($ products ) != count ($ newShoppingList ->productIds )) {
70+ // Some product IDs aren't known
71+ throw new AppHttpException (HTTP_NOT_FOUND );
72+ }
73+ $ shopId = $ products [0 ]['shopId ' ];
74+ foreach ($ products as $ product ) {
75+ if ($ product ['shopId ' ] !== $ shopId )
76+ throw new AppHttpException (HTTP_BAD_REQUEST );
77+ }
78+
79+ $ userId = AuthService::getAuthContext ()['id ' ];
80+ $ id = $ this ->shoppingListDao ->addUserShoppingList ($ userId , $ newShoppingList );
81+ $ entity = $ this ->shoppingListDao ->getListById ($ id );
82+ return new ShoppingList ($ entity );
83+ }
84+
85+ /**
86+ * Delete a list created by the current user
87+ * @param int $listId
88+ * @throws AppHttpException
89+ */
90+ public function deleteList (int $ listId ) {
91+ $ authContext = AuthService::getAuthContext ();
92+ $ userId = $ authContext ['id ' ];
93+ $ userRole = $ authContext ['role ' ];
94+ $ userShopId = $ authContext ['shopId ' ];
95+
96+ $ entity = $ this ->shoppingListDao ->getListById ($ listId );
97+ if ($ entity == null )
98+ throw new AppHttpException (HTTP_NOT_FOUND );
99+ $ shoppingList = new ShoppingList ($ entity );
100+
101+ if ($ shoppingList ->userId == $ userId ) {
102+ $ this ->shoppingListDao ->deleteShoppingList ($ listId );
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+ );
111+ }
112+ $ this ->shoppingListDao ->deleteShoppingList ($ listId );
113+ } else {
114+ throw new AppHttpException (HTTP_NOT_AUTHORIZED );
115+ }
116+ }
117+
118+ /**
119+ * Set a shopping list as ready to be retired
120+ * @param int $listId
121+ * @return ShoppingList
122+ * @throws AppHttpException
123+ */
124+ public function prepareList (int $ listId ) {
125+ $ this ->shoppingListDao ->prepareShoppingList ($ listId );
126+
127+ $ entity = $ this ->shoppingListDao ->getListById ($ listId );
128+ if ($ entity == null )
129+ throw new AppHttpException (HTTP_NOT_FOUND );
130+
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+ );
141+
142+ return new ShoppingList ($ entity );
143+ }
144+ }
145+
146+ onInit (function () {
147+ registerController (ShoppingListController::class);
148+ });
0 commit comments