@labdigital/commercetools-mock 2.26.1 → 2.28.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/dist/index.cjs CHANGED
@@ -81,18 +81,21 @@ var import_uuid = require("uuid");
81
81
  var PWRESET_SECRET = "pwreset";
82
82
  var EMAIL_VERIFY_SECRET = "emailverifysecret";
83
83
  var hashPassword = (clearPassword) => Buffer.from(clearPassword).toString("base64");
84
- var createPasswordResetToken = (customer) => Buffer.from(`${customer.id}:${PWRESET_SECRET}:${(0, import_uuid.v4)()}`).toString(
85
- "base64"
86
- );
84
+ var createPasswordResetToken = (customer, expiresAt) => Buffer.from(
85
+ `${customer.id}:${PWRESET_SECRET}:${expiresAt.getTime()}`
86
+ ).toString("base64");
87
87
  var createEmailVerifyToken = (customer) => Buffer.from(`${customer.id}:${EMAIL_VERIFY_SECRET}:${(0, import_uuid.v4)()}`).toString(
88
88
  "base64"
89
89
  );
90
90
  var validatePasswordResetToken = (token) => {
91
91
  const items = Buffer.from(token, "base64").toString("utf-8").split(":");
92
- const [customerId, secret] = items;
92
+ const [customerId, secret, time] = items;
93
93
  if (secret !== PWRESET_SECRET) {
94
94
  return void 0;
95
95
  }
96
+ if (parseInt(time) < (/* @__PURE__ */ new Date()).getTime()) {
97
+ return void 0;
98
+ }
96
99
  return customerId;
97
100
  };
