From de67c5f71dc4e9622fad5078809b6c61f7672f72 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] 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 87112bc..af28f84 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 cfd4a80..313bf46 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,