addio-admin-sdk 1.7.141 → 1.7.143

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.
@@ -108,6 +108,8 @@ const IExternalService_1 = require("../../Interfaces/Space/IExternalService");
108
108
  const utils_1 = require("../../services/products/utils");
109
109
  const Menu_1 = __importDefault(require("../../lib/Menu"));
110
110
  const Note_1 = __importDefault(require("../Note"));
111
+ const CashDrawers_1 = require("../../Interfaces/CashDrawers");
112
+ const CashDrawerShift_1 = __importDefault(require("../CashDrawerShift"));
111
113
  //#endregion
112
114
  /**
113
115
  * Class Space
@@ -5069,8 +5071,9 @@ class Space {
5069
5071
  }
5070
5072
  return credits;
5071
5073
  };
5072
- //#
5073
- //#region MENUS
5074
+ // #endregion
5075
+ // -------------------------------------------------------------------------------
5076
+ // #region MENUS
5074
5077
  /**
5075
5078
  * Retourne tous les menus du space
5076
5079
  */
@@ -5122,6 +5125,148 @@ class Space {
5122
5125
  return results;
5123
5126
  };
5124
5127
  //#endregion
5128
+ // -------------------------------------------------------------------------------
5129
+ // #region CASH DRAWERS
5130
+ /**
5131
+ * Utils to check and return current space cash drawers. Throws an error if space does not has corresponding feature set, or if id provided and same id not found in current cash drawers.
5132
+ * @param idToCheck Optionnal. Cart drawer id to check in current space data.
5133
+ */
5134
+ this._checkCurrentSpaceDataForCashDrawer = (idToCheck) => {
5135
+ let currentCashDrawers = [];
5136
+ const spaceHasCashDrawersActive = this.options.checkIfOptionIsSet('features.can_manage_cash_drawers');
5137
+ if (!spaceHasCashDrawersActive)
5138
+ throw new Error('Cash drawers not active for space!');
5139
+ if (typeof idToCheck == 'string') {
5140
+ currentCashDrawers = this._space.cash_drawers || [];
5141
+ const sameDrawerInData = currentCashDrawers.some((d) => d.id == idToCheck);
5142
+ if (!sameDrawerInData)
5143
+ throw new Error(`Cash drawer with id ${idToCheck} not found in space !`);
5144
+ }
5145
+ return currentCashDrawers;
5146
+ };
5147
+ /**
5148
+ * Generates new cash drawer from data provided, and saves to cash_drawer array in Space data.
5149
+ * @param data.nickname Nickname for cash drawer (required)
5150
+ * @param data.drawerNumber Optionnal. Number for given cash drawer
5151
+ * @throws If feature for cash drawers is not set in space_options
5152
+ * @returns {ICashDrawer[]} Space's cash drawer array with newly added drawer at last index
5153
+ */
5154
+ this._createNewCashDrawer = async (data) => {
5155
+ this._checkCurrentSpaceDataForCashDrawer();
5156
+ const cashDrawerData = Object.assign(Object.assign({}, (0, CashDrawers_1.getDefaultCashDrawer)()), { nickname: data.nickname, drawer_number: data.drawerNumber });
5157
+ const currentCashDrawersForSpace = this._space.cash_drawers || [];
5158
+ await this.save({ cash_drawers: [...currentCashDrawersForSpace, cashDrawerData] });
5159
+ return this._space.cash_drawers;
5160
+ };
5161
+ /**
5162
+ * Updates existing cash drawer from data provided, and saves to cash_drawer array in Space data.
5163
+ * @param id Id for the cash drawer to update
5164
+ * @param data.nickname Nickname for cash drawer (required)
5165
+ * @param data.drawerNumber Optionnal. Number for given cash drawer
5166
+ * @throws If feature for cash drawers is not set in space_options
5167
+ * @throws If same drawer id not found in current space data cash drawers.
5168
+ * @returns Updated space cash drawer array
5169
+ */
5170
+ this._editCashDrawer = async (id, data) => {
5171
+ const currentCashDrawersForSpace = this._checkCurrentSpaceDataForCashDrawer(id);
5172
+ await this.save({
5173
+ cash_drawers: currentCashDrawersForSpace.map((d) => {
5174
+ if (d.id != id)
5175
+ return d;
5176
+ return Object.assign(Object.assign({}, d), { nickname: data.nickname, drawer_number: data.drawerNumber });
5177
+ })
5178
+ });
5179
+ return this._space.cash_drawers;
5180
+ };
5181
+ /**
5182
+ * Toggles active property for existing cash drawer, and saves to cash_drawer array in Space data.
5183
+ * @param id Id for the cash drawer to toggle
5184
+ * @throws If feature for cash drawers is not set in space_options
5185
+ * @throws If same drawer id not found in current space data cash drawers.
5186
+ * @returns Updated space cash drawer array
5187
+ */
5188
+ this._toggleCashDrawer = async (id) => {
5189
+ const currentCashDrawersForSpace = this._checkCurrentSpaceDataForCashDrawer(id);
5190
+ await this.save({
5191
+ cash_drawers: currentCashDrawersForSpace.map((d) => {
5192
+ if (d.id != id)
5193
+ return d;
5194
+ return Object.assign(Object.assign({}, d), { active: !d.active });
5195
+ })
5196
+ });
5197
+ return this._space.cash_drawers;
5198
+ };
5199
+ /**
5200
+ * Checks if an open shift is found for a corresponding cash drawer id.
5201
+ * @param id The cash drawer id
5202
+ * @returns If open shift found for drawer.
5203
+ */
5204
+ this._checkIfOpenShiftExistsForDrawer = async (drawerId) => {
5205
+ const foundShift = await this._getOpenShiftForDrawer(drawerId);
5206
+ return !!foundShift && foundShift.data().is_open;
5207
+ };
5208
+ /**
5209
+ * Fetch corresponding open shift for a given drawer ID.
5210
+ * @param id Id for the cash drawer to search.
5211
+ * @throws If feature for cash drawers is not set in space_options
5212
+ * @throws If same drawer id not found in current space data cash drawers.
5213
+ * @returns Found shift or undefined
5214
+ */
5215
+ this._getOpenShiftForDrawer = async (drawerId) => {
5216
+ this._checkCurrentSpaceDataForCashDrawer(drawerId);
5217
+ const path = DatabaseService_1.default.addToPath(this._space_ref.path, this._space_ref.id + '/cash_drawer_shifts');
5218
+ const collRef = DatabaseService_1.default.asCollectionObject(this._space_ref.path, this._space_ref.id + '/cash_drawer_shifts');
5219
+ const docs = await DatabaseService_1.default.getDocuments(path, {
5220
+ query: [
5221
+ { field: 'cash_drawer_id', operator: '==', value: drawerId },
5222
+ { field: 'is_open', operator: '==', value: true }
5223
+ ]
5224
+ });
5225
+ let filteredDocs = docs
5226
+ .filter((d) => d.exists && !!d.data)
5227
+ .map((d) => new CashDrawerShift_1.default(this, drawerId, Object.assign(Object.assign({}, d.data), { id: d.id }), collRef, this._user));
5228
+ if (filteredDocs.length > 1) {
5229
+ console.log("WARNING - Found more than one shift open for a cash drawer, which shouldn't happen! Will only return value opened most recently.");
5230
+ filteredDocs = (0, orderBy_1.default)(filteredDocs, (d) => d.data().opened_details.date, 'asc');
5231
+ }
5232
+ return filteredDocs[0];
5233
+ };
5234
+ /**
5235
+ * Fetch all cash drawer interaction history entries for specified options. All options are optionnal.
5236
+ * @param options.drawerId Specific cash drawer Id
5237
+ * @param options.shiftId Specific shift Id
5238
+ * @param options.action Specific action type
5239
+ * @param options.userEmail Specific user email that generated interaction
5240
+ * @param options.dates Specific dates to filter results. If options.dates if provided, start date is obligatory, and end date optionnal.
5241
+ * @throws If feature for cash drawers is not set in space_options
5242
+ * @throws If same drawer id not found in current space data cash drawers.
5243
+ * @returns Array of corresponding Interaction history entries, ordered by oldest to newest. If not date provided in options, returns max 100 entries. Else returns all found.
5244
+ */
5245
+ this._getInteractionHistoryForDrawer = async (options = {}) => {
5246
+ this._checkCurrentSpaceDataForCashDrawer(options.drawerId);
5247
+ // Construct search query
5248
+ let queryOptions = [];
5249
+ if (options.drawerId)
5250
+ queryOptions.push({ field: 'cash_drawer_id', operator: '==', value: options.drawerId });
5251
+ if (options.shiftId)
5252
+ queryOptions.push({ field: 'shift_id', operator: '==', value: options.shiftId });
5253
+ if (options.action)
5254
+ queryOptions.push({ field: 'action', operator: '==', value: options.action });
5255
+ if (options.userEmail)
5256
+ queryOptions.push({ field: 'user', operator: '==', value: options.userEmail });
5257
+ if (options.dates) {
5258
+ queryOptions.push({ field: 'date', operator: '>=', value: options.dates.start });
5259
+ if (options.dates.end)
5260
+ queryOptions.push({ field: 'date', operator: '<=', value: options.dates.end });
5261
+ }
5262
+ const path = DatabaseService_1.default.addToPath(this._space_ref.path, this._space_ref.id + '/cash_drawer_interactions');
5263
+ const docs = await DatabaseService_1.default.getDocuments(path, Object.assign({ query: queryOptions, orderBy: 'date', order: 'asc' }, (!options.dates ? { limit: 100 } : {})));
5264
+ const filteredDocs = docs.filter((d) => d.exists && !!d.data).map((d) => d.data);
5265
+ return filteredDocs;
5266
+ };
5267
+ // #endregion
5268
+ // -------------------------------------------------------------------------------
5269
+ // #region PUBLIC GETTER FUNCTIONS
5125
5270
  this.attributes = {
5126
5271
  get: this._getAttributes,
5127
5272
  add: this._addAttribute,
@@ -5508,6 +5653,15 @@ class Space {
5508
5653
  return await this._getAllActiveMenus();
5509
5654
  }
5510
5655
  };
5656
+ this.cashDrawers = {
5657
+ create: this._createNewCashDrawer,
5658
+ edit: this._editCashDrawer,
5659
+ toggle: this._toggleCashDrawer,
5660
+ hasOpenShift: this._checkIfOpenShiftExistsForDrawer,
5661
+ getOpenShift: this._getOpenShiftForDrawer,
5662
+ getInteractionHistory: this._getInteractionHistoryForDrawer
5663
+ };
5664
+ // #endregion
5511
5665
  /**
5512
5666
  * Get all space properties
5513
5667
  */
@@ -5529,6 +5683,7 @@ class Space {
5529
5683
  return !!this._space.multipromo_aware ? this._space.multipromo_aware : false;
5530
5684
  };
5531
5685
  //#endregion
5686
+ // -------------------------------------------------------------------------------
5532
5687
  //#region NOTES
5533
5688
  this._getNotes = async (parentId, type) => {
5534
5689
  try {