98
101
  var validateEmailVerifyToken = (token) => {
@@ -1537,6 +1540,7 @@ var InMemoryStorage = class extends AbstractStorage {
1537
1540
  "product-selection": /* @__PURE__ */ new Map(),
1538
1541
  "product-type": /* @__PURE__ */ new Map(),
1539
1542
  "product-projection": /* @__PURE__ */ new Map(),
1543
+ "product-tailoring": /* @__PURE__ */ new Map(),
1540
1544
  "review": /* @__PURE__ */ new Map(),
1541
1545
  "shipping-method": /* @__PURE__ */ new Map(),
1542
1546
  "staged-quote": /* @__PURE__ */ new Map(),
@@ -1921,6 +1925,22 @@ var AbstractUpdateHandler = class {
1921
1925
  }
1922
1926
  };
1923
1927
 
1928
+ // src/repositories/product-tailoring.ts
1929
+ var ProductTailoringRepository = class extends AbstractResourceRepository {
1930
+ constructor(storage) {
1931
+ super("product-tailoring", storage);
1932
+ this.actions = new ProductTailoringUpdateHandler(this._storage);
1933
+ }
1934
+ create(context, draft) {
1935
+ throw new Error("Create method for product-tailoring not implemented.");
1936
+ }
1937
+ };
1938
+ var ProductTailoringUpdateHandler = class extends AbstractUpdateHandler {
1939
+ setSlug() {
1940
+ throw new Error("SetSlug method for product-tailoring not implemented.");
1941
+ }
1942
+ };
1943
+
1924
1944
  // src/repositories/helpers.ts
1925
1945
  var import_uuid4 = require("uuid");
1926
1946
  var createAddress = (base, projectKey, storage) => {
@@ -2244,6 +2264,18 @@ var BusinessUnitRepository = class extends AbstractResourceRepository {
2244
2264
  this.actions = new BusinessUnitUpdateHandler(this._storage);
2245
2265
  }
2246
2266
  create(context, draft) {
2267
+ const addresses = draft.addresses?.map((address) => ({
2268
+ ...address,
2269
+ id: generateRandomString(5)
2270
+ })) ?? [];
2271
+ const defaultBillingAddressId = addresses.length > 0 && draft.defaultBillingAddress !== void 0 ? addresses[draft.defaultBillingAddress].id : void 0;
2272
+ const defaultShippingAddressId = addresses.length > 0 && draft.defaultShippingAddress !== void 0 ? addresses[draft.defaultShippingAddress].id : void 0;
2273
+ const shippingAddressIds = draft.shippingAddresses?.map(
2274
+ (i) => addresses[i].id
2275
+ );
2276
+ const billingAddressIds = draft.billingAddresses?.map(
2277
+ (i) => addresses[i].id
2278
+ );
2247
2279
  const resource = {
2248
2280
  ...getBaseResourceProperties(),
2249
2281
  key: draft.key,
@@ -2254,18 +2286,18 @@ var BusinessUnitRepository = class extends AbstractResourceRepository {
2254
2286
  storeMode: draft.storeMode,
2255
2287
  name: draft.name,
2256
2288
  contactEmail: draft.contactEmail,
2257
- addresses: draft.addresses?.map(
2258
- (a) => createAddress(a, context.projectKey, this._storage)
2259
- ),
2289
+ addresses,
2260
2290
  custom: createCustomFields(
2261
2291
  draft.custom,
2262
2292
  context.projectKey,
2263
2293
  this._storage
2264
2294
  ),
2265
- shippingAddressIds: draft.shippingAddresses,
2266
- defaultShippingAddressId: draft.defaultShippingAddress,
2267
- billingAddressIds: draft.billingAddresses,
2295
+ shippingAddressIds,
2296
+ billingAddressIds,
2297
+ defaultShippingAddressId,
2298
+ defaultBillingAddressId,
2268
2299
  associateMode: draft.associateMode,
2300
+ approvalRuleMode: draft.approvalRuleMode,
2269
2301
  associates: draft.associates?.map(
2270
2302
  (a) => createAssociate(a, context.projectKey, this._storage)
2271
2303
  )
@@ -2339,6 +2371,9 @@ var BusinessUnitUpdateHandler = class extends AbstractUpdateHandler {
2339
2371
  resource.addresses.push(newAddress);
2340
2372
  }
2341
2373
  }
2374
+ changeApprovalRuleMode(context, resource, { approvalRuleMode }) {
2375
+ resource.approvalRuleMode = approvalRuleMode;
2376
+ }
2342
2377
  changeAssociateMode(context, resource, { associateMode }) {
2343
2378
  resource.associateMode = associateMode;
2344
2379
  }
@@ -2638,10 +2673,12 @@ var CartUpdateHandler = class extends AbstractUpdateHandler {
2638
2673
  }
2639
2674
  }
2640
2675
  setDirectDiscounts(context, resource, { discounts }) {
2641
- resource.directDiscounts = discounts.map((discount) => ({
2642
- ...discount,
2643
- id: (0, import_uuid5.v4)()
2644
- }));
2676
+ resource.directDiscounts = discounts.map(
2677
+ (discount) => ({
2678
+ ...discount,
2679
+ id: (0, import_uuid5.v4)()
2680
+ })
2681
+ );
2645
2682
  }
2646
2683
  setLineItemShippingDetails(context, resource, {
2647
2684
  action,
@@ -3451,24 +3488,26 @@ var CustomerRepository = class extends AbstractResourceRepository {
3451
3488
  draft.custom,
3452
3489
  context.projectKey,
3453
3490
  this._storage
3454
- )
3491
+ ),
3492
+ stores: []
3455
3493
  };
3456
3494
  return this.saveNew(context, resource);
3457
3495
  }
3458
- passwordResetToken(context, email) {
3496
+ passwordResetToken(context, request) {
3459
3497
  const results = this._storage.query(context.projectKey, this.getTypeId(), {
3460
- where: [`email="${email.toLocaleLowerCase()}"`]
3498
+ where: [`email="${request.email.toLocaleLowerCase()}"`]
3461
3499
  });
3462
3500
  if (results.count === 0) {
3463
3501
  throw new CommercetoolsError({
3464
3502
  code: "ResourceNotFound",
3465
- message: `The Customer with ID '${email}' was not found.`
3503
+ message: `The Customer with ID '${request.email}' was not found.`
3466
3504
  });
3467
3505
  }
3468
- const expiresAt = new Date(Date.now() + 30 * 60);
3506
+ const ttlMinutes = request.ttlMinutes ?? 34560;
3507
+ const expiresAt = new Date((/* @__PURE__ */ new Date()).getTime() + ttlMinutes * 60 * 1e3);
3469
3508
  const customer = results.results[0];
3470
3509
  const rest = getBaseResourceProperties();
3471
- const token = createPasswordResetToken(customer);
3510
+ const token = createPasswordResetToken(customer, expiresAt);
3472
3511
  return {
3473
3512
  id: rest.id,
3474
3513
  createdAt: rest.createdAt,
@@ -3478,6 +3517,31 @@ var CustomerRepository = class extends AbstractResourceRepository {
3478
3517
  value: token
3479
3518
  };
3480
3519
  }
3520
+ passwordReset(context, resetPassword) {
3521
+ const { newPassword, tokenValue } = resetPassword;
3522
+ const customerId = validatePasswordResetToken(tokenValue);
3523
+ if (!customerId) {
3524
+ throw new CommercetoolsError({
3525
+ code: "ResourceNotFound",
3526
+ message: `The Customer with ID 'Token(${tokenValue})' was not found.`
3527
+ });
3528
+ }
3529
+ const customer = this._storage.get(
3530
+ context.projectKey,
3531
+ "customer",
3532
+ customerId
3533
+ );
3534
+ if (!customer) {
3535
+ throw new CommercetoolsError({
3536
+ code: "ResourceNotFound",
3537
+ message: `The Customer with ID 'Token(${tokenValue})' was not found.`
3538
+ });
3539
+ }
3540
+ customer.password = hashPassword(newPassword);
3541
+ customer.version += 1;
3542
+ this._storage.add(context.projectKey, "customer", customer);
3543
+ return customer;
3544
+ }
3481
3545
  verifyEmailToken(context, id) {
3482
3546
  const results = this._storage.query(context.projectKey, this.getTypeId(), {
3483
3547
  where: [`id="${id.toLocaleLowerCase()}"`]
@@ -3860,31 +3924,6 @@ var MyCustomerRepository = class extends CustomerRepository {
3860
3924
  }
3861
3925
  return;
3862
3926
  }
3863
- resetPassword(context, resetPassword) {
3864
- const { newPassword, tokenValue } = resetPassword;
3865
- const customerId = validatePasswordResetToken(tokenValue);
3866
- if (!customerId) {
3867
- throw new CommercetoolsError({
3868
- code: "ResourceNotFound",
3869
- message: `The Customer with ID 'Token(${tokenValue})' was not found.`
3870
- });
3871
- }
3872
- const customer = this._storage.get(
3873
- context.projectKey,
3874
- "customer",
3875
- customerId
3876
- );
3877
- if (!customer) {
3878
- throw new CommercetoolsError({
3879
- code: "ResourceNotFound",
3880
- message: `The Customer with ID 'Token(${tokenValue})' was not found.`
3881
- });
3882
- }
3883
- customer.password = hashPassword(newPassword);
3884
- customer.version += 1;
3885
- this._storage.add(context.projectKey, "customer", customer);
3886
- return customer;
3887
- }
3888
3927
  };
3889
3928
 
3890
3929
  // src/repositories/my-order.ts
@@ -6245,6 +6284,9 @@ var ShippingMethodUpdateHandler = class extends AbstractUpdateHandler {
6245
6284
  shippingRates: []
6246
6285
  });
6247
6286
  }
6287
+ changeActive(_context, resource, { active }) {
6288
+ resource.active = active;
6289
+ }
6248
6290
  changeIsDefault(_context, resource, { isDefault }) {
6249
6291
  resource.isDefault = isDefault;
6250
6292
  }
@@ -6314,6 +6356,7 @@ var ShippingMethodRepository = class extends AbstractResourceRepository {
6314
6356
  const resource = {
6315
6357
  ...getBaseResourceProperties(),
6316
6358
  ...draft,
6359
+ active: draft.active ?? true,
6317
6360
  taxCategory: getReferenceFromResourceIdentifier(
6318
6361
  draft.taxCategory,
6319
6362
  context.projectKey,
@@ -7130,6 +7173,7 @@ var createRepositories = (storage) => ({
7130
7173
  "product-discount": new ProductDiscountRepository(storage),
7131
7174
  "product-projection": new ProductProjectionRepository(storage),
7132
7175
  "product-selection": new ProductSelectionRepository(storage),
7176
+ "product-tailoring": new ProductTailoringRepository(storage),
7133
7177
  "project": new ProjectRepository(storage),
7134
7178
  "review": new ReviewRepository(storage),
7135
7179
  "quote": new QuoteRepository(storage),
@@ -7527,6 +7571,11 @@ var CustomerService = class extends AbstractService {
7527
7571
  getBasePath() {
7528
7572
  return "customers";
7529
7573
  }
7574
+ extraRoutes(parent) {
7575
+ parent.post("/password-token", this.passwordResetToken.bind(this));
7576
+ parent.post("/password/reset", this.passwordReset.bind(this));
7577
+ parent.post("/email-token", this.confirmEmailToken.bind(this));
7578
+ }
7530
7579
  post(request, response) {
7531
7580
  const draft = request.body;
7532
7581
  const resource = this.repository.create(
@@ -7539,23 +7588,27 @@ var CustomerService = class extends AbstractService {
7539
7588
  };
7540
7589
  return response.status(this.createStatusCode).send(result);
7541
7590
  }
7542
- extraRoutes(parent) {
7543
- parent.post("/password-token", (request, response) => {
7544
- const email = request.body.email;
7545
- const token = this.repository.passwordResetToken(
7546
- getRepositoryContext(request),
7547
- email
7548
- );
7549
- return response.status(200).send(token);
7550
- });
7551
- parent.post("/email-token", (request, response) => {
7552
- const id = request.body.id;
7553
- const token = this.repository.verifyEmailToken(
7554
- getRepositoryContext(request),
7555
- id
7556
- );
7557
- return response.status(200).send(token);
7558
- });
7591
+ passwordResetToken(request, response) {
7592
+ const customer = this.repository.passwordResetToken(
7593
+ getRepositoryContext(request),
7594
+ request.body
7595
+ );
7596
+ return response.status(200).send(customer);
7597
+ }
7598
+ passwordReset(request, response) {
7599
+ const customer = this.repository.passwordReset(
7600
+ getRepositoryContext(request),
7601
+ request.body
7602
+ );
7603
+ return response.status(200).send(customer);
7604
+ }
7605
+ confirmEmailToken(request, response) {
7606
+ const id = request.body.id;
7607
+ const token = this.repository.verifyEmailToken(
7608
+ getRepositoryContext(request),
7609
+ id
7610
+ );
7611
+ return response.status(200).send(token);
7559
7612
  }
7560
7613
  };
7561
7614
 
@@ -7713,7 +7766,7 @@ var MyCustomerService = class extends AbstractService {
7713
7766
  return response.status(200).send(customer);
7714
7767
  }
7715
7768
  resetPassword(request, response) {
7716
- const customer = this.repository.resetPassword(
7769
+ const customer = this.repository.passwordReset(
7717
7770
  getRepositoryContext(request),
7718
7771
  request.body
7719
7772
  );