@labdigital/commercetools-mock 3.0.0-beta.1 → 3.0.0-beta.3

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.
@@ -1,4 +1,4 @@
1
- import { a as QueryParams, c as ResourceType, n as AbstractStorage, o as PagedQueryResponseMap, r as GetParams, s as ResourceMap } from "../config-BcNSzPZz.mjs";
1
+ import { a as QueryParams, c as ResourceType, n as AbstractStorage, o as PagedQueryResponseMap, r as GetParams, s as ResourceMap } from "../config-BGZiZhn4.mjs";
2
2
  import { CustomObject, Project } from "@commercetools/platform-sdk";
3
3
 
4
4
  //#region src/storage/sqlite.d.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
- "version": "3.0.0-beta.1",
3
+ "version": "3.0.0-beta.3",
4
4
  "license": "MIT",
5
5
  "author": "Michael van Tellingen",
6
6
  "type": "module",
@@ -953,8 +953,12 @@ describe("Order repository", () => {
953
953
  );
954
954
  expect(result.shippingInfo?.taxCategory?.id).toBe("tax-category-456");
955
955
  expect(result.shippingInfo?.taxRate?.amount).toBe(0.19);
956
- // Note: deliveries from import drafts are not currently supported in native implementation
957
- expect(result.shippingInfo?.deliveries).toEqual([]);
956
+ expect(result.shippingInfo?.deliveries).toHaveLength(1);
957
+ expect(result.shippingInfo?.deliveries?.[0].key).toBe("delivery-1");
958
+ expect(result.shippingInfo?.deliveries?.[0].parcels).toHaveLength(1);
959
+ expect(result.shippingInfo?.deliveries?.[0].parcels[0].key).toBe(
960
+ "parcel-1",
961
+ );
958
962
  });
959
963
 
