Skip to content

Commit

Permalink
fix: update lowercaseEmail value on customer update
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedebock authored and mvantellingen committed Nov 21, 2024
1 parent a0593b1 commit de67c5f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/repositories/customer/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -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",
Expand Down
16 changes: 15 additions & 1 deletion src/repositories/customer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -162,6 +162,20 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
return this.saveNew(context, resource);
}

saveUpdate(
context: RepositoryContext,
version: number,
resource: ShallowWritable<ResourceMap["customer"]>,
): ShallowWritable<ResourceMap["customer"]> {
// 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,
Expand Down

0 comments on commit de67c5f

Please sign in to comment.