@infrab4a/connect 3.9.0-beta.1 → 3.9.0-beta.10

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.
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('reflect-metadata'), require('class-transformer'), require('date-fns'), require('lodash'), require('ts-custom-error'), require('axios'), require('@elastic/elasticsearch'), require('firebase'), require('gql-query-builder')) :
3
- typeof define === 'function' && define.amd ? define('@infrab4a/connect', ['exports', 'reflect-metadata', 'class-transformer', 'date-fns', 'lodash', 'ts-custom-error', 'axios', '@elastic/elasticsearch', 'firebase', 'gql-query-builder'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.infrab4a = global.infrab4a || {}, global.infrab4a.connect = {}), null, global["class-transformer"], global["date-fns"], global.lodash, global["ts-custom-error"], global.axios, global["@elasticElasticsearch"], global.firebase, global["gql-query-builder"]));
5
- })(this, (function (exports, reflectMetadata, classTransformer, dateFns, lodash, tsCustomError, axios, elasticsearch, firebase, gqlQueryBuilder) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('reflect-metadata'), require('class-transformer'), require('date-fns'), require('lodash'), require('ts-custom-error'), require('axios'), require('firebase'), require('gql-query-builder')) :
3
+ typeof define === 'function' && define.amd ? define('@infrab4a/connect', ['exports', 'reflect-metadata', 'class-transformer', 'date-fns', 'lodash', 'ts-custom-error', 'axios', 'firebase', 'gql-query-builder'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.infrab4a = global.infrab4a || {}, global.infrab4a.connect = {}), null, global["class-transformer"], global["date-fns"], global.lodash, global["ts-custom-error"], global.axios, global.firebase, global["gql-query-builder"]));
5
+ })(this, (function (exports, reflectMetadata, classTransformer, dateFns, lodash, tsCustomError, axios, firebase, gqlQueryBuilder) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
@@ -1750,7 +1750,7 @@
1750
1750
  function AxiosAdapter(config) {
1751
1751
  this.config = config;
1752
1752
  }
1753
- AxiosAdapter.prototype.get = function (index) {
1753
+ AxiosAdapter.prototype.get = function (index, id) {
1754
1754
  return __awaiter(this, void 0, void 0, function () {
1755
1755
  var data, error_1;
1756
1756
  return __generator(this, function (_a) {
@@ -1758,9 +1758,13 @@
1758
1758
  case 0:
1759
1759
  _a.trys.push([0, 2, , 3]);
1760
1760
  return [4 /*yield*/, axios__default["default"]({
1761
- url: this.config.url + "/" + index,
1761
+ url: this.config.url + "/" + index + "/_doc/" + id,
1762
1762
  method: 'GET',
1763
- headers: { Authorization: "Basic " + this.config.credential },
1763
+ responseType: 'json',
1764
+ headers: {
1765
+ 'Content-Type': 'application/json',
1766
+ Authorization: "ApiKey " + this.config.credential,
1767
+ },
1764
1768
  })];
1765
1769
  case 1:
1766
1770
  data = (_a.sent()).data;
@@ -1779,9 +1783,14 @@
1779
1783
  return __generator(this, function (_a) {
1780
1784
  switch (_a.label) {
1781
1785
  case 0: return [4 /*yield*/, axios__default["default"]({
1782
- url: this.config.url + "/" + index,
1786
+ url: this.config.url + "/" + index + "/_search",
1783
1787
  method: 'POST',
1784
- headers: { Authorization: "Basic " + this.config.credential },
1788
+ responseType: 'json',
1789
+ headers: {
1790
+ Accept: 'application/vnd.elasticsearch+json;compatible-with=7',
1791
+ 'Content-Type': 'application/vnd.elasticsearch+json;compatible-with=7',
1792
+ Authorization: "ApiKey " + this.config.credential,
1793
+ },
1785
1794
  data: query,
1786
1795
  })];
1787
1796
  case 1:
@@ -1799,9 +1808,9 @@
1799
1808
  return __generator(this, function (_a) {
1800
1809
  switch (_a.label) {
1801
1810
  case 0: return [4 /*yield*/, axios__default["default"]({
1802
- url: this.config.url + "/" + index,
1803
- method: 'PUT',
1804
- headers: { Authorization: "Basic " + this.config.credential },
1811
+ url: this.config.url + "/" + index + "/_doc",
1812
+ method: 'POST',
1813
+ headers: { Authorization: "ApiKey " + this.config.credential },
1805
1814
  data: data,
1806
1815
  })];
1807
1816
  case 1:
@@ -1811,14 +1820,15 @@
1811
1820
  });
1812
1821
  });
1813
1822
  };
1814
- AxiosAdapter.prototype.delete = function (index) {
1823
+ AxiosAdapter.prototype.update = function (index, id, data) {
1815
1824
  return __awaiter(this, void 0, void 0, function () {
1816
1825
  return __generator(this, function (_a) {
1817
1826
  switch (_a.label) {
1818
1827
  case 0: return [4 /*yield*/, axios__default["default"]({
1819
- url: this.config.url + "/" + index,
1820
- method: 'DELETE',
1821
- headers: { Authorization: "Basic " + this.config.credential },
1828
+ url: this.config.url + "/" + index + "/_update/" + id,
1829
+ method: 'PUT',
1830
+ headers: { Authorization: "ApiKey " + this.config.credential },
1831
+ data: data,
1822
1832
  })];
1823
1833
  case 1:
1824
1834
  _a.sent();
@@ -1827,201 +1837,145 @@
1827
1837
  });
1828
1838
  });
1829
1839
  };
1830
- return AxiosAdapter;
1831
- }());
1832
-
1833
- var ElasticSearchAdapter = /** @class */ (function () {
1834
- function ElasticSearchAdapter(config) {
1835
- this.client = new elasticsearch.Client(config);
1836
- }
1837
- ElasticSearchAdapter.prototype.getById = function (index, id) {
1838
- return __awaiter(this, void 0, void 0, function () {
1839
- var data, erro_1;
1840
- return __generator(this, function (_a) {
1841
- switch (_a.label) {
1842
- case 0:
1843
- _a.trys.push([0, 2, , 3]);
1844
- return [4 /*yield*/, this.client.get({ index: index, id: id })];
1845
- case 1:
1846
- data = _a.sent();
1847
- return [2 /*return*/, data._source];
1848
- case 2:
1849
- erro_1 = _a.sent();
1850
- throw new NotFoundError(erro_1.message);
1851
- case 3: return [2 /*return*/];
1852
- }
1853
- });
1854
- });
1855
- };
1856
- ElasticSearchAdapter.prototype.search = function (indice, query) {
1840
+ AxiosAdapter.prototype.delete = function (index, id) {
1857
1841
  return __awaiter(this, void 0, void 0, function () {
1858
- var result;
1859
1842
  return __generator(this, function (_a) {
1860
1843
  switch (_a.label) {
1861
- case 0: return [4 /*yield*/, this.client.search({
1862
- index: indice,
1863
- query: query,
1844
+ case 0: return [4 /*yield*/, axios__default["default"]({
1845
+ url: this.config.url + "/" + index + "/_doc/" + id,
1846
+ method: 'DELETE',
1847
+ headers: { Authorization: "ApiKey " + this.config.credential },
1864
1848
  })];
1865
1849
  case 1:
1866
- result = _a.sent();
1867
- return [2 /*return*/, {
1868
- total: result.hits.total,
1869
- hits: result.hits.hits,
1870
- }];
1850
+ _a.sent();
1851
+ return [2 /*return*/];
1871
1852
  }
1872
1853
  });
1873
1854
  });
1874
1855
  };
1875
- ElasticSearchAdapter.prototype.save = function (index, data) {
1876
- return this.client.index({
1877
- index: index,
1878
- document: data,
1879
- });
1880
- };
1881
- ElasticSearchAdapter.prototype.update = function (index, id, data) {
1882
- try {
1883
- return this.client.update({
1884
- index: index,
1885
- id: id,
1886
- doc: data,
1887
- });
1888
- }
1889
- catch (error) {
1890
- console.log(error);
1891
- throw new Error(error.message);
1892
- }
1893
- };
1894
- ElasticSearchAdapter.prototype.delete = function (index, id) {
1895
- try {
1896
- this.client.delete({
1897
- index: index,
1898
- id: id,
1899
- });
1900
- }
1901
- catch (error) {
1902
- console.log(error);
1903
- }
1904
- };
1905
- return ElasticSearchAdapter;
1856
+ return AxiosAdapter;
1906
1857
  }());
1907
1858
 
1908
1859
  var ProductsIndex = /** @class */ (function () {
1909
- function ProductsIndex(
1910
- // private readonly adapter: AxiosAdapter<Product>,
1911
- esadapter) {
1912
- this.esadapter = esadapter;
1860
+ function ProductsIndex(adapter) {
1861
+ this.adapter = adapter;
1862
+ this.index = "products";
1913
1863
  }
1914
- ProductsIndex.prototype.get = function (id) {
1864
+ ProductsIndex.prototype.getById = function (id) {
1915
1865
  return __awaiter(this, void 0, void 0, function () {
1916
1866
  var data;
1917
1867
  return __generator(this, function (_a) {
1918
1868
  switch (_a.label) {
1919
- case 0: return [4 /*yield*/, this.esadapter.getById("products", id)];
1869
+ case 0: return [4 /*yield*/, this.adapter.get(this.index, id)];
1920
1870
  case 1:
1921
1871
  data = _a.sent();
1922
- // const data = await this.adapter.get(`products/_doc/${id}`)
1923
1872
  return [2 /*return*/, Product.toInstance(data)];
1924
1873
  }
1925
1874
  });
1926
1875
  });
1927
1876
  };
1928
- ProductsIndex.prototype.search = function (searchTerm) {
1877
+ ProductsIndex.prototype.getByAlloyId = function (id) {
1929
1878
  return __awaiter(this, void 0, void 0, function () {
1930
- var search;
1879
+ var data;
1931
1880
  return __generator(this, function (_a) {
1932
1881
  switch (_a.label) {
1933
- case 0: return [4 /*yield*/, this.esadapter.search("products", {
1934
- bool: {
1935
- must: {
1936
- multi_match: {
1937
- query: "" + searchTerm,
1938
- fields: ['name', 'name.folded', 'name.search', 'description', 'brand'],
1939
- fuzziness: 2,
1940
- },
1941
- },
1942
- should: {
1943
- match_phrase_prefix: {
1944
- 'name.search': {
1945
- query: "" + searchTerm,
1946
- slop: 10,
1882
+ case 0: return [4 /*yield*/, this.adapter.query(this.index, {
1883
+ query: {
1884
+ bool: {
1885
+ must: {
1886
+ match: {
1887
+ id: id,
1947
1888
  },
1948
1889
  },
1949
1890
  },
1950
- filter: [{ term: { published: true } }],
1951
1891
  },
1952
1892
  })];
1893
+ case 1:
1894
+ data = _a.sent();
1895
+ return [2 /*return*/, Product.toInstance(data.hits[0]._source)];
1896
+ }
1897
+ });
1898
+ });
1899
+ };
1900
+ ProductsIndex.prototype.search = function (searchTerm, total, shop) {
1901
+ return __awaiter(this, void 0, void 0, function () {
1902
+ var size, fields, filter, search;
1903
+ return __generator(this, function (_a) {
1904
+ switch (_a.label) {
1905
+ case 0:
1906
+ size = total >= 10 ? 500 : 9;
1907
+ fields = [
1908
+ 'EAN',
1909
+ 'brand',
1910
+ 'id',
1911
+ 'images',
1912
+ 'miniatures',
1913
+ 'name',
1914
+ 'price',
1915
+ 'sku',
1916
+ 'stock',
1917
+ 'slug',
1918
+ 'reviews',
1919
+ 'hasVariants',
1920
+ 'rate',
1921
+ ];
1922
+ filter = [{ term: { published: true } }];
1923
+ if (size > 9) {
1924
+ fields.push.apply(fields, ['pricePaid', 'isGift', 'stock', 'weight', 'tags']);
1925
+ }
1926
+ else {
1927
+ filter.push({ term: { tags: shop == exports.Shops.GLAMSHOP ? 'feminino' : 'masculino' } });
1928
+ }
1929
+ return [4 /*yield*/, this.adapter.query(this.index, {
1930
+ size: size,
1931
+ _source: fields,
1932
+ query: {
1933
+ bool: {
1934
+ must: {
1935
+ multi_match: {
1936
+ query: "" + searchTerm,
1937
+ type: 'bool_prefix',
1938
+ fields: [
1939
+ 'name',
1940
+ 'name.folded',
1941
+ 'name.search',
1942
+ 'description',
1943
+ 'description.search',
1944
+ 'description.folded',
1945
+ 'brand',
1946
+ 'brand.search',
1947
+ 'brand.folded',
1948
+ ],
1949
+ fuzziness: 2,
1950
+ },
1951
+ },
1952
+ should: {
1953
+ match_phrase_prefix: {
1954
+ 'name.search': {
1955
+ query: "" + searchTerm,
1956
+ slop: 10,
1957
+ },
1958
+ },
1959
+ },
1960
+ filter: filter,
1961
+ },
1962
+ },
1963
+ })];
1953
1964
  case 1:
1954
1965
  search = _a.sent();
1955
- search.hits.forEach(function (row) {
1956
- row._source.price = Object.assign(Object.assign({}, row._source.price), (!!row._source.price.Glamshop && row._source.price.Glamshop));
1957
- });
1958
1966
  search.hits = search.hits.filter(function (e) { return e._source.name !== ''; });
1959
1967
  return [2 /*return*/, search];
1960
1968
  }
1961
1969
  });
1962
1970
  });
1963
1971
  };
1964
- // async findById(ids: string[], options?: { hasStock: boolean; size: number; shop: Shops }): Promise<Product[]> {
1965
- // const fields = [
1966
- // 'brand',
1967
- // 'id',
1968
- // 'images',
1969
- // 'miniatures',
1970
- // 'name',
1971
- // 'price',
1972
- // 'sku',
1973
- // 'stock',
1974
- // 'slug',
1975
- // 'reviews',
1976
- // 'pricePaid',
1977
- // 'isGift',
1978
- // 'stock',
1979
- // 'weight',
1980
- // 'tags',
1981
- // 'filters',
1982
- // 'hasVariants',
1983
- // 'type',
1984
- // ]
1985
- // const { hits } = await this.adapter.query('products/_search', {
1986
- // _source: fields,
1987
- // query: {
1988
- // bool: {
1989
- // filter: [
1990
- // {
1991
- // terms: {
1992
- // _id: ids,
1993
- // },
1994
- // },
1995
- // {
1996
- // term: {
1997
- // published: true,
1998
- // },
1999
- // },
2000
- // ...(options.hasStock
2001
- // ? [
2002
- // {
2003
- // range: {
2004
- // 'stock.quantity': {
2005
- // gt: 0,
2006
- // },
2007
- // },
2008
- // },
2009
- // ]
2010
- // : []),
2011
- // ],
2012
- // },
2013
- // },
2014
- // ...(options.size ? { size: options.size } : {}),
2015
- // })
2016
- // return hits.map((hit) => Product.toInstance(hit._source))
2017
- // }
2018
1972
  ProductsIndex.prototype.save = function (product) {
2019
1973
  return __awaiter(this, void 0, void 0, function () {
2020
1974
  var createdAt, updatedAt, kitProducts, data;
2021
1975
  return __generator(this, function (_a) {
2022
1976
  try {
2023
1977
  createdAt = product.createdAt, updatedAt = product.updatedAt, kitProducts = product.kitProducts, data = __rest(product, ["createdAt", "updatedAt", "kitProducts"]);
2024
- this.esadapter.save("product", data);
1978
+ this.adapter.save(this.index, data);
2025
1979
  }
2026
1980
  catch (error) {
2027
1981
  console.error(error);
@@ -2031,23 +1985,42 @@
2031
1985
  });
2032
1986
  };
2033
1987
  ProductsIndex.prototype.update = function (product) {
2034
- try {
2035
- this.esadapter.update("products", product.id, product);
2036
- }
2037
- catch (error) {
2038
- console.error(error);
2039
- }
1988
+ return __awaiter(this, void 0, void 0, function () {
1989
+ var error_1;
1990
+ return __generator(this, function (_a) {
1991
+ switch (_a.label) {
1992
+ case 0:
1993
+ _a.trys.push([0, 2, , 3]);
1994
+ return [4 /*yield*/, this.adapter.update(this.index, product.id, product)];
1995
+ case 1:
1996
+ _a.sent();
1997
+ return [3 /*break*/, 3];
1998
+ case 2:
1999
+ error_1 = _a.sent();
2000
+ console.error(error_1);
2001
+ return [3 /*break*/, 3];
2002
+ case 3: return [2 /*return*/];
2003
+ }
2004
+ });
2005
+ });
2040
2006
  };
2041
2007
  ProductsIndex.prototype.delete = function (id) {
2042
2008
  return __awaiter(this, void 0, void 0, function () {
2009
+ var error_2;
2043
2010
  return __generator(this, function (_a) {
2044
- try {
2045
- this.esadapter.delete("products", id);
2046
- }
2047
- catch (error) {
2048
- console.error(error);
2011
+ switch (_a.label) {
2012
+ case 0:
2013
+ _a.trys.push([0, 2, , 3]);
2014
+ return [4 /*yield*/, this.adapter.delete(this.index, id)];
2015
+ case 1:
2016
+ _a.sent();
2017
+ return [3 /*break*/, 3];
2018
+ case 2:
2019
+ error_2 = _a.sent();
2020
+ console.error(error_2);
2021
+ return [3 /*break*/, 3];
2022
+ case 3: return [2 /*return*/];
2049
2023
  }
2050
- return [2 /*return*/];
2051
2024
  });
2052
2025
  });
2053
2026
  };
@@ -5636,7 +5609,6 @@
5636
5609
  exports.CouponFirestoreRepository = CouponFirestoreRepository;
5637
5610
  exports.DuplicatedResultsError = DuplicatedResultsError;
5638
5611
  exports.Edition = Edition;
5639
- exports.ElasticSearchAdapter = ElasticSearchAdapter;
5640
5612
  exports.Filter = Filter;
5641
5613
  exports.FilterHasuraGraphQLRepository = FilterHasuraGraphQLRepository;
5642
5614
  exports.FilterOption = FilterOption;