From 02f3c85796ade7cb69edbdda2367c0d91ac1bdf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20S=C3=B3jko?= Date: Mon, 5 Dec 2022 11:38:18 +0100 Subject: [PATCH] feat(domain-core): add email subscription rejection levels --- .../EmailSubscriptionRejectionLevel.spec.ts | 18 +++++++++++ .../Email/EmailSubscriptionRejectionLevel.ts | 30 +++++++++++++++++++ .../EmailSubscriptionRejectionLevelProps.ts | 3 ++ packages/domain-core/src/Domain/index.ts | 3 ++ 4 files changed, 54 insertions(+) create mode 100644 packages/domain-core/src/Domain/Email/EmailSubscriptionRejectionLevel.spec.ts create mode 100644 packages/domain-core/src/Domain/Email/EmailSubscriptionRejectionLevel.ts create mode 100644 packages/domain-core/src/Domain/Email/EmailSubscriptionRejectionLevelProps.ts diff --git a/packages/domain-core/src/Domain/Email/EmailSubscriptionRejectionLevel.spec.ts b/packages/domain-core/src/Domain/Email/EmailSubscriptionRejectionLevel.spec.ts new file mode 100644 index 000000000..03c82f3ce --- /dev/null +++ b/packages/domain-core/src/Domain/Email/EmailSubscriptionRejectionLevel.spec.ts @@ -0,0 +1,18 @@ +import { EmailSubscriptionRejectionLevel } from './EmailSubscriptionRejectionLevel' + +describe('EmailSubscriptionRejectionLevel', () => { + it('should create a value object', () => { + const valueOrError = EmailSubscriptionRejectionLevel.create(EmailSubscriptionRejectionLevel.LEVELS.SignIn) + + expect(valueOrError.isFailed()).toBeFalsy() + expect(valueOrError.getValue().value).toEqual('SIGN_IN') + }) + + it('should not create an invalid value object', () => { + for (const value of ['', undefined, null, 0, 'FOOBAR']) { + const valueOrError = EmailSubscriptionRejectionLevel.create(value as string) + + expect(valueOrError.isFailed()).toBeTruthy() + } + }) +}) diff --git a/packages/domain-core/src/Domain/Email/EmailSubscriptionRejectionLevel.ts b/packages/domain-core/src/Domain/Email/EmailSubscriptionRejectionLevel.ts new file mode 100644 index 000000000..52eef6e02 --- /dev/null +++ b/packages/domain-core/src/Domain/Email/EmailSubscriptionRejectionLevel.ts @@ -0,0 +1,30 @@ +import { Result } from '../Core/Result' +import { ValueObject } from '../Core/ValueObject' + +import { EmailSubscriptionRejectionLevelProps } from './EmailSubscriptionRejectionLevelProps' + +export class EmailSubscriptionRejectionLevel extends ValueObject { + static readonly LEVELS = { + SignIn: 'SIGN_IN', + Marketing: 'MARKETING', + FailedCloudBackup: 'FAILED_CLOUD_BACKUP', + FailedEmailBackup: 'FAILED_EMAIL_BACKUP', + } + + get value(): string { + return this.props.value + } + + private constructor(props: EmailSubscriptionRejectionLevelProps) { + super(props) + } + + static create(name: string): Result { + const isValidName = Object.values(this.LEVELS).includes(name) + if (!isValidName) { + return Result.fail(`Invalid subscription rejection level: ${name}`) + } else { + return Result.ok(new EmailSubscriptionRejectionLevel({ value: name })) + } + } +} diff --git a/packages/domain-core/src/Domain/Email/EmailSubscriptionRejectionLevelProps.ts b/packages/domain-core/src/Domain/Email/EmailSubscriptionRejectionLevelProps.ts new file mode 100644 index 000000000..80b3ce0fe --- /dev/null +++ b/packages/domain-core/src/Domain/Email/EmailSubscriptionRejectionLevelProps.ts @@ -0,0 +1,3 @@ +export interface EmailSubscriptionRejectionLevelProps { + value: string +} diff --git a/packages/domain-core/src/Domain/index.ts b/packages/domain-core/src/Domain/index.ts index 42f547e6b..c05d2091e 100644 --- a/packages/domain-core/src/Domain/index.ts +++ b/packages/domain-core/src/Domain/index.ts @@ -20,6 +20,9 @@ export * from './Core/Validator' export * from './Core/ValueObject' export * from './Core/ValueObjectProps' +export * from './Email/EmailSubscriptionRejectionLevel' +export * from './Email/EmailSubscriptionRejectionLevelProps' + export * from './Mapping/MapperInterface' export * from './Subscription/SubscriptionPlanName'