@labdigital/commercetools-mock 2.66.0 → 3.0.0-beta.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/README.md +31 -8
- package/dist/abstract-BKFcva6S.mjs +1044 -0
- package/dist/abstract-BKFcva6S.mjs.map +1 -0
- package/dist/config-BcNSzPZz.d.mts +1718 -0
- package/dist/index.d.mts +50 -1633
- package/dist/index.mjs +3769 -2653
- package/dist/index.mjs.map +1 -1
- package/dist/storage/sqlite.d.mts +59 -0
- package/dist/storage/sqlite.mjs +234 -0
- package/dist/storage/sqlite.mjs.map +1 -0
- package/package.json +26 -29
- package/src/ctMock.ts +125 -136
- package/src/helpers.ts +14 -6
- package/src/index.ts +5 -0
- package/src/lib/masking.ts +4 -5
- package/src/lib/product-review-statistics.test.ts +257 -294
- package/src/lib/review-statistics.ts +17 -4
- package/src/oauth/helpers.ts +7 -4
- package/src/oauth/server.test.ts +102 -62
- package/src/oauth/server.ts +215 -213
- package/src/oauth/store.ts +20 -6
- package/src/orderSearch.ts +3 -3
- package/src/product-projection-search.ts +38 -20
- package/src/product-search-availability.test.ts +31 -52
- package/src/product-search.ts +19 -10
- package/src/projectAPI.ts +6 -22
- package/src/repositories/abstract.ts +182 -48
- package/src/repositories/as-associate.test.ts +19 -19
- package/src/repositories/associate-role.ts +12 -23
- package/src/repositories/attribute-group.test.ts +23 -23
- package/src/repositories/attribute-group.ts +6 -4
- package/src/repositories/business-unit.test.ts +63 -57
- package/src/repositories/business-unit.ts +107 -55
- package/src/repositories/cart/actions.ts +96 -65
- package/src/repositories/cart/helpers.ts +15 -11
- package/src/repositories/cart/index.test.ts +136 -30
- package/src/repositories/cart/index.ts +76 -59
- package/src/repositories/cart-discount/actions.ts +12 -44
- package/src/repositories/cart-discount/index.ts +20 -8
- package/src/repositories/category/actions.ts +27 -27
- package/src/repositories/category/index.test.ts +13 -9
- package/src/repositories/category/index.ts +40 -23
- package/src/repositories/channel.test.ts +53 -51
- package/src/repositories/channel.ts +12 -22
- package/src/repositories/custom-object.ts +34 -25
- package/src/repositories/customer/actions.ts +47 -25
- package/src/repositories/customer/index.test.ts +11 -11
- package/src/repositories/customer/index.ts +65 -35
- package/src/repositories/customer-group.test.ts +44 -42
- package/src/repositories/customer-group.ts +12 -22
- package/src/repositories/discount-code/actions.ts +3 -19
- package/src/repositories/discount-code/index.ts +9 -4
- package/src/repositories/discount-group/index.ts +8 -3
- package/src/repositories/extension.test.ts +27 -27
- package/src/repositories/extension.ts +10 -5
- package/src/repositories/helpers.ts +126 -47
- package/src/repositories/inventory-entry/actions.ts +3 -24
- package/src/repositories/inventory-entry/index.ts +19 -11
- package/src/repositories/my-customer.ts +13 -12
- package/src/repositories/my-order.ts +5 -2
- package/src/repositories/order/actions.ts +84 -56
- package/src/repositories/order/index.test.ts +36 -31
- package/src/repositories/order/index.ts +83 -49
- package/src/repositories/order-edit.ts +8 -3
- package/src/repositories/payment/actions.ts +64 -44
- package/src/repositories/payment/helpers.ts +3 -3
- package/src/repositories/payment/index.ts +28 -12
- package/src/repositories/product/actions.ts +133 -98
- package/src/repositories/product/helpers.ts +29 -16
- package/src/repositories/product/index.ts +42 -25
- package/src/repositories/product-discount.ts +6 -4
- package/src/repositories/product-projection.ts +41 -21
- package/src/repositories/product-selection.ts +8 -15
- package/src/repositories/product-tailoring.ts +22 -3
- package/src/repositories/product-type.ts +45 -4
- package/src/repositories/project.ts +57 -13
- package/src/repositories/quote/actions.ts +5 -28
- package/src/repositories/quote/index.ts +29 -6
- package/src/repositories/quote-request/actions.ts +5 -28
- package/src/repositories/quote-request/index.test.ts +3 -3
- package/src/repositories/quote-request/index.ts +31 -11
- package/src/repositories/quote-staged/actions.ts +5 -28
- package/src/repositories/quote-staged/index.ts +22 -8
- package/src/repositories/recurrence-policy/index.ts +6 -4
- package/src/repositories/recurring-order/actions.ts +7 -32
- package/src/repositories/recurring-order/index.ts +8 -6
- package/src/repositories/review.test.ts +147 -142
- package/src/repositories/review.ts +31 -37
- package/src/repositories/shipping-method/actions.ts +11 -28
- package/src/repositories/shipping-method/index.ts +26 -15
- package/src/repositories/shopping-list/actions.ts +21 -31
- package/src/repositories/shopping-list/index.ts +44 -22
- package/src/repositories/standalone-price.ts +6 -4
- package/src/repositories/state.ts +15 -9
- package/src/repositories/store.ts +21 -32
- package/src/repositories/subscription.test.ts +22 -22
- package/src/repositories/subscription.ts +8 -3
- package/src/repositories/tax-category/index.ts +8 -3
- package/src/repositories/type/actions.ts +21 -3
- package/src/repositories/type/index.ts +5 -3
- package/src/repositories/zone.test.ts +112 -77
- package/src/repositories/zone.ts +5 -3
- package/src/schemas/generated/associate-role.ts +13 -0
- package/src/schemas/generated/attribute-group.ts +12 -0
- package/src/schemas/generated/business-unit.ts +38 -0
- package/src/schemas/generated/cart-discount.ts +33 -0
- package/src/schemas/generated/cart.ts +61 -0
- package/src/schemas/generated/category.ts +25 -0
- package/src/schemas/generated/channel.ts +21 -0
- package/src/schemas/generated/common.ts +1372 -0
- package/src/schemas/generated/custom-object.ts +11 -0
- package/src/schemas/generated/customer-group.ts +11 -0
- package/src/schemas/generated/customer.ts +47 -0
- package/src/schemas/generated/discount-code.ts +25 -0
- package/src/schemas/generated/discount-group.ts +13 -0
- package/src/schemas/generated/extension.ts +15 -0
- package/src/schemas/generated/index.ts +42 -0
- package/src/schemas/generated/inventory-entry.ts +20 -0
- package/src/schemas/generated/my-quote-request.ts +10 -0
- package/src/schemas/generated/order-edit.ts +18 -0
- package/src/schemas/generated/order-from-cart.ts +25 -0
- package/src/schemas/generated/payment.ts +30 -0
- package/src/schemas/generated/product-discount.ts +20 -0
- package/src/schemas/generated/product-selection.ts +18 -0
- package/src/schemas/generated/product-tailoring.ts +26 -0
- package/src/schemas/generated/product-type.ts +12 -0
- package/src/schemas/generated/product.ts +37 -0
- package/src/schemas/generated/quote-request.ts +19 -0
- package/src/schemas/generated/quote.ts +18 -0
- package/src/schemas/generated/recurrence-policy.ts +15 -0
- package/src/schemas/generated/recurring-order.ts +19 -0
- package/src/schemas/generated/review.ts +24 -0
- package/src/schemas/generated/shipping-method.ts +24 -0
- package/src/schemas/generated/shopping-list.ts +28 -0
- package/src/schemas/generated/staged-quote.ts +18 -0
- package/src/schemas/generated/standalone-price.ts +32 -0
- package/src/schemas/generated/state.ts +20 -0
- package/src/schemas/generated/store.ts +23 -0
- package/src/schemas/generated/subscription.ts +20 -0
- package/src/schemas/generated/tax-category.ts +12 -0
- package/src/schemas/generated/type.ts +17 -0
- package/src/schemas/generated/zone.ts +12 -0
- package/src/schemas/update-request.ts +3 -5
- package/src/server.ts +32 -4
- package/src/services/abstract.ts +207 -101
- package/src/services/as-associate-cart.test.ts +28 -36
- package/src/services/as-associate-cart.ts +15 -12
- package/src/services/as-associate-order.test.ts +33 -40
- package/src/services/as-associate-order.ts +15 -12
- package/src/services/as-associate-quote-request.ts +15 -12
- package/src/services/as-associate-shopping-list.test.ts +25 -35
- package/src/services/as-associate-shopping-list.ts +15 -12
- package/src/services/as-associate.test.ts +21 -15
- package/src/services/as-associate.ts +23 -22
- package/src/services/associate-roles.test.ts +16 -22
- package/src/services/associate-roles.ts +2 -2
- package/src/services/attribute-group.test.ts +40 -44
- package/src/services/attribute-group.ts +2 -2
- package/src/services/business-units.test.ts +227 -163
- package/src/services/business-units.ts +2 -2
- package/src/services/cart-discount.test.ts +253 -187
- package/src/services/cart-discount.ts +2 -2
- package/src/services/cart.test.ts +833 -832
- package/src/services/cart.ts +31 -12
- package/src/services/category.test.ts +208 -130
- package/src/services/category.ts +2 -2
- package/src/services/channel.test.ts +39 -44
- package/src/services/channel.ts +2 -2
- package/src/services/custom-object.test.ts +103 -79
- package/src/services/custom-object.ts +106 -38
- package/src/services/customer-group.test.ts +39 -44
- package/src/services/customer-group.ts +2 -2
- package/src/services/customer.test.ts +357 -292
- package/src/services/customer.ts +70 -23
- package/src/services/discount-code.test.ts +57 -68
- package/src/services/discount-code.ts +2 -2
- package/src/services/discount-group.test.ts +111 -134
- package/src/services/discount-group.ts +2 -2
- package/src/services/draft-validation.test.ts +255 -0
- package/src/services/extension.test.ts +39 -44
- package/src/services/extension.ts +2 -2
- package/src/services/inventory-entry.test.ts +106 -87
- package/src/services/inventory-entry.ts +2 -2
- package/src/services/my-business-unit.test.ts +82 -112
- package/src/services/my-business-unit.ts +25 -19
- package/src/services/my-cart.test.ts +46 -41
- package/src/services/my-cart.ts +32 -28
- package/src/services/my-customer.test.ts +153 -88
- package/src/services/my-customer.ts +130 -61
- package/src/services/my-order.ts +15 -12
- package/src/services/my-payment.test.ts +30 -24
- package/src/services/my-payment.ts +2 -2
- package/src/services/my-shopping-list.ts +2 -2
- package/src/services/order.test.ts +332 -276
- package/src/services/order.ts +45 -27
- package/src/services/payment.test.ts +31 -29
- package/src/services/payment.ts +2 -2
- package/src/services/product-discount.test.ts +39 -46
- package/src/services/product-discount.ts +2 -2
- package/src/services/product-projection.test.ts +176 -166
- package/src/services/product-projection.ts +31 -15
- package/src/services/product-selection.test.ts +17 -9
- package/src/services/product-selection.ts +2 -2
- package/src/services/product-type.test.ts +80 -21
- package/src/services/product-type.ts +2 -2
- package/src/services/product.test.ts +569 -534
- package/src/services/product.ts +14 -7
- package/src/services/project.test.ts +22 -12
- package/src/services/project.ts +28 -13
- package/src/services/quote-request.test.ts +36 -39
- package/src/services/quote-request.ts +2 -2
- package/src/services/quote-staged.ts +2 -2
- package/src/services/quote.ts +2 -2
- package/src/services/recurrence-policy.test.ts +114 -139
- package/src/services/recurrence-policy.ts +2 -2
- package/src/services/recurring-order.test.ts +149 -194
- package/src/services/recurring-order.ts +2 -2
- package/src/services/reviews.test.ts +127 -106
- package/src/services/reviews.ts +2 -2
- package/src/services/shipping-method.test.ts +96 -125
- package/src/services/shipping-method.ts +24 -12
- package/src/services/shopping-list.test.ts +183 -141
- package/src/services/shopping-list.ts +2 -2
- package/src/services/standalone-price.test.ts +60 -46
- package/src/services/standalone-price.ts +2 -2
- package/src/services/state.test.ts +20 -25
- package/src/services/state.ts +2 -2
- package/src/services/store.test.ts +26 -45
- package/src/services/store.ts +2 -2
- package/src/services/subscription.test.ts +39 -44
- package/src/services/subscription.ts +2 -2
- package/src/services/tax-category.test.ts +33 -36
- package/src/services/tax-category.ts +2 -2
- package/src/services/type.test.ts +45 -44
- package/src/services/type.ts +2 -2
- package/src/services/zone.test.ts +40 -44
- package/src/services/zone.ts +2 -2
- package/src/shipping.ts +41 -11
- package/src/storage/abstract.ts +248 -17
- package/src/storage/in-memory.ts +147 -290
- package/src/storage/sqlite.ts +429 -0
- package/src/storage/storage-map.ts +75 -0
- package/src/storage/storage.test-helpers.ts +97 -0
- package/src/storage/storage.test.ts +802 -0
- package/src/testing/associate-role.ts +28 -0
- package/src/testing/attribute-group.ts +27 -0
- package/src/testing/business-unit.ts +9 -8
- package/src/testing/cart-discount.ts +34 -0
- package/src/testing/cart.ts +20 -0
- package/src/testing/category.ts +25 -0
- package/src/testing/channel.ts +23 -0
- package/src/testing/custom-object.ts +27 -0
- package/src/testing/customer-group.ts +26 -0
- package/src/testing/customer.ts +36 -33
- package/src/testing/discount-code.ts +29 -0
- package/src/testing/discount-group.ts +27 -0
- package/src/testing/extension.ts +32 -0
- package/src/testing/index.ts +33 -0
- package/src/testing/inventory-entry.ts +26 -0
- package/src/testing/order.ts +27 -0
- package/src/testing/payment.ts +23 -0
- package/src/testing/product-discount.ts +33 -0
- package/src/testing/product-selection.ts +28 -0
- package/src/testing/product-type.ts +27 -0
- package/src/testing/product.ts +38 -0
- package/src/testing/quote-request.ts +29 -0
- package/src/testing/recurrence-policy.ts +33 -0
- package/src/testing/recurring-order.ts +32 -0
- package/src/testing/review.ts +24 -0
- package/src/testing/shipping-method.ts +31 -0
- package/src/testing/shopping-list.ts +25 -0
- package/src/testing/standalone-price.ts +31 -0
- package/src/testing/state.ts +21 -0
- package/src/testing/store.ts +26 -0
- package/src/testing/subscription.ts +38 -0
- package/src/testing/tax-category.ts +27 -0
- package/src/testing/type.ts +9 -6
- package/src/testing/zone.ts +22 -0
- package/src/validate.test.ts +122 -0
- package/src/validate.ts +78 -7
- package/src/.env +0 -0
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { Product } from "@commercetools/platform-sdk";
|
|
2
|
-
import supertest from "supertest";
|
|
3
2
|
import { beforeEach, describe, expect, test } from "vitest";
|
|
4
3
|
import { CommercetoolsMock } from "#src/index.ts";
|
|
4
|
+
import { productDraftFactory } from "#src/testing/product.ts";
|
|
5
|
+
import { productTypeDraftFactory } from "#src/testing/product-type.ts";
|
|
6
|
+
import { reviewDraftFactory } from "#src/testing/review.ts";
|
|
5
7
|
|
|
6
8
|
describe("Product Review Statistics", () => {
|
|
7
9
|
let ctMock: CommercetoolsMock;
|
|
@@ -10,335 +12,296 @@ describe("Product Review Statistics", () => {
|
|
|
10
12
|
beforeEach(async () => {
|
|
11
13
|
ctMock = new CommercetoolsMock();
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
value: {
|
|
28
|
-
currencyCode: "EUR",
|
|
29
|
-
centAmount: 1000,
|
|
30
|
-
},
|
|
15
|
+
const productType = await productTypeDraftFactory(ctMock).create();
|
|
16
|
+
|
|
17
|
+
product = await productDraftFactory(ctMock).create({
|
|
18
|
+
productType: {
|
|
19
|
+
typeId: "product-type",
|
|
20
|
+
id: productType.id,
|
|
21
|
+
},
|
|
22
|
+
masterVariant: {
|
|
23
|
+
sku: "test-sku-1",
|
|
24
|
+
prices: [
|
|
25
|
+
{
|
|
26
|
+
value: {
|
|
27
|
+
currencyCode: "EUR",
|
|
28
|
+
centAmount: 1000,
|
|
31
29
|
},
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
product = productResponse.body;
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
});
|
|
37
34
|
});
|
|
38
35
|
|
|
39
36
|
test("product has no review statistics when no reviews exist", async () => {
|
|
40
|
-
const response = await
|
|
41
|
-
|
|
42
|
-
|
|
37
|
+
const response = await ctMock.app.inject({
|
|
38
|
+
method: "GET",
|
|
39
|
+
url: `/dummy/products/${product.id}`,
|
|
40
|
+
});
|
|
43
41
|
|
|
44
|
-
expect(response.
|
|
45
|
-
expect(response.
|
|
42
|
+
expect(response.statusCode).toBe(200);
|
|
43
|
+
expect(response.json().reviewRatingStatistics).toBeUndefined();
|
|
46
44
|
});
|
|
47
45
|
|
|
48
46
|
test("product has review statistics when reviews exist", async () => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
.
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
expect(
|
|
94
|
-
expect(
|
|
95
|
-
expect(response.body.reviewRatingStatistics.count).toBe(3);
|
|
96
|
-
expect(response.body.reviewRatingStatistics.averageRating).toBe(4.66667);
|
|
97
|
-
expect(response.body.reviewRatingStatistics.highestRating).toBe(5);
|
|
98
|
-
expect(response.body.reviewRatingStatistics.lowestRating).toBe(4);
|
|
99
|
-
expect(response.body.reviewRatingStatistics.ratingsDistribution).toEqual({
|
|
47
|
+
await reviewDraftFactory(ctMock).create({
|
|
48
|
+
authorName: "John Doe",
|
|
49
|
+
title: "Great product!",
|
|
50
|
+
text: "I really love this product.",
|
|
51
|
+
rating: 5,
|
|
52
|
+
target: {
|
|
53
|
+
typeId: "product",
|
|
54
|
+
id: product.id,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
await reviewDraftFactory(ctMock).create({
|
|
59
|
+
authorName: "Jane Smith",
|
|
60
|
+
title: "Good product",
|
|
61
|
+
text: "Pretty good overall.",
|
|
62
|
+
rating: 4,
|
|
63
|
+
target: {
|
|
64
|
+
typeId: "product",
|
|
65
|
+
id: product.id,
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
await reviewDraftFactory(ctMock).create({
|
|
70
|
+
authorName: "Bob Wilson",
|
|
71
|
+
title: "Excellent!",
|
|
72
|
+
text: "Amazing quality.",
|
|
73
|
+
rating: 5,
|
|
74
|
+
target: {
|
|
75
|
+
typeId: "product",
|
|
76
|
+
id: product.id,
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const response = await ctMock.app.inject({
|
|
81
|
+
method: "GET",
|
|
82
|
+
url: `/dummy/products/${product.id}`,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
expect(response.statusCode).toBe(200);
|
|
86
|
+
const body = response.json();
|
|
87
|
+
expect(body.reviewRatingStatistics).toBeDefined();
|
|
88
|
+
expect(body.reviewRatingStatistics.count).toBe(3);
|
|
89
|
+
expect(body.reviewRatingStatistics.averageRating).toBe(4.66667);
|
|
90
|
+
expect(body.reviewRatingStatistics.highestRating).toBe(5);
|
|
91
|
+
expect(body.reviewRatingStatistics.lowestRating).toBe(4);
|
|
92
|
+
expect(body.reviewRatingStatistics.ratingsDistribution).toEqual({
|
|
100
93
|
"4": 1,
|
|
101
94
|
"5": 2,
|
|
102
95
|
});
|
|
103
96
|
});
|
|
104
97
|
|
|
105
98
|
test("product projection has review statistics", async () => {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
.
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
);
|
|
123
|
-
|
|
124
|
-
expect(
|
|
125
|
-
expect(
|
|
126
|
-
expect(
|
|
127
|
-
expect(
|
|
128
|
-
expect(
|
|
129
|
-
expect(
|
|
130
|
-
expect(response.body.reviewRatingStatistics.ratingsDistribution).toEqual({
|
|
99
|
+
await reviewDraftFactory(ctMock).create({
|
|
100
|
+
authorName: "Test User",
|
|
101
|
+
title: "Test Review",
|
|
102
|
+
text: "Test review text.",
|
|
103
|
+
rating: 3,
|
|
104
|
+
target: {
|
|
105
|
+
typeId: "product",
|
|
106
|
+
id: product.id,
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const response = await ctMock.app.inject({
|
|
111
|
+
method: "GET",
|
|
112
|
+
url: `/dummy/product-projections/${product.id}`,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
expect(response.statusCode).toBe(200);
|
|
116
|
+
const body = response.json();
|
|
117
|
+
expect(body.reviewRatingStatistics).toBeDefined();
|
|
118
|
+
expect(body.reviewRatingStatistics.count).toBe(1);
|
|
119
|
+
expect(body.reviewRatingStatistics.averageRating).toBe(3);
|
|
120
|
+
expect(body.reviewRatingStatistics.highestRating).toBe(3);
|
|
121
|
+
expect(body.reviewRatingStatistics.lowestRating).toBe(3);
|
|
122
|
+
expect(body.reviewRatingStatistics.ratingsDistribution).toEqual({
|
|
131
123
|
"3": 1,
|
|
132
124
|
});
|
|
133
125
|
});
|
|
134
126
|
|
|
135
127
|
test("product query includes review statistics", async () => {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
expect(
|
|
162
|
-
expect(
|
|
163
|
-
expect(
|
|
164
|
-
expect(
|
|
165
|
-
expect(
|
|
166
|
-
|
|
167
|
-
);
|
|
168
|
-
expect(response.body.results[0].reviewRatingStatistics.highestRating).toBe(
|
|
169
|
-
4,
|
|
170
|
-
);
|
|
171
|
-
expect(response.body.results[0].reviewRatingStatistics.lowestRating).toBe(
|
|
172
|
-
2,
|
|
173
|
-
);
|
|
128
|
+
await reviewDraftFactory(ctMock).create({
|
|
129
|
+
authorName: "Reviewer 1",
|
|
130
|
+
rating: 2,
|
|
131
|
+
target: {
|
|
132
|
+
typeId: "product",
|
|
133
|
+
id: product.id,
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
await reviewDraftFactory(ctMock).create({
|
|
138
|
+
authorName: "Reviewer 2",
|
|
139
|
+
rating: 4,
|
|
140
|
+
target: {
|
|
141
|
+
typeId: "product",
|
|
142
|
+
id: product.id,
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const response = await ctMock.app.inject({
|
|
147
|
+
method: "GET",
|
|
148
|
+
url: "/dummy/products",
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
expect(response.statusCode).toBe(200);
|
|
152
|
+
const body = response.json();
|
|
153
|
+
expect(body.results).toHaveLength(1);
|
|
154
|
+
expect(body.results[0].reviewRatingStatistics).toBeDefined();
|
|
155
|
+
expect(body.results[0].reviewRatingStatistics.count).toBe(2);
|
|
156
|
+
expect(body.results[0].reviewRatingStatistics.averageRating).toBe(3);
|
|
157
|
+
expect(body.results[0].reviewRatingStatistics.highestRating).toBe(4);
|
|
158
|
+
expect(body.results[0].reviewRatingStatistics.lowestRating).toBe(2);
|
|
174
159
|
});
|
|
175
160
|
|
|
176
161
|
test("only reviews with includedInStatistics=true are counted", async () => {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
);
|
|
204
|
-
|
|
205
|
-
expect(response.status).toBe(200);
|
|
206
|
-
expect(response.body.reviewRatingStatistics).toBeDefined();
|
|
207
|
-
expect(response.body.reviewRatingStatistics.count).toBe(2);
|
|
208
|
-
expect(response.body.reviewRatingStatistics.averageRating).toBe(3);
|
|
209
|
-
|
|
210
|
-
// Now exclude one review from statistics by updating it
|
|
211
|
-
// (Note: In a real implementation, this would be done via state transitions,
|
|
212
|
-
// but for now we can test the filtering works with includedInStatistics directly)
|
|
162
|
+
await reviewDraftFactory(ctMock).create({
|
|
163
|
+
authorName: "Reviewer 1",
|
|
164
|
+
rating: 5,
|
|
165
|
+
target: {
|
|
166
|
+
typeId: "product",
|
|
167
|
+
id: product.id,
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
await reviewDraftFactory(ctMock).create({
|
|
172
|
+
authorName: "Reviewer 2",
|
|
173
|
+
rating: 1,
|
|
174
|
+
target: {
|
|
175
|
+
typeId: "product",
|
|
176
|
+
id: product.id,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const response = await ctMock.app.inject({
|
|
181
|
+
method: "GET",
|
|
182
|
+
url: `/dummy/products/${product.id}`,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
expect(response.statusCode).toBe(200);
|
|
186
|
+
const body = response.json();
|
|
187
|
+
expect(body.reviewRatingStatistics).toBeDefined();
|
|
188
|
+
expect(body.reviewRatingStatistics.count).toBe(2);
|
|
189
|
+
expect(body.reviewRatingStatistics.averageRating).toBe(3);
|
|
213
190
|
});
|
|
214
191
|
|
|
215
192
|
test("reviews without ratings are not included in statistics", async () => {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
.
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
);
|
|
245
|
-
|
|
246
|
-
expect(
|
|
247
|
-
// Only the review with rating should be counted
|
|
248
|
-
expect(response.body.reviewRatingStatistics).toBeDefined();
|
|
249
|
-
expect(response.body.reviewRatingStatistics.count).toBe(1);
|
|
250
|
-
expect(response.body.reviewRatingStatistics.averageRating).toBe(4);
|
|
193
|
+
await reviewDraftFactory(ctMock).create({
|
|
194
|
+
authorName: "No Rating User",
|
|
195
|
+
title: "No rating review",
|
|
196
|
+
text: "This review has no rating.",
|
|
197
|
+
rating: undefined,
|
|
198
|
+
target: {
|
|
199
|
+
typeId: "product",
|
|
200
|
+
id: product.id,
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
await reviewDraftFactory(ctMock).create({
|
|
205
|
+
authorName: "Rated User",
|
|
206
|
+
title: "Rated review",
|
|
207
|
+
rating: 4,
|
|
208
|
+
target: {
|
|
209
|
+
typeId: "product",
|
|
210
|
+
id: product.id,
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const response = await ctMock.app.inject({
|
|
215
|
+
method: "GET",
|
|
216
|
+
url: `/dummy/products/${product.id}`,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
expect(response.statusCode).toBe(200);
|
|
220
|
+
const body = response.json();
|
|
221
|
+
expect(body.reviewRatingStatistics).toBeDefined();
|
|
222
|
+
expect(body.reviewRatingStatistics.count).toBe(1);
|
|
223
|
+
expect(body.reviewRatingStatistics.averageRating).toBe(4);
|
|
251
224
|
});
|
|
252
225
|
|
|
253
226
|
test("reviews on other products are excluded from statistics", async () => {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
prices: [
|
|
267
|
-
{
|
|
268
|
-
value: {
|
|
269
|
-
currencyCode: "EUR",
|
|
270
|
-
centAmount: 2000,
|
|
271
|
-
},
|
|
227
|
+
const otherProduct = await productDraftFactory(ctMock).create({
|
|
228
|
+
productType: {
|
|
229
|
+
typeId: "product-type",
|
|
230
|
+
id: product.productType.id,
|
|
231
|
+
},
|
|
232
|
+
masterVariant: {
|
|
233
|
+
sku: "other-sku",
|
|
234
|
+
prices: [
|
|
235
|
+
{
|
|
236
|
+
value: {
|
|
237
|
+
currencyCode: "EUR",
|
|
238
|
+
centAmount: 2000,
|
|
272
239
|
},
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
authorName: "User C",
|
|
308
|
-
title: "Another review for first product",
|
|
309
|
-
rating: 3,
|
|
310
|
-
target: {
|
|
311
|
-
typeId: "product",
|
|
312
|
-
id: product.id,
|
|
313
|
-
},
|
|
314
|
-
});
|
|
240
|
+
},
|
|
241
|
+
],
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
await reviewDraftFactory(ctMock).create({
|
|
246
|
+
authorName: "User A",
|
|
247
|
+
title: "Review for first product",
|
|
248
|
+
rating: 5,
|
|
249
|
+
target: {
|
|
250
|
+
typeId: "product",
|
|
251
|
+
id: product.id,
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
await reviewDraftFactory(ctMock).create({
|
|
256
|
+
authorName: "User B",
|
|
257
|
+
title: "Review for second product",
|
|
258
|
+
rating: 1,
|
|
259
|
+
target: {
|
|
260
|
+
typeId: "product",
|
|
261
|
+
id: otherProduct.id,
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
await reviewDraftFactory(ctMock).create({
|
|
266
|
+
authorName: "User C",
|
|
267
|
+
title: "Another review for first product",
|
|
268
|
+
rating: 3,
|
|
269
|
+
target: {
|
|
270
|
+
typeId: "product",
|
|
271
|
+
id: product.id,
|
|
272
|
+
},
|
|
273
|
+
});
|
|
315
274
|
|
|
316
275
|
// Check statistics for the first product - should only include its own reviews
|
|
317
|
-
const response1 = await
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
expect(response1.
|
|
322
|
-
|
|
323
|
-
expect(
|
|
324
|
-
expect(
|
|
325
|
-
expect(
|
|
326
|
-
expect(
|
|
276
|
+
const response1 = await ctMock.app.inject({
|
|
277
|
+
method: "GET",
|
|
278
|
+
url: `/dummy/products/${product.id}`,
|
|
279
|
+
});
|
|
280
|
+
expect(response1.statusCode).toBe(200);
|
|
281
|
+
const body1 = response1.json();
|
|
282
|
+
expect(body1.reviewRatingStatistics).toBeDefined();
|
|
283
|
+
expect(body1.reviewRatingStatistics.count).toBe(2);
|
|
284
|
+
expect(body1.reviewRatingStatistics.averageRating).toBe(4);
|
|
285
|
+
expect(body1.reviewRatingStatistics.highestRating).toBe(5);
|
|
286
|
+
expect(body1.reviewRatingStatistics.lowestRating).toBe(3);
|
|
287
|
+
expect(body1.reviewRatingStatistics.ratingsDistribution).toEqual({
|
|
327
288
|
"3": 1,
|
|
328
289
|
"5": 1,
|
|
329
290
|
});
|
|
330
291
|
|
|
331
292
|
// Check statistics for the second product - should only include its own review
|
|
332
|
-
const response2 = await
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
expect(response2.
|
|
337
|
-
|
|
338
|
-
expect(
|
|
339
|
-
expect(
|
|
340
|
-
expect(
|
|
341
|
-
expect(
|
|
293
|
+
const response2 = await ctMock.app.inject({
|
|
294
|
+
method: "GET",
|
|
295
|
+
url: `/dummy/products/${otherProduct.id}`,
|
|
296
|
+
});
|
|
297
|
+
expect(response2.statusCode).toBe(200);
|
|
298
|
+
const body2 = response2.json();
|
|
299
|
+
expect(body2.reviewRatingStatistics).toBeDefined();
|
|
300
|
+
expect(body2.reviewRatingStatistics.count).toBe(1);
|
|
301
|
+
expect(body2.reviewRatingStatistics.averageRating).toBe(1);
|
|
302
|
+
expect(body2.reviewRatingStatistics.highestRating).toBe(1);
|
|
303
|
+
expect(body2.reviewRatingStatistics.lowestRating).toBe(1);
|
|
304
|
+
expect(body2.reviewRatingStatistics.ratingsDistribution).toEqual({
|
|
342
305
|
"1": 1,
|
|
343
306
|
});
|
|
344
307
|
});
|
|
@@ -5,14 +5,27 @@ import type {
|
|
|
5
5
|
import type { AbstractStorage } from "../storage/index.ts";
|
|
6
6
|
|
|
7
7
|
export class ReviewStatisticsService {
|
|
8
|
-
|
|
8
|
+
private _storage: AbstractStorage;
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
constructor(_storage: AbstractStorage) {
|
|
11
|
+
this._storage = _storage;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async calculateProductReviewStatistics(
|
|
11
15
|
projectKey: string,
|
|
12
16
|
productId: string,
|
|
13
|
-
): ReviewRatingStatistics | undefined {
|
|
17
|
+
): Promise<ReviewRatingStatistics | undefined> {
|
|
18
|
+
// Fast path: if there are no reviews at all, skip the expensive load
|
|
19
|
+
const reviewCount = await this._storage.count(projectKey, "review");
|
|
20
|
+
if (reviewCount === 0) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
14
24
|
// Get all reviews for this product
|
|
15
|
-
const allReviews = this._storage.all(
|
|
25
|
+
const allReviews = (await this._storage.all(
|
|
26
|
+
projectKey,
|
|
27
|
+
"review",
|
|
28
|
+
)) as Review[];
|
|
16
29
|
const productReviews = allReviews.filter(
|
|
17
30
|
(review) =>
|
|
18
31
|
review.target?.typeId === "product" &&
|
package/src/oauth/helpers.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FastifyRequest } from "fastify";
|
|
2
2
|
|
|
3
|
-
export const getBearerToken = (request:
|
|
4
|
-
const authHeader = request.
|
|
5
|
-
const
|
|
3
|
+
export const getBearerToken = (request: FastifyRequest): string | undefined => {
|
|
4
|
+
const authHeader = request.headers.authorization;
|
|
5
|
+
const normalizedAuthHeader = Array.isArray(authHeader)
|
|
6
|
+
? authHeader[0]
|
|
7
|
+
: authHeader;
|
|
8
|
+
const match = normalizedAuthHeader?.match(/^Bearer\s(?<token>[^\s]+)$/);
|
|
6
9
|
if (match) {
|
|
7
10
|
return match.groups?.token;
|
|
8
11
|
}
|