@infrab4a/connect 1.0.0-beta.19 → 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.
@@ -3,7 +3,7 @@ import { plainToInstance, instanceToPlain, Expose, Type } from 'class-transforme
3
3
  import { __decorate, __metadata, __awaiter, __rest } from 'tslib';
4
4
  import { parseISO } from 'date-fns';
5
5
  export { add, addBusinessDays, addDays, addMonths, addYears, endOfDay, format, formatISO9075, parseISO, startOfDay, sub } from 'date-fns';
6
- import { isString, isNil, isNumber, isDate, set, isObject, isEmpty, chunk, isBoolean, isInteger, isNaN as isNaN$1 } from 'lodash';
6
+ import { isString, isNil, isNumber, isDate, set, isObject, isEmpty, chunk, isBoolean, isInteger, isNaN as isNaN$1, omit } from 'lodash';
7
7
  export { chunk, isBoolean, isDate, isEmpty, isInteger, isNaN, isNil, isNumber, isObject, isString, now, omit, pick, set } from 'lodash';
8
8
  import { Md5 } from 'ts-md5';
9
9
  import axios from 'axios';
@@ -1967,7 +1967,11 @@ const withCreateHasuraGraphQL = (MixinBase) => {
1967
1967
  columnOptions.foreignKeyColumn && [
1968
1968
  ...Object.values(columnOptions.foreignKeyColumn),
1969
1969
  {
1970
- [columnOptions.columnName]: Object.keys(columnOptions.foreignKeyColumn),
1970
+ [columnOptions.columnName]: Object.keys(columnOptions.foreignKeyColumn).map((foreignKeyField) => {
1971
+ var _a;
1972
+ return ((_a = AttributeOptionHelper.FindByAttribute(foreignKeyField, columnOptions === null || columnOptions === void 0 ? void 0 : columnOptions.fields)) === null || _a === void 0 ? void 0 : _a.columnName) ||
1973
+ foreignKeyField;
1974
+ }),
1971
1975
  },
1972
1976
  ]);
1973
1977
  })
@@ -2432,6 +2436,15 @@ class CategoryHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGr
2432
2436
 
