@infrab4a/connect 4.25.0-beta.8 → 4.25.0

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/index.cjs.js CHANGED
@@ -5602,6 +5602,17 @@ class CategoryHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGr
5602
5602
  });
5603
5603
  this.productRepository = productRepository;
5604
5604
  this.categoryFilterRepository = categoryFilterRepository;
5605
+ this.reorganizeMostRelevantsProducts = (products, mostRelevantsIds, limit) => {
5606
+ const mostRelevantWithouyStock = products.filter((product) => mostRelevantsIds.includes(product.id) && product.stock.quantity <= 0);
5607
+ const firstProducts = products
5608
+ .filter((product) => mostRelevantsIds.includes(product.id) && product.stock.quantity > 0)
5609
+ .sort((a, b) => mostRelevantsIds.indexOf(a.id) - mostRelevantsIds.indexOf(b.id));
5610
+ const lastProducts = products
5611
+ .filter((product) => !mostRelevantsIds.includes(product.id) && product.stock.quantity > 0)
5612
+ .concat(mostRelevantWithouyStock);
5613
+ const categoryMostRelevants = firstProducts.concat(lastProducts);
5614
+ return limit ? categoryMostRelevants.slice(0, limit) : categoryMostRelevants;
5615
+ };
5605
5616
  }
5606
5617
  async create(params) {
5607
5618
  const { images, mostRelevants, metadatas } = params, data = tslib.__rest(params, ["images", "mostRelevants", "metadatas"]);
@@ -5701,63 +5712,83 @@ class CategoryHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGr
5701
5712
  return homeSections;
5702
5713
  }
