@infrab4a/connect 1.0.0-beta.18 → 1.0.0-beta.20

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.
@@ -2863,9 +2863,7 @@
2863
2863
  if (update === void 0) { update = false; }
2864
2864
  var _a;
2865
2865
  var data = ((_a = instance.toPlain) === null || _a === void 0 ? void 0 : _a.call(instance)) || instance;
2866
- return Object.keys(data)
2867
- .filter(function (key) { return !lodash.isNil(data[key]); })
2868
- .reduce(function (result, attributeName) {
2866
+ return Object.keys(data).reduce(function (result, attributeName) {
2869
2867
  var _b, _c, _d;
2870
2868
  var _e = AttributeOptionHelper.FindByAttribute(attributeName, fields), columnName = _e.columnName, attributeFields = _e.fields, foreignKeyColumn = _e.foreignKeyColumn, to = _e.to, bindPersistData = _e.bindPersistData;
2871
2869
  if (bindPersistData)
@@ -2874,10 +2872,11 @@
2874
2872
  return result;
2875
2873
  if (!!foreignKeyColumn &&
2876
2874
  !lodash.isEmpty(foreignKeyColumn) &&
2877
- !Object.keys(foreignKeyColumn).filter(function (key) { return !data[attributeName][key]; }).length)
2875
+ !Object.keys(foreignKeyColumn).filter(function (key) { var _a; return !((_a = data[attributeName]) === null || _a === void 0 ? void 0 : _a[key]); }).length)
2878
2876
  return Object.keys(foreignKeyColumn).reduce(function (object, current) {
2879
2877
  var _b;
2880
- return (Object.assign(Object.assign({}, object), (_b = {}, _b[foreignKeyColumn[current]] = data[attributeName][current], _b)));
2878
+ var _a;
2879
+ return (Object.assign(Object.assign({}, object), (_b = {}, _b[foreignKeyColumn[current]] = (_a = data[attributeName]) === null || _a === void 0 ? void 0 : _a[current], _b)));
2881
2880
  }, Object.assign({}, result));
2882
2881
  if (update && lodash.isObject(data[attributeName]) && !lodash.isNil(attributeFields) && !lodash.isDate(data[attributeName]))
2883
2882
  return result;
@@ -2935,11 +2934,11 @@
2935
2934
  CreateHasuraGraphQLMixin.prototype.create = function (data) {
2936
2935
  return __awaiter(this, void 0, void 0, function () {
2937
2936
  var newData;
2938
- return __generator(this, function (_a) {
2939
- switch (_a.label) {
2937
+ return __generator(this, function (_b) {
2938
+ switch (_b.label) {
2940
2939
  case 0: return [4 /*yield*/, this.save(this.model.toInstance(data))];
2941
2940
  case 1:
2942
- newData = _a.sent();
2941
+ newData = _b.sent();
2943
2942
  return [2 /*return*/, this.model.toInstance(newData)];
2944
2943
  }
2945
2944
  });
@@ -2947,15 +2946,34 @@
2947
2946
  };
2948
2947
  CreateHasuraGraphQLMixin.prototype.save = function (data) {
2949
2948
  return __awaiter(this, void 0, void 0, function () {
2950
- var result;
2949
+ var primaryKeyColumns, foreignKeyColumns, result;
2951
2950
  var _this = this;
2952
- return __generator(this, function (_a) {
2953
- switch (_a.label) {
2954
- case 0: return [4 /*yield*/, this.mutation(this.insertGraphQLOperation, this.model.identifiersFields.map(function (field) { return AttributeOptionHelper.FindByAttribute(field, _this.fields).columnName; }), {
2955
- object: { type: this.insertGraphQLObjectType, required: true, value: this.convertDataToHasura(data) },
2956
- })];
2951
+ return __generator(this, function (_b) {
2952
+ switch (_b.label) {
2953
+ case 0:
2954
+ primaryKeyColumns = this.model.identifiersFields.map(function (field) { return AttributeOptionHelper.FindByAttribute(field, _this.fields).columnName; });
2955
+ foreignKeyColumns = this.fields
2956
+ .map(function (field) {
2957
+ var _b;
2958
+ var columnOptions = Object.values(field).shift();
2959
+ return (AttributeOptionHelper.CheckIsColumnOption(columnOptions) &&
2960
+ columnOptions.foreignKeyColumn && __spreadArray(__spreadArray([], __read(Object.values(columnOptions.foreignKeyColumn))), [
2961
+ (_b = {},
2962
+ _b[columnOptions.columnName] = Object.keys(columnOptions.foreignKeyColumn).map(function (foreignKeyField) {
2963
+ var _a;
2964
+ return ((_a = AttributeOptionHelper.FindByAttribute(foreignKeyField, columnOptions === null || columnOptions === void 0 ? void 0 : columnOptions.fields)) === null || _a === void 0 ? void 0 : _a.columnName) ||
2965
+ foreignKeyField;
2966
+ }),
2967
+ _b),
2968
+ ]));
2969
+ })
2970
+ .filter(Boolean)
2971
+ .reduce(function (keys, current) { return __spreadArray(__spreadArray([], __read(keys)), __read(current)); }, []);
2972
+ return [4 /*yield*/, this.mutation(this.insertGraphQLOperation, __spreadArray(__spreadArray([], __read(primaryKeyColumns)), __read(foreignKeyColumns)), {
2973
+ object: { type: this.insertGraphQLObjectType, required: true, value: this.convertDataToHasura(data) },
2974
+ })];
2957
2975
  case 1:
2958
- result = _a.sent();
2976
+ result = _b.sent();
2959
2977
  return [2 /*return*/, Object.assign(Object.assign({}, data.toPlain()), this.convertDataFromHasura(result[this.insertGraphQLOperation]).toPlain())];
2960
2978
  }
2961
2979
  });
@@ -3134,7 +3152,7 @@
3134
3152
  return options.toPlain();
3135
3153
  if (lodash.isNil(options.action))
3136
3154
  return options;
3137
- if (options.action === exports.UpdateOptionActions.REMOVE_FIELD)
3155
+ if ([exports.UpdateOptionActions.REMOVE_FIELD.toString(), exports.UpdateOptionActions.NULL.toString()].includes(options.action))
3138
3156
  return null;
3139
3157
  return options.value;
3140
3158
  };
@@ -3182,7 +3200,7 @@
3182
3200
  return params.toPlain();
3183
3201
  return Object.keys(params).reduce(function (data, currentKey) {
3184
3202
  var _b;
3185
- return (Object.assign(Object.assign({}, data), (params[currentKey] && (_b = {}, _b[currentKey] = getValueByAction(params[currentKey]), _b))));
3203
+ return (Object.assign(Object.assign({}, data), (params[currentKey] !== undefined && (_b = {}, _b[currentKey] = getValueByAction(params[currentKey]), _b))));
3186
3204
  }, {});
3187
3205
  };
3188
3206
  UpdateHasuraGraphQLMixin.prototype.getUpdateModelKeys = function (data) {
@@ -3220,24 +3238,25 @@
3220
3238
  return __awaiter(this, void 0, void 0, function () {
3221
3239
  var instance, result, data;
3222
3240
  var _this = this;
3223
- return __generator(this, function (_a) {
3224
- switch (_a.label) {
3241
+ return __generator(this, function (_b) {
3242
+ switch (_b.label) {
3225
3243
  case 0:
3226
3244
  instance = this.model.toInstance(identifiers);
3227
3245
  return [4 /*yield*/, this.query(this.getGraphQLOperation, this.fields, this.model.identifiersFields.reduce(function (ids, identifier) {
3246
+ var _b;
3228
3247
  var _a;
3229
3248
  if (lodash.isNil(instance[identifier]))
3230
3249
  return ids;
3231
3250
  var columnOption = AttributeOptionHelper.FindByAttribute(identifier, _this.fields);
3232
- var value = columnOption.to(identifiers[identifier.toString()], instance);
3233
- return Object.assign(Object.assign({}, ids), (_a = {}, _a[columnOption.columnName] = {
3251
+ var value = ((_a = columnOption === null || columnOption === void 0 ? void 0 : columnOption.to) === null || _a === void 0 ? void 0 : _a.call(columnOption, identifiers[identifier.toString()], instance)) || identifiers[identifier.toString()];
3252
+ return Object.assign(Object.assign({}, ids), (_b = {}, _b[columnOption.columnName] = {
3234
3253
  type: _this.getAttributeGraphQLTypeOf(columnOption.type || value),
3235
3254
  value: value,
3236
3255
  required: true,
3237
- }, _a));
3256
+ }, _b));
3238
3257
  }, {}))];
3239
3258
  case 1:
3240
- result = _a.sent();
3259
+ result = _b.sent();
3241
3260
  data = result[this.getGraphQLOperation];
3242
3261
  if (lodash.isNil(data))
3243
3262
  throw new NotFoundError(instance.constructor.name + " not found");
@@ -3463,11 +3482,12 @@
3463
3482
  conditions: {
3464
3483
  columnName: 'tag_condition',
3465
3484
  type: HasuraGraphQLColumnType.Jsonb,
3466
- from: function (tags, row) { return ({ brand: row.brand_condition, tags: tags }); },
3485
+ from: function (tags, row) { return ({ brand: row.brand_condition, tags: Array.isArray(tags) ? tags : [] }); },
3467
3486
  bindPersistData: function (value) {
3487
+ var _a, _b;
3468
3488
  return {
3469
3489
  brand_condition: value.brand,
3470
- tag_condition: "{\"" + value.tags.join('","') + "\"}",
3490
+ tag_condition: "{\"" + (((_b = (_a = value === null || value === void 0 ? void 0 : value.tags) === null || _a === void 0 ? void 0 : _a.join) === null || _b === void 0 ? void 0 : _b.call(_a, '","')) || '') + "\"}",
3471
3491
  };
3472
3492
  },
3473
3493
  bindFindFilter: function (sentence) {
@@ -3499,34 +3519,34 @@
3499
3519
  });
3500
3520
  var _a;
3501
3521
  return __awaiter(this, void 0, void 0, function () {
3502
- var _b;
3503
- return __generator(this, function (_c) {
3504
- switch (_c.label) {
3522
+ var _c;
3523
+ return __generator(this, function (_d) {
3524
+ switch (_d.label) {
3505
3525
  case 0:
3506
3526
  if (!Number.isNaN(+identifiers.id)) return [3 /*break*/, 2];
3507
3527
  return [4 /*yield*/, this.find({ filters: { firestoreId: identifiers.id } })];
3508
3528
  case 1:
3509
- _b = (_a = (_c.sent()).data) === null || _a === void 0 ? void 0 : _a[0];
3529
+ _c = (_a = (_d.sent()).data) === null || _a === void 0 ? void 0 : _a[0];
3510
3530
  return [3 /*break*/, 3];
3511
3531
  case 2:
3512
- _b = _super.get.call(this, identifiers);
3513
- _c.label = 3;
3514
- case 3: return [2 /*return*/, _b];
3532
+ _c = _super.get.call(this, identifiers);
3533
+ _d.label = 3;
3534
+ case 3: return [2 /*return*/, _c];
3515
3535
  }
3516
3536
  });
3517
3537
  });
3518
3538
  };
3519
3539
  CategoryHasuraGraphQLRepository.prototype.getCategoryBySlug = function (slug, shop) {
3520
3540
  return __awaiter(this, void 0, void 0, function () {
3521
- var _b, data, count;
3522
- return __generator(this, function (_c) {
3523
- switch (_c.label) {
3541
+ var _c, data, count;
3542
+ return __generator(this, function (_d) {
3543
+ switch (_d.label) {
3524
3544
  case 0:
3525
3545
  if (!slug)
3526
3546
  return [2 /*return*/];
3527
3547
  return [4 /*yield*/, this.find({ filters: { slug: slug, shop: shop, published: true } })];
3528
3548
  case 1:
3529
- _b = _c.sent(), data = _b.data, count = _b.count;
3549
+ _c = _d.sent(), data = _c.data, count = _c.count;
3530
3550
  if (count > 1)
3531
3551
  throw new DuplicatedResultsError('Query returned duplicated values');
3532
3552
  if (!count)
@@ -3539,33 +3559,33 @@
3539
3559
  CategoryHasuraGraphQLRepository.prototype.getCategoriesForHome = function (categoryIds, limit) {
3540
3560
  if (limit === void 0) { limit = 4; }
3541
3561
  return __awaiter(this, void 0, void 0, function () {
3542
- var _b, categories, count, homeSections;
3562
+ var _c, categories, count, homeSections;
3543
3563
  var _this = this;
3544
- return __generator(this, function (_c) {
3545
- switch (_c.label) {
3564
+ return __generator(this, function (_d) {
3565
+ switch (_d.label) {
3546
3566
  case 0: return [4 /*yield*/, this.find({
3547
3567
  filters: { firestoreId: { operator: exports.Where.IN, value: categoryIds.filter(Boolean) }, published: true },
3548
3568
  })];
3549
3569
  case 1:
3550
- _b = _c.sent(), categories = _b.data, count = _b.count;
3570
+ _c = _d.sent(), categories = _c.data, count = _c.count;
3551
3571
  if (!count)
3552
3572
  throw new NotFoundError('Categories not found');
3553
3573
  return [4 /*yield*/, Promise.all(categories.map(function (category) { return __awaiter(_this, void 0, void 0, function () {
3554
- var _b;
3555
- return __generator(this, function (_c) {
3556
- switch (_c.label) {
3574
+ var _c;
3575
+ return __generator(this, function (_d) {
3576
+ switch (_d.label) {
3557
3577
  case 0:
3558
- _b = {
3578
+ _c = {
3559
3579
  category: category
3560
3580
  };
3561
3581
  return [4 /*yield*/, this.mountCategory(category, { limit: limit, hasStock: true })];
3562
- case 1: return [2 /*return*/, (_b.products = _c.sent(),
3563
- _b)];
3582
+ case 1: return [2 /*return*/, (_c.products = _d.sent(),
3583
+ _c)];
3564
3584
  }
3565
3585
  });
3566
3586
  }); }))];
3567
3587
  case 2:
3568
- homeSections = _c.sent();
3588
+ homeSections = _d.sent();
3569
3589
  return [2 /*return*/, homeSections];
3570
3590
  }
3571
3591
  });
@@ -3574,17 +3594,17 @@
3574
3594
  CategoryHasuraGraphQLRepository.prototype.mountCategory = function (category, options) {
3575
3595
  return __awaiter(this, void 0, void 0, function () {
3576
3596
  var products, publishedField, productsData;
3577
- var _b;
3578
- return __generator(this, function (_c) {
3579
- switch (_c.label) {
3597
+ var _c;
3598
+ return __generator(this, function (_d) {
3599
+ switch (_d.label) {
3580
3600
  case 0:
3581
3601
  if (!category.products)
3582
3602
  throw new RequiredArgumentError(['Category products is empty']);
3583
3603
  products = [];
3584
3604
  publishedField = category.shop === exports.Shops.GLAMSHOP ? 'publishedGlam' : 'published';
3585
- return [4 /*yield*/, this.productRepository.find(Object.assign({ filters: Object.assign((_b = { id: { operator: exports.Where.IN, value: category.products } }, _b[publishedField] = true, _b), ((options === null || options === void 0 ? void 0 : options.hasStock) ? { stock: { quantity: { operator: exports.Where.GT, value: 0 } } } : {})) }, ((options === null || options === void 0 ? void 0 : options.limit) ? { limits: { limit: options === null || options === void 0 ? void 0 : options.limit } } : {})))];
3605
+ return [4 /*yield*/, this.productRepository.find(Object.assign({ filters: Object.assign((_c = { id: { operator: exports.Where.IN, value: category.products } }, _c[publishedField] = true, _c), ((options === null || options === void 0 ? void 0 : options.hasStock) ? { stock: { quantity: { operator: exports.Where.GT, value: 0 } } } : {})) }, ((options === null || options === void 0 ? void 0 : options.limit) ? { limits: { limit: options === null || options === void 0 ? void 0 : options.limit } } : {})))];
3586
3606
  case 1:
3587
- productsData = (_c.sent()).data;
3607
+ productsData = (_d.sent()).data;
3588
3608
  products.push.apply(products, __spreadArray([], __read(productsData)));
3589
3609
  return [2 /*return*/, products];
3590
3610
  }
@@ -3597,7 +3617,15 @@
3597
3617
  var ProductHasuraGraphQLRepository = /** @class */ (function (_super_1) {
3598
3618
  __extends(ProductHasuraGraphQLRepository, _super_1);
3599
3619
  function ProductHasuraGraphQLRepository(endpoint, authOptions) {
3600
- var _this = this;
3620
+ var _this = _super_1.call(this, {
3621
+ tableName: 'product',
3622
+ model: ProductHasuraGraphQL,
3623
+ endpoint: endpoint,
3624
+ authOptions: authOptions,
3625
+ fields: [],
3626
+ }) || this;
3627
+ _this.bindReviewToModel = function (plain) { return (Object.assign(Object.assign({}, lodash.omit(plain, ['product_id', 'created_at', 'updated_at', 'person_id', 'order_id'])), { createdAt: plain.created_at, updatedAt: plain.updated_at, personId: plain.person_id, orderId: plain.order_id })); };
3628
+ _this.bindReviewToHasura = function (review) { return (Object.assign(Object.assign({}, lodash.omit(review, ['productId', 'createdAt', 'updatedAt', 'personId', 'orderId'])), { person_id: review.personId, order_id: review.orderId })); };
3601
3629
  var commonFields = [
3602
3630
  { id: { columnName: 'id', to: function (value) { return +value; }, from: function (value) { return value.toString(); } } },
3603
3631
  { firestoreId: { columnName: 'firestore_id' } },
@@ -3617,8 +3645,8 @@
3617
3645
  },
3618
3646
  },
3619
3647
  { hasVariants: { columnName: 'has_variants' } },
3620
- { images: { columnName: 'images', to: function (value) { return "{\"" + value.join("\",\"") + "\"}"; } } },
3621
- { miniatures: { columnName: 'miniatures', to: function (value) { return "{\"" + value.join("\",\"") + "\"}"; } } },
3648
+ { images: { columnName: 'images', to: function (value) { var _a; return "{\"" + (((_a = value === null || value === void 0 ? void 0 : value.join) === null || _a === void 0 ? void 0 : _a.call(value, "\",\"")) || '') + "\"}"; } } },
3649
+ { miniatures: { columnName: 'miniatures', to: function (value) { var _a; return "{\"" + (((_a = value === null || value === void 0 ? void 0 : value.join) === null || _a === void 0 ? void 0 : _a.call(value, "\",\"")) || '') + "\"}"; } } },
3622
3650
  'name',
3623
3651
  {
3624
3652
  price: {
@@ -3634,15 +3662,17 @@
3634
3662
  }, {}); },
3635
3663
  bindFindFilter: function (sentence) {
3636
3664
  var filters = Object.values(sentence).shift();
3637
- return Object.assign(Object.assign(Object.assign(Object.assign({}, ((filters === null || filters === void 0 ? void 0 : filters.price) && { price: filters.price })), (filters.fullPrice && { full_price: filters.fullPrice })), (filters.subscriberDiscountPercentage && {
3665
+ return Object.assign(Object.assign(Object.assign(Object.assign({}, (((filters === null || filters === void 0 ? void 0 : filters.price) || (filters === null || filters === void 0 ? void 0 : filters.price) === 0) && { price: filters.price })), ((filters.fullPrice || filters.fullPrice === 0) && { full_price: filters.fullPrice })), ((filters.subscriberDiscountPercentage || filters.subscriberDiscountPercentage === 0) && {
3638
3666
  subscriber_discount_percentage: filters.subscriberDiscountPercentage,
3639
- })), (filters.subscriberPrice && { subscriber_price: filters.subscriberPrice }));
3667
+ })), ((filters.subscriberPrice || filters.subscriberPrice === 0) && {
3668
+ subscriber_price: filters.subscriberPrice,
3669
+ }));
3640
3670
  },
3641
3671
  bindPersistData: function (value) {
3642
3672
  var priceData = Object.values(value).shift();
3643
- return Object.assign(Object.assign(Object.assign(Object.assign({}, ((priceData === null || priceData === void 0 ? void 0 : priceData.price) && { price: priceData.price })), (priceData.fullPrice && { full_price: priceData.fullPrice })), (priceData.subscriberDiscountPercentage && {
3673
+ return Object.assign(Object.assign(Object.assign(Object.assign({}, ((priceData === null || priceData === void 0 ? void 0 : priceData.price) >= 0 && { price: priceData.price })), (priceData.fullPrice >= 0 && { full_price: priceData.fullPrice })), (priceData.subscriberDiscountPercentage >= 0 && {
3644
3674
  subscriber_discount_percentage: priceData.subscriberDiscountPercentage,
3645
- })), (priceData.subscriberPrice && { subscriber_price: priceData.subscriberPrice }));
3675
+ })), (priceData.subscriberPrice >= 0 && { subscriber_price: priceData.subscriberPrice }));
3646
3676
  },
3647
3677
  },
3648
3678
  },
@@ -3676,58 +3706,111 @@
3676
3706
  { createdAt: { columnName: 'created_at' } },
3677
3707
  { updatedAt: { columnName: 'updated_at' } },
3678
3708
  ];
3679
- _this = _super_1.call(this, {
3680
- tableName: 'product',
3681
- model: ProductHasuraGraphQL,
3682
- endpoint: endpoint,
3683
- authOptions: authOptions,
3684
- fields: __spreadArray(__spreadArray([], __read(commonFields)), [
3685
- {
3686
- categories: {
3687
- columnName: 'categories',
3688
- fields: ['category_id'],
3689
- bindPersistData: function (value) { return ({
3690
- categories: { data: value.map(function (category) { return ({ category_id: +category }); }) },
3691
- }); },
3692
- to: function (categories) { return categories.map(function (categoryId) { return +categoryId; }); },
3693
- from: function (categories) { return (categories === null || categories === void 0 ? void 0 : categories.map(function (category) { var _a; return (_a = category === null || category === void 0 ? void 0 : category.category_id) === null || _a === void 0 ? void 0 : _a.toString(); })) || []; },
3694
- },
3709
+ _this.fields = __spreadArray(__spreadArray([], __read(commonFields)), [
3710
+ {
3711
+ categories: {
3712
+ columnName: 'categories',
3713
+ fields: ['category_id'],
3714
+ bindPersistData: function (value) { return ({
3715
+ categories: { data: value.map(function (category) { return ({ category_id: +category }); }) },
3716
+ }); },
3717
+ to: function (categories) { return categories.map(function (categoryId) { return +categoryId; }); },
3718
+ from: function (categories) { return (categories === null || categories === void 0 ? void 0 : categories.map(function (category) { var _a; return (_a = category === null || category === void 0 ? void 0 : category.category_id) === null || _a === void 0 ? void 0 : _a.toString(); })) || []; },
3695
3719
  },
3696
- {
3697
- kitProducts: {
3698
- columnName: 'kit_products',
3699
- foreignKeyColumn: { productId: 'id' },
3700
- fields: [
3701
- { productId: { columnName: 'product_id' } },
3702
- { kitProductId: { columnName: 'kit_product_id' } },
3703
- 'quantity',
3704
- { product: { columnName: 'product', foreignKeyColumn: { id: 'product_id' }, fields: commonFields } },
3705
- ],
3706
- },
3720
+ },
3721
+ {
3722
+ kitProducts: {
3723
+ columnName: 'kit_products',
3724
+ foreignKeyColumn: { productId: 'id' },
3725
+ fields: [
3726
+ { productId: { columnName: 'product_id' } },
3727
+ { kitProductId: { columnName: 'kit_product_id' } },
3728
+ 'quantity',
3729
+ { product: { columnName: 'product', foreignKeyColumn: { id: 'product_id' }, fields: commonFields } },
3730
+ ],
3707
3731
  },
3708
- ]),
3709
- }) || this;
3732
+ },
3733
+ ]);
3710
3734
  return _this;
3711
3735
  }
3736
+ Object.defineProperty(ProductHasuraGraphQLRepository.prototype, "reviewsFields", {
3737
+ get: function () {
3738
+ return [
3739
+ 'id',
3740
+ 'shop',
3741
+ 'rate',
3742
+ 'author',
3743
+ 'email',
3744
+ 'location',
3745
+ 'review',
3746
+ 'status',
3747
+ 'title',
3748
+ 'person_id',
3749
+ 'points',
3750
+ 'order_id',
3751
+ 'created_at',
3752
+ 'updated_at',
3753
+ ];
3754
+ },
3755
+ enumerable: false,
3756
+ configurable: true
3757
+ });
3758
+ ProductHasuraGraphQLRepository.prototype.create = function (data) {
3759
+ var _super = Object.create(null, {
3760
+ create: { get: function () { return _super_1.prototype.create; } }
3761
+ });
3762
+ return __awaiter(this, void 0, void 0, function () {
3763
+ var product, _c, error_1;
3764
+ return __generator(this, function (_d) {
3765
+ switch (_d.label) {
3766
+ case 0: return [4 /*yield*/, _super.create.call(this, lodash.omit(data, ['reviews']))];
3767
+ case 1:
3768
+ product = _d.sent();
3769
+ _d.label = 2;
3770
+ case 2:
3771
+ _d.trys.push([2, 4, , 6]);
3772
+ _c = product;
3773
+ return [4 /*yield*/, this.updateReviews(+product.id, data)];
3774
+ case 3:
3775
+ _c.reviews = _d.sent();
3776
+ return [3 /*break*/, 6];
3777
+ case 4:
3778
+ error_1 = _d.sent();
3779
+ return [4 /*yield*/, this.delete({ id: product.id })];
3780
+ case 5:
3781
+ _d.sent();
3782
+ throw error_1;
3783
+ case 6: return [2 /*return*/, product];
3784
+ }
3785
+ });
3786
+ });
3787
+ };
3712
3788
  ProductHasuraGraphQLRepository.prototype.get = function (identifiers) {
3713
3789
  var _super = Object.create(null, {
3714
3790
  get: { get: function () { return _super_1.prototype.get; } }
3715
3791
  });
3716
3792
  var _a;
3717
3793
  return __awaiter(this, void 0, void 0, function () {
3718
- var _c;
3719
- return __generator(this, function (_d) {
3720
- switch (_d.label) {
3794
+ var product, _c, _d;
3795
+ return __generator(this, function (_e) {
3796
+ switch (_e.label) {
3721
3797
  case 0:
3722
3798
  if (!Number.isNaN(+identifiers.id)) return [3 /*break*/, 2];
3723
3799
  return [4 /*yield*/, this.find({ filters: { firestoreId: identifiers.id } })];
3724
3800
  case 1:
3725
- _c = (_a = (_d.sent()).data) === null || _a === void 0 ? void 0 : _a[0];
3726
- return [3 /*break*/, 3];
3727
- case 2:
3728
- _c = _super.get.call(this, identifiers);
3729
- _d.label = 3;
3730
- case 3: return [2 /*return*/, _c];
3801
+ _c = (_a = (_e.sent()).data) === null || _a === void 0 ? void 0 : _a[0];
3802
+ return [3 /*break*/, 4];
3803
+ case 2: return [4 /*yield*/, _super.get.call(this, identifiers)];
3804
+ case 3:
3805
+ _c = _e.sent();
3806
+ _e.label = 4;
3807
+ case 4:
3808
+ product = _c;
3809
+ _d = product;
3810
+ return [4 /*yield*/, this.findReviewsByProduct(+product.id)];
3811
+ case 5:
3812
+ _d.reviews = _e.sent();
3813
+ return [2 /*return*/, product];
3731
3814
  }
3732
3815
  });
3733
3816
  });
@@ -3756,25 +3839,25 @@
3756
3839
  update: { get: function () { return _super_1.prototype.update; } }
3757
3840
  });
3758
3841
  return __awaiter(this, void 0, void 0, function () {
3759
- var categories, kitProducts, checkId, data, plainData, id, product, _c, _d, _e, _f;
3760
- return __generator(this, function (_g) {
3761
- switch (_g.label) {
3842
+ var categories, kitProducts, reviews, checkId, data, plainData, id, product, _c, _d, _e, _f, _g, _h;
3843
+ return __generator(this, function (_j) {
3844
+ switch (_j.label) {
3762
3845
  case 0:
3763
- categories = params.categories, kitProducts = params.kitProducts, checkId = params.id, data = __rest(params, ["categories", "kitProducts", "id"]);
3846
+ categories = params.categories, kitProducts = params.kitProducts, reviews = params.reviews, checkId = params.id, data = __rest(params, ["categories", "kitProducts", "reviews", "id"]);
3764
3847
  plainData = this.paramsToPlain({ id: checkId });
3765
3848
  return [4 /*yield*/, this.getId(plainData.id)];
3766
3849
  case 1:
3767
- id = _g.sent();
3850
+ id = _j.sent();
3768
3851
  return [4 /*yield*/, _super.update.call(this, Object.assign({ id: id }, data))];
3769
3852
  case 2:
3770
- product = _g.sent();
3853
+ product = _j.sent();
3771
3854
  _c = product;
3772
3855
  _d = categories;
3773
3856
  if (!_d) return [3 /*break*/, 4];
3774
3857
  return [4 /*yield*/, this.updateCategories(+id, { categories: categories })];
3775
3858
  case 3:
3776
- _d = (_g.sent());
3777
- _g.label = 4;
3859
+ _d = (_j.sent());
3860
+ _j.label = 4;
3778
3861
  case 4:
3779
3862
  _c.categories = _d;
3780
3863
  _e = product;
@@ -3782,10 +3865,19 @@
3782
3865
  if (!_f) return [3 /*break*/, 6];
3783
3866
  return [4 /*yield*/, this.updateKitProducts(+id, { kitProducts: kitProducts })];
3784
3867
  case 5:
3785
- _f = (_g.sent());
3786
- _g.label = 6;
3868
+ _f = (_j.sent());
3869
+ _j.label = 6;
3787
3870
  case 6:
3788
3871
  _e.kitProducts = _f;
3872
+ _g = product;
3873
+ _h = reviews;
3874
+ if (!_h) return [3 /*break*/, 8];
3875
+ return [4 /*yield*/, this.updateReviews(+id, { reviews: reviews })];
3876
+ case 7:
3877
+ _h = (_j.sent());
3878
+ _j.label = 8;
3879
+ case 8:
3880
+ _g.reviews = _h;
3789
3881
  return [2 /*return*/, product];
3790
3882
  }
3791
3883
  });
@@ -3844,7 +3936,7 @@
3844
3936
  type: '[product_kit_insert_input!]',
3845
3937
  required: true,
3846
3938
  value: plainData.kitProducts.map(function (kitProduct) { return ({
3847
- kit_product_id: kitProduct.kitProductId,
3939
+ kit_product_id: kitProduct.productId || kitProduct.product.id,
3848
3940
  product_id: productId,
3849
3941
  quantity: kitProduct.quantity,
3850
3942
  }); }),
@@ -3857,6 +3949,75 @@
3857
3949
  });
3858
3950
  });
3859
3951
  };
3952
+ ProductHasuraGraphQLRepository.prototype.updateReviews = function (productId, _c) {
3953
+ var reviews = _c.reviews;
3954
+ return __awaiter(this, void 0, void 0, function () {
3955
+ var reviewIds_1, plainData;
3956
+ var _this = this;
3957
+ return __generator(this, function (_c) {
3958
+ switch (_c.label) {
3959
+ case 0:
3960
+ if (!('action' in reviews && reviews.action === 'remove')) return [3 /*break*/, 3];
3961
+ return [4 /*yield*/, Promise.all(reviews.value.map(function (reviewData) { return __awaiter(_this, void 0, void 0, function () {
3962
+ var review;
3963
+ return __generator(this, function (_c) {
3964
+ switch (_c.label) {
3965
+ case 0: return [4 /*yield*/, this.findReview(reviewData, productId)];
3966
+ case 1:
3967
+ review = _c.sent();
3968
+ return [2 /*return*/, review === null || review === void 0 ? void 0 : review.id];
3969
+ }
3970
+ });
3971
+ }); }))];
3972
+ case 1:
3973
+ reviewIds_1 = _c.sent();
3974
+ return [4 /*yield*/, this.mutation('delete_product_review', ['affected_rows'], {
3975
+ where: { value: { id: { _in: reviewIds_1.filter(Boolean) } }, type: 'product_review_bool_exp', required: true },
3976
+ })];
3977
+ case 2:
3978
+ _c.sent();
3979
+ return [2 /*return*/, reviews.value.map(function (review, index) { return !reviewIds_1[index] && review; }).filter(Boolean)];
3980
+ case 3:
3981
+ plainData = this.paramsToPlain({ reviews: reviews });
3982
+ return [2 /*return*/, Promise.all(plainData.reviews.map(function (reviewData) { return __awaiter(_this, void 0, void 0, function () {
3983
+ var review, _c, _d;
3984
+ return __generator(this, function (_e) {
3985
+ switch (_e.label) {
3986
+ case 0: return [4 /*yield*/, this.findReview(reviewData, productId)];
3987
+ case 1:
3988
+ review = _e.sent();
3989
+ if (!review) return [3 /*break*/, 3];
3990
+ _c = this.bindReviewToModel;
3991
+ return [4 /*yield*/, this.mutation('update_product_review_by_pk', this.reviewsFields, {
3992
+ pk_columns: {
3993
+ value: { id: review.id },
3994
+ type: 'product_review_pk_columns_input',
3995
+ required: true,
3996
+ },
3997
+ _set: {
3998
+ value: lodash.omit(this.bindReviewToHasura(reviewData), ['id', 'product_id']),
3999
+ type: 'product_review_set_input',
4000
+ required: true,
4001
+ },
4002
+ })];
4003
+ case 2: return [2 /*return*/, _c.apply(this, [(_e.sent()).update_product_review_by_pk])];
4004
+ case 3:
4005
+ _d = this.bindReviewToModel;
4006
+ return [4 /*yield*/, this.mutation('insert_product_review_one', this.reviewsFields, {
4007
+ object: {
4008
+ value: lodash.omit(Object.assign(Object.assign({}, this.bindReviewToHasura(reviewData)), { product_id: productId }), ['id']),
4009
+ type: 'product_review_insert_input',
4010
+ required: true,
4011
+ },
4012
+ })];
4013
+ case 4: return [2 /*return*/, _d.apply(this, [(_e.sent()).insert_product_review_one])];
4014
+ }
4015
+ });
4016
+ }); }))];
4017
+ }
4018
+ });
4019
+ });
4020
+ };
3860
4021
  ProductHasuraGraphQLRepository.prototype.getId = function (id) {
3861
4022
  var _a, _b;
3862
4023
  return __awaiter(this, void 0, void 0, function () {
@@ -3876,6 +4037,97 @@
3876
4037
  });
3877
4038
  });
3878
4039
  };
4040
+ ProductHasuraGraphQLRepository.prototype.findReviewsByProduct = function (productId) {
4041
+ return __awaiter(this, void 0, void 0, function () {
4042
+ var data;
4043
+ var _this = this;
4044
+ return __generator(this, function (_c) {
4045
+ switch (_c.label) {
4046
+ case 0: return [4 /*yield*/, this.query('product_review', this.reviewsFields, {
4047
+ where: {
4048
+ value: {
4049
+ product_id: { _eq: productId },
4050
+ },
4051
+ type: 'product_review_bool_exp',
4052
+ required: true,
4053
+ },
4054
+ })];
4055
+ case 1:
4056
+ data = (_c.sent()).product_review;
4057
+ return [2 /*return*/, data && data.map(function (review) { return _this.bindReviewToModel(review); })];
4058
+ }
4059
+ });
4060
+ });
4061
+ };
4062
+ ProductHasuraGraphQLRepository.prototype.findReview = function (review, productId) {
4063
+ return __awaiter(this, void 0, void 0, function () {
4064
+ var loadedReview;
4065
+ return __generator(this, function (_c) {
4066
+ switch (_c.label) {
4067
+ case 0:
4068
+ if (review.id)
4069
+ return [2 /*return*/, review];
4070
+ if (!review.personId) return [3 /*break*/, 2];
4071
+ return [4 /*yield*/, this.getReviewByPersonId(review.personId, productId)];
4072
+ case 1:
4073
+ loadedReview = _c.sent();
4074
+ _c.label = 2;
4075
+ case 2:
4076
+ if (!(!loadedReview && review.author && review.email)) return [3 /*break*/, 4];
4077
+ return [4 /*yield*/, this.getReviewByAuthorAndEmail(review.author, review.email, productId)];
4078
+ case 3:
4079
+ loadedReview = _c.sent();
4080
+ _c.label = 4;
4081
+ case 4: return [2 /*return*/, loadedReview || review];
4082
+ }
4083
+ });
4084
+ });
4085
+ };
4086
+ ProductHasuraGraphQLRepository.prototype.getReviewByPersonId = function (personId, productId) {
4087
+ return __awaiter(this, void 0, void 0, function () {
4088
+ var data;
4089
+ return __generator(this, function (_c) {
4090
+ switch (_c.label) {
4091
+ case 0: return [4 /*yield*/, this.query('product_review', this.reviewsFields, {
4092
+ where: {
4093
+ value: {
4094
+ product_id: { _eq: productId },
4095
+ person_id: { _eq: personId },
4096
+ },
4097
+ type: "product_review_bool_exp",
4098
+ required: true,
4099
+ },
4100
+ })];
4101
+ case 1:
4102
+ data = (_c.sent()).product_review;
4103
+ return [2 /*return*/, data && data[0] && this.bindReviewToModel(data[0])];
4104
+ }
4105
+ });
4106
+ });
4107
+ };
4108
+ ProductHasuraGraphQLRepository.prototype.getReviewByAuthorAndEmail = function (author, email, productId) {
4109
+ return __awaiter(this, void 0, void 0, function () {
4110
+ var data;
4111
+ return __generator(this, function (_c) {
4112
+ switch (_c.label) {
4113
+ case 0: return [4 /*yield*/, this.query('product_review', this.reviewsFields, {
4114
+ where: {
4115
+ value: {
4116
+ product_id: { _eq: productId },
4117
+ author: { _eq: author },
4118
+ email: { _eq: email },
4119
+ },
4120
+ type: "product_review_bool_exp",
4121
+ required: true,
4122
+ },
4123
+ })];
4124
+ case 1:
4125
+ data = (_c.sent()).product_review;
4126
+ return [2 /*return*/, data && data[0] && this.bindReviewToModel(data[0])];
4127
+ }
4128
+ });
4129
+ });
4130
+ };
3879
4131
  return ProductHasuraGraphQLRepository;
3880
4132
  }(withCrudHasuraGraphQL(withHasuraGraphQL(Base))));
3881
4133