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

Cleanup Segment app #1699

Merged
merged 2 commits into from
Jan 21, 2025
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/fair-moles-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"segment": patch
---

Cleanup Segment app - rename of files or fix naming.
30 changes: 15 additions & 15 deletions apps/segment/src/lib/dyanmodb-apl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";

import { BaseError } from "@/errors";

import { InMemoryAPLRepository } from "./__tests__/in-memory-apl-repository";
import { MemoryAPLRepository } from "../modules/db/__tests__/memory-apl-repository";
import { DynamoAPL } from "./dynamodb-apl";

describe("DynamoAPL", () => {
Expand All @@ -20,7 +20,7 @@ describe("DynamoAPL", () => {
});

it("should get auth data if it exists", async () => {
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const apl = new DynamoAPL({ repository });

repository.setEntry({
Expand All @@ -33,7 +33,7 @@ describe("DynamoAPL", () => {
});

it("should return undefined if auth data does not exist", async () => {
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const apl = new DynamoAPL({ repository });

const result = await apl.get("saleorApiUrl");
Expand All @@ -42,7 +42,7 @@ describe("DynamoAPL", () => {
});

it("should throw an error if getting auth data fails", async () => {
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const apl = new DynamoAPL({ repository });

vi.spyOn(repository, "getEntry").mockReturnValue(
Expand All @@ -53,7 +53,7 @@ describe("DynamoAPL", () => {
});

it("should set auth data", async () => {
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const apl = new DynamoAPL({ repository });

const result = await apl.set(mockedAuthData);
Expand All @@ -68,7 +68,7 @@ describe("DynamoAPL", () => {
});

it("should throw an error if setting auth data fails", async () => {
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();

vi.spyOn(repository, "setEntry").mockResolvedValue(err(new BaseError("Error setting data")));

Expand All @@ -78,7 +78,7 @@ describe("DynamoAPL", () => {
});

it("should update existing auth data", async () => {
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const apl = new DynamoAPL({ repository });

repository.setEntry({
Expand All @@ -103,7 +103,7 @@ describe("DynamoAPL", () => {
});

it("should delete auth data", async () => {
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const apl = new DynamoAPL({ repository });

repository.setEntry({
Expand All @@ -118,14 +118,14 @@ describe("DynamoAPL", () => {
});

it("should throw an error if deleting auth data fails", async () => {
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const apl = new DynamoAPL({ repository });

await expect(apl.delete("saleorApiUrl")).rejects.toThrowError(DynamoAPL.DeleteAuthDataError);
});

it("should get all auth data", async () => {
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const secondEntry: AuthData = {
saleorApiUrl: "saleorApiUrl2",
token: "appToken2",
Expand All @@ -148,7 +148,7 @@ describe("DynamoAPL", () => {
});

it("should throw an error if getting all auth data fails", async () => {
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const apl = new DynamoAPL({ repository });

vi.spyOn(repository, "getAllEntries").mockResolvedValue(
Expand All @@ -159,7 +159,7 @@ describe("DynamoAPL", () => {
});

it("should return ready:true when APL related env variables are set", async () => {
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const apl = new DynamoAPL({ repository });

const result = await apl.isReady();
Expand All @@ -183,7 +183,7 @@ describe("DynamoAPL", () => {
NODE_ENV: "test",
ENV: "local",
});
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const apl = new DynamoAPL({ repository });

const result = await apl.isReady();
Expand All @@ -195,7 +195,7 @@ describe("DynamoAPL", () => {
});

it("should return configured:true when APL related env variables are set", async () => {
const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const apl = new DynamoAPL({ repository });

const result = await apl.isConfigured();
Expand All @@ -217,7 +217,7 @@ describe("DynamoAPL", () => {
ENV: "local",
});

const repository = new InMemoryAPLRepository();
const repository = new MemoryAPLRepository();
const apl = new DynamoAPL({ repository });

const result = await apl.isConfigured();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const WebhookStatus = () => {
{config.areWebhooksActive ? (
<>
<Text size={2} color="default2">
Webhooks are active. For more information about webhooks check Manage app button in
header above.
App webhooks are active. For more information about webhooks check Manage app button
in header above.
</Text>
<SemanticChip marginLeft={"auto"} size="small" variant={"success"}>
ACTIVE
Expand All @@ -26,7 +26,7 @@ export const WebhookStatus = () => {
) : (
<>
<Text size={2} color="default2">
Your webhooks were disabled. Most likely, your configuration is invalid. Check your
App webhooks were disabled. Most likely, your configuration is invalid. Check your
credentials.
</Text>
<SemanticChip marginLeft={"auto"} size="small" variant={"error"}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { err, ok, Result } from "neverthrow";
import { BaseError } from "@/errors";
import { APLRepository } from "@/modules/db/types";

export class InMemoryAPLRepository implements APLRepository {
export class MemoryAPLRepository implements APLRepository {
public entries: Record<string, AuthData> = {};

async getEntry(args: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { FormattedItem, type PutItemInput } from "dynamodb-toolbox";

import { SegmentAPLEntityType, SegmentMainTable } from "@/modules/db/segment-main-table";

export class SegmentAPLMapper {
dynamoDBEntityToAuthData(entity: FormattedItem<SegmentAPLEntityType>): AuthData {
export class DynamoAPLMapper {
dynamoEntityToAuthData(entity: FormattedItem<SegmentAPLEntityType>): AuthData {
return {
domain: entity.domain,
token: entity.token,
Expand Down
18 changes: 18 additions & 0 deletions apps/segment/src/modules/db/dynamo-apl-repository-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { BaseError } from "@/errors";

import { DynamoAPLRepository } from "./dynamo-apl-repository";

export class DynamoAPLRepositoryFactory {
static RepositoryCreationError = BaseError.subclass("RepositoryCreationError");

static create(): DynamoAPLRepository {
try {
return new DynamoAPLRepository();
} catch (error) {
throw new DynamoAPLRepositoryFactory.RepositoryCreationError(
"Failed to create DynamoDB APL repository",
{ cause: error },
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,12 @@ import { mockClient } from "aws-sdk-client-mock";
import { SavedItem } from "dynamodb-toolbox";
import { beforeEach, describe, expect, it } from "vitest";

import { SegmentAPLRepository } from "./segment-apl-repository";
import { SegmentMainTable, SegmentMainTableEntityFactory } from "./segment-main-table";
import { DynamoAPLRepository } from "./dynamo-apl-repository";
import { SegmentAPLEntityType } from "./segment-main-table";

describe("SegmentAPLRepository", () => {
describe("DynamoAPLRepository", () => {
const mockDocumentClient = mockClient(DynamoDBDocumentClient);

const segmentMainTable = SegmentMainTable.create({
// @ts-expect-error https://github.com/m-radzikowski/aws-sdk-client-mock/issues/197
documentClient: mockDocumentClient,
tableName: "segment-test-table",
});

const segmentAPLEntity = SegmentMainTableEntityFactory.createAPLEntity(segmentMainTable);

const mockedAuthData: AuthData = {
appId: "appId",
saleorApiUrl: "saleorApiUrl",
Expand All @@ -35,7 +27,7 @@ describe("SegmentAPLRepository", () => {
});

it("should successfully get AuthData entry from DynamoDB", async () => {
const mockedAPLEntry: SavedItem<typeof segmentAPLEntity> = {
const mockedAPLEntry: SavedItem<SegmentAPLEntityType> = {
PK: "saleorApiUrl",
SK: "APL",
token: "appToken",
Expand All @@ -50,7 +42,7 @@ describe("SegmentAPLRepository", () => {
Item: mockedAPLEntry,
});

const repository = new SegmentAPLRepository({ segmentAPLEntity });
const repository = new DynamoAPLRepository();

const result = await repository.getEntry({ saleorApiUrl: "saleorApiUrl" });

Expand All @@ -68,19 +60,19 @@ describe("SegmentAPLRepository", () => {
it("should handle errors when getting AuthData from DynamoDB", async () => {
mockDocumentClient.on(GetCommand, {}).rejectsOnce("Exception");

const repository = new SegmentAPLRepository({ segmentAPLEntity });
const repository = new DynamoAPLRepository();

const result = await repository.getEntry({ saleorApiUrl: "saleorApiUrl" });

expect(result.isErr()).toBe(true);

expect(result._unsafeUnwrapErr()).toBeInstanceOf(SegmentAPLRepository.ReadEntityError);
expect(result._unsafeUnwrapErr()).toBeInstanceOf(DynamoAPLRepository.ReadEntityError);
});

it("should return null if AuthData entry does not exist in DynamoDB", async () => {
mockDocumentClient.on(GetCommand, {}).resolvesOnce({});

const repository = new SegmentAPLRepository({ segmentAPLEntity });
const repository = new DynamoAPLRepository();

const result = await repository.getEntry({ saleorApiUrl: "saleorApiUrl" });

Expand All @@ -92,7 +84,7 @@ describe("SegmentAPLRepository", () => {
it("should successfully set AuthData entry in DynamoDB", async () => {
mockDocumentClient.on(PutCommand, {}).resolvesOnce({});

const repository = new SegmentAPLRepository({ segmentAPLEntity });
const repository = new DynamoAPLRepository();

const result = await repository.setEntry({
authData: mockedAuthData,
Expand All @@ -106,21 +98,21 @@ describe("SegmentAPLRepository", () => {
it("should handle errors when setting AuthData entry DynamoDB", async () => {
mockDocumentClient.on(PutCommand, {}).rejectsOnce("Exception");

const repository = new SegmentAPLRepository({ segmentAPLEntity });
const repository = new DynamoAPLRepository();

const result = await repository.setEntry({
authData: mockedAuthData,
});

expect(result.isErr()).toBe(true);

expect(result._unsafeUnwrapErr()).toBeInstanceOf(SegmentAPLRepository.WriteEntityError);
expect(result._unsafeUnwrapErr()).toBeInstanceOf(DynamoAPLRepository.WriteEntityError);
});

it("should successfully delete AuthData entry from DynamoDB", async () => {
mockDocumentClient.on(DeleteCommand, {}).resolvesOnce({});

const repository = new SegmentAPLRepository({ segmentAPLEntity });
const repository = new DynamoAPLRepository();

const result = await repository.deleteEntry({ saleorApiUrl: "saleorApiUrl" });

Expand All @@ -132,17 +124,17 @@ describe("SegmentAPLRepository", () => {
it("should handle errors when deleting AuthData entry from DynamoDB", async () => {
mockDocumentClient.on(DeleteCommand, {}).rejectsOnce("Exception");

const repository = new SegmentAPLRepository({ segmentAPLEntity });
const repository = new DynamoAPLRepository();

const result = await repository.deleteEntry({ saleorApiUrl: "saleorApiUrl" });

expect(result.isErr()).toBe(true);

expect(result._unsafeUnwrapErr()).toBeInstanceOf(SegmentAPLRepository.DeleteEntityError);
expect(result._unsafeUnwrapErr()).toBeInstanceOf(DynamoAPLRepository.DeleteEntityError);
});

it("should successfully get all AuthData entries from DynamoDB", async () => {
const mockedAPLEntries: SavedItem<typeof segmentAPLEntity>[] = [
const mockedAPLEntries: SavedItem<SegmentAPLEntityType>[] = [
{
PK: "saleorApiUrl",
SK: "APL",
Expand All @@ -169,7 +161,7 @@ describe("SegmentAPLRepository", () => {
Items: mockedAPLEntries,
});

const repository = new SegmentAPLRepository({ segmentAPLEntity });
const repository = new DynamoAPLRepository();

const result = await repository.getAllEntries();

Expand Down Expand Up @@ -198,7 +190,7 @@ describe("SegmentAPLRepository", () => {
Items: [],
});

const repository = new SegmentAPLRepository({ segmentAPLEntity });
const repository = new DynamoAPLRepository();

const result = await repository.getAllEntries();

Expand All @@ -210,12 +202,12 @@ describe("SegmentAPLRepository", () => {
it("should handle error when getting all AuthData entries from DynamoDB", async () => {
mockDocumentClient.on(ScanCommand, {}).rejectsOnce("Exception");

const repository = new SegmentAPLRepository({ segmentAPLEntity });
const repository = new DynamoAPLRepository();

const result = await repository.getAllEntries();

expect(result.isErr()).toBe(true);

expect(result._unsafeUnwrapErr()).toBeInstanceOf(SegmentAPLRepository.ScanEntityError);
expect(result._unsafeUnwrapErr()).toBeInstanceOf(DynamoAPLRepository.ScanEntityError);
});
});
Loading
Loading