2433
2437
  class ProductHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGraphQL(Base)) {
2434
2438
  constructor(endpoint, authOptions) {
2439
+ super({
2440
+ tableName: 'product',
2441
+ model: ProductHasuraGraphQL,
2442
+ endpoint,
2443
+ authOptions,
2444
+ fields: [],
2445
+ });
2446
+ this.bindReviewToModel = (plain) => (Object.assign(Object.assign({}, 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 }));
2447
+ this.bindReviewToHasura = (review) => (Object.assign(Object.assign({}, omit(review, ['productId', 'createdAt', 'updatedAt', 'personId', 'orderId'])), { person_id: review.personId, order_id: review.orderId }));
2435
2448
  const commonFields = [
2436
2449
  { id: { columnName: 'id', to: (value) => +value, from: (value) => value.toString() } },
2437
2450
  { firestoreId: { columnName: 'firestore_id' } },
@@ -2506,37 +2519,65 @@ class ProductHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGra
2506
2519
  { createdAt: { columnName: 'created_at' } },
2507
2520
  { updatedAt: { columnName: 'updated_at' } },
2508
2521
  ];
2509
- super({
2510
- tableName: 'product',
2511
- model: ProductHasuraGraphQL,
2512
- endpoint,
2513
- authOptions,
2514
- fields: [
2515
- ...commonFields,
2516
- {
2517
- categories: {
2518
- columnName: 'categories',
2519
- fields: ['category_id'],
2520
- bindPersistData: (value) => ({
2521
- categories: { data: value.map((category) => ({ category_id: +category })) },
2522
- }),
2523
- to: (categories) => categories.map((categoryId) => +categoryId),
2524
- from: (categories) => (categories === null || categories === void 0 ? void 0 : categories.map((category) => { var _a; return (_a = category === null || category === void 0 ? void 0 : category.category_id) === null || _a === void 0 ? void 0 : _a.toString(); })) || [],
2525
- },
2522
+ this.fields = [
2523
+ ...commonFields,
2524
+ {
2525
+ categories: {
2526
+ columnName: 'categories',
2527
+ fields: ['category_id'],
2528
+ bindPersistData: (value) => ({
2529
+ categories: { data: value.map((category) => ({ category_id: +category })) },
2530
+ }),
2531
+ to: (categories) => categories.map((categoryId) => +categoryId),
2532
+ from: (categories) => (categories === null || categories === void 0 ? void 0 : categories.map((category) => { var _a; return (_a = category === null || category === void 0 ? void 0 : category.category_id) === null || _a === void 0 ? void 0 : _a.toString(); })) || [],
2526
2533
  },
2527
- {
2528
- kitProducts: {
2529
- columnName: 'kit_products',
2530
- foreignKeyColumn: { productId: 'id' },
2531
- fields: [
2532
- { productId: { columnName: 'product_id' } },
2533
- { kitProductId: { columnName: 'kit_product_id' } },
2534
- 'quantity',
2535
- { product: { columnName: 'product', foreignKeyColumn: { id: 'product_id' }, fields: commonFields } },
2536
- ],
2537
- },
2534
+ },
2535
+ {
2536
+ kitProducts: {
2537
+ columnName: 'kit_products',
2538
+ foreignKeyColumn: { productId: 'id' },
2539
+ fields: [
2540
+ { productId: { columnName: 'product_id' } },
2541
+ { kitProductId: { columnName: 'kit_product_id' } },
2542
+ 'quantity',
2543
+ { product: { columnName: 'product', foreignKeyColumn: { id: 'product_id' }, fields: commonFields } },
2544
+ ],
2538
2545
  },
2539
- ],
2546
+ },
2547
+ ];
2548
+ }
2549
+ get reviewsFields() {
2550
+ return [
2551
+ 'id',
2552
+ 'shop',
2553
+ 'rate',
2554
+ 'author',
2555
+ 'email',
2556
+ 'location',
2557
+ 'review',
2558
+ 'status',
2559
+ 'title',
2560
+ 'person_id',
2561
+ 'points',
2562
+ 'order_id',
2563
+ 'created_at',
2564
+ 'updated_at',
2565
+ ];
2566
+ }
2567
+ create(data) {
2568
+ const _super = Object.create(null, {
2569
+ create: { get: () => super.create }
2570
+ });
2571
+ return __awaiter(this, void 0, void 0, function* () {
2572
+ const product = yield _super.create.call(this, omit(data, ['reviews']));
2573
+ try {
2574
+ product.reviews = yield this.updateReviews(+product.id, data);
2575
+ }
2576
+ catch (error) {
2577
+ yield this.delete({ id: product.id });
2578
+ throw error;
2579
+ }
2580
+ return product;
2540
2581
  });
2541
2582
  }
2542
2583
  get(identifiers) {
@@ -2545,9 +2586,11 @@ class ProductHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGra
2545
2586
  });
2546
2587
  var _a;
2547
2588
  return __awaiter(this, void 0, void 0, function* () {
2548
- return Number.isNaN(+identifiers.id)
2589
+ const product = Number.isNaN(+identifiers.id)
2549
2590
  ? (_a = (yield this.find({ filters: { firestoreId: identifiers.id } })).data) === null || _a === void 0 ? void 0 : _a[0]
2550
- : _super.get.call(this, identifiers);
2591
+ : yield _super.get.call(this, identifiers);
2592
+ product.reviews = yield this.findReviewsByProduct(+product.id);
2593
+ return product;
2551
2594
  });
2552
2595
  }
2553
2596
  getBySlug(slug, shop) {
@@ -2567,12 +2610,13 @@ class ProductHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGra
2567
2610
  update: { get: () => super.update }
2568
2611
  });
2569
2612
  return __awaiter(this, void 0, void 0, function* () {
2570
- const { categories, kitProducts, id: checkId } = params, data = __rest(params, ["categories", "kitProducts", "id"]);
2613
+ const { categories, kitProducts, reviews, id: checkId } = params, data = __rest(params, ["categories", "kitProducts", "reviews", "id"]);
2571
2614
  const plainData = this.paramsToPlain({ id: checkId });
2572
2615
  const id = yield this.getId(plainData.id);
2573
2616
  const product = yield _super.update.call(this, Object.assign({ id }, data));
2574
2617
  product.categories = categories && (yield this.updateCategories(+id, { categories }));
2575
2618
  product.kitProducts = kitProducts && (yield this.updateKitProducts(+id, { kitProducts }));
2619
+ product.reviews = reviews && (yield this.updateReviews(+id, { reviews }));
2576
2620
  return product;
2577
2621
  });
2578
2622
  }
@@ -2620,6 +2664,44 @@ class ProductHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGra
2620
2664
  return plainData.kitProducts;
2621
2665
  });
2622
2666
  }
