@labdigital/commercetools-mock 2.26.0 → 2.27.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.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import express from 'express';
2
2
  import { SetupServerApi, SetupServer } from 'msw/node';
3
3
  import * as ctp from '@commercetools/platform-sdk';
4
- import { QueryParam, BaseResource, Project, UpdateAction, ResourceIdentifier, AssociateRoleDraft, AssociateRole, AttributeGroupDraft, AttributeGroup, BusinessUnitDraft, BusinessUnit, CartDraft, Cart, LineItemDraft, LineItem, CartDiscountDraft, CartDiscount, CategoryDraft, Category, ChannelDraft, Channel, CustomObjectDraft, CustomObject, CustomerDraft, Customer, CustomerToken, CustomerGroupDraft, CustomerGroup, DiscountCodeDraft, DiscountCode, ExtensionDraft, Extension, InventoryEntryDraft, InventoryEntry, MyCustomerChangePassword, MyCustomerEmailVerify, MyCustomerResetPassword, OrderFromCartDraft, Order, CartReference, OrderImportDraft, MyOrderFromCartDraft, OrderEditDraft, OrderEdit, PaymentDraft, Payment, ProductDraft, Product, ProductDiscountDraft, ProductDiscount, ProductProjectionPagedSearchResponse, ProductProjection, FacetResults, TermFacetResult, FilteredFacetResult, RangeFacetResult, ProductSelectionDraft, ProductSelection, ProductTypeDraft, ProductType, QuoteDraft, Quote, QuoteRequestDraft, QuoteRequest, ReviewDraft, Review, ShippingMethodDraft, ShippingMethod, ZoneReference, ShoppingListDraft, ShoppingList, ShoppingListLineItem, StagedQuoteDraft, StagedQuote, StandalonePriceDraft, StandalonePrice, ChannelResourceIdentifier, ChannelReference, StateDraft, State, StoreDraft, Store, SubscriptionDraft, Subscription, TaxCategoryDraft, TaxCategory, TypeDraft, Type, ZoneDraft, Zone } from '@commercetools/platform-sdk';
4
+ import { QueryParam, BaseResource, Project, UpdateAction, ResourceIdentifier, AssociateRoleDraft, AssociateRole, AttributeGroupDraft, AttributeGroup, BusinessUnitDraft, BusinessUnit, CartDraft, Cart, LineItemDraft, LineItem, CartDiscountDraft, CartDiscount, CategoryDraft, Category, ChannelDraft, Channel, CustomObjectDraft, CustomObject, CustomerDraft, Customer, CustomerCreatePasswordResetToken, CustomerToken, CustomerResetPassword, MyCustomerResetPassword, CustomerGroupDraft, CustomerGroup, DiscountCodeDraft, DiscountCode, ExtensionDraft, Extension, InventoryEntryDraft, InventoryEntry, MyCustomerChangePassword, MyCustomerEmailVerify, OrderFromCartDraft, Order, CartReference, OrderImportDraft, MyOrderFromCartDraft, OrderEditDraft, OrderEdit, PaymentDraft, Payment, ProductDraft, Product, ProductDiscountDraft, ProductDiscount, ProductProjectionPagedSearchResponse, ProductProjection, FacetResults, TermFacetResult, FilteredFacetResult, RangeFacetResult, ProductSelectionDraft, ProductSelection, ProductTypeDraft, ProductType, QuoteDraft, Quote, QuoteRequestDraft, QuoteRequest, ReviewDraft, Review, ShippingMethodDraft, ShippingMethod, ZoneReference, ShoppingListDraft, ShoppingList, ShoppingListLineItem, StagedQuoteDraft, StagedQuote, StandalonePriceDraft, StandalonePrice, ChannelResourceIdentifier, ChannelReference, StateDraft, State, StoreDraft, Store, SubscriptionDraft, Subscription, TaxCategoryDraft, TaxCategory, TypeDraft, Type, ZoneDraft, Zone } from '@commercetools/platform-sdk';
5
5
 
