@labdigital/commercetools-mock 2.35.0 → 2.36.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
- "version": "2.35.0",
3
+ "version": "2.36.0",
4
4
  "license": "MIT",
5
5
  "author": "Michael van Tellingen",
6
6
  "type": "module",
@@ -1,10 +1,12 @@
1
1
  import type {
2
+ Address,
2
3
  Customer,
3
4
  CustomerCreatePasswordResetToken,
4
5
  CustomerDraft,
5
6
  CustomerResetPassword,
6
7
  CustomerToken,
7
8
  DuplicateFieldError,
9
+ InvalidInputError,
8
10
  MyCustomerResetPassword,
9
11
  ResourceNotFoundError,
10
12
  } from "@commercetools/platform-sdk";
@@ -53,20 +55,50 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
53
55
  });
54
56
  }
55
57
 
56
- const addresses =
58
+ const addresses: Address[] =
57
59
  draft.addresses?.map((address) => ({
58
60
  ...address,
59
61
  id: generateRandomString(5),
60
62
  })) ?? [];
61
63
 
62
- const defaultBillingAddressId =
63
- addresses.length > 0 && draft.defaultBillingAddress !== undefined
64
- ? addresses[draft.defaultBillingAddress].id
65
- : undefined;
66
- const defaultShippingAddressId =
67
- addresses.length > 0 && draft.defaultShippingAddress !== undefined
68
- ? addresses[draft.defaultShippingAddress].id
69
- : undefined;
64
+ const lookupAdressId = (
65
+ addresses: Address[],
66
+ addressId: number,
67
+ ): string => {
68
+ if (addressId < addresses.length) {
69
+ const id = addresses[addressId].id;
70
+ if (!id) {
71
+ throw new Error("Address ID is missing");
72
+ }
73
+ return id;
74
+ }
75
+ throw new CommercetoolsError<InvalidInputError>({
76
+ code: "InvalidInput",
77
+ message: `Address with ID '${addressId}' not found.`,
78
+ errors: [
79
+ {
80
+ code: "InvalidInput",
81
+ message: `Address with ID '${addressId}' not found.`,
82
+ field: "addressId",
83
+ },
84
+ ],
85
+ });
86
+ };
87
+
88
+ const defaultBillingAddressId = draft.defaultBillingAddress
89
+ ? lookupAdressId(addresses, draft.defaultBillingAddress)
90
+ : undefined;
91
+ const defaultShippingAddressId = draft.defaultShippingAddress
92
+ ? lookupAdressId(addresses, draft.defaultShippingAddress)
93
+ : undefined;
94
+ const shippingAddressIds =
95
+ draft.shippingAddresses?.map((addressId) =>
96
+ lookupAdressId(addresses, addressId),
97
+ ) ?? [];
98
+ const billingAddressIds =
99
+ draft.billingAddresses?.map((addressId) =>
100
+ lookupAdressId(addresses, addressId),
101
+ ) ?? [];
70
102
 