2667
+ updateReviews(productId, { reviews }) {
2668
+ return __awaiter(this, void 0, void 0, function* () {
2669
+ if ('action' in reviews && reviews.action === 'remove') {
2670
+ const reviewIds = yield Promise.all(reviews.value.map((reviewData) => __awaiter(this, void 0, void 0, function* () {
2671
+ const review = yield this.findReview(reviewData, productId);
2672
+ return review === null || review === void 0 ? void 0 : review.id;
2673
+ })));
2674
+ yield this.mutation('delete_product_review', ['affected_rows'], {
2675
+ where: { value: { id: { _in: reviewIds.filter(Boolean) } }, type: 'product_review_bool_exp', required: true },
2676
+ });
2677
+ return reviews.value.map((review, index) => !reviewIds[index] && review).filter(Boolean);
2678
+ }
2679
+ const plainData = this.paramsToPlain({ reviews });
2680
+ return Promise.all(plainData.reviews.map((reviewData) => __awaiter(this, void 0, void 0, function* () {
2681
+ const review = yield this.findReview(reviewData, productId);
2682
+ if (review)
2683
+ return this.bindReviewToModel((yield this.mutation('update_product_review_by_pk', this.reviewsFields, {
2684
+ pk_columns: {
2685
+ value: { id: review.id },
2686
+ type: 'product_review_pk_columns_input',
2687
+ required: true,
2688
+ },
2689
+ _set: {
2690
+ value: omit(this.bindReviewToHasura(reviewData), ['id', 'product_id']),
2691
+ type: 'product_review_set_input',
2692
+ required: true,
2693
+ },
2694
+ })).update_product_review_by_pk);
2695
+ return this.bindReviewToModel((yield this.mutation('insert_product_review_one', this.reviewsFields, {
2696
+ object: {
2697
+ value: omit(Object.assign(Object.assign({}, this.bindReviewToHasura(reviewData)), { product_id: productId }), ['id']),
2698
+ type: 'product_review_insert_input',
2699
+ required: true,
2700
+ },
2701
+ })).insert_product_review_one);
2702
+ })));
2703
+ });
2704
+ }
2623
2705
  getId(id) {
2624
2706
  var _a, _b;
2625
2707
  return __awaiter(this, void 0, void 0, function* () {
@@ -2631,6 +2713,63 @@ class ProductHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGra
2631
2713
  throw new NotFoundError(`Product with id ${id} not found`);
2632
2714
  });
2633
2715
  }
2716
+ findReviewsByProduct(productId) {
2717
+ return __awaiter(this, void 0, void 0, function* () {
2718
+ const { product_review: data } = yield this.query('product_review', this.reviewsFields, {
2719
+ where: {
2720
+ value: {
2721
+ product_id: { _eq: productId },
2722
+ },
2723
+ type: 'product_review_bool_exp',
2724
+ required: true,
2725
+ },
2726
+ });
2727
+ return data && data.map((review) => this.bindReviewToModel(review));
2728
+ });
2729
+ }
2730
+ findReview(review, productId) {
2731
+ return __awaiter(this, void 0, void 0, function* () {
2732
+ if (review.id)
2733
+ return review;
2734
+ let loadedReview;
2735
+ if (review.personId)
2736
+ loadedReview = yield this.getReviewByPersonId(review.personId, productId);
2737
+ if (!loadedReview && review.author && review.email)
2738
+ loadedReview = yield this.getReviewByAuthorAndEmail(review.author, review.email, productId);
2739
+ return loadedReview || review;
2740
+ });
2741
+ }
2742
+ getReviewByPersonId(personId, productId) {
2743
+ return __awaiter(this, void 0, void 0, function* () {
2744
+ const { product_review: data } = yield this.query('product_review', this.reviewsFields, {
2745
+ where: {
2746
+ value: {
2747
+ product_id: { _eq: productId },
2748
+ person_id: { _eq: personId },
2749
+ },
2750
+ type: `product_review_bool_exp`,
2751
+ required: true,
2752
+ },
2753
+ });
2754
+ return data && data[0] && this.bindReviewToModel(data[0]);
2755
+ });
2756
+ }
2757
+ getReviewByAuthorAndEmail(author, email, productId) {
2758
+ return __awaiter(this, void 0, void 0, function* () {
2759
+ const { product_review: data } = yield this.query('product_review', this.reviewsFields, {
2760
+ where: {
2761
+ value: {
2762
+ product_id: { _eq: productId },
2763
+ author: { _eq: author },
2764
+ email: { _eq: email },
2765
+ },
2766
+ type: `product_review_bool_exp`,
2767
+ required: true,
2768
+ },
2769
+ });
2770
+ return data && data[0] && this.bindReviewToModel(data[0]);
2771
+ });
2772
+ }
2634
2773
  }
2635
2774
 
2636
2775
  class VariantHasuraGraphQLRepository extends withCrudHasuraGraphQL(withHasuraGraphQL(Base)) {