orderiom-api-package 0.4.90 → 0.4.92

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orderiom-api-package",
3
- "version": "0.4.90",
3
+ "version": "0.4.92",
4
4
  "description": "This package will install all necessary API calls for every orderiom restaurant",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/src/common.js CHANGED
@@ -137,26 +137,52 @@ 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 getAuthToken = () => {
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 Promise.all(
159
+ basketsIdToUnassign.map(basketId =>
160
+ axios.post(`api/basket/unassigned-basket`, {
161
+ basketId
162
+ })
163
+ )
164
+ ).catch(commonErrorCallback())
165
+ }
166
+
167
+ const getAuthToken = async (hadPrivateToken = false) => {
141
168
  isFetchingToken = true;
142
- return axios
143
- .post(getTokenAPI, {
169
+ try {
170
+ const getTokenRes = await axios.post(getTokenAPI, {
144
171
  grant_type: process.env.VUE_APP_GRANT_TYPE || window.dynamicData.VUE_APP_GRANT_TYPE,
145
172
  client_id: process.env.VUE_APP_CLIENT_ID || window.dynamicData.VUE_APP_CLIENT_ID,
146
173
  client_secret: process.env.VUE_APP_CLIENT_SECRET || window.dynamicData.VUE_APP_CLIENT_SECRET
147
174
  })
148
- .then(res => {
149
- window.localStorage.setItem("publicToken", res.data.data.access_token);
150
- })
151
- .catch(
152
- commonErrorCallback()
153
- )
154
- .finally(() => {
155
- while (reqBuffer.length) {
156
- reqBuffer.shift()(); // First in First out
157
- }
158
- isFetchingToken = false;
159
- });
175
+ window.localStorage.setItem("publicToken", getTokenRes.data.data.access_token);
176
+
177
+ if(hadPrivateToken) await unassignBasket();
178
+ } catch (error) {
179
+ return commonErrorCallback()(error)
180
+ } finally {
181
+ while (reqBuffer.length) {
182
+ reqBuffer.shift()(); // First in First out
183
+ }
184
+ isFetchingToken = false;
185
+ }
160
186
  }
161
187
 
162
188
  axios.defaults.baseURL = process.env.VUE_APP_BASE_API_URL || window.dynamicData.VUE_APP_BASE_API_URL;
@@ -180,6 +206,7 @@ axios.interceptors.request.use(config => {
180
206
 
181
207
  // if the token is expired or does not exist, wait for public token to be fetched
182
208
  return new Promise((resolve, reject) => {
209
+ const hadPrivateToken = !!localStorage.getItem("privateToken");
183
210
  // remove invalid or expired tokens
184
211
  token = null;
185
212
  localStorage.removeItem("privateToken");
@@ -198,7 +225,7 @@ axios.interceptors.request.use(config => {
198
225
  });
199
226
 
200
227
  // Start to fetch token if it is not already fetching
201
- if(!isFetchingToken) getAuthToken();
228
+ if(!isFetchingToken) getAuthToken(hadPrivateToken);
202
229
  })
203
230
  }, error => {
204
231
  return Promise.reject(error);
@@ -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({ }, restaurantId) {
191
- const basket = JSON.parse(localStorage.getItem("basket"));
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