orderiom-api-package 0.4.90 → 0.4.91
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/common.js +41 -16
- package/src/modules/auth.js +4 -22
package/package.json
CHANGED
package/src/common.js
CHANGED
|
@@ -137,26 +137,50 @@ let isFetchingToken = false;
|
|
|
137
137
|
const reqBuffer = []; // List of axios requests waiting for the fetch of public token (Each item is a function)
|
|
138
138
|
const getTokenAPI = "api/oauth/token"; // API to get public token needs
|
|
139
139
|
|
|
140
|
-
const
|
|
140
|
+
export const unassignBasket = (restaurantId = null) => {
|
|
141
|
+
restaurantId = restaurantId || restaurantIdEnv();
|
|
142
|
+
let baskets = JSON.parse(localStorage.getItem("basket"));
|
|
143
|
+
|
|
144
|
+
if(!Array.isArray(baskets) || !baskets.length) return;
|
|
145
|
+
|
|
146
|
+
// Find all basketId where restaurantId does not exist, or find all basketId linked to a specific restaurantId if it exists.
|
|
147
|
+
const basketsIdToUnassign = baskets.filter(basket => (
|
|
148
|
+
basket.basketId &&
|
|
149
|
+
(
|
|
150
|
+
!Number(restaurantId) ||
|
|
151
|
+
(
|
|
152
|
+
Number(restaurantId) === Number(basket.restaurantId)
|
|
153
|
+
)
|
|
154
|
+
)
|
|
155
|
+
)).map(basket => basket.basketId)
|
|
156
|
+
|
|
157
|
+
// unassign all filtered baskets from logged-in user (so these baskets would not need a private token anymore)
|
|
158
|
+
return axios.post(`api/basket/unassigned-basket`, {
|
|
159
|
+
basketId: basketsIdToUnassign
|
|
160
|
+
})
|
|
161
|
+
.then(res => res)
|
|
162
|
+
.catch(commonErrorCallback());
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const getAuthToken = async (hadPrivateToken = false) => {
|
|
141
166
|
isFetchingToken = true;
|
|
142
|
-
|
|
143
|
-
.post(getTokenAPI, {
|
|
167
|
+
try {
|
|
168
|
+
const getTokenRes = await axios.post(getTokenAPI, {
|
|
144
169
|
grant_type: process.env.VUE_APP_GRANT_TYPE || window.dynamicData.VUE_APP_GRANT_TYPE,
|
|
145
170
|
client_id: process.env.VUE_APP_CLIENT_ID || window.dynamicData.VUE_APP_CLIENT_ID,
|
|
146
171
|
client_secret: process.env.VUE_APP_CLIENT_SECRET || window.dynamicData.VUE_APP_CLIENT_SECRET
|
|
147
172
|
})
|
|
148
|
-
.
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
.
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
});
|
|
173
|
+
window.localStorage.setItem("publicToken", getTokenRes.data.data.access_token);
|
|
174
|
+
|
|
175
|
+
if(hadPrivateToken) await unassignBasket();
|
|
176
|
+
} catch (error) {
|
|
177
|
+
return commonErrorCallback()(error)
|
|
178
|
+
} finally {
|
|
179
|
+
while (reqBuffer.length) {
|
|
180
|
+
reqBuffer.shift()(); // First in First out
|
|
181
|
+
}
|
|
182
|
+
isFetchingToken = false;
|
|
183
|
+
}
|
|
160
184
|
}
|
|
161
185
|
|
|
162
186
|
axios.defaults.baseURL = process.env.VUE_APP_BASE_API_URL || window.dynamicData.VUE_APP_BASE_API_URL;
|
|
@@ -180,6 +204,7 @@ axios.interceptors.request.use(config => {
|
|
|
180
204
|
|
|
181
205
|
// if the token is expired or does not exist, wait for public token to be fetched
|
|
182
206
|
return new Promise((resolve, reject) => {
|
|
207
|
+
const hadPrivateToken = !!localStorage.getItem("privateToken");
|
|
183
208
|
// remove invalid or expired tokens
|
|
184
209
|
token = null;
|
|
185
210
|
localStorage.removeItem("privateToken");
|
|
@@ -198,7 +223,7 @@ axios.interceptors.request.use(config => {
|
|
|
198
223
|
});
|
|
199
224
|
|
|
200
225
|
// Start to fetch token if it is not already fetching
|
|
201
|
-
if(!isFetchingToken) getAuthToken();
|
|
226
|
+
if(!isFetchingToken) getAuthToken(hadPrivateToken);
|
|
202
227
|
})
|
|
203
228
|
}, error => {
|
|
204
229
|
return Promise.reject(error);
|
package/src/modules/auth.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {commonErrorCallback, $http, restaurantIdEnv } from '../common';
|
|
1
|
+
import {commonErrorCallback, $http, restaurantIdEnv, unassignBasket } from '../common';
|
|
2
2
|
|
|
3
3
|
const state = {
|
|
4
4
|
privateToken: null,
|
|
@@ -178,6 +178,7 @@ const actions = {
|
|
|
178
178
|
const foundBasket = basket.find(item => Number(item.restaurantId) === Number(restaurantId || restaurantIdEnv()))
|
|
179
179
|
const basketId = foundBasket ? foundBasket.basketId : undefined;
|
|
180
180
|
|
|
181
|
+
// assign basket id to logged-in use (so basket actions need a private token now)
|
|
181
182
|
basketId && $http
|
|
182
183
|
.post(`api/basket/assign-basket`, { basketId, restaurantId })
|
|
183
184
|
.then(() => {
|
|
@@ -187,27 +188,8 @@ const actions = {
|
|
|
187
188
|
commonErrorCallback()
|
|
188
189
|
);
|
|
189
190
|
},
|
|
190
|
-
unassignBasket(
|
|
191
|
-
|
|
192
|
-
if(!basket || !Array.isArray(basket)) return;
|
|
193
|
-
let baskets = [];
|
|
194
|
-
|
|
195
|
-
if (restaurantId || restaurantIdEnv()) {
|
|
196
|
-
const foundBasket = basket.find(item => Number(item.restaurantId) === Number(restaurantId || restaurantIdEnv()))
|
|
197
|
-
const basketId = foundBasket ? foundBasket.basketId : undefined;
|
|
198
|
-
|
|
199
|
-
if(basketId) baskets.push(basketId);
|
|
200
|
-
} else {
|
|
201
|
-
baskets = basket.map(m => m.basketId)
|
|
202
|
-
}
|
|
203
|
-
for (let i = 0; i < baskets.length; i++) {
|
|
204
|
-
$http
|
|
205
|
-
.post(`api/basket/unassigned-basket`, { basketId: baskets[i] })
|
|
206
|
-
.then(res => res)
|
|
207
|
-
.catch(
|
|
208
|
-
commonErrorCallback()
|
|
209
|
-
);
|
|
210
|
-
}
|
|
191
|
+
unassignBasket(_, restaurantId) {
|
|
192
|
+
return unassignBasket(restaurantId);
|
|
211
193
|
},
|
|
212
194
|
restaurantInfo({ commit }, restaurantId) {
|
|
213
195
|
return $http
|