@infrab4a/connect 0.14.4 → 0.14.5-beta.1

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.
@@ -2081,39 +2081,79 @@
2081
2081
  CategoryFirestoreRepository.prototype.getCategoriesForHome = function (categoryIds, limit) {
2082
2082
  if (limit === void 0) { limit = 4; }
2083
2083
  return __awaiter(this, void 0, void 0, function () {
2084
- var categorySnap, categories, homeSections, categories_1, categories_1_1, category, products, e_1_1;
2085
- var e_1, _a;
2086
- return __generator(this, function (_b) {
2087
- switch (_b.label) {
2084
+ var categorySnap, categories, homeSections;
2085
+ var _this = this;
2086
+ return __generator(this, function (_a) {
2087
+ switch (_a.label) {
2088
2088
  case 0: return [4 /*yield*/, this.collection(this.collectionName)
2089
2089
  .where('id', 'in', categoryIds)
2090
2090
  .where('published', '==', true)
2091
2091
  .get()];
2092
2092
  case 1:
2093
- categorySnap = _b.sent();
2093
+ categorySnap = _a.sent();
2094
2094
  if (categorySnap.empty)
2095
2095
  throw new NotFoundError('Categories not found');
2096
2096
  categories = categorySnap.docs.map(function (doc) { return doc.data(); });
2097
- homeSections = [];
2097
+ return [4 /*yield*/, Promise.all(categories.map(function (category) { return __awaiter(_this, void 0, void 0, function () {
2098
+ var _a;
2099
+ return __generator(this, function (_b) {
2100
+ switch (_b.label) {
2101
+ case 0:
2102
+ _a = {
2103
+ category: category
2104
+ };
2105
+ return [4 /*yield*/, this.mountCategory(category, { limit: limit, hasStock: true })];
2106
+ case 1: return [2 /*return*/, (_a.products = _b.sent(),
2107
+ _a)];
2108
+ }
2109
+ });
2110
+ }); }))];
2111
+ case 2:
2112
+ homeSections = _a.sent();
2113
+ return [2 /*return*/, homeSections];
2114
+ }
2115
+ });
2116
+ });
2117
+ };
2118
+ CategoryFirestoreRepository.prototype.mountCategory = function (category, options) {
2119
+ return __awaiter(this, void 0, void 0, function () {
2120
+ var chunks, products, publishedField, chunks_1, chunks_1_1, productIds, query, productSnap, e_1_1;
2121
+ var e_1, _a;
2122
+ return __generator(this, function (_b) {
2123
+ switch (_b.label) {
2124
+ case 0:
2125
+ if (!category.products)
2126
+ throw new RequiredArgumentError(['Category products is empty']);
2127
+ chunks = lodash.chunk(category.products, 10);
2128
+ products = [];
2129
+ publishedField = category.shop === exports.Shops.GLAMSHOP ? 'publishedGlam' : 'published';
2130
+ _b.label = 1;
2131
+ case 1:
2132
+ _b.trys.push([1, 7, 8, 9]);
2133
+ chunks_1 = __values(chunks), chunks_1_1 = chunks_1.next();
2098
2134
  _b.label = 2;
2099
2135
  case 2:
2100
- _b.trys.push([2, 7, 8, 9]);
2101
- categories_1 = __values(categories), categories_1_1 = categories_1.next();
2102
- _b.label = 3;
2136
+ if (!!chunks_1_1.done) return [3 /*break*/, 6];
2137
+ productIds = chunks_1_1.value;
2138
+ if ((options === null || options === void 0 ? void 0 : options.limit) && products.length >= (options === null || options === void 0 ? void 0 : options.limit))
2139
+ return [3 /*break*/, 6];
2140
+ return [4 /*yield*/, this.collection('products').where(publishedField, '==', true).where('id', 'in', productIds)];
2103
2141
  case 3:
2104
- if (!!categories_1_1.done) return [3 /*break*/, 6];
2105
- category = categories_1_1.value;
2106
- return [4 /*yield*/, this.mountCategory(category)];
2142
+ query = _b.sent();
2143
+ if (options === null || options === void 0 ? void 0 : options.hasStock)
2144
+ query = query.where('stock.quantity', '>', 0);
2145
+ if (options === null || options === void 0 ? void 0 : options.limit)
2146
+ query = query.limit(options === null || options === void 0 ? void 0 : options.limit);
2147
+ return [4 /*yield*/, query.get()];
2107
2148
  case 4:
2108
- products = _b.sent();
2109
- homeSections.push({
2110
- category: category,
2111
- products: products.filter(function (product) { return product.stock.quantity > 0; }).splice(0, limit),
2112
- });
2149
+ productSnap = _b.sent();
2150
+ if (productSnap.empty)
2151
+ return [3 /*break*/, 5];
2152
+ products.push.apply(products, __spreadArray([], __read(productSnap.docs.map(function (doc) { return doc.data(); }))));
2113
2153
  _b.label = 5;
2114
2154
  case 5:
2115
- categories_1_1 = categories_1.next();
2116
- return [3 /*break*/, 3];
2155
+ chunks_1_1 = chunks_1.next();
2156
+ return [3 /*break*/, 2];
2117
2157
  case 6: return [3 /*break*/, 9];
2118
2158
  case 7:
2119
2159
  e_1_1 = _b.sent();
@@ -2121,53 +2161,11 @@
2121
2161
  return [3 /*break*/, 9];
2122
2162
  case 8:
2123
2163
  try {
2124
- if (categories_1_1 && !categories_1_1.done && (_a = categories_1.return)) _a.call(categories_1);
2164
+ if (chunks_1_1 && !chunks_1_1.done && (_a = chunks_1.return)) _a.call(chunks_1);
2125
2165
  }
2126
2166
  finally { if (e_1) throw e_1.error; }
2127
2167
  return [7 /*endfinally*/];
2128
- case 9: return [2 /*return*/, homeSections];
2129
- }
2130
- });
2131
- });
2132
- };
2133
- CategoryFirestoreRepository.prototype.mountCategory = function (category) {
2134
- return __awaiter(this, void 0, void 0, function () {
2135
- var chunks, products, publishedField;
2136
- var _this = this;
2137
- return __generator(this, function (_a) {
2138
- switch (_a.label) {
2139
- case 0:
2140
- if (!category.products)
2141
- throw new RequiredArgumentError(['Category products is empty']);
2142
- chunks = category.products.reduce(function (resultArray, item, index) {
2143
- var chunkIndex = Math.floor(index / 10);
2144
- if (!resultArray[chunkIndex])
2145
- resultArray[chunkIndex] = [];
2146
- resultArray[chunkIndex].push(item);
2147
- return resultArray;
2148
- }, []);
2149
- products = [];
2150
- publishedField = category.shop === exports.Shops.GLAMSHOP ? 'publishedGlam' : 'published';
2151
- return [4 /*yield*/, Promise.all(chunks.map(function (productIds) { return __awaiter(_this, void 0, void 0, function () {
2152
- var productSnap;
2153
- return __generator(this, function (_a) {
2154
- switch (_a.label) {
2155
- case 0: return [4 /*yield*/, this.collection('products')
2156
- .where(publishedField, '==', true)
2157
- .where('id', 'in', productIds)
2158
- .get()];
2159
- case 1:
2160
- productSnap = _a.sent();
2161
- if (productSnap.empty)
2162
- return [2 /*return*/];
2163
- products.push.apply(products, __spreadArray([], __read(productSnap.docs.map(function (doc) { return doc.data(); }))));
2164
- return [2 /*return*/];
2165
- }
2166
- });
2167
- }); }))];
2168
- case 1:
2169
- _a.sent();
2170
- return [2 /*return*/, products];
2168
+ case 9: return [2 /*return*/, products];
2171
2169
  }
2172
2170
  });
2173
2171
  });