@reactionary/fake 0.6.9 → 0.6.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.
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
CartMutationItemQuantityChangeSchema,
|
|
19
19
|
CartMutationItemRemoveSchema,
|
|
20
20
|
CartMutationRemoveCouponSchema,
|
|
21
|
+
CartMutationRenameCartSchema,
|
|
21
22
|
CartCapability,
|
|
22
23
|
CartQueryByIdSchema,
|
|
23
24
|
CartSchema,
|
|
@@ -50,9 +51,10 @@ class FakeCartCapability extends CartCapability {
|
|
|
50
51
|
});
|
|
51
52
|
}
|
|
52
53
|
if (!this.carts.has(cartId)) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
return error({
|
|
55
|
+
type: "NotFound",
|
|
56
|
+
identifier: cartId
|
|
57
|
+
});
|
|
56
58
|
}
|
|
57
59
|
const cart = this.carts.get(cartId);
|
|
58
60
|
if (!cart) {
|
|
@@ -129,20 +131,110 @@ class FakeCartCapability extends CartCapability {
|
|
|
129
131
|
this.recalculateCart(cart);
|
|
130
132
|
return success(this.factory.parseCart(this.context, cart));
|
|
131
133
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
+
async listCarts(_payload) {
|
|
135
|
+
return success(this.factory.parseCartPaginatedSearchResult(this.context, {
|
|
136
|
+
pageNumber: 0,
|
|
137
|
+
pageSize: 0,
|
|
138
|
+
totalCount: 0,
|
|
139
|
+
totalPages: 0,
|
|
140
|
+
items: [],
|
|
141
|
+
identifier: _payload.search
|
|
142
|
+
}, _payload));
|
|
143
|
+
}
|
|
144
|
+
async createCart(_payload) {
|
|
145
|
+
const emptyCart = this.createEmptyCart();
|
|
146
|
+
emptyCart.identifier.key = `cart-${this.generator.string.uuid()}`;
|
|
147
|
+
this.carts.set(emptyCart.identifier.key, emptyCart);
|
|
148
|
+
return success(this.factory.parseCart(this.context, emptyCart));
|
|
134
149
|
}
|
|
135
|
-
|
|
136
|
-
|
|
150
|
+
async renameCart(_payload) {
|
|
151
|
+
const cart = this.carts.get(_payload.cart.key);
|
|
152
|
+
if (!cart) {
|
|
153
|
+
return error({
|
|
154
|
+
type: "NotFound",
|
|
155
|
+
identifier: _payload.cart.key
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
cart.name = _payload.newName;
|
|
159
|
+
return success(this.factory.parseCart(this.context, cart));
|
|
137
160
|
}
|
|
138
|
-
|
|
139
|
-
|
|
161
|
+
async getActiveCartId() {
|
|
162
|
+
if (this.carts.size > 0) {
|
|
163
|
+
const cartId = { key: this.carts.values().next().value.identifier.key };
|
|
164
|
+
return success(this.factory.parseCartIdentifier(this.context, cartId));
|
|
165
|
+
} else {
|
|
166
|
+
return error({
|
|
167
|
+
type: "NotFound",
|
|
168
|
+
identifier: "activeCart"
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async deleteCart(payload) {
|
|
173
|
+
const cartId = payload.cart.key;
|
|
174
|
+
if (this.carts.has(cartId)) {
|
|
175
|
+
this.carts.delete(cartId);
|
|
176
|
+
}
|
|
177
|
+
return success(void 0);
|
|
178
|
+
}
|
|
179
|
+
async applyCouponCode(payload) {
|
|
180
|
+
const cart = this.carts.get(payload.cart.key);
|
|
181
|
+
if (!cart) {
|
|
182
|
+
return error({
|
|
183
|
+
type: "NotFound",
|
|
184
|
+
identifier: payload.cart.key
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
cart.appliedPromotions.push({
|
|
188
|
+
code: payload.couponCode,
|
|
189
|
+
isCouponCode: true,
|
|
190
|
+
name: `Promotion for ${payload.couponCode}`,
|
|
191
|
+
description: `Description for promotion with code ${payload.couponCode}`
|
|
192
|
+
});
|
|
193
|
+
cart.price.totalDiscount.value += 10;
|
|
194
|
+
cart.price.grandTotal.value -= 10;
|
|
195
|
+
return success(this.factory.parseCart(this.context, cart));
|
|
140
196
|
}
|
|
141
|
-
removeCouponCode(payload) {
|
|
142
|
-
|
|
197
|
+
async removeCouponCode(payload) {
|
|
198
|
+
const cart = this.carts.get(payload.cart.key);
|
|
199
|
+
if (!cart) {
|
|
200
|
+
return error({
|
|
201
|
+
type: "NotFound",
|
|
202
|
+
identifier: payload.cart.key
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
const promoIndex = cart.appliedPromotions.findIndex(
|
|
206
|
+
(promo) => promo.code === payload.couponCode
|
|
207
|
+
);
|
|
208
|
+
if (promoIndex >= 0) {
|
|
209
|
+
cart.appliedPromotions.splice(promoIndex, 1);
|
|
210
|
+
cart.price.totalDiscount.value -= 10;
|
|
211
|
+
cart.price.grandTotal.value += 10;
|
|
212
|
+
}
|
|
213
|
+
return success(this.factory.parseCart(this.context, cart));
|
|
143
214
|
}
|
|
144
|
-
changeCurrency(payload) {
|
|
145
|
-
|
|
215
|
+
async changeCurrency(payload) {
|
|
216
|
+
const cart = this.carts.get(payload.cart.key);
|
|
217
|
+
if (!cart) {
|
|
218
|
+
return Promise.resolve(
|
|
219
|
+
error({
|
|
220
|
+
type: "NotFound",
|
|
221
|
+
identifier: payload.cart.key
|
|
222
|
+
})
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
cart.price.grandTotal.currency = payload.newCurrency;
|
|
226
|
+
cart.price.totalDiscount.currency = payload.newCurrency;
|
|
227
|
+
cart.price.totalProductPrice.currency = payload.newCurrency;
|
|
228
|
+
cart.price.totalShipping.currency = payload.newCurrency;
|
|
229
|
+
cart.price.totalSurcharge.currency = payload.newCurrency;
|
|
230
|
+
cart.price.totalTax.currency = payload.newCurrency;
|
|
231
|
+
cart.items.forEach((item) => {
|
|
232
|
+
item.price.unitPrice.currency = payload.newCurrency;
|
|
233
|
+
item.price.totalPrice.currency = payload.newCurrency;
|
|
234
|
+
item.price.unitDiscount.currency = payload.newCurrency;
|
|
235
|
+
item.price.totalDiscount.currency = payload.newCurrency;
|
|
236
|
+
});
|
|
237
|
+
return success(this.factory.parseCart(this.context, cart));
|
|
146
238
|
}
|
|
147
239
|
recalculateCart(cart) {
|
|
148
240
|
cart.items.forEach((item) => {
|
|
@@ -162,6 +254,11 @@ class FakeCartCapability extends CartCapability {
|
|
|
162
254
|
),
|
|
163
255
|
currency: cart.items[0]?.price.unitPrice.currency || "USD"
|
|
164
256
|
};
|
|
257
|
+
cart.price.totalDiscount = {
|
|
258
|
+
value: cart.appliedPromotions.length * 10,
|
|
259
|
+
currency: cart.price.grandTotal.currency
|
|
260
|
+
};
|
|
261
|
+
cart.price.grandTotal.value -= cart.price.totalDiscount.value;
|
|
165
262
|
}
|
|
166
263
|
createEmptyCart() {
|
|
167
264
|
const cart = {
|
|
@@ -196,7 +293,7 @@ class FakeCartCapability extends CartCapability {
|
|
|
196
293
|
}
|
|
197
294
|
},
|
|
198
295
|
appliedPromotions: [],
|
|
199
|
-
|
|
296
|
+
user: {
|
|
200
297
|
userId: ""
|
|
201
298
|
}
|
|
202
299
|
};
|
|
@@ -227,6 +324,12 @@ __decorateClass([
|
|
|
227
324
|
outputSchema: CartSchema
|
|
228
325
|
})
|
|
229
326
|
], FakeCartCapability.prototype, "changeQuantity", 1);
|
|
327
|
+
__decorateClass([
|
|
328
|
+
Reactionary({
|
|
329
|
+
inputSchema: CartMutationRenameCartSchema,
|
|
330
|
+
outputSchema: CartSchema
|
|
331
|
+
})
|
|
332
|
+
], FakeCartCapability.prototype, "renameCart", 1);
|
|
230
333
|
__decorateClass([
|
|
231
334
|
Reactionary({
|
|
232
335
|
outputSchema: CartIdentifierSchema
|
package/core/initialize.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CartIdentifierSchema as CoreCartIdentifierSchema,
|
|
3
|
+
CartPaginatedSearchResultSchema as CoreCartPaginatedSearchResultSchema,
|
|
3
4
|
CartSchema as CoreCartSchema,
|
|
4
5
|
CategoryPaginatedResultSchema as CoreCategoryPaginatedResultSchema,
|
|
5
6
|
CategorySchema as CoreCategorySchema,
|
|
@@ -103,7 +104,11 @@ function withFakeCapabilities(configuration, capabilities) {
|
|
|
103
104
|
client.cart = resolveCapabilityWithFactory(
|
|
104
105
|
capabilities.cart,
|
|
105
106
|
{
|
|
106
|
-
factory: new FakeCartFactory(
|
|
107
|
+
factory: new FakeCartFactory(
|
|
108
|
+
CoreCartSchema,
|
|
109
|
+
CoreCartIdentifierSchema,
|
|
110
|
+
CoreCartPaginatedSearchResultSchema
|
|
111
|
+
),
|
|
107
112
|
capability: (args) => new FakeCartCapability(args.config, args.cache, args.context, args.factory)
|
|
108
113
|
},
|
|
109
114
|
buildCapabilityArgs
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
class FakeCartFactory {
|
|
2
2
|
cartSchema;
|
|
3
3
|
cartIdentifierSchema;
|
|
4
|
-
|
|
4
|
+
cartPaginatedSearchResultSchema;
|
|
5
|
+
constructor(cartSchema, cartIdentifierSchema, cartPaginatedSearchResultSchema) {
|
|
5
6
|
this.cartSchema = cartSchema;
|
|
6
7
|
this.cartIdentifierSchema = cartIdentifierSchema;
|
|
8
|
+
this.cartPaginatedSearchResultSchema = cartPaginatedSearchResultSchema;
|
|
7
9
|
}
|
|
8
10
|
parseCart(_context, data) {
|
|
9
11
|
return this.cartSchema.parse(data);
|
|
@@ -11,6 +13,9 @@ class FakeCartFactory {
|
|
|
11
13
|
parseCartIdentifier(_context, data) {
|
|
12
14
|
return this.cartIdentifierSchema.parse(data);
|
|
13
15
|
}
|
|
16
|
+
parseCartPaginatedSearchResult(_context, data, _query) {
|
|
17
|
+
return this.cartPaginatedSearchResultSchema.parse(data);
|
|
18
|
+
}
|
|
14
19
|
}
|
|
15
20
|
export {
|
|
16
21
|
FakeCartFactory
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactionary/fake",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"types": "./src/index.d.ts",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"vitest": "^4.0.9",
|
|
9
9
|
"@nx/vite": "22.4.5",
|
|
10
|
-
"@reactionary/core": "0.6.
|
|
10
|
+
"@reactionary/core": "0.6.10",
|
|
11
11
|
"zod": "4.1.9",
|
|
12
12
|
"@faker-js/faker": "^9.8.0"
|
|
13
13
|
},
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { Cart, CartFactory, CartFactoryCartOutput, CartFactoryWithOutput, CartQueryById, CartMutationItemAdd, CartMutationItemRemove, CartMutationItemQuantityChange, RequestContext, Cache,
|
|
1
|
+
import type { Cart, CartFactory, CartFactoryCartOutput, CartFactoryIdentifierOutput, CartFactoryWithOutput, CartQueryById, CartQueryList, CartPaginatedSearchResult, CartMutationItemAdd, CartMutationItemRemove, CartMutationItemQuantityChange, CartMutationCreateCart, CartMutationRenameCart, RequestContext, Cache, CartMutationApplyCoupon, CartMutationChangeCurrency, CartMutationDeleteCart, CartMutationRemoveCoupon, Result, NotFoundError } from '@reactionary/core';
|
|
2
2
|
import { CartCapability } from '@reactionary/core';
|
|
3
3
|
import type { FakeConfiguration } from '../schema/configuration.schema.js';
|
|
4
4
|
import type { FakeCartFactory } from '../factories/cart/cart.factory.js';
|
|
5
|
-
export declare class FakeCartCapability<TFactory extends CartFactory = FakeCartFactory> extends CartCapability<CartFactoryCartOutput<TFactory>,
|
|
5
|
+
export declare class FakeCartCapability<TFactory extends CartFactory = FakeCartFactory> extends CartCapability<CartFactoryCartOutput<TFactory>, CartFactoryIdentifierOutput<TFactory>> {
|
|
6
6
|
protected config: FakeConfiguration;
|
|
7
7
|
protected factory: CartFactoryWithOutput<TFactory>;
|
|
8
8
|
private carts;
|
|
@@ -12,7 +12,10 @@ export declare class FakeCartCapability<TFactory extends CartFactory = FakeCartF
|
|
|
12
12
|
add(payload: CartMutationItemAdd): Promise<Result<CartFactoryCartOutput<TFactory>>>;
|
|
13
13
|
remove(payload: CartMutationItemRemove): Promise<Result<CartFactoryCartOutput<TFactory>>>;
|
|
14
14
|
changeQuantity(payload: CartMutationItemQuantityChange): Promise<Result<CartFactoryCartOutput<TFactory>>>;
|
|
15
|
-
|
|
15
|
+
listCarts(_payload: CartQueryList): Promise<Result<CartPaginatedSearchResult>>;
|
|
16
|
+
createCart(_payload: CartMutationCreateCart): Promise<Result<CartFactoryCartOutput<TFactory>>>;
|
|
17
|
+
renameCart(_payload: CartMutationRenameCart): Promise<Result<CartFactoryCartOutput<TFactory>>>;
|
|
18
|
+
getActiveCartId(): Promise<Result<CartFactoryIdentifierOutput<TFactory>, NotFoundError>>;
|
|
16
19
|
deleteCart(payload: CartMutationDeleteCart): Promise<Result<void>>;
|
|
17
20
|
applyCouponCode(payload: CartMutationApplyCoupon): Promise<Result<CartFactoryCartOutput<TFactory>>>;
|
|
18
21
|
removeCouponCode(payload: CartMutationRemoveCoupon): Promise<Result<CartFactoryCartOutput<TFactory>>>;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import type { AnyCartIdentifierSchema, AnyCartSchema, CartFactory, CartIdentifierSchema, CartSchema, RequestContext } from '@reactionary/core';
|
|
1
|
+
import type { AnyCartIdentifierSchema, AnyCartPaginatedSearchResult, AnyCartSchema, CartFactory, CartIdentifierSchema, CartPaginatedSearchResultSchema, CartQueryList, CartSchema, RequestContext } from '@reactionary/core';
|
|
2
2
|
import type * as z from 'zod';
|
|
3
|
-
export declare class FakeCartFactory<TCartSchema extends AnyCartSchema = typeof CartSchema, TCartIdentifierSchema extends AnyCartIdentifierSchema = typeof CartIdentifierSchema> implements CartFactory<TCartSchema, TCartIdentifierSchema> {
|
|
3
|
+
export declare class FakeCartFactory<TCartSchema extends AnyCartSchema = typeof CartSchema, TCartIdentifierSchema extends AnyCartIdentifierSchema = typeof CartIdentifierSchema, TCartPaginatedSearchResult extends AnyCartPaginatedSearchResult = typeof CartPaginatedSearchResultSchema> implements CartFactory<TCartSchema, TCartIdentifierSchema, TCartPaginatedSearchResult> {
|
|
4
4
|
readonly cartSchema: TCartSchema;
|
|
5
5
|
readonly cartIdentifierSchema: TCartIdentifierSchema;
|
|
6
|
-
|
|
6
|
+
readonly cartPaginatedSearchResultSchema: TCartPaginatedSearchResult;
|
|
7
|
+
constructor(cartSchema: TCartSchema, cartIdentifierSchema: TCartIdentifierSchema, cartPaginatedSearchResultSchema: TCartPaginatedSearchResult);
|
|
7
8
|
parseCart(_context: RequestContext, data: unknown): z.output<TCartSchema>;
|
|
8
9
|
parseCartIdentifier(_context: RequestContext, data: unknown): z.output<TCartIdentifierSchema>;
|
|
10
|
+
parseCartPaginatedSearchResult(_context: RequestContext, data: unknown, _query: CartQueryList): z.output<TCartPaginatedSearchResult>;
|
|
9
11
|
}
|