@labdigital/commercetools-mock 2.26.1 → 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.cjs +65 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +65 -52
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/password.ts +12 -5
- package/src/repositories/customer/index.ts +52 -5
- package/src/repositories/my-customer.ts +1 -41
- package/src/services/customer.test.ts +62 -2
- package/src/services/customer.ts +31 -18
- package/src/services/my-customer.ts +1 -1
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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(
|
|
48
|
-
|
|
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) => {
|
|
@@ -3418,20 +3421,21 @@ var CustomerRepository = class extends AbstractResourceRepository {
|
|
|
3418
3421
|
};
|
|
3419
3422
|
return this.saveNew(context, resource);
|
|
3420
3423
|
}
|
|
3421
|
-
passwordResetToken(context,
|
|
3424
|
+
passwordResetToken(context, request) {
|
|
3422
3425
|
const results = this._storage.query(context.projectKey, this.getTypeId(), {
|
|
3423
|
-
where: [`email="${email.toLocaleLowerCase()}"`]
|
|
3426
|
+
where: [`email="${request.email.toLocaleLowerCase()}"`]
|
|
3424
3427
|
});
|
|
3425
3428
|
if (results.count === 0) {
|
|
3426
3429
|
throw new CommercetoolsError({
|
|
3427
3430
|
code: "ResourceNotFound",
|
|
3428
|
-
message: `The Customer with ID '${email}' was not found.`
|
|
3431
|
+
message: `The Customer with ID '${request.email}' was not found.`
|
|
3429
3432
|
});
|
|
3430
3433
|
}
|
|
3431
|
-
const
|
|
3434
|
+
const ttlMinutes = request.ttlMinutes ?? 34560;
|
|
3435
|
+
const expiresAt = new Date((/* @__PURE__ */ new Date()).getTime() + ttlMinutes * 60 * 1e3);
|
|
3432
3436
|
const customer = results.results[0];
|
|
3433
3437
|
const rest = getBaseResourceProperties();
|
|
3434
|
-
const token = createPasswordResetToken(customer);
|
|
3438
|
+
const token = createPasswordResetToken(customer, expiresAt);
|
|
3435
3439
|
return {
|
|
3436
3440
|
id: rest.id,
|
|
3437
3441
|
createdAt: rest.createdAt,
|
|
@@ -3441,6 +3445,31 @@ var CustomerRepository = class extends AbstractResourceRepository {
|
|
|
3441
3445
|
value: token
|
|
3442
3446
|
};
|
|
3443
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
|
+
}
|
|
3444
3473
|
verifyEmailToken(context, id) {
|
|
3445
3474
|
const results = this._storage.query(context.projectKey, this.getTypeId(), {
|
|
3446
3475
|
where: [`id="${id.toLocaleLowerCase()}"`]
|
|
@@ -3823,31 +3852,6 @@ var MyCustomerRepository = class extends CustomerRepository {
|
|
|
3823
3852
|
}
|
|
3824
3853
|
return;
|
|
3825
3854
|
}
|
|
3826
|
-
resetPassword(context, resetPassword) {
|
|
3827
|
-
const { newPassword, tokenValue } = resetPassword;
|
|
3828
|
-
const customerId = validatePasswordResetToken(tokenValue);
|
|
3829
|
-
if (!customerId) {
|
|
3830
|
-
throw new CommercetoolsError({
|
|
3831
|
-
code: "ResourceNotFound",
|
|
3832
|
-
message: `The Customer with ID 'Token(${tokenValue})' was not found.`
|
|
3833
|
-
});
|
|
3834
|
-
}
|
|
3835
|
-
const customer = this._storage.get(
|
|
3836
|
-
context.projectKey,
|
|
3837
|
-
"customer",
|
|
3838
|
-
customerId
|
|
3839
|
-
);
|
|
3840
|
-
if (!customer) {
|
|
3841
|
-
throw new CommercetoolsError({
|
|
3842
|
-
code: "ResourceNotFound",
|
|
3843
|
-
message: `The Customer with ID 'Token(${tokenValue})' was not found.`
|
|
3844
|
-
});
|
|
3845
|
-
}
|
|
3846
|
-
customer.password = hashPassword(newPassword);
|
|
3847
|
-
customer.version += 1;
|
|
3848
|
-
this._storage.add(context.projectKey, "customer", customer);
|
|
3849
|
-
return customer;
|
|
3850
|
-
}
|
|
3851
3855
|
};
|
|
3852
3856
|
|
|
3853
3857
|
// src/repositories/my-order.ts
|
|
@@ -7490,6 +7494,11 @@ var CustomerService = class extends AbstractService {
|
|
|
7490
7494
|
getBasePath() {
|
|
7491
7495
|
return "customers";
|
|
7492
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
|
+
}
|
|
7493
7502
|
post(request, response) {
|
|
7494
7503
|
const draft = request.body;
|
|
7495
7504
|
const resource = this.repository.create(
|
|
@@ -7502,23 +7511,27 @@ var CustomerService = class extends AbstractService {
|
|
|
7502
7511
|
};
|
|
7503
7512
|
return response.status(this.createStatusCode).send(result);
|
|
7504
7513
|
}
|
|
7505
|
-
|
|
7506
|
-
|
|
7507
|
-
|
|
7508
|
-
|
|
7509
|
-
|
|
7510
|
-
|
|
7511
|
-
|
|
7512
|
-
|
|
7513
|
-
|
|
7514
|
-
|
|
7515
|
-
|
|
7516
|
-
|
|
7517
|
-
|
|
7518
|
-
|
|
7519
|
-
|
|
7520
|
-
|
|
7521
|
-
|
|
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);
|
|
7522
7535
|
}
|
|
7523
7536
|
};
|
|
7524
7537
|
|
|
@@ -7676,7 +7689,7 @@ var MyCustomerService = class extends AbstractService {
|
|
|
7676
7689
|
return response.status(200).send(customer);
|
|
7677
7690
|
}
|
|
7678
7691
|
resetPassword(request, response) {
|
|
7679
|
-
const customer = this.repository.
|
|
7692
|
+
const customer = this.repository.passwordReset(
|
|
7680
7693
|
getRepositoryContext(request),
|
|
7681
7694
|
request.body
|
|
7682
7695
|
);
|