71
103
  const resource: Customer = {
72
104
  ...getBaseResourceProperties(),
@@ -86,6 +118,8 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
86
118
  externalId: draft.externalId,
87
119
  defaultBillingAddressId: defaultBillingAddressId,
88
120
  defaultShippingAddressId: defaultShippingAddressId,
121
+ shippingAddressIds: shippingAddressIds,
122
+ billingAddressIds: billingAddressIds,
89
123
  custom: createCustomFields(
90
124
  draft.custom,
91
125
  context.projectKey,
@@ -1,4 +1,8 @@
1
- import { Customer, CustomerToken } from "@commercetools/platform-sdk";
1
+ import {
2
+ Customer,
3
+ CustomerDraft,
4
+ CustomerToken,
5
+ } from "@commercetools/platform-sdk";
2
6
  import assert from "assert";
3
7
  import supertest from "supertest";
4
8
  import { afterEach, beforeEach, describe, expect, test } from "vitest";
@@ -7,6 +11,46 @@ import { CommercetoolsMock, getBaseResourceProperties } from "../index";
7
11
 
8
12
  const ctMock = new CommercetoolsMock();
9
13
 
14
+ afterEach(() => {
15
+ ctMock.clear();
16
+ });
17
+
18
+ describe("Customer create", () => {
19
+ test("create new customer", async () => {
20
+ const draft: CustomerDraft = {
21
+ email: "new-user@example.com",
22
+ password: "supersecret",
23
+ authenticationMode: "Password",
24
+ stores: [],
25
+ addresses: [
26
+ {
27
+ key: "address-key",
28
+ firstName: "John",
29
+ lastName: "Doe",
30
+ streetName: "Main Street",
31
+ streetNumber: "1",
32
+ postalCode: "12345",
33
+ country: "DE",
34
+ },
35
+ ],
36
+ billingAddresses: [0],
37
+ shippingAddresses: [0],
38
+ };
39
+
40
+ const response = await supertest(ctMock.app)
41
+ .post(`/dummy/customers`)
42
+ .send(draft);
43
+
44
+ const customer = response.body.customer as Customer;
45
+ expect(response.status, JSON.stringify(customer)).toBe(201);
46
+ expect(customer.version).toBe(1);
47
+ expect(customer.defaultBillingAddressId).toBeUndefined();
48
+ expect(customer.defaultShippingAddressId).toBeUndefined();
49
+ expect(customer.billingAddressIds).toHaveLength(1);
50
+ expect(customer.shippingAddressIds).toHaveLength(1);
51
+ });
52
+ });
53
+
10
54
  describe("Customer Update Actions", () => {
11
55
  let customer: Customer | undefined;
12
56
 
@@ -25,10 +69,6 @@ describe("Customer Update Actions", () => {
25
69
  ctMock.project("dummy").add("customer", customer);
26
70
  });
27
71
 
28
- afterEach(() => {
29
- ctMock.clear();
30
- });
31
-
32
72
  test("exists", async () => {
33
73
  assert(customer, "customer not created");
34
74
 
@@ -12,6 +12,7 @@ import { CustomerGroupService } from "./customer-group";
12
12
  import { DiscountCodeService } from "./discount-code";
13
13
  import { ExtensionServices } from "./extension";
14
14
  import { InventoryEntryService } from "./inventory-entry";
15
+ import { MyBusinessUnitService } from "./my-business-unit";
15
16
  import { MyCartService } from "./my-cart";
16
17
  import { MyCustomerService } from "./my-customer";
17
18
  import { MyOrderService } from "./my-order";
@@ -66,6 +67,7 @@ export const createServices = (
66
67
  "my-cart": new MyCartService(router, repos["my-cart"]),
67
68
  "my-order": new MyOrderService(router, repos["my-order"]),
68
69
  "my-customer": new MyCustomerService(router, repos["my-customer"]),
70
+ "my-business-unit": new MyBusinessUnitService(router, repos["business-unit"]),
69
71
  "my-payment": new MyPaymentService(router, repos["my-payment"]),
70
72
  "my-shopping-list": new MyShoppingListService(
71
73
  router,
@@ -0,0 +1,28 @@
1
+ import { Router } from "express";
2
+ import { BusinessUnitRepository } from "~src/repositories/business-unit";
3
+ import AbstractService from "./abstract";
4
+
5
+ export class MyBusinessUnitService extends AbstractService {
6
+ public repository: BusinessUnitRepository;
7
+
8
+ constructor(parent: Router, repository: BusinessUnitRepository) {
9
+ super(parent);
10
+ this.repository = repository;
11
+ }
12
+
13
+ getBasePath() {
14
+ return "me";
15
+ }
16
+
17
+ registerRoutes(parent: Router) {
18
+ // Overwrite this function to be able to handle /me/business-units path.
19
+ const basePath = this.getBasePath();
20
+ const router = Router({ mergeParams: true });
21
+
22
+ this.extraRoutes(router);
23
+
24
+ router.get("/business-units/", this.get.bind(this));
25
+
26
+ parent.use(`/${basePath}`, router);
27
+ }
28
+ }
@@ -35,6 +35,8 @@ describe("Me", () => {
35
35
  version: 1,
36
36
  isEmailVerified: false,
37
37
  addresses: [],
38
+ billingAddressIds: [],
39
+ shippingAddressIds: [],
38
40
  id: expect.anything(),
39
41
  createdAt: expect.anything(),
40
42
  lastModifiedAt: expect.anything(),
package/src/types.ts CHANGED
@@ -14,7 +14,8 @@ export type ServiceTypes =
14
14
  | "my-cart"
15
15
  | "my-order"
16
16
  | "my-payment"
17
- | "my-customer";
17
+ | "my-customer"
18
+ | "my-business-unit";
18
19
 
19
20
  export type Services = Partial<{
20
21
  [index in ServiceTypes]: AbstractService;