960
964
  test("createShippingInfo throws error for non-existent shipping method", async () => {
@@ -4,6 +4,7 @@ import type {
4
4
  CartReference,
5
5
  CustomLineItem,
6
6
  CustomLineItemImportDraft,
7
+ Delivery,
7
8
  DuplicateFieldError,
8
9
  GeneralError,
9
10
  LineItem,
@@ -18,6 +19,7 @@ import type {
18
19
  ReferencedResourceNotFoundError,
19
20
  ResourceNotFoundError,
20
21
  ShippingInfo,
22
+ ShippingInfoImportDraft,
21
23
  ShippingMethodDoesNotMatchCartError,
22
24
  ShippingMethodReference,
23
25
  } from "@commercetools/platform-sdk";
@@ -206,30 +208,12 @@ export class OrderRepository extends AbstractResourceRepository<"order"> {
206
208
  totalPrice: createCentPrecisionMoney(draft.totalPrice),
207
209
  };
208
210
 
209
- // Set shipping info after resource is created
210
- if (draft.shippingInfo?.shippingMethod) {
211
- const { ...shippingMethodRef } = draft.shippingInfo.shippingMethod;
212
-
213
- // get id when reference is by key only
214
- if (shippingMethodRef.key && !shippingMethodRef.id) {
215
- const shippingMethod =
216
- await this._storage.getByResourceIdentifier<"shipping-method">(
217
- context.projectKey,
218
- shippingMethodRef,
219
- );
220
- if (!shippingMethod) {
221
- throw new CommercetoolsError<GeneralError>({
222
- code: "General",
223
- message: `A shipping method with key '${shippingMethodRef.key}' does not exist.`,
224
- });
225
- }
226
- shippingMethodRef.id = shippingMethod.id;
227
- }
228
-
229
- resource.shippingInfo = await this.createShippingInfo(context, resource, {
230
- typeId: "shipping-method",
231
- id: shippingMethodRef.id as string,
232
- });
211
+ if (draft.shippingInfo) {
212
+ resource.shippingInfo = await this.shippingInfoFromImportDraft(
213
+ context,
214
+ resource,
215
+ draft.shippingInfo,
216
+ );
233
217
  }
234
218
 
235
219
  const { taxedPrice, taxedShippingPrice } = calculateTaxTotals({
@@ -399,7 +383,7 @@ export class OrderRepository extends AbstractResourceRepository<"order"> {
399
383
  context: RepositoryContext,
400
384
  resource: Writable<Order>,
401
385
  shippingMethodRef: ShippingMethodReference,
402
- ): Promise<ShippingInfo> {
386
+ ): Promise<Writable<ShippingInfo>> {
403
387
  const cartLikeForMatching: Writable<Cart> = {
404
388
  ...resource,
405
389
  cartState: "Active" as const,
@@ -447,6 +431,79 @@ export class OrderRepository extends AbstractResourceRepository<"order"> {
447
431
  };
448
432
  }
449
433
 
434
+ private async shippingInfoFromImportDraft(
435
+ context: RepositoryContext,
436
+ resource: Writable<Order>,
437
+ draft: ShippingInfoImportDraft,
438
+ ): Promise<ShippingInfo | undefined> {
439
+ if (!draft.shippingMethod) {
440
+ return undefined;
441
+ }
442
+
443
+ const { ...shippingMethodRef } = draft.shippingMethod;
444
+ if (shippingMethodRef.key && !shippingMethodRef.id) {
445
+ const shippingMethod =
446
+ await this._storage.getByResourceIdentifier<"shipping-method">(
447
+ context.projectKey,
448
+ shippingMethodRef,
449
+ );
450
+ if (!shippingMethod) {
451
+ throw new CommercetoolsError<GeneralError>({
452
+ code: "General",
453
+ message: `A shipping method with key '${shippingMethodRef.key}' does not exist.`,
454
+ });
455
+ }
456
+ shippingMethodRef.id = shippingMethod.id;
457
+ }
458
+
459
+ const shippingInfo = await this.createShippingInfo(context, resource, {
460
+ typeId: "shipping-method",
461
+ id: shippingMethodRef.id as string,
462
+ });
463
+
464
+ shippingInfo.deliveries = await this.deliveriesFromImportDraft(
465
+ context,
466
+ draft,
467
+ );
468
+ return shippingInfo;
469
+ }
470
+
471
+ private async deliveriesFromImportDraft(
472
+ context: RepositoryContext,
473
+ draft: ShippingInfoImportDraft,
474
+ ): Promise<Delivery[]> {
475
+ if (!draft.deliveries) return [];
476
+
477
+ return Promise.all(
478
+ draft.deliveries.map(async (deliveryDraft) => ({
479
+ ...getBaseResourceProperties(),
480
+ key: deliveryDraft.key,
481
+ items: deliveryDraft.items ?? [],
482
+ parcels: await Promise.all(
483
+ deliveryDraft.parcels?.map(async (parcel) => ({
484
+ ...getBaseResourceProperties(),
485
+ ...parcel,
486
+ custom: await createCustomFields(
487
+ parcel.custom,
488
+ context.projectKey,
489
+ this._storage,
490
+ ),
491
+ })) ?? [],
492
+ ),
493
+ address: createAddress(
494
+ deliveryDraft.address,
495
+ context.projectKey,
496
+ this._storage,
497
+ ),
498
+ custom: await createCustomFields(
499
+ deliveryDraft.custom,
500
+ context.projectKey,
501
+ this._storage,
502
+ ),
503
+ })),
504
+ );
505
+ }
506
+
450
507
  async search(
451
508
  context: RepositoryContext,
452
509
  searchRequest: OrderSearchRequest,
package/src/server.ts CHANGED
@@ -2,15 +2,17 @@ import pino from "pino";
2
2
  import { CommercetoolsMock } from "./index.ts";
3
3
  import { SQLiteStorage } from "./storage/sqlite.ts";
4
4
 
5
- const storage = new SQLiteStorage();
5
+ const enableLogging = process.env.ENABLE_LOGGING === "true";
6
+ const experimentalSQLiteStorage =
7
+ process.env.EXPERIMENTAL_SQLITE_STORAGE === "true";
8
+
9
+ const storage = experimentalSQLiteStorage ? new SQLiteStorage() : undefined;
6
10
 
7
11
  process.on("SIGINT", () => {
8
- storage.close();
12
+ if (storage) storage.close();
9
13
  process.exit();
10
14
  });
11
15
 
12
- const enableLogging = process.env.ENABLE_LOGGING === "true";
13
-
14
16
  const logger = enableLogging
15
17
  ? pino({ transport: { target: "pino-pretty" } })
16
18
  : undefined;