From 1de7239e79e3c210a27f48e5302d5a475e501379 Mon Sep 17 00:00:00 2001 From: Mike de Bock <1261006+mikedebock@users.noreply.github.com> Date: Thu, 21 Nov 2024 00:09:17 +0100 Subject: [PATCH 1/3] feat(customer): add lowercaseEmail support --- .changeset/ten-rocks-walk.md | 5 +++++ src/repositories/customer/index.test.ts | 15 +++++++++++++++ src/repositories/customer/index.ts | 6 ++++-- src/services/my-customer.test.ts | 1 + 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 .changeset/ten-rocks-walk.md diff --git a/.changeset/ten-rocks-walk.md b/.changeset/ten-rocks-walk.md new file mode 100644 index 00000000..13321b01 --- /dev/null +++ b/.changeset/ten-rocks-walk.md @@ -0,0 +1,5 @@ +--- +"@labdigital/commercetools-mock": minor +--- + +Add support for lowercaseEmail on customers diff --git a/src/repositories/customer/index.test.ts b/src/repositories/customer/index.test.ts index 64766fc6..87112bce 100644 --- a/src/repositories/customer/index.test.ts +++ b/src/repositories/customer/index.test.ts @@ -7,6 +7,21 @@ describe("Order repository", () => { const storage = new InMemoryStorage(); const repository = new CustomerRepository(storage); + test("lowercaseEmail", async () => { + const customer = repository.create( + { projectKey: "dummy" }, + { email: "my-customer-UPPERCASE@email.com" }, + ); + + const result = repository.query( + { projectKey: "dummy" }, + { where: [`lowercaseEmail = "my-customer-uppercase@email.com"`] }, + ); + + expect(result.results).toHaveLength(1); + expect(result.results[0].id).toEqual(customer.id); + }); + test("adding stores to customer", async () => { const store1: Store = { id: "d0016081-e9af-48a7-8133-1f04f340a335", diff --git a/src/repositories/customer/index.ts b/src/repositories/customer/index.ts index 37fb4285..81fa18c4 100644 --- a/src/repositories/customer/index.ts +++ b/src/repositories/customer/index.ts @@ -37,7 +37,7 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> { create(context: RepositoryContext, draft: CustomerDraft): Customer { // Check uniqueness const results = this._storage.query(context.projectKey, this.getTypeId(), { - where: [`email="${draft.email.toLocaleLowerCase()}"`], + where: [`lowercaseEmail="${draft.email.toLocaleLowerCase()}"`], }); if (results.count > 0) { throw new CommercetoolsError({ @@ -141,6 +141,7 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> { dateOfBirth: draft.dateOfBirth, companyName: draft.companyName, email: draft.email.toLowerCase(), + lowercaseEmail: draft.email.toLowerCase(), password: draft.password ? hashPassword(draft.password) : undefined, isEmailVerified: draft.isEmailVerified || false, addresses: addresses, @@ -156,7 +157,8 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> { this._storage, ), stores: storesForCustomer, - }; + } satisfies unknown as Customer; + return this.saveNew(context, resource); } diff --git a/src/services/my-customer.test.ts b/src/services/my-customer.test.ts index f887ecac..56264eaf 100644 --- a/src/services/my-customer.test.ts +++ b/src/services/my-customer.test.ts @@ -31,6 +31,7 @@ describe("Me", () => { customer: { ...draft, password: "cDRzc3cwcmQ=", + lowercaseEmail: draft.email.toLocaleLowerCase(), authenticationMode: "Password", version: 1, isEmailVerified: false, From 9b24c0e94bcfbf0c1a068f373983310a712197ca Mon Sep 17 00:00:00 2001 From: Mike de Bock <1261006+mikedebock@users.noreply.github.com> Date: Thu, 21 Nov 2024 00:13:15 +0100 Subject: [PATCH 2/3] use toLowerCase --- src/repositories/customer/index.ts | 2 +- src/services/my-customer.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/repositories/customer/index.ts b/src/repositories/customer/index.ts index 81fa18c4..cfd4a802 100644 --- a/src/repositories/customer/index.ts +++ b/src/repositories/customer/index.ts @@ -37,7 +37,7 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> { create(context: RepositoryContext, draft: CustomerDraft): Customer { // Check uniqueness const results = this._storage.query(context.projectKey, this.getTypeId(), { - where: [`lowercaseEmail="${draft.email.toLocaleLowerCase()}"`], + where: [`lowercaseEmail="${draft.email.toLowerCase()}"`], }); if (results.count > 0) { throw new CommercetoolsError({ diff --git a/src/services/my-customer.test.ts b/src/services/my-customer.test.ts index 56264eaf..3ef57f8c 100644 --- a/src/services/my-customer.test.ts +++ b/src/services/my-customer.test.ts @@ -31,7 +31,7 @@ describe("Me", () => { customer: { ...draft, password: "cDRzc3cwcmQ=", - lowercaseEmail: draft.email.toLocaleLowerCase(), + lowercaseEmail: draft.email.toLowerCase(), authenticationMode: "Password", version: 1, isEmailVerified: false, From 615b4e20e9ef89ae5877979d677e6ec2e3a287f8 Mon Sep 17 00:00:00 2001 From: Mike de Bock <1261006+mikedebock@users.noreply.github.com> Date: Thu, 21 Nov 2024 09:49:52 +0100 Subject: [PATCH 3/3] fix: update lowercaseEmail value on customer update --- src/repositories/customer/index.test.ts | 26 ++++++++++++++++++++++++- src/repositories/customer/index.ts | 16 ++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/repositories/customer/index.test.ts b/src/repositories/customer/index.test.ts index 87112bce..af28f84b 100644 --- a/src/repositories/customer/index.test.ts +++ b/src/repositories/customer/index.test.ts @@ -7,7 +7,7 @@ describe("Order repository", () => { const storage = new InMemoryStorage(); const repository = new CustomerRepository(storage); - test("lowercaseEmail", async () => { + test("query by lowercaseEmail", async () => { const customer = repository.create( { projectKey: "dummy" }, { email: "my-customer-UPPERCASE@email.com" }, @@ -22,6 +22,30 @@ describe("Order repository", () => { expect(result.results[0].id).toEqual(customer.id); }); + test("updating lowercaseEmail", async () => { + const customer = repository.create( + { projectKey: "dummy" }, + { email: "my-customer-UPPERCASE-v1@email.com" }, + ); + + repository.saveUpdate({ projectKey: "dummy" }, customer.version, { + ...customer, + email: "my-customer-UPPERCASE-v2@email.com", + version: customer.version + 1, + }); + + const result = repository.query( + { projectKey: "dummy" }, + { where: [`lowercaseEmail = "my-customer-uppercase-v2@email.com"`] }, + ); + + expect(result.results).toHaveLength(1); + expect(result.results[0].id).toEqual(customer.id); + expect(result.results[0].email).toEqual( + "my-customer-UPPERCASE-v2@email.com", + ); + }); + test("adding stores to customer", async () => { const store1: Store = { id: "d0016081-e9af-48a7-8133-1f04f340a335", diff --git a/src/repositories/customer/index.ts b/src/repositories/customer/index.ts index cfd4a802..313bf46d 100644 --- a/src/repositories/customer/index.ts +++ b/src/repositories/customer/index.ts @@ -20,7 +20,7 @@ import { validatePasswordResetToken, } from "~src/lib/password"; import type { AbstractStorage } from "~src/storage/abstract"; -import type { Writable } from "~src/types"; +import type { ResourceMap, ShallowWritable, Writable } from "~src/types"; import { AbstractResourceRepository, type RepositoryContext, @@ -162,6 +162,20 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> { return this.saveNew(context, resource); } + saveUpdate( + context: RepositoryContext, + version: number, + resource: ShallowWritable, + ): ShallowWritable { + // Also update lowercaseEmail attribute + const updatedResource: Customer = { + ...resource, + lowercaseEmail: resource.email.toLowerCase(), + } satisfies unknown as Customer; + + return super.saveUpdate(context, version, updatedResource); + } + passwordResetToken( context: RepositoryContext, request: CustomerCreatePasswordResetToken,