6
6
  type Token = {
7
7
  access_token: string;
@@ -235,7 +235,8 @@ declare class CustomObjectRepository extends AbstractResourceRepository<"key-val
235
235
  declare class CustomerRepository extends AbstractResourceRepository<"customer"> {
236
236
  constructor(storage: AbstractStorage);
237
237
  create(context: RepositoryContext, draft: CustomerDraft): Customer;
238
- passwordResetToken(context: RepositoryContext, email: string): CustomerToken;
238
+ passwordResetToken(context: RepositoryContext, request: CustomerCreatePasswordResetToken): CustomerToken;
239
+ passwordReset(context: RepositoryContext, resetPassword: CustomerResetPassword | MyCustomerResetPassword): Writable<Customer>;
239
240
  verifyEmailToken(context: RepositoryContext, id: string): CustomerToken;
240
241
  }
241
242
 
@@ -265,7 +266,6 @@ declare class MyCustomerRepository extends CustomerRepository {
265
266
  confirmEmail(context: RepositoryContext, resetPassword: MyCustomerEmailVerify): Writable<Customer>;
266
267
  deleteMe(context: RepositoryContext): Customer | undefined;
267
268
  getMe(context: RepositoryContext): Customer | undefined;
268
- resetPassword(context: RepositoryContext, resetPassword: MyCustomerResetPassword): Writable<Customer>;
269
269
  }
270
270
 
271
271
  declare class OrderRepository extends AbstractResourceRepository<"order"> {
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import express from 'express';
2
2
  import { SetupServerApi, SetupServer } from 'msw/node';
3
3
  import * as ctp from '@commercetools/platform-sdk';
4
- import { QueryParam, BaseResource, Project, UpdateAction, ResourceIdentifier, AssociateRoleDraft, AssociateRole, AttributeGroupDraft, AttributeGroup, BusinessUnitDraft, BusinessUnit, CartDraft, Cart, LineItemDraft, LineItem, CartDiscountDraft, CartDiscount, CategoryDraft, Category, ChannelDraft, Channel, CustomObjectDraft, CustomObject, CustomerDraft, Customer, CustomerToken, CustomerGroupDraft, CustomerGroup, DiscountCodeDraft, DiscountCode, ExtensionDraft, Extension, InventoryEntryDraft, InventoryEntry, MyCustomerChangePassword, MyCustomerEmailVerify, MyCustomerResetPassword, OrderFromCartDraft, Order, CartReference, OrderImportDraft, MyOrderFromCartDraft, OrderEditDraft, OrderEdit, PaymentDraft, Payment, ProductDraft, Product, ProductDiscountDraft, ProductDiscount, ProductProjectionPagedSearchResponse, ProductProjection, FacetResults, TermFacetResult, FilteredFacetResult, RangeFacetResult, ProductSelectionDraft, ProductSelection, ProductTypeDraft, ProductType, QuoteDraft, Quote, QuoteRequestDraft, QuoteRequest, ReviewDraft, Review, ShippingMethodDraft, ShippingMethod, ZoneReference, ShoppingListDraft, ShoppingList, ShoppingListLineItem, StagedQuoteDraft, StagedQuote, StandalonePriceDraft, StandalonePrice, ChannelResourceIdentifier, ChannelReference, StateDraft, State, StoreDraft, Store, SubscriptionDraft, Subscription, TaxCategoryDraft, TaxCategory, TypeDraft, Type, ZoneDraft, Zone } from '@commercetools/platform-sdk';
4
+ import { QueryParam, BaseResource, Project, UpdateAction, ResourceIdentifier, AssociateRoleDraft, AssociateRole, AttributeGroupDraft, AttributeGroup, BusinessUnitDraft, BusinessUnit, CartDraft, Cart, LineItemDraft, LineItem, CartDiscountDraft, CartDiscount, CategoryDraft, Category, ChannelDraft, Channel, CustomObjectDraft, CustomObject, CustomerDraft, Customer, CustomerCreatePasswordResetToken, CustomerToken, CustomerResetPassword, MyCustomerResetPassword, CustomerGroupDraft, CustomerGroup, DiscountCodeDraft, DiscountCode, ExtensionDraft, Extension, InventoryEntryDraft, InventoryEntry, MyCustomerChangePassword, MyCustomerEmailVerify, OrderFromCartDraft, Order, CartReference, OrderImportDraft, MyOrderFromCartDraft, OrderEditDraft, OrderEdit, PaymentDraft, Payment, ProductDraft, Product, ProductDiscountDraft, ProductDiscount, ProductProjectionPagedSearchResponse, ProductProjection, FacetResults, TermFacetResult, FilteredFacetResult, RangeFacetResult, ProductSelectionDraft, ProductSelection, ProductTypeDraft, ProductType, QuoteDraft, Quote, QuoteRequestDraft, QuoteRequest, ReviewDraft, Review, ShippingMethodDraft, ShippingMethod, ZoneReference, ShoppingListDraft, ShoppingList, ShoppingListLineItem, StagedQuoteDraft, StagedQuote, StandalonePriceDraft, StandalonePrice, ChannelResourceIdentifier, ChannelReference, StateDraft, State, StoreDraft, Store, SubscriptionDraft, Subscription, TaxCategoryDraft, TaxCategory, TypeDraft, Type, ZoneDraft, Zone } from '@commercetools/platform-sdk';
5
5
 
6
6
  type Token = {
7
7
  access_token: string;
@@ -235,7 +235,8 @@ declare class CustomObjectRepository extends AbstractResourceRepository<"key-val
235
235
  declare class CustomerRepository extends AbstractResourceRepository<"customer"> {
236
236
  constructor(storage: AbstractStorage);
237
237
  create(context: RepositoryContext, draft: CustomerDraft): Customer;
238
- passwordResetToken(context: RepositoryContext, email: string): CustomerToken;
238
+ passwordResetToken(context: RepositoryContext, request: CustomerCreatePasswordResetToken): CustomerToken;
239
+ passwordReset(context: RepositoryContext, resetPassword: CustomerResetPassword | MyCustomerResetPassword): Writable<Customer>;
239
240
  verifyEmailToken(context: RepositoryContext, id: string): CustomerToken;
240
241
  }
241
242
 
@@ -265,7 +266,6 @@ declare class MyCustomerRepository extends CustomerRepository {
265
266
  confirmEmail(context: RepositoryContext, resetPassword: MyCustomerEmailVerify): Writable<Customer>;
266
267
  deleteMe(context: RepositoryContext): Customer | undefined;
267
268
  getMe(context: RepositoryContext): Customer | undefined;
268
- resetPassword(context: RepositoryContext, resetPassword: MyCustomerResetPassword): Writable<Customer>;
269
269
  }
270
270
 
271
271
  declare class OrderRepository extends AbstractResourceRepository<"order"> {
package/dist/index.js CHANGED
@@ -44,18 +44,21 @@ import { v4 as uuidv4 } from "uuid";
44
44
  var PWRESET_SECRET = "pwreset";
45
45
  var EMAIL_VERIFY_SECRET = "emailverifysecret";
46
46
  var hashPassword = (clearPassword) => Buffer.from(clearPassword).toString("base64");
47
- var createPasswordResetToken = (customer) => Buffer.from(`${customer.id}:${PWRESET_SECRET}:${uuidv4()}`).toString(
48
- "base64"
49
- );
47
+ var createPasswordResetToken = (customer, expiresAt) => Buffer.from(
48
+ `${customer.id}:${PWRESET_SECRET}:${expiresAt.getTime()}`
49
+ ).toString("base64");
50
50
  var createEmailVerifyToken = (customer) => Buffer.from(`${customer.id}:${EMAIL_VERIFY_SECRET}:${uuidv4()}`).toString(
51
51
  "base64"
52
52
  );
53
53
  var validatePasswordResetToken = (token) => {
54
54
  const items = Buffer.from(token, "base64").toString("utf-8").split(":");
55
- const [customerId, secret] = items;
55
+ const [customerId, secret, time] = items;
56
56
  if (secret !== PWRESET_SECRET) {
57
57
  return void 0;
58
58
  }
59
+ if (parseInt(time) < (/* @__PURE__ */ new Date()).getTime()) {
60
+ return void 0;
61
+ }
59
62
  return customerId;
60
63
  };
61
64
  var validateEmailVerifyToken = (token) => {
@@ -3327,6 +3330,17 @@ var CustomerUpdateHandler = class extends AbstractUpdateHandler {
3327
3330
  }
3328
3331
  resource.custom.fields[name] = value;
3329
3332
  }
3333
+ setCustomType(context, resource, { type, fields }) {
3334
+ if (type) {
3335
+ resource.custom = createCustomFields(
3336
+ { type, fields },
3337
+ context.projectKey,
3338
+ this._storage
3339
+ );
3340
+ } else {
3341
+ resource.custom = void 0;
3342
+ }
3343
+ }
3330
3344
  setExternalId(_context, resource, { externalId }) {
3331
3345
  resource.externalId = externalId;
3332
3346
  }
@@ -3407,20 +3421,21 @@ var CustomerRepository = class extends AbstractResourceRepository {
3407
3421
  };
3408
3422
  return this.saveNew(context, resource);
3409
3423
  }
3410
- passwordResetToken(context, email) {
3424
+ passwordResetToken(context, request) {
3411
3425
  const results = this._storage.query(context.projectKey, this.getTypeId(), {
3412
- where: [`email="${email.toLocaleLowerCase()}"`]
3426
+ where: [`email="${request.email.toLocaleLowerCase()}"`]
3413
3427
  });
3414
3428
  if (results.count === 0) {
3415
3429
  throw new CommercetoolsError({
3416
3430
  code: "ResourceNotFound",
3417
- message: `The Customer with ID '${email}' was not found.`
3431
+ message: `The Customer with ID '${request.email}' was not found.`
3418
3432
  });
3419
3433
  }
3420
- const expiresAt = new Date(Date.now() + 30 * 60);
3434
+ const ttlMinutes = request.ttlMinutes ?? 34560;
3435
+ const expiresAt = new Date((/* @__PURE__ */ new Date()).getTime() + ttlMinutes * 60 * 1e3);
3421
3436
  const customer = results.results[0];
3422
3437
  const rest = getBaseResourceProperties();
3423
- const token = createPasswordResetToken(customer);
3438
+ const token = createPasswordResetToken(customer, expiresAt);
3424
3439
  return {
3425
3440
  id: rest.id,
3426
3441
  createdAt: rest.createdAt,
@@ -3430,6 +3445,31 @@ var CustomerRepository = class extends AbstractResourceRepository {
3430
3445
  value: token
3431
3446
  };
3432
3447
  }
3448
+ passwordReset(context, resetPassword) {
3449
+ const { newPassword, tokenValue } = resetPassword;
3450
+ const customerId = validatePasswordResetToken(tokenValue);
3451
+ if (!customerId) {
3452
+ throw new CommercetoolsError({
3453
+ code: "ResourceNotFound",
3454
+ message: `The Customer with ID 'Token(${tokenValue})' was not found.`
3455
+ });
3456
+ }
3457
+ const customer = this._storage.get(
3458
+ context.projectKey,
3459
+ "customer",
3460
+ customerId
3461
+ );
3462
+ if (!customer) {
3463
+ throw new CommercetoolsError({
3464
+ code: "ResourceNotFound",
3465
+ message: `The Customer with ID 'Token(${tokenValue})' was not found.`
3466
+ });
3467
+ }
3468
+ customer.password = hashPassword(newPassword);
3469
+ customer.version += 1;
3470
+ this._storage.add(context.projectKey, "customer", customer);
3471
+ return customer;
3472
+ }
3433
3473
  verifyEmailToken(context, id) {
3434
3474
  const results = this._storage.query(context.projectKey, this.getTypeId(), {
3435
3475
  where: [`id="${id.toLocaleLowerCase()}"`]
@@ -3812,31 +3852,6 @@ var MyCustomerRepository = class extends CustomerRepository {
3812
3852
  }
3813
3853
  return;
3814
3854
  }
3815
- resetPassword(context, resetPassword) {
3816
- const { newPassword, tokenValue } = resetPassword;
3817
- const customerId = validatePasswordResetToken(tokenValue);
3818
- if (!customerId) {
3819
- throw new CommercetoolsError({
3820
- code: "ResourceNotFound",
3821
- message: `The Customer with ID 'Token(${tokenValue})' was not found.`
3822
- });
3823
- }
3824
- const customer = this._storage.get(
3825
- context.projectKey,
3826
- "customer",
3827
- customerId
3828
- );
3829
- if (!customer) {
3830
- throw new CommercetoolsError({
3831
- code: "ResourceNotFound",
3832
- message: `The Customer with ID 'Token(${tokenValue})' was not found.`
3833
- });
3834
- }
3835
- customer.password = hashPassword(newPassword);
3836
- customer.version += 1;
3837
- this._storage.add(context.projectKey, "customer", customer);
3838
- return customer;
3839
- }
3840
3855
  };
3841
3856
 
3842
3857
  // src/repositories/my-order.ts
@@ -7479,6 +7494,11 @@ var CustomerService = class extends AbstractService {
7479
7494
  getBasePath() {
7480
7495
  return "customers";
7481
7496
  }
7497
+ extraRoutes(parent) {
7498
+ parent.post("/password-token", this.passwordResetToken.bind(this));
7499
+ parent.post("/password/reset", this.passwordReset.bind(this));
7500
+ parent.post("/email-token", this.confirmEmailToken.bind(this));
7501
+ }
7482
7502
  post(request, response) {
7483
7503
  const draft = request.body;
7484
7504
  const resource = this.repository.create(
@@ -7491,23 +7511,27 @@ var CustomerService = class extends AbstractService {
7491
7511
  };
7492
7512
  return response.status(this.createStatusCode).send(result);
7493
7513
  }
7494
- extraRoutes(parent) {
7495
- parent.post("/password-token", (request, response) => {
7496
- const email = request.body.email;
7497
- const token = this.repository.passwordResetToken(
7498
- getRepositoryContext(request),
7499
- email
7500
- );
7501
- return response.status(200).send(token);
7502
- });
7503
- parent.post("/email-token", (request, response) => {
7504
- const id = request.body.id;
7505
- const token = this.repository.verifyEmailToken(
7506
- getRepositoryContext(request),
7507
- id
7508
- );
7509
- return response.status(200).send(token);
7510
- });
7514
+ passwordResetToken(request, response) {
7515
+ const customer = this.repository.passwordResetToken(
7516
+ getRepositoryContext(request),
7517
+ request.body
7518
+ );
7519
+ return response.status(200).send(customer);
7520
+ }
7521
+ passwordReset(request, response) {
7522
+ const customer = this.repository.passwordReset(
7523
+ getRepositoryContext(request),
7524
+ request.body
7525
+ );
7526
+ return response.status(200).send(customer);
7527
+ }
7528
+ confirmEmailToken(request, response) {
7529
+ const id = request.body.id;
7530
+ const token = this.repository.verifyEmailToken(
7531
+ getRepositoryContext(request),
7532
+ id
7533
+ );
7534
+ return response.status(200).send(token);
7511
7535
  }
7512
7536
  };
7513
7537
 
@@ -7665,7 +7689,7 @@ var MyCustomerService = class extends AbstractService {
7665
7689
  return response.status(200).send(customer);
7666
7690
  }
7667
7691
  resetPassword(request, response) {
7668
- const customer = this.repository.resetPassword(
7692
+ const customer = this.repository.passwordReset(
7669
7693
  getRepositoryContext(request),
7670
7694
  request.body
7671
7695
  );