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

[#IOPID-1197] Add get method to table storage model #354

Merged
merged 1 commit into from
Jan 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ describe("isEmailAlreadyTaken", () => {
({ entries, expected }) => {
const result = isEmailAlreadyTaken(mocks.email)({
profileEmails: {
list: generateProfileEmails(entries)
list: generateProfileEmails(entries),
get: jest.fn()
}
});
expect(result).resolves.toBe(expected);
Expand Down
41 changes: 40 additions & 1 deletion src/utils/unique_email_enforcement/__tests__/storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { describe, it, expect, jest } from "@jest/globals";

import { odata, TableEntityResult } from "@azure/data-tables";
import {
GetTableEntityResponse,
odata,
TableEntityResult
} from "@azure/data-tables";
import { DataTableProfileEmailsRepository } from "../storage";

import { TableClient } from "@azure/data-tables";
Expand Down Expand Up @@ -45,12 +49,47 @@ MockedTableClient.prototype.listEntities.mockReturnValue(
new MockPagedAsyncIterableIterator()
);

MockedTableClient.prototype.getEntity.mockImplementation(
<T extends object = Record<string, unknown>>(
partitionKey: string,
rowKey: string
) =>
Promise.resolve(({
"odata.metadata": "odata.metadata",
etag: "etag",
partitionKey,
rowKey,
timestamp: "2024-01-09T09:41:37.7269414Z"
} as unknown) as GetTableEntityResponse<TableEntityResult<T>>)
);

const tableClient = new MockedTableClient(
"https://test.localhost",
"test-table"
);

describe("DataTableProfileEmailsRepository", () => {
describe("get", () => {
it.each(["citizen@email.test.pagopa.it", "CITIZEN@EMAIL.TEST.PAGOPA.IT"])(
"normalizes input e-mail addresses",
async email => {
const repo = new DataTableProfileEmailsRepository(tableClient);
const profileEmail = ProfileEmail.decode({
email,
fiscalCode: "RLDBSV36A78Y792X"
});
if (E.isRight(profileEmail)) {
await repo.get(profileEmail.right);
expect(tableClient.getEntity).toHaveBeenCalledWith(
"citizen@email.test.pagopa.it",
"RLDBSV36A78Y792X"
);
}
expect.hasAssertions();
}
);
});

describe("list", () => {
it("normalizes input e-mail address", async () => {
const repo = new DataTableProfileEmailsRepository(tableClient);
Expand Down
1 change: 1 addition & 0 deletions src/utils/unique_email_enforcement/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const ProfileEmail = t.type({
export type ProfileEmail = t.TypeOf<typeof ProfileEmail>;

export interface IProfileEmailReader {
readonly get: (p: ProfileEmail) => Promise<ProfileEmail>;
readonly list: (filter: EmailString) => AsyncIterableIterator<ProfileEmail>;
}

Expand Down
20 changes: 20 additions & 0 deletions src/utils/unique_email_enforcement/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ export class DataTableProfileEmailsRepository
implements IProfileEmailReader, IProfileEmailWriter {
constructor(private readonly tableClient: TableClient) {}

public async get(p: ProfileEmail): Promise<ProfileEmail> {
try {
const entity = await this.tableClient.getEntity(
p.email.toLowerCase(),
p.fiscalCode
);
const profileEmail = ProfileEmailToTableEntity.decode(entity);
if (E.isLeft(profileEmail)) {
throw new Error(`can't parse a profile email from the given entity`, {
cause: "parsing"
});
}
return profileEmail.right;
} catch {
throw new Error(
`unable to get a profile entity from ${this.tableClient.tableName} table`
);
}
}

// Generates an AsyncIterable<ProfileEmail>
public async *list(filter: EmailString): AsyncIterableIterator<ProfileEmail> {
const queryOptions = {
Expand Down