Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(customer): add lowercaseEmail support #252

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ten-rocks-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": minor
---

Add support for lowercaseEmail on customers
39 changes: 39 additions & 0 deletions src/repositories/customer/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,45 @@ describe("Order repository", () => {
const storage = new InMemoryStorage();
const repository = new CustomerRepository(storage);

test("query by lowercaseEmail", async () => {
const customer = repository.create(
{ projectKey: "dummy" },
{ email: "[email protected]" },
);

const result = repository.query(
{ projectKey: "dummy" },
{ where: [`lowercaseEmail = "[email protected]"`] },
);

expect(result.results).toHaveLength(1);
expect(result.results[0].id).toEqual(customer.id);
});

test("updating lowercaseEmail", async () => {
const customer = repository.create(
{ projectKey: "dummy" },
{ email: "[email protected]" },
);

repository.saveUpdate({ projectKey: "dummy" }, customer.version, {
...customer,
email: "[email protected]",
version: customer.version + 1,
});

const result = repository.query(
{ projectKey: "dummy" },
{ where: [`lowercaseEmail = "[email protected]"`] },
);

expect(result.results).toHaveLength(1);
expect(result.results[0].id).toEqual(customer.id);
expect(result.results[0].email).toEqual(
"[email protected]",
);
});

test("adding stores to customer", async () => {
const store1: Store = {
id: "d0016081-e9af-48a7-8133-1f04f340a335",
Expand Down
22 changes: 19 additions & 3 deletions 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 All @@ -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.toLowerCase()}"`],
});
if (results.count > 0) {
throw new CommercetoolsError<any>({
Expand Down Expand Up @@ -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,
Expand All @@ -156,10 +157,25 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
this._storage,
),
stores: storesForCustomer,
};
} satisfies unknown as 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
1 change: 1 addition & 0 deletions src/services/my-customer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe("Me", () => {
customer: {
...draft,
password: "cDRzc3cwcmQ=",
lowercaseEmail: draft.email.toLowerCase(),
authenticationMode: "Password",
version: 1,
isEmailVerified: false,
Expand Down