5703
5714
  async mountCategory(category, shop, options, optionsCache) {
5704
- var _a;
5715
+ var _a, _b, _c, _d, _e, _f, _g;
5705
5716
  if (!((_a = category === null || category === void 0 ? void 0 : category.products) === null || _a === void 0 ? void 0 : _a.length))
5706
5717
  return [];
5707
- const mostRelevants = category.getMostRelevantByShop(shop);
5708
- const mostRelevantProductsIds = [...new Set(mostRelevants.concat(category.products))];
5718
+ if (((_b = this.cache) === null || _b === void 0 ? void 0 : _b.cacheAdapter) && ((_c = optionsCache === null || optionsCache === void 0 ? void 0 : optionsCache.cache) === null || _c === void 0 ? void 0 : _c.enabled)) {
5719
+ const cacheKey = `${this.model.name.toLowerCase()}:mountCategory:${category.id}:${shop}:${MD5GeneratorHelper.generateMD5(options)}`;
5720
+ const cachedData = await this.cache.cacheAdapter.get(cacheKey);
5721
+ if (cachedData) {
5722
+ this.logger.log(`Dados recuperados do cache: ${cacheKey}`);
5723
+ return JSON.parse(cachedData);
5724
+ }
5725
+ }
5726
+ const fields = [
5727
+ 'id',
5728
+ 'name',
5729
+ 'slug',
5730
+ 'images',
5731
+ 'miniatures',
5732
+ 'price',
5733
+ 'fullPrice',
5734
+ 'subscriberDiscountPercentage',
5735
+ 'subscriberPrice',
5736
+ 'stock',
5737
+ 'published',
5738
+ 'CEST',
5739
+ 'EAN',
5740
+ 'NCM',
5741
+ 'brand',
5742
+ 'costPrice',
5743
+ 'hasVariants',
5744
+ 'isKit',
5745
+ 'sku',
5746
+ 'rate',
5747
+ 'tags',
5748
+ 'type',
5749
+ 'shoppingCount',
5750
+ 'gender',
5751
+ 'createdAt',
5752
+ ];
5709
5753
  const products = [];
5710
- const { data: productsData } = await this.productRepository.findCatalog({
5711
- filters: {
5712
- id: { operator: exports.Where.IN, value: mostRelevantProductsIds },
5713
- published: true,
5714
- },
5715
- fields: [
5716
- 'id',
5717
- 'name',
5718
- 'slug',
5719
- 'images',
5720
- 'miniatures',
5721
- 'price',
5722
- 'fullPrice',
5723
- 'subscriberDiscountPercentage',
5724
- 'subscriberPrice',
5725
- 'stock',
5726
- 'published',
5727
- 'CEST',
5728
- 'EAN',
5729
- 'NCM',
5730
- 'brand',
5731
- 'costPrice',
5732
- 'hasVariants',
5733
- 'isKit',
5734
- 'sku',
5735
- 'rate',
5736
- 'tags',
5737
- 'type',
5738
- 'shoppingCount',
5739
- 'gender',
5740
- 'createdAt',
5741
- ],
5742
- options: { enableCount: false },
5743
- orderBy: {
5754
+ const mostRelevantsIds = category.getMostRelevantByShop(shop);
5755
+ if (mostRelevantsIds === null || mostRelevantsIds === void 0 ? void 0 : mostRelevantsIds.length) {
5756
+ const { data: mostRelevants } = await this.productRepository.findCatalog({
5757
+ filters: Object.assign({ id: {
5758
+ operator: exports.Where.IN,
5759
+ value: mostRelevantsIds,
5760
+ }, published: true }, (options.hasStock ? { stock: { quantity: { operator: exports.Where.GT, value: 0 } } } : {})),
5761
+ fields,
5762
+ options: { enableCount: false },
5763
+ orderBy: {
5764
+ stock: 'desc',
5765
+ shoppingCount: 'desc',
5766
+ rate: 'desc',
5767
+ name: 'asc',
5768
+ },
5769
+ }, shop === exports.Shops.MENSMARKET ? 'male' : 'female', optionsCache);
5770
+ if (mostRelevants.length >= mostRelevantsIds.length) {
5771
+ const result = this.reorganizeMostRelevantsProducts(mostRelevants, mostRelevantsIds, options.limit);
5772
+ if (((_d = this.cache) === null || _d === void 0 ? void 0 : _d.cacheAdapter) && ((_e = optionsCache === null || optionsCache === void 0 ? void 0 : optionsCache.cache) === null || _e === void 0 ? void 0 : _e.enabled))
5773
+ await this.saveToCache(category, shop, options, optionsCache, result);
5774
+ return result;
5775
+ }
5776
+ products.push(...mostRelevants);
5777
+ }
5778
+ const productIds = category.products.filter((productId) => !mostRelevantsIds.includes(productId));
5779
+ const { data: productsData } = await this.productRepository.findCatalog(Object.assign(Object.assign({ filters: Object.assign({ id: {
5780
+ operator: exports.Where.IN,
5781
+ value: productIds,
5782
+ }, published: true }, (options.hasStock ? { stock: { quantity: { operator: exports.Where.GT, value: 0 } } } : {})) }, (options.limit ? { limits: { limit: options.limit } } : {})), { fields, options: { enableCount: false }, orderBy: {
5783
+ stock: 'desc',
5744
5784
  shoppingCount: 'desc',
5745
5785
  rate: 'desc',
5746
- stock: 'desc',
5747
5786
  name: 'asc',
5748
- },
5749
- }, shop === exports.Shops.MENSMARKET ? 'male' : 'female', optionsCache);
5750
- const mostRelevantWithouyStock = productsData.filter((product) => mostRelevants.includes(product.id) && product.stock.quantity <= 0);
5751
- const firstProducts = productsData
5752
- .filter((product) => mostRelevants.includes(product.id) && product.stock.quantity > 0)
5753
- .sort((a, b) => mostRelevants.indexOf(a.id) - mostRelevants.indexOf(b.id));
5754
- const lastProducts = productsData
5755
- .filter((product) => !mostRelevants.includes(product.id) && product.stock.quantity > 0)
5756
- .concat(mostRelevantWithouyStock);
5757
- const categoryMostRelevants = firstProducts.concat(lastProducts);
5758
- const resultFinal = categoryMostRelevants.slice(0, options.limit);
5759
- products.push(...resultFinal);
5760
- return products;
5787
+ } }), shop === exports.Shops.MENSMARKET ? 'male' : 'female', optionsCache);
5788
+ const result = this.reorganizeMostRelevantsProducts([...products, ...productsData], mostRelevantsIds, options.limit);
5789
+ if (((_f = this.cache) === null || _f === void 0 ? void 0 : _f.cacheAdapter) && ((_g = optionsCache === null || optionsCache === void 0 ? void 0 : optionsCache.cache) === null || _g === void 0 ? void 0 : _g.enabled))
5790
+ await this.saveToCache(category, shop, options, optionsCache, result);
5791
+ return result;
5761
5792
  }
5762
5793
  async getChildren(parentId, _optionsCache) {
5763
5794
  const { category_tree } = await this.query('category_tree', ['id', 'name', 'parent_id', 'slug', 'reference', 'published', 'shops'], {
@@ -5893,6 +5924,16 @@ class CategoryHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGr
5893
5924
  return filters;
5894
5925
  }
5895
5926
  }
5927
+ async saveToCache(category, shop, options, optionsCache, result) {
5928
+ var _a;
5929
+ const cacheKey = `${this.model.name.toLowerCase()}:mountCategory:${category.id}:${shop}:${MD5GeneratorHelper.generateMD5(options)}`;
5930
+ await this.cache.cacheAdapter.set({
5931
+ key: cacheKey,
5932
+ data: JSON.stringify(result),
5933
+ expirationInSeconds: ((_a = optionsCache === null || optionsCache === void 0 ? void 0 : optionsCache.cache) === null || _a === void 0 ? void 0 : _a.ttl) || this.cache.ttlDefault,
5934
+ });
5935
+ this.logger.log(`Dados salvos no cache: ${cacheKey}`);
5936
+ }
5896
5937
  }
5897
5938
  tslib.__decorate([
5898
5939
  Log(),
@@ -5913,6 +5954,7 @@ tslib.__decorate([
5913
5954
  tslib.__metadata("design:returntype", Promise)
5914
5955
  ], CategoryHasuraGraphQLRepository.prototype, "getCategoriesForHome", null);
5915
5956
  tslib.__decorate([
5957
+ Log(),
5916
5958
  Log(),
5917
5959
  tslib.__metadata("design:type", Function),
5918
5960
  tslib.__metadata("design:paramtypes", [Category, String, Object, Object]),
package/index.esm.js CHANGED
@@ -5596,6 +5596,17 @@ class CategoryHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGr
5596
5596
  });
5597
5597
  this.productRepository = productRepository;
5598
5598
  this.categoryFilterRepository = categoryFilterRepository;
5599
+ this.reorganizeMostRelevantsProducts = (products, mostRelevantsIds, limit) => {
5600
+ const mostRelevantWithouyStock = products.filter((product) => mostRelevantsIds.includes(product.id) && product.stock.quantity <= 0);
5601
+ const firstProducts = products
5602
+ .filter((product) => mostRelevantsIds.includes(product.id) && product.stock.quantity > 0)
5603
+ .sort((a, b) => mostRelevantsIds.indexOf(a.id) - mostRelevantsIds.indexOf(b.id));
5604
+ const lastProducts = products
5605
+ .filter((product) => !mostRelevantsIds.includes(product.id) && product.stock.quantity > 0)
5606
+ .concat(mostRelevantWithouyStock);
5607
+ const categoryMostRelevants = firstProducts.concat(lastProducts);
5608
+ return limit ? categoryMostRelevants.slice(0, limit) : categoryMostRelevants;
5609
+ };
5599
5610
  }
5600
5611
  async create(params) {
5601
5612
  const { images, mostRelevants, metadatas } = params, data = __rest(params, ["images", "mostRelevants", "metadatas"]);
@@ -5695,63 +5706,83 @@ class CategoryHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGr
5695
5706
  return homeSections;
5696
5707
  }
5697
5708
  async mountCategory(category, shop, options, optionsCache) {
5698
- var _a;
5709
+ var _a, _b, _c, _d, _e, _f, _g;
5699
5710
  if (!((_a = category === null || category === void 0 ? void 0 : category.products) === null || _a === void 0 ? void 0 : _a.length))
5700
5711
  return [];
5701
- const mostRelevants = category.getMostRelevantByShop(shop);
5702
- const mostRelevantProductsIds = [...new Set(mostRelevants.concat(category.products))];
5712
+ if (((_b = this.cache) === null || _b === void 0 ? void 0 : _b.cacheAdapter) && ((_c = optionsCache === null || optionsCache === void 0 ? void 0 : optionsCache.cache) === null || _c === void 0 ? void 0 : _c.enabled)) {
5713
+ const cacheKey = `${this.model.name.toLowerCase()}:mountCategory:${category.id}:${shop}:${MD5GeneratorHelper.generateMD5(options)}`;
5714
+ const cachedData = await this.cache.cacheAdapter.get(cacheKey);
5715
+ if (cachedData) {
5716
+ this.logger.log(`Dados recuperados do cache: ${cacheKey}`);
5717
+ return JSON.parse(cachedData);
5718
+ }
5719
+ }
5720
+ const fields = [
5721
+ 'id',
5722
+ 'name',
5723
+ 'slug',
5724
+ 'images',
5725
+ 'miniatures',
5726
+ 'price',
5727
+ 'fullPrice',
5728
+ 'subscriberDiscountPercentage',
5729
+ 'subscriberPrice',
5730
+ 'stock',
5731
+ 'published',
5732
+ 'CEST',
5733
+ 'EAN',
5734
+ 'NCM',
5735
+ 'brand',
5736
+ 'costPrice',
5737
+ 'hasVariants',
5738
+ 'isKit',
5739
+ 'sku',
5740
+ 'rate',
5741
+ 'tags',
5742
+ 'type',
5743
+ 'shoppingCount',
5744
+ 'gender',
5745
+ 'createdAt',
5746
+ ];
5703
5747
  const products = [];
5704
- const { data: productsData } = await this.productRepository.findCatalog({
5705
- filters: {
5706
- id: { operator: Where.IN, value: mostRelevantProductsIds },
5707
- published: true,
5708
- },
5709
- fields: [
5710
- 'id',
5711
- 'name',
5712
- 'slug',
5713
- 'images',
5714
- 'miniatures',
5715
- 'price',
5716
- 'fullPrice',
5717
- 'subscriberDiscountPercentage',
5718
- 'subscriberPrice',
5719
- 'stock',
5720
- 'published',
5721
- 'CEST',
5722
- 'EAN',
5723
- 'NCM',
5724
- 'brand',
5725
- 'costPrice',
5726
- 'hasVariants',
5727
- 'isKit',
5728
- 'sku',
5729
- 'rate',
5730
- 'tags',
5731
- 'type',
5732
- 'shoppingCount',
5733
- 'gender',
5734
- 'createdAt',
5735
- ],
5736
- options: { enableCount: false },
5737
- orderBy: {
5748
+ const mostRelevantsIds = category.getMostRelevantByShop(shop);
5749
+ if (mostRelevantsIds === null || mostRelevantsIds === void 0 ? void 0 : mostRelevantsIds.length) {
5750
+ const { data: mostRelevants } = await this.productRepository.findCatalog({
5751
+ filters: Object.assign({ id: {
5752
+ operator: Where.IN,
5753
+ value: mostRelevantsIds,
5754
+ }, published: true }, (options.hasStock ? { stock: { quantity: { operator: Where.GT, value: 0 } } } : {})),
5755
+ fields,
5756
+ options: { enableCount: false },
5757
+ orderBy: {
5758
+ stock: 'desc',
5759
+ shoppingCount: 'desc',
5760
+ rate: 'desc',
5761
+ name: 'asc',
5762
+ },
5763
+ }, shop === Shops.MENSMARKET ? 'male' : 'female', optionsCache);
5764
+ if (mostRelevants.length >= mostRelevantsIds.length) {
5765
+ const result = this.reorganizeMostRelevantsProducts(mostRelevants, mostRelevantsIds, options.limit);
5766
+ if (((_d = this.cache) === null || _d === void 0 ? void 0 : _d.cacheAdapter) && ((_e = optionsCache === null || optionsCache === void 0 ? void 0 : optionsCache.cache) === null || _e === void 0 ? void 0 : _e.enabled))
5767
+ await this.saveToCache(category, shop, options, optionsCache, result);
5768
+ return result;
5769
+ }
5770
+ products.push(...mostRelevants);
5771
+ }
5772
+ const productIds = category.products.filter((productId) => !mostRelevantsIds.includes(productId));
5773
+ const { data: productsData } = await this.productRepository.findCatalog(Object.assign(Object.assign({ filters: Object.assign({ id: {
5774
+ operator: Where.IN,
5775
+ value: productIds,
5776
+ }, published: true }, (options.hasStock ? { stock: { quantity: { operator: Where.GT, value: 0 } } } : {})) }, (options.limit ? { limits: { limit: options.limit } } : {})), { fields, options: { enableCount: false }, orderBy: {
5777
+ stock: 'desc',
5738
5778
  shoppingCount: 'desc',
5739
5779
  rate: 'desc',
5740
- stock: 'desc',
5741
5780
  name: 'asc',
5742
- },
5743
- }, shop === Shops.MENSMARKET ? 'male' : 'female', optionsCache);
5744
- const mostRelevantWithouyStock = productsData.filter((product) => mostRelevants.includes(product.id) && product.stock.quantity <= 0);
5745
- const firstProducts = productsData
5746
- .filter((product) => mostRelevants.includes(product.id) && product.stock.quantity > 0)
5747
- .sort((a, b) => mostRelevants.indexOf(a.id) - mostRelevants.indexOf(b.id));
5748
- const lastProducts = productsData
5749
- .filter((product) => !mostRelevants.includes(product.id) && product.stock.quantity > 0)
5750
- .concat(mostRelevantWithouyStock);
5751
- const categoryMostRelevants = firstProducts.concat(lastProducts);
5752
- const resultFinal = categoryMostRelevants.slice(0, options.limit);
5753
- products.push(...resultFinal);
5754
- return products;
5781
+ } }), shop === Shops.MENSMARKET ? 'male' : 'female', optionsCache);
5782
+ const result = this.reorganizeMostRelevantsProducts([...products, ...productsData], mostRelevantsIds, options.limit);
5783
+ if (((_f = this.cache) === null || _f === void 0 ? void 0 : _f.cacheAdapter) && ((_g = optionsCache === null || optionsCache === void 0 ? void 0 : optionsCache.cache) === null || _g === void 0 ? void 0 : _g.enabled))
5784
+ await this.saveToCache(category, shop, options, optionsCache, result);
5785
+ return result;
5755
5786
  }
5756
5787
  async getChildren(parentId, _optionsCache) {
5757
5788
  const { category_tree } = await this.query('category_tree', ['id', 'name', 'parent_id', 'slug', 'reference', 'published', 'shops'], {
@@ -5887,6 +5918,16 @@ class CategoryHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGr
5887
5918
  return filters;
5888
5919
  }
5889
5920
  }
5921
+ async saveToCache(category, shop, options, optionsCache, result) {
5922
+ var _a;
5923
+ const cacheKey = `${this.model.name.toLowerCase()}:mountCategory:${category.id}:${shop}:${MD5GeneratorHelper.generateMD5(options)}`;
5924
+ await this.cache.cacheAdapter.set({
5925
+ key: cacheKey,
5926
+ data: JSON.stringify(result),
5927
+ expirationInSeconds: ((_a = optionsCache === null || optionsCache === void 0 ? void 0 : optionsCache.cache) === null || _a === void 0 ? void 0 : _a.ttl) || this.cache.ttlDefault,
5928
+ });
5929
+ this.logger.log(`Dados salvos no cache: ${cacheKey}`);
5930
+ }
5890
5931
  }
5891
5932
  __decorate([
5892
5933
  Log(),
@@ -5907,6 +5948,7 @@ __decorate([
5907
5948
  __metadata("design:returntype", Promise)
5908
5949
  ], CategoryHasuraGraphQLRepository.prototype, "getCategoriesForHome", null);
5909
5950
  __decorate([
5951
+ Log(),
5910
5952
  Log(),
5911
5953
  __metadata("design:type", Function),
5912
5954
  __metadata("design:paramtypes", [Category, String, Object, Object]),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infrab4a/connect",
3
- "version": "4.25.0-beta.8",
3
+ "version": "4.25.0",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org"
6
6
  },
@@ -42,5 +42,7 @@ export declare class CategoryHasuraGraphQLRepository extends CategoryHasuraGraph
42
42
  private updateProducts;
43
43
  private updateMetadata;
44
44
  private updateFilters;
45
+ private saveToCache;
46
+ private reorganizeMostRelevantsProducts;
45
47
  }
46
48
  export {};