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

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.
@@ -1760,11 +1760,7 @@
1760
1760
  return [4 /*yield*/, axios__default["default"]({
1761
1761
  url: this.config.url + "/" + index + "/_doc/" + id,
1762
1762
  method: 'GET',
1763
- responseType: 'json',
1764
- headers: {
1765
- 'Content-Type': 'application/json',
1766
- Authorization: "ApiKey " + this.config.credential,
1767
- },
1763
+ headers: { Authorization: "ApiKey " + this.config.credential },
1768
1764
  })];
1769
1765
  case 1:
1770
1766
  data = (_a.sent()).data;
@@ -1784,13 +1780,8 @@
1784
1780
  switch (_a.label) {
1785
1781
  case 0: return [4 /*yield*/, axios__default["default"]({
1786
1782
  url: this.config.url + "/" + index + "/_search",
1787
- method: 'POST',
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
- },
1783
+ method: 'GET',
1784
+ headers: { Authorization: "ApiKey " + this.config.credential },
1794
1785
  data: query,
1795
1786
  })];
1796
1787
  case 1:
@@ -1808,24 +1799,7 @@
1808
1799
  return __generator(this, function (_a) {
1809
1800
  switch (_a.label) {
1810
1801
  case 0: return [4 /*yield*/, axios__default["default"]({
1811
- url: this.config.url + "/" + index + "/_doc",
1812
- method: 'POST',
1813
- headers: { Authorization: "ApiKey " + this.config.credential },
1814
- data: data,
1815
- })];
1816
- case 1:
1817
- _a.sent();
1818
- return [2 /*return*/];
1819
- }
1820
- });
1821
- });
1822
- };
1823
- AxiosAdapter.prototype.update = function (index, id, data) {
1824
- return __awaiter(this, void 0, void 0, function () {
1825
- return __generator(this, function (_a) {
1826
- switch (_a.label) {
1827
- case 0: return [4 /*yield*/, axios__default["default"]({
1828
- url: this.config.url + "/" + index + "/_update/" + id,
1802
+ url: this.config.url + "/" + index,
1829
1803
  method: 'PUT',
1830
1804
  headers: { Authorization: "ApiKey " + this.config.credential },
1831
1805
  data: data,
@@ -1837,12 +1811,12 @@
1837
1811
  });
1838
1812
  });
1839
1813
  };
1840
- AxiosAdapter.prototype.delete = function (index, id) {
1814
+ AxiosAdapter.prototype.delete = function (index) {
1841
1815
  return __awaiter(this, void 0, void 0, function () {
1842
1816
  return __generator(this, function (_a) {
1843
1817
  switch (_a.label) {
1844
1818
  case 0: return [4 /*yield*/, axios__default["default"]({
1845
- url: this.config.url + "/" + index + "/_doc/" + id,
1819
+ url: this.config.url + "/" + index,
1846
1820
  method: 'DELETE',
1847
1821
  headers: { Authorization: "ApiKey " + this.config.credential },
1848
1822
  })];
@@ -1856,17 +1830,18 @@
1856
1830
  return AxiosAdapter;
1857
1831
  }());
1858
1832
 
1833
+ // export * from './elastic-search.adapter'
1834
+
1859
1835
  var ProductsIndex = /** @class */ (function () {
1860
1836
  function ProductsIndex(adapter) {
1861
1837
  this.adapter = adapter;
1862
- this.index = "products";
1863
1838
  }
1864
- ProductsIndex.prototype.getById = function (id) {
1839
+ ProductsIndex.prototype.get = function (id) {
1865
1840
  return __awaiter(this, void 0, void 0, function () {
1866
1841
  var data;
1867
1842
  return __generator(this, function (_a) {
1868
1843
  switch (_a.label) {
1869
- case 0: return [4 /*yield*/, this.adapter.get(this.index, id)];
1844
+ case 0: return [4 /*yield*/, this.adapter.get('products', id)];
1870
1845
  case 1:
1871
1846
  data = _a.sent();
1872
1847
  return [2 /*return*/, Product.toInstance(data)];
@@ -1874,153 +1849,168 @@
1874
1849
  });
1875
1850
  });
1876
1851
  };
1877
- ProductsIndex.prototype.getByAlloyId = function (id) {
1852
+ ProductsIndex.prototype.search = function (searchTerm) {
1878
1853
  return __awaiter(this, void 0, void 0, function () {
1879
- var data;
1854
+ var search;
1880
1855
  return __generator(this, function (_a) {
1881
1856
  switch (_a.label) {
1882
- case 0: return [4 /*yield*/, this.adapter.query(this.index, {
1857
+ case 0: return [4 /*yield*/, this.adapter.query('products', {
1883
1858
  query: {
1884
1859
  bool: {
1885
1860
  must: {
1886
- match: {
1887
- id: id,
1861
+ multi_match: {
1862
+ query: "" + searchTerm,
1863
+ fields: ['name', 'name.folded', 'name.search', 'description', 'brand'],
1864
+ fuzziness: 2,
1888
1865
  },
1889
1866
  },
1890
- },
1891
- },
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: {
1867
+ should: {
1868
+ match_phrase_prefix: {
1869
+ 'name.search': {
1936
1870
  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,
1871
+ slop: 10,
1950
1872
  },
1951
1873
  },
1952
- should: {
1953
- match_phrase_prefix: {
1954
- 'name.search': {
1955
- query: "" + searchTerm,
1956
- slop: 10,
1957
- },
1958
- },
1959
- },
1960
- filter: filter,
1961
1874
  },
1875
+ filter: [{ term: { published: true } }],
1962
1876
  },
1963
- })];
1877
+ },
1878
+ })];
1964
1879
  case 1:
1965
1880
  search = _a.sent();
1881
+ // const search = await this.esadapter.search(`products`, {
1882
+ // bool: {
1883
+ // must: {
1884
+ // multi_match: {
1885
+ // query: `${searchTerm}`,
1886
+ // fields: ['name', 'name.folded', 'name.search', 'description', 'brand'],
1887
+ // fuzziness: 2,
1888
+ // },
1889
+ // },
1890
+ // should: {
1891
+ // match_phrase_prefix: {
1892
+ // 'name.search': {
1893
+ // query: `${searchTerm}`,
1894
+ // slop: 10,
1895
+ // },
1896
+ // },
1897
+ // },
1898
+ // filter: [{ term: { published: true } }],
1899
+ // },
1900
+ // })
1901
+ search.hits.forEach(function (row) {
1902
+ row._source.price = Object.assign(Object.assign({}, row._source.price), (!!row._source.price.Glamshop && row._source.price.Glamshop));
1903
+ });
1966
1904
  search.hits = search.hits.filter(function (e) { return e._source.name !== ''; });
1967
1905
  return [2 /*return*/, search];
1968
1906
  }
1969
1907
  });
1970
1908
  });
1971
1909
  };
1910
+ // async findById(ids: string[], options?: { hasStock: boolean; size: number; shop: Shops }): Promise<Product[]> {
1911
+ // const fields = [
1912
+ // 'brand',
1913
+ // 'id',
1914
+ // 'images',
1915
+ // 'miniatures',
1916
+ // 'name',
1917
+ // 'price',
1918
+ // 'sku',
1919
+ // 'stock',
1920
+ // 'slug',
1921
+ // 'reviews',
1922
+ // 'pricePaid',
1923
+ // 'isGift',
1924
+ // 'stock',
1925
+ // 'weight',
1926
+ // 'tags',
1927
+ // 'filters',
1928
+ // 'hasVariants',
1929
+ // 'type',
1930
+ // ]
1931
+ // const { hits } = await this.adapter.query('products/_search', {
1932
+ // _source: fields,
1933
+ // query: {
1934
+ // bool: {
1935
+ // filter: [
1936
+ // {
1937
+ // terms: {
1938
+ // _id: ids,
1939
+ // },
1940
+ // },
1941
+ // {
1942
+ // term: {
1943
+ // published: true,
1944
+ // },
1945
+ // },
1946
+ // ...(options.hasStock
1947
+ // ? [
1948
+ // {
1949
+ // range: {
1950
+ // 'stock.quantity': {
1951
+ // gt: 0,
1952
+ // },
1953
+ // },
1954
+ // },
1955
+ // ]
1956
+ // : []),
1957
+ // ],
1958
+ // },
1959
+ // },
1960
+ // ...(options.size ? { size: options.size } : {}),
1961
+ // })
1962
+ // return hits.map((hit) => Product.toInstance(hit._source))
1963
+ // }
1972
1964
  ProductsIndex.prototype.save = function (product) {
1973
- return __awaiter(this, void 0, void 0, function () {
1974
- var createdAt, updatedAt, kitProducts, data;
1975
- return __generator(this, function (_a) {
1976
- try {
1977
- createdAt = product.createdAt, updatedAt = product.updatedAt, kitProducts = product.kitProducts, data = __rest(product, ["createdAt", "updatedAt", "kitProducts"]);
1978
- this.adapter.save(this.index, data);
1979
- }
1980
- catch (error) {
1981
- console.error(error);
1982
- }
1983
- return [2 /*return*/];
1984
- });
1985
- });
1986
- };
1987
- ProductsIndex.prototype.update = function (product) {
1988
1965
  return __awaiter(this, void 0, void 0, function () {
1989
1966
  var error_1;
1990
1967
  return __generator(this, function (_a) {
1991
1968
  switch (_a.label) {
1992
1969
  case 0:
1993
- _a.trys.push([0, 2, , 3]);
1994
- return [4 /*yield*/, this.adapter.update(this.index, product.id, product)];
1970
+ // try {
1971
+ // const { createdAt, updatedAt, kitProducts, ...data } = product
1972
+ // this.esadapter.save(`product`, data)
1973
+ // } catch (error) {
1974
+ // console.error(error)
1975
+ // }
1976
+ delete product.createdAt;
1977
+ delete product.updatedAt;
1978
+ delete product.kitProducts;
1979
+ _a.label = 1;
1995
1980
  case 1:
1996
- _a.sent();
1997
- return [3 /*break*/, 3];
1981
+ _a.trys.push([1, 4, , 6]);
1982
+ if (!product.firestoreId)
1983
+ throw new Error('Is not a product from firestore');
1984
+ return [4 /*yield*/, this.get(product.firestoreId)];
1998
1985
  case 2:
1986
+ _a.sent();
1987
+ return [4 /*yield*/, this.adapter.save("products/_doc/" + product.firestoreId, product.toPlain())];
1988
+ case 3:
1989
+ _a.sent();
1990
+ return [3 /*break*/, 6];
1991
+ case 4:
1999
1992
  error_1 = _a.sent();
2000
- console.error(error_1);
2001
- return [3 /*break*/, 3];
2002
- case 3: return [2 /*return*/];
1993
+ console.error(error_1.message);
1994
+ return [4 /*yield*/, this.adapter.save("products/_doc/" + product.id, product.toPlain())];
1995
+ case 5:
1996
+ _a.sent();
1997
+ return [3 /*break*/, 6];
1998
+ case 6: return [2 /*return*/];
2003
1999
  }
2004
2000
  });
2005
2001
  });
2006
2002
  };
2003
+ ProductsIndex.prototype.update = function (product) {
2004
+ // try {
2005
+ // this.esadapter.update(`products`, product.id, product)
2006
+ // } catch (error) {
2007
+ // console.error(error)
2008
+ // }
2009
+ };
2007
2010
  ProductsIndex.prototype.delete = function (id) {
2008
2011
  return __awaiter(this, void 0, void 0, function () {
2009
- var error_2;
2010
2012
  return __generator(this, function (_a) {
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*/];
2023
- }
2013
+ return [2 /*return*/];
2024
2014
  });
2025
2015
  });
2026
2016
  };