From c67e3c3a3bb247091715c078d733a55e99371e00 Mon Sep 17 00:00:00 2001 From: Steliyan Dinkov Date: Wed, 2 Oct 2024 11:14:53 +0200 Subject: [PATCH 1/7] init media source metadata --- .../entity/external-tool.entity.ts | 8 ++++- .../domain/media-source-oauth-config.ts | 18 ++++++++++ .../user-license/domain/media-source.ts | 14 ++++++++ .../src/modules/user-license/entity/index.ts | 4 ++- .../media-source-oauth-config.embeddable.ts | 34 +++++++++++++++++++ .../entity/media-source.entity.ts | 24 ++++++++++--- .../entity/media-user-license.entity.ts | 2 +- .../entity/user-license.entity.ts | 2 +- .../src/modules/user-license/enum/index.ts | 3 ++ .../enum/media-source-auth-method.enum.ts | 3 ++ .../enum/media-source-data-format.enum.ts | 3 ++ .../{entity => enum}/user-license-type.ts | 0 apps/server/src/modules/user-license/index.ts | 13 +++---- .../user-license/repo/media-source.mapper.ts | 4 +++ .../repo/media-user-license.repo.ts | 2 ++ .../service/media-source.service.ts | 8 +++-- .../testing/media-source-config.factory.ts | 18 ++++++++++ .../testing/media-source-entity.factory.ts | 5 ++- 18 files changed, 147 insertions(+), 18 deletions(-) create mode 100644 apps/server/src/modules/user-license/domain/media-source-oauth-config.ts create mode 100644 apps/server/src/modules/user-license/entity/media-source-oauth-config.embeddable.ts create mode 100644 apps/server/src/modules/user-license/enum/index.ts create mode 100644 apps/server/src/modules/user-license/enum/media-source-auth-method.enum.ts create mode 100644 apps/server/src/modules/user-license/enum/media-source-data-format.enum.ts rename apps/server/src/modules/user-license/{entity => enum}/user-license-type.ts (100%) create mode 100644 apps/server/src/modules/user-license/testing/media-source-config.factory.ts diff --git a/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts b/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts index 2e7063fe9fa..7e32a45dccd 100644 --- a/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts +++ b/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts @@ -1,5 +1,5 @@ import { Embedded, Entity, Property, Unique } from '@mikro-orm/core'; - +import { MediaSourceEntity } from '@modules/user-license/entity'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain/types'; import { ToolContextType } from '../../common/enum'; @@ -36,6 +36,8 @@ export interface ExternalToolEntityProps { restrictToContexts?: ToolContextType[]; medium?: ExternalToolMediumEntity; + + //mediaSource?: MediaSourceEntity; } @Entity({ tableName: 'external-tools' }) @@ -80,6 +82,9 @@ export class ExternalToolEntity extends BaseEntityWithTimestamps { @Embedded(() => ExternalToolMediumEntity, { nullable: true, object: true }) medium?: ExternalToolMediumEntity; +// @Embedded(() => MediaSourceEntity, { nullable: true, object: true }) + //mediaSource?: MediaSourceEntity; + constructor(props: ExternalToolEntityProps) { super(); if (props.id) { @@ -98,5 +103,6 @@ export class ExternalToolEntity extends BaseEntityWithTimestamps { this.openNewTab = props.openNewTab; this.restrictToContexts = props.restrictToContexts; this.medium = props.medium; + // this.mediaSource = props.mediaSource; } } diff --git a/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts b/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts new file mode 100644 index 00000000000..c4670e008a0 --- /dev/null +++ b/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts @@ -0,0 +1,18 @@ +import { MediaSourceAuthMethod } from '../enum'; + +export class MediaSourceOauthConfig { + clientId: string; + + clientSecret: string; + + authEndpoint: string; + + method: MediaSourceAuthMethod; + + constructor(props: MediaSourceOauthConfig) { + this.clientId = props.clientId; + this.clientSecret = props.clientSecret; + this.authEndpoint = props.authEndpoint; + this.method = props.method; + } +} diff --git a/apps/server/src/modules/user-license/domain/media-source.ts b/apps/server/src/modules/user-license/domain/media-source.ts index 7759d36bb4e..2663f2aa92d 100644 --- a/apps/server/src/modules/user-license/domain/media-source.ts +++ b/apps/server/src/modules/user-license/domain/media-source.ts @@ -1,4 +1,6 @@ import { DomainObject } from '@shared/domain/domain-object'; +import { MediaSourceOauthConfig } from './media-source-oauth-config'; +import { MediaSourceDataFormat } from '../enum'; export interface MediaSourceProps { id: string; @@ -6,6 +8,10 @@ export interface MediaSourceProps { name?: string; sourceId: string; + + format?: MediaSourceDataFormat; + + config?: MediaSourceOauthConfig; } export class MediaSource extends DomainObject { @@ -16,4 +22,12 @@ export class MediaSource extends DomainObject { get sourceId(): string { return this.props.sourceId; } + + get format(): MediaSourceDataFormat | undefined { + return this.props.format; + } + + get config(): MediaSourceOauthConfig | undefined { + return this.props.config; + } } diff --git a/apps/server/src/modules/user-license/entity/index.ts b/apps/server/src/modules/user-license/entity/index.ts index 7b7925040ed..7343a42f18b 100644 --- a/apps/server/src/modules/user-license/entity/index.ts +++ b/apps/server/src/modules/user-license/entity/index.ts @@ -1,4 +1,6 @@ -export { UserLicenseType } from './user-license-type'; +export { UserLicenseType } from '../enum/user-license-type'; export { MediaUserLicenseEntity, MediaUserLicenseEntityProps } from './media-user-license.entity'; export { UserLicenseEntity } from './user-license.entity'; export { MediaSourceEntity, MediaSourceEntityProps } from './media-source.entity'; +export { MediaSourceDataFormat } from '../enum/media-source-data-format.enum'; +export { MediaSourceAuthMethod } from '../enum/media-source-auth-method.enum'; diff --git a/apps/server/src/modules/user-license/entity/media-source-oauth-config.embeddable.ts b/apps/server/src/modules/user-license/entity/media-source-oauth-config.embeddable.ts new file mode 100644 index 00000000000..e19777c5733 --- /dev/null +++ b/apps/server/src/modules/user-license/entity/media-source-oauth-config.embeddable.ts @@ -0,0 +1,34 @@ +import { Embeddable, Enum, Property } from '@mikro-orm/core'; +import { MediaSourceAuthMethod } from '../enum/media-source-auth-method.enum'; + +export interface MediaSourceConfigEmbeddableProps { + clientId: string; + + clientSecret: string; + + authEndpoint: string; + + method: MediaSourceAuthMethod; +} + +@Embeddable() +export class MediaSourceConfigEmbeddable { + @Property() + clientId: string; + + @Property() + clientSecret: string; + + @Property() + authEndpoint: string; + + @Enum({ nullable: false }) + method: MediaSourceAuthMethod; + + constructor(props: MediaSourceConfigEmbeddableProps) { + this.clientId = props.clientId; + this.clientSecret = props.clientSecret; + this.authEndpoint = props.authEndpoint; + this.method = props.method; + } +} diff --git a/apps/server/src/modules/user-license/entity/media-source.entity.ts b/apps/server/src/modules/user-license/entity/media-source.entity.ts index 57ee3b55cfa..3960f19d4f3 100644 --- a/apps/server/src/modules/user-license/entity/media-source.entity.ts +++ b/apps/server/src/modules/user-license/entity/media-source.entity.ts @@ -1,6 +1,8 @@ -import { Entity, Index, Property } from '@mikro-orm/core'; +import { Embedded, Entity, Index, Property } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain/types'; +import { MediaSourceDataFormat } from '../enum/media-source-data-format.enum'; +import { MediaSourceConfigEmbeddable } from './media-source-oauth-config.embeddable'; export interface MediaSourceEntityProps { id?: EntityId; @@ -8,23 +10,35 @@ export interface MediaSourceEntityProps { name?: string; sourceId: string; + + config: MediaSourceConfigEmbeddable; + + format: MediaSourceDataFormat; } @Entity({ tableName: 'media-sources' }) export class MediaSourceEntity extends BaseEntityWithTimestamps { constructor(props: MediaSourceEntityProps) { super(); - if (props.id != null) { + if (props.id) { this.id = props.id; } - this.name = props.name; this.sourceId = props.sourceId; + this.name = props.name; + this.format = props.format; + this.config = props.config; } + @Index() + @Property() + sourceId: string; + @Property({ nullable: true }) name?: string; - @Index() @Property() - sourceId: string; + format: MediaSourceDataFormat; + + @Embedded(() => MediaSourceConfigEmbeddable, { object: true }) + config: MediaSourceConfigEmbeddable; } diff --git a/apps/server/src/modules/user-license/entity/media-user-license.entity.ts b/apps/server/src/modules/user-license/entity/media-user-license.entity.ts index 4cc501c4975..ded564bdce3 100644 --- a/apps/server/src/modules/user-license/entity/media-user-license.entity.ts +++ b/apps/server/src/modules/user-license/entity/media-user-license.entity.ts @@ -1,6 +1,6 @@ import { Entity, ManyToOne, Property } from '@mikro-orm/core'; import { MediaSourceEntity } from './media-source.entity'; -import { UserLicenseType } from './user-license-type'; +import { UserLicenseType } from '../enum/user-license-type'; import { UserLicenseEntity, UserLicenseProps } from './user-license.entity'; export interface MediaUserLicenseEntityProps extends UserLicenseProps { diff --git a/apps/server/src/modules/user-license/entity/user-license.entity.ts b/apps/server/src/modules/user-license/entity/user-license.entity.ts index 1e3ddcd7b71..0d8948a1126 100644 --- a/apps/server/src/modules/user-license/entity/user-license.entity.ts +++ b/apps/server/src/modules/user-license/entity/user-license.entity.ts @@ -2,7 +2,7 @@ import { Entity, Enum, Index, ManyToOne } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { User as UserEntity } from '@shared/domain/entity/user.entity'; import { EntityId } from '@shared/domain/types'; -import { UserLicenseType } from './user-license-type'; +import { UserLicenseType } from '../enum/user-license-type'; export interface UserLicenseProps { id?: EntityId; diff --git a/apps/server/src/modules/user-license/enum/index.ts b/apps/server/src/modules/user-license/enum/index.ts new file mode 100644 index 00000000000..66d96a9a346 --- /dev/null +++ b/apps/server/src/modules/user-license/enum/index.ts @@ -0,0 +1,3 @@ +export { UserLicenseType } from './user-license-type'; +export { MediaSourceDataFormat } from './media-source-data-format.enum'; +export { MediaSourceAuthMethod } from './media-source-auth-method.enum'; diff --git a/apps/server/src/modules/user-license/enum/media-source-auth-method.enum.ts b/apps/server/src/modules/user-license/enum/media-source-auth-method.enum.ts new file mode 100644 index 00000000000..5f839c307c2 --- /dev/null +++ b/apps/server/src/modules/user-license/enum/media-source-auth-method.enum.ts @@ -0,0 +1,3 @@ +export enum MediaSourceAuthMethod { + CLIENT_CREDENTIALS = 'CLIENT_CREDENTIALS', +} diff --git a/apps/server/src/modules/user-license/enum/media-source-data-format.enum.ts b/apps/server/src/modules/user-license/enum/media-source-data-format.enum.ts new file mode 100644 index 00000000000..98bfa8f4b73 --- /dev/null +++ b/apps/server/src/modules/user-license/enum/media-source-data-format.enum.ts @@ -0,0 +1,3 @@ +export enum MediaSourceDataFormat { + BILDUNGSLOGIN = 'BILDUNGSLOGIN', +} diff --git a/apps/server/src/modules/user-license/entity/user-license-type.ts b/apps/server/src/modules/user-license/enum/user-license-type.ts similarity index 100% rename from apps/server/src/modules/user-license/entity/user-license-type.ts rename to apps/server/src/modules/user-license/enum/user-license-type.ts diff --git a/apps/server/src/modules/user-license/index.ts b/apps/server/src/modules/user-license/index.ts index ebb28ca5a46..7a37a14200c 100644 --- a/apps/server/src/modules/user-license/index.ts +++ b/apps/server/src/modules/user-license/index.ts @@ -1,10 +1,11 @@ -export { UserLicenseModule } from './user-license.module'; -export { MediaUserLicenseService, MediaSourceService } from './service'; -export { MediaUserLicense, MediaSource, MediaUserLicenseProps, MediaSourceProps, AnyUserLicense } from './domain'; -export { UserLicenseType } from './entity/user-license-type'; +export { AnyUserLicense, MediaSource, MediaSourceProps, MediaUserLicense, MediaUserLicenseProps } from './domain'; +export { MediaSourceEntity } from './entity'; +export { UserLicenseType } from './enum/user-license-type'; +export { MediaSourceService, MediaUserLicenseService } from './service'; export { - mediaUserLicenseFactory, - mediaSourceFactory, mediaSourceEntityFactory, + mediaSourceFactory, mediaUserLicenseEntityFactory, + mediaUserLicenseFactory, } from './testing'; +export { UserLicenseModule } from './user-license.module'; diff --git a/apps/server/src/modules/user-license/repo/media-source.mapper.ts b/apps/server/src/modules/user-license/repo/media-source.mapper.ts index f1ae887a3b0..dd0d5a20915 100644 --- a/apps/server/src/modules/user-license/repo/media-source.mapper.ts +++ b/apps/server/src/modules/user-license/repo/media-source.mapper.ts @@ -7,6 +7,8 @@ export class MediaSourceMapper { const entityProps: EntityData = { name: entityDO.name, sourceId: entityDO.sourceId, + config: entityDO.config, + format: entityDO.format, }; return entityProps; @@ -17,6 +19,8 @@ export class MediaSourceMapper { id: entity.id, name: entity.name, sourceId: entity.sourceId, + config: entity.config, + format: entity.format, }); return domainObject; diff --git a/apps/server/src/modules/user-license/repo/media-user-license.repo.ts b/apps/server/src/modules/user-license/repo/media-user-license.repo.ts index 19685fd8656..c24619bb301 100644 --- a/apps/server/src/modules/user-license/repo/media-user-license.repo.ts +++ b/apps/server/src/modules/user-license/repo/media-user-license.repo.ts @@ -20,6 +20,8 @@ export class MediaUserLicenseRepo extends BaseDomainObjectRepo { const domainObject: MediaSource | null = await this.mediaSourceRepo.findBySourceId(id); diff --git a/apps/server/src/modules/user-license/testing/media-source-config.factory.ts b/apps/server/src/modules/user-license/testing/media-source-config.factory.ts new file mode 100644 index 00000000000..6a742c9764b --- /dev/null +++ b/apps/server/src/modules/user-license/testing/media-source-config.factory.ts @@ -0,0 +1,18 @@ +import { BaseFactory } from '@shared/testing'; +import { + MediaSourceConfigEmbeddable, + MediaSourceConfigEmbeddableProps, +} from '../entity/media-source-oauth-config.embeddable'; +import { MediaSourceAuthMethod } from '../entity'; + +export const mediaSourceConfigFactory = BaseFactory.define< + MediaSourceConfigEmbeddable, + MediaSourceConfigEmbeddableProps +>(MediaSourceConfigEmbeddable, ({ sequence }) => { + return { + clientId: `media-source-client-id${sequence}`, + clientSecret: `media-source-client-secret${sequence}`, + authEndpoint: `media-source-auth-endpoint-${sequence}`, + method: MediaSourceAuthMethod.CLIENT_CREDENTIALS, + }; +}); diff --git a/apps/server/src/modules/user-license/testing/media-source-entity.factory.ts b/apps/server/src/modules/user-license/testing/media-source-entity.factory.ts index 801927dca57..f674f0935af 100644 --- a/apps/server/src/modules/user-license/testing/media-source-entity.factory.ts +++ b/apps/server/src/modules/user-license/testing/media-source-entity.factory.ts @@ -1,6 +1,7 @@ import { ObjectId } from '@mikro-orm/mongodb'; import { BaseFactory } from '@shared/testing'; -import { MediaSourceEntity, MediaSourceEntityProps } from '../entity'; +import { MediaSourceDataFormat, MediaSourceEntity, MediaSourceEntityProps } from '../entity'; +import { mediaSourceConfigFactory } from './media-source-config.factory'; export const mediaSourceEntityFactory = BaseFactory.define( MediaSourceEntity, @@ -9,6 +10,8 @@ export const mediaSourceEntityFactory = BaseFactory.define Date: Wed, 2 Oct 2024 11:14:53 +0200 Subject: [PATCH 2/7] init media source metadata --- .../entity/external-tool.entity.ts | 6 +++- .../domain/media-source-oauth-config.ts | 18 ++++++++++ .../user-license/domain/media-source.ts | 14 ++++++++ .../src/modules/user-license/entity/index.ts | 4 ++- .../media-source-oauth-config.embeddable.ts | 34 +++++++++++++++++++ .../entity/media-source.entity.ts | 24 ++++++++++--- .../entity/media-user-license.entity.ts | 2 +- .../entity/user-license.entity.ts | 2 +- .../src/modules/user-license/enum/index.ts | 3 ++ .../enum/media-source-auth-method.enum.ts | 3 ++ .../enum/media-source-data-format.enum.ts | 3 ++ .../{entity => enum}/user-license-type.ts | 0 apps/server/src/modules/user-license/index.ts | 13 +++---- .../user-license/repo/media-source.mapper.ts | 4 +++ .../repo/media-user-license.repo.ts | 2 ++ .../service/media-source.service.ts | 8 +++-- .../testing/media-source-config.factory.ts | 18 ++++++++++ .../testing/media-source-entity.factory.ts | 5 ++- 18 files changed, 145 insertions(+), 18 deletions(-) create mode 100644 apps/server/src/modules/user-license/domain/media-source-oauth-config.ts create mode 100644 apps/server/src/modules/user-license/entity/media-source-oauth-config.embeddable.ts create mode 100644 apps/server/src/modules/user-license/enum/index.ts create mode 100644 apps/server/src/modules/user-license/enum/media-source-auth-method.enum.ts create mode 100644 apps/server/src/modules/user-license/enum/media-source-data-format.enum.ts rename apps/server/src/modules/user-license/{entity => enum}/user-license-type.ts (100%) create mode 100644 apps/server/src/modules/user-license/testing/media-source-config.factory.ts diff --git a/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts b/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts index d73a57b54f4..26eaa14e69d 100644 --- a/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts +++ b/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts @@ -1,5 +1,5 @@ import { Embedded, Entity, Property, Unique } from '@mikro-orm/core'; - +import { MediaSourceEntity } from '@modules/user-license/entity'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain/types'; import { ToolContextType } from '../../common/enum'; @@ -40,6 +40,7 @@ export interface ExternalToolEntityProps { isPreferred: boolean; iconName?: string; + //mediaSource?: MediaSourceEntity; } @Entity({ tableName: 'external-tools' }) @@ -89,6 +90,8 @@ export class ExternalToolEntity extends BaseEntityWithTimestamps { @Property({ nullable: true }) iconName?: string; +// @Embedded(() => MediaSourceEntity, { nullable: true, object: true }) + //mediaSource?: MediaSourceEntity; constructor(props: ExternalToolEntityProps) { super(); @@ -110,5 +113,6 @@ export class ExternalToolEntity extends BaseEntityWithTimestamps { this.medium = props.medium; this.isPreferred = props.isPreferred; this.iconName = props.iconName; + // this.mediaSource = props.mediaSource; } } diff --git a/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts b/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts new file mode 100644 index 00000000000..c4670e008a0 --- /dev/null +++ b/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts @@ -0,0 +1,18 @@ +import { MediaSourceAuthMethod } from '../enum'; + +export class MediaSourceOauthConfig { + clientId: string; + + clientSecret: string; + + authEndpoint: string; + + method: MediaSourceAuthMethod; + + constructor(props: MediaSourceOauthConfig) { + this.clientId = props.clientId; + this.clientSecret = props.clientSecret; + this.authEndpoint = props.authEndpoint; + this.method = props.method; + } +} diff --git a/apps/server/src/modules/user-license/domain/media-source.ts b/apps/server/src/modules/user-license/domain/media-source.ts index 7759d36bb4e..2663f2aa92d 100644 --- a/apps/server/src/modules/user-license/domain/media-source.ts +++ b/apps/server/src/modules/user-license/domain/media-source.ts @@ -1,4 +1,6 @@ import { DomainObject } from '@shared/domain/domain-object'; +import { MediaSourceOauthConfig } from './media-source-oauth-config'; +import { MediaSourceDataFormat } from '../enum'; export interface MediaSourceProps { id: string; @@ -6,6 +8,10 @@ export interface MediaSourceProps { name?: string; sourceId: string; + + format?: MediaSourceDataFormat; + + config?: MediaSourceOauthConfig; } export class MediaSource extends DomainObject { @@ -16,4 +22,12 @@ export class MediaSource extends DomainObject { get sourceId(): string { return this.props.sourceId; } + + get format(): MediaSourceDataFormat | undefined { + return this.props.format; + } + + get config(): MediaSourceOauthConfig | undefined { + return this.props.config; + } } diff --git a/apps/server/src/modules/user-license/entity/index.ts b/apps/server/src/modules/user-license/entity/index.ts index 7b7925040ed..7343a42f18b 100644 --- a/apps/server/src/modules/user-license/entity/index.ts +++ b/apps/server/src/modules/user-license/entity/index.ts @@ -1,4 +1,6 @@ -export { UserLicenseType } from './user-license-type'; +export { UserLicenseType } from '../enum/user-license-type'; export { MediaUserLicenseEntity, MediaUserLicenseEntityProps } from './media-user-license.entity'; export { UserLicenseEntity } from './user-license.entity'; export { MediaSourceEntity, MediaSourceEntityProps } from './media-source.entity'; +export { MediaSourceDataFormat } from '../enum/media-source-data-format.enum'; +export { MediaSourceAuthMethod } from '../enum/media-source-auth-method.enum'; diff --git a/apps/server/src/modules/user-license/entity/media-source-oauth-config.embeddable.ts b/apps/server/src/modules/user-license/entity/media-source-oauth-config.embeddable.ts new file mode 100644 index 00000000000..e19777c5733 --- /dev/null +++ b/apps/server/src/modules/user-license/entity/media-source-oauth-config.embeddable.ts @@ -0,0 +1,34 @@ +import { Embeddable, Enum, Property } from '@mikro-orm/core'; +import { MediaSourceAuthMethod } from '../enum/media-source-auth-method.enum'; + +export interface MediaSourceConfigEmbeddableProps { + clientId: string; + + clientSecret: string; + + authEndpoint: string; + + method: MediaSourceAuthMethod; +} + +@Embeddable() +export class MediaSourceConfigEmbeddable { + @Property() + clientId: string; + + @Property() + clientSecret: string; + + @Property() + authEndpoint: string; + + @Enum({ nullable: false }) + method: MediaSourceAuthMethod; + + constructor(props: MediaSourceConfigEmbeddableProps) { + this.clientId = props.clientId; + this.clientSecret = props.clientSecret; + this.authEndpoint = props.authEndpoint; + this.method = props.method; + } +} diff --git a/apps/server/src/modules/user-license/entity/media-source.entity.ts b/apps/server/src/modules/user-license/entity/media-source.entity.ts index 57ee3b55cfa..3960f19d4f3 100644 --- a/apps/server/src/modules/user-license/entity/media-source.entity.ts +++ b/apps/server/src/modules/user-license/entity/media-source.entity.ts @@ -1,6 +1,8 @@ -import { Entity, Index, Property } from '@mikro-orm/core'; +import { Embedded, Entity, Index, Property } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain/types'; +import { MediaSourceDataFormat } from '../enum/media-source-data-format.enum'; +import { MediaSourceConfigEmbeddable } from './media-source-oauth-config.embeddable'; export interface MediaSourceEntityProps { id?: EntityId; @@ -8,23 +10,35 @@ export interface MediaSourceEntityProps { name?: string; sourceId: string; + + config: MediaSourceConfigEmbeddable; + + format: MediaSourceDataFormat; } @Entity({ tableName: 'media-sources' }) export class MediaSourceEntity extends BaseEntityWithTimestamps { constructor(props: MediaSourceEntityProps) { super(); - if (props.id != null) { + if (props.id) { this.id = props.id; } - this.name = props.name; this.sourceId = props.sourceId; + this.name = props.name; + this.format = props.format; + this.config = props.config; } + @Index() + @Property() + sourceId: string; + @Property({ nullable: true }) name?: string; - @Index() @Property() - sourceId: string; + format: MediaSourceDataFormat; + + @Embedded(() => MediaSourceConfigEmbeddable, { object: true }) + config: MediaSourceConfigEmbeddable; } diff --git a/apps/server/src/modules/user-license/entity/media-user-license.entity.ts b/apps/server/src/modules/user-license/entity/media-user-license.entity.ts index 4cc501c4975..ded564bdce3 100644 --- a/apps/server/src/modules/user-license/entity/media-user-license.entity.ts +++ b/apps/server/src/modules/user-license/entity/media-user-license.entity.ts @@ -1,6 +1,6 @@ import { Entity, ManyToOne, Property } from '@mikro-orm/core'; import { MediaSourceEntity } from './media-source.entity'; -import { UserLicenseType } from './user-license-type'; +import { UserLicenseType } from '../enum/user-license-type'; import { UserLicenseEntity, UserLicenseProps } from './user-license.entity'; export interface MediaUserLicenseEntityProps extends UserLicenseProps { diff --git a/apps/server/src/modules/user-license/entity/user-license.entity.ts b/apps/server/src/modules/user-license/entity/user-license.entity.ts index 1e3ddcd7b71..0d8948a1126 100644 --- a/apps/server/src/modules/user-license/entity/user-license.entity.ts +++ b/apps/server/src/modules/user-license/entity/user-license.entity.ts @@ -2,7 +2,7 @@ import { Entity, Enum, Index, ManyToOne } from '@mikro-orm/core'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { User as UserEntity } from '@shared/domain/entity/user.entity'; import { EntityId } from '@shared/domain/types'; -import { UserLicenseType } from './user-license-type'; +import { UserLicenseType } from '../enum/user-license-type'; export interface UserLicenseProps { id?: EntityId; diff --git a/apps/server/src/modules/user-license/enum/index.ts b/apps/server/src/modules/user-license/enum/index.ts new file mode 100644 index 00000000000..66d96a9a346 --- /dev/null +++ b/apps/server/src/modules/user-license/enum/index.ts @@ -0,0 +1,3 @@ +export { UserLicenseType } from './user-license-type'; +export { MediaSourceDataFormat } from './media-source-data-format.enum'; +export { MediaSourceAuthMethod } from './media-source-auth-method.enum'; diff --git a/apps/server/src/modules/user-license/enum/media-source-auth-method.enum.ts b/apps/server/src/modules/user-license/enum/media-source-auth-method.enum.ts new file mode 100644 index 00000000000..5f839c307c2 --- /dev/null +++ b/apps/server/src/modules/user-license/enum/media-source-auth-method.enum.ts @@ -0,0 +1,3 @@ +export enum MediaSourceAuthMethod { + CLIENT_CREDENTIALS = 'CLIENT_CREDENTIALS', +} diff --git a/apps/server/src/modules/user-license/enum/media-source-data-format.enum.ts b/apps/server/src/modules/user-license/enum/media-source-data-format.enum.ts new file mode 100644 index 00000000000..98bfa8f4b73 --- /dev/null +++ b/apps/server/src/modules/user-license/enum/media-source-data-format.enum.ts @@ -0,0 +1,3 @@ +export enum MediaSourceDataFormat { + BILDUNGSLOGIN = 'BILDUNGSLOGIN', +} diff --git a/apps/server/src/modules/user-license/entity/user-license-type.ts b/apps/server/src/modules/user-license/enum/user-license-type.ts similarity index 100% rename from apps/server/src/modules/user-license/entity/user-license-type.ts rename to apps/server/src/modules/user-license/enum/user-license-type.ts diff --git a/apps/server/src/modules/user-license/index.ts b/apps/server/src/modules/user-license/index.ts index ebb28ca5a46..7a37a14200c 100644 --- a/apps/server/src/modules/user-license/index.ts +++ b/apps/server/src/modules/user-license/index.ts @@ -1,10 +1,11 @@ -export { UserLicenseModule } from './user-license.module'; -export { MediaUserLicenseService, MediaSourceService } from './service'; -export { MediaUserLicense, MediaSource, MediaUserLicenseProps, MediaSourceProps, AnyUserLicense } from './domain'; -export { UserLicenseType } from './entity/user-license-type'; +export { AnyUserLicense, MediaSource, MediaSourceProps, MediaUserLicense, MediaUserLicenseProps } from './domain'; +export { MediaSourceEntity } from './entity'; +export { UserLicenseType } from './enum/user-license-type'; +export { MediaSourceService, MediaUserLicenseService } from './service'; export { - mediaUserLicenseFactory, - mediaSourceFactory, mediaSourceEntityFactory, + mediaSourceFactory, mediaUserLicenseEntityFactory, + mediaUserLicenseFactory, } from './testing'; +export { UserLicenseModule } from './user-license.module'; diff --git a/apps/server/src/modules/user-license/repo/media-source.mapper.ts b/apps/server/src/modules/user-license/repo/media-source.mapper.ts index f1ae887a3b0..dd0d5a20915 100644 --- a/apps/server/src/modules/user-license/repo/media-source.mapper.ts +++ b/apps/server/src/modules/user-license/repo/media-source.mapper.ts @@ -7,6 +7,8 @@ export class MediaSourceMapper { const entityProps: EntityData = { name: entityDO.name, sourceId: entityDO.sourceId, + config: entityDO.config, + format: entityDO.format, }; return entityProps; @@ -17,6 +19,8 @@ export class MediaSourceMapper { id: entity.id, name: entity.name, sourceId: entity.sourceId, + config: entity.config, + format: entity.format, }); return domainObject; diff --git a/apps/server/src/modules/user-license/repo/media-user-license.repo.ts b/apps/server/src/modules/user-license/repo/media-user-license.repo.ts index 19685fd8656..c24619bb301 100644 --- a/apps/server/src/modules/user-license/repo/media-user-license.repo.ts +++ b/apps/server/src/modules/user-license/repo/media-user-license.repo.ts @@ -20,6 +20,8 @@ export class MediaUserLicenseRepo extends BaseDomainObjectRepo { const domainObject: MediaSource | null = await this.mediaSourceRepo.findBySourceId(id); diff --git a/apps/server/src/modules/user-license/testing/media-source-config.factory.ts b/apps/server/src/modules/user-license/testing/media-source-config.factory.ts new file mode 100644 index 00000000000..6a742c9764b --- /dev/null +++ b/apps/server/src/modules/user-license/testing/media-source-config.factory.ts @@ -0,0 +1,18 @@ +import { BaseFactory } from '@shared/testing'; +import { + MediaSourceConfigEmbeddable, + MediaSourceConfigEmbeddableProps, +} from '../entity/media-source-oauth-config.embeddable'; +import { MediaSourceAuthMethod } from '../entity'; + +export const mediaSourceConfigFactory = BaseFactory.define< + MediaSourceConfigEmbeddable, + MediaSourceConfigEmbeddableProps +>(MediaSourceConfigEmbeddable, ({ sequence }) => { + return { + clientId: `media-source-client-id${sequence}`, + clientSecret: `media-source-client-secret${sequence}`, + authEndpoint: `media-source-auth-endpoint-${sequence}`, + method: MediaSourceAuthMethod.CLIENT_CREDENTIALS, + }; +}); diff --git a/apps/server/src/modules/user-license/testing/media-source-entity.factory.ts b/apps/server/src/modules/user-license/testing/media-source-entity.factory.ts index 801927dca57..f674f0935af 100644 --- a/apps/server/src/modules/user-license/testing/media-source-entity.factory.ts +++ b/apps/server/src/modules/user-license/testing/media-source-entity.factory.ts @@ -1,6 +1,7 @@ import { ObjectId } from '@mikro-orm/mongodb'; import { BaseFactory } from '@shared/testing'; -import { MediaSourceEntity, MediaSourceEntityProps } from '../entity'; +import { MediaSourceDataFormat, MediaSourceEntity, MediaSourceEntityProps } from '../entity'; +import { mediaSourceConfigFactory } from './media-source-config.factory'; export const mediaSourceEntityFactory = BaseFactory.define( MediaSourceEntity, @@ -9,6 +10,8 @@ export const mediaSourceEntityFactory = BaseFactory.define Date: Mon, 7 Oct 2024 10:53:50 +0200 Subject: [PATCH 3/7] update media-source --- .../external-tool/entity/external-tool.entity.ts | 4 ---- .../user-license/entity/media-source.entity.ts | 12 ++++++------ .../user-license/service/media-source.service.ts | 8 ++------ .../user-license/testing/media-source.factory.ts | 4 ++++ 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts b/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts index 26eaa14e69d..3898116888e 100644 --- a/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts +++ b/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts @@ -40,7 +40,6 @@ export interface ExternalToolEntityProps { isPreferred: boolean; iconName?: string; - //mediaSource?: MediaSourceEntity; } @Entity({ tableName: 'external-tools' }) @@ -90,8 +89,6 @@ export class ExternalToolEntity extends BaseEntityWithTimestamps { @Property({ nullable: true }) iconName?: string; -// @Embedded(() => MediaSourceEntity, { nullable: true, object: true }) - //mediaSource?: MediaSourceEntity; constructor(props: ExternalToolEntityProps) { super(); @@ -113,6 +110,5 @@ export class ExternalToolEntity extends BaseEntityWithTimestamps { this.medium = props.medium; this.isPreferred = props.isPreferred; this.iconName = props.iconName; - // this.mediaSource = props.mediaSource; } } diff --git a/apps/server/src/modules/user-license/entity/media-source.entity.ts b/apps/server/src/modules/user-license/entity/media-source.entity.ts index 3960f19d4f3..bd3f9da8b79 100644 --- a/apps/server/src/modules/user-license/entity/media-source.entity.ts +++ b/apps/server/src/modules/user-license/entity/media-source.entity.ts @@ -11,9 +11,9 @@ export interface MediaSourceEntityProps { sourceId: string; - config: MediaSourceConfigEmbeddable; + config?: MediaSourceConfigEmbeddable; - format: MediaSourceDataFormat; + format?: MediaSourceDataFormat; } @Entity({ tableName: 'media-sources' }) @@ -36,9 +36,9 @@ export class MediaSourceEntity extends BaseEntityWithTimestamps { @Property({ nullable: true }) name?: string; - @Property() - format: MediaSourceDataFormat; + @Property({ nullable: true }) + format?: MediaSourceDataFormat; - @Embedded(() => MediaSourceConfigEmbeddable, { object: true }) - config: MediaSourceConfigEmbeddable; + @Embedded(() => MediaSourceConfigEmbeddable, { object: true, nullable: true }) + config?: MediaSourceConfigEmbeddable; } diff --git a/apps/server/src/modules/user-license/service/media-source.service.ts b/apps/server/src/modules/user-license/service/media-source.service.ts index a69de958876..151ce483184 100644 --- a/apps/server/src/modules/user-license/service/media-source.service.ts +++ b/apps/server/src/modules/user-license/service/media-source.service.ts @@ -1,15 +1,11 @@ -//import { DefaultEncryptionService, EncryptionService } from '@infra/encryption'; -import { Inject, Injectable } from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import { EntityId } from '@shared/domain/types'; import { MediaSource } from '../domain'; import { MediaSourceRepo } from '../repo'; @Injectable() export class MediaSourceService { - constructor( - private readonly mediaSourceRepo: MediaSourceRepo, - // @Inject(DefaultEncryptionService) private readonly encryptionService: EncryptionService - ) {} + constructor(private readonly mediaSourceRepo: MediaSourceRepo) {} public async findBySourceId(id: EntityId): Promise { const domainObject: MediaSource | null = await this.mediaSourceRepo.findBySourceId(id); diff --git a/apps/server/src/modules/user-license/testing/media-source.factory.ts b/apps/server/src/modules/user-license/testing/media-source.factory.ts index a7f14a14b23..8331614ff7d 100644 --- a/apps/server/src/modules/user-license/testing/media-source.factory.ts +++ b/apps/server/src/modules/user-license/testing/media-source.factory.ts @@ -1,11 +1,15 @@ import { ObjectId } from '@mikro-orm/mongodb'; import { BaseFactory } from '@shared/testing'; import { MediaSource, MediaSourceProps } from '../domain'; +import { MediaSourceDataFormat } from '../entity'; +import { mediaSourceConfigFactory } from './media-source-config.factory'; export const mediaSourceFactory = BaseFactory.define(MediaSource, ({ sequence }) => { return { id: new ObjectId().toHexString(), name: `media-source-${sequence}`, sourceId: `source-id-${sequence}`, + format: MediaSourceDataFormat.BILDUNGSLOGIN, + config: mediaSourceConfigFactory.build(), }; }); From 942914d399b37730f45dc879a2a5edec79e9f1ec Mon Sep 17 00:00:00 2001 From: Steliyan Dinkov Date: Mon, 7 Oct 2024 11:14:08 +0200 Subject: [PATCH 4/7] update media-source repo tests --- .../modules/tool/external-tool/entity/external-tool.entity.ts | 1 - .../src/modules/user-license/repo/media-source.repo.spec.ts | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts b/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts index 3898116888e..e5a6fbbf6f8 100644 --- a/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts +++ b/apps/server/src/modules/tool/external-tool/entity/external-tool.entity.ts @@ -1,5 +1,4 @@ import { Embedded, Entity, Property, Unique } from '@mikro-orm/core'; -import { MediaSourceEntity } from '@modules/user-license/entity'; import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; import { EntityId } from '@shared/domain/types'; import { ToolContextType } from '../../common/enum'; diff --git a/apps/server/src/modules/user-license/repo/media-source.repo.spec.ts b/apps/server/src/modules/user-license/repo/media-source.repo.spec.ts index 12708211687..fdbf9a1dcee 100644 --- a/apps/server/src/modules/user-license/repo/media-source.repo.spec.ts +++ b/apps/server/src/modules/user-license/repo/media-source.repo.spec.ts @@ -54,6 +54,8 @@ describe(MediaSourceRepo.name, () => { id: mediaSource.id, name: mediaSource.name, sourceId: mediaSource.sourceId, + format: mediaSource.format, + config: mediaSource.config, }) ); }); From 25b49947986b477ff28f20ffa447f2f98c4c7e25 Mon Sep 17 00:00:00 2001 From: Steliyan Dinkov Date: Mon, 7 Oct 2024 11:40:31 +0200 Subject: [PATCH 5/7] update media-user-license repo test --- .../modules/user-license/repo/media-user-license.repo.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/server/src/modules/user-license/repo/media-user-license.repo.spec.ts b/apps/server/src/modules/user-license/repo/media-user-license.repo.spec.ts index 5940d568137..12487458d17 100644 --- a/apps/server/src/modules/user-license/repo/media-user-license.repo.spec.ts +++ b/apps/server/src/modules/user-license/repo/media-user-license.repo.spec.ts @@ -70,6 +70,8 @@ describe(MediaUserLicenseRepo.name, () => { id: mediaSource.id, name: mediaSource.name, sourceId: mediaSource.sourceId, + format: mediaSource.format, + config: mediaSource.config, }), }), ]); From a6a7cdb1c4dbc687428644d74f730f01bf5186f7 Mon Sep 17 00:00:00 2001 From: Steliyan Dinkov Date: Tue, 8 Oct 2024 11:40:17 +0200 Subject: [PATCH 6/7] update oath config model + entity; add mapper; update test --- .../domain/media-source-oauth-config.ts | 15 ++-- .../user-license/domain/media-source.ts | 4 +- .../media-source-oauth-config.embeddable.ts | 7 ++ .../src/modules/user-license/repo/index.ts | 3 +- .../repo/media-source-config.mapper.spec.ts | 79 +++++++++++++++++++ .../repo/media-source-config.mapper.ts | 25 ++++++ .../user-license/repo/media-source.mapper.ts | 5 +- .../repo/media-source.repo.spec.ts | 12 ++- .../repo/media-user-license.repo.spec.ts | 11 ++- .../repo/media-user-license.repo.ts | 3 +- .../media-source-config.embeddable.factory.ts | 20 +++++ .../testing/media-source-config.factory.ts | 31 ++++---- .../testing/media-source-entity.factory.ts | 4 +- 13 files changed, 182 insertions(+), 37 deletions(-) create mode 100644 apps/server/src/modules/user-license/repo/media-source-config.mapper.spec.ts create mode 100644 apps/server/src/modules/user-license/repo/media-source-config.mapper.ts create mode 100644 apps/server/src/modules/user-license/testing/media-source-config.embeddable.factory.ts diff --git a/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts b/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts index c4670e008a0..3f51b1b7306 100644 --- a/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts +++ b/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts @@ -1,6 +1,10 @@ +import { AuthorizableObject, DomainObject } from '@shared/domain/domain-object'; import { MediaSourceAuthMethod } from '../enum'; +import { EntityId } from '../../../shared/domain/types'; + +export interface MediaSourceOauthConfigProps extends AuthorizableObject { + id: EntityId; -export class MediaSourceOauthConfig { clientId: string; clientSecret: string; @@ -8,11 +12,6 @@ export class MediaSourceOauthConfig { authEndpoint: string; method: MediaSourceAuthMethod; - - constructor(props: MediaSourceOauthConfig) { - this.clientId = props.clientId; - this.clientSecret = props.clientSecret; - this.authEndpoint = props.authEndpoint; - this.method = props.method; - } } + +export class MediaSourceOauthConfig extends DomainObject {} diff --git a/apps/server/src/modules/user-license/domain/media-source.ts b/apps/server/src/modules/user-license/domain/media-source.ts index 2663f2aa92d..62147d86d0a 100644 --- a/apps/server/src/modules/user-license/domain/media-source.ts +++ b/apps/server/src/modules/user-license/domain/media-source.ts @@ -1,8 +1,8 @@ -import { DomainObject } from '@shared/domain/domain-object'; +import { AuthorizableObject, DomainObject } from '@shared/domain/domain-object'; import { MediaSourceOauthConfig } from './media-source-oauth-config'; import { MediaSourceDataFormat } from '../enum'; -export interface MediaSourceProps { +export interface MediaSourceProps extends AuthorizableObject { id: string; name?: string; diff --git a/apps/server/src/modules/user-license/entity/media-source-oauth-config.embeddable.ts b/apps/server/src/modules/user-license/entity/media-source-oauth-config.embeddable.ts index e19777c5733..a236c308865 100644 --- a/apps/server/src/modules/user-license/entity/media-source-oauth-config.embeddable.ts +++ b/apps/server/src/modules/user-license/entity/media-source-oauth-config.embeddable.ts @@ -1,7 +1,10 @@ import { Embeddable, Enum, Property } from '@mikro-orm/core'; +import { ObjectId } from '@mikro-orm/mongodb'; import { MediaSourceAuthMethod } from '../enum/media-source-auth-method.enum'; export interface MediaSourceConfigEmbeddableProps { + _id: ObjectId; + clientId: string; clientSecret: string; @@ -13,6 +16,9 @@ export interface MediaSourceConfigEmbeddableProps { @Embeddable() export class MediaSourceConfigEmbeddable { + @Property() + _id: ObjectId; + @Property() clientId: string; @@ -26,6 +32,7 @@ export class MediaSourceConfigEmbeddable { method: MediaSourceAuthMethod; constructor(props: MediaSourceConfigEmbeddableProps) { + this._id = props._id; this.clientId = props.clientId; this.clientSecret = props.clientSecret; this.authEndpoint = props.authEndpoint; diff --git a/apps/server/src/modules/user-license/repo/index.ts b/apps/server/src/modules/user-license/repo/index.ts index 102b731f497..0aed70e9853 100644 --- a/apps/server/src/modules/user-license/repo/index.ts +++ b/apps/server/src/modules/user-license/repo/index.ts @@ -1,3 +1,4 @@ +export { MediaSourceConfigMapper } from './media-source-config.mapper'; +export { MediaSourceRepo } from './media-source.repo'; export { MediaUserLicenseRepo } from './media-user-license.repo'; export { UserLicenseQuery } from './user-license-query'; -export { MediaSourceRepo } from './media-source.repo'; diff --git a/apps/server/src/modules/user-license/repo/media-source-config.mapper.spec.ts b/apps/server/src/modules/user-license/repo/media-source-config.mapper.spec.ts new file mode 100644 index 00000000000..0a73a28f018 --- /dev/null +++ b/apps/server/src/modules/user-license/repo/media-source-config.mapper.spec.ts @@ -0,0 +1,79 @@ +import { ObjectId } from '@mikro-orm/mongodb'; +import { setupEntities } from '@shared/testing'; +import { MediaSourceOauthConfig } from '../domain/media-source-oauth-config'; +import { MediaSourceConfigEmbeddable } from '../entity/media-source-oauth-config.embeddable'; +import { mediaSourceConfigEmbeddableFactory } from '../testing/media-source-config.embeddable.factory'; +import { mediaSourceConfigFactory } from '../testing/media-source-config.factory'; +import { MediaSourceConfigMapper } from './media-source-config.mapper'; + +describe('MediaSourceConfigMapper', () => { + describe('mapToDo', () => { + describe('when entity is passed', () => { + const setup = async () => { + await setupEntities(); + + const entity = mediaSourceConfigEmbeddableFactory.build(); + const expected = new MediaSourceOauthConfig({ + id: entity._id.toHexString(), + clientId: entity.clientId, + clientSecret: entity.clientSecret, + authEndpoint: entity.authEndpoint, + method: entity.method, + }); + + return { entity, expected }; + }; + + it('should return an instance of config', async () => { + const { entity } = await setup(); + + const result = MediaSourceConfigMapper.mapToDo(entity); + + expect(result).toBeInstanceOf(MediaSourceOauthConfig); + }); + + it('should return a do with all properties', async () => { + const { entity, expected } = await setup(); + + const result = MediaSourceConfigMapper.mapToDo(entity); + + expect(result).toEqual(expected); + }); + }); + }); + + describe('mapToEntity', () => { + describe('when config do is passed', () => { + const setup = async () => { + await setupEntities(); + + const configDo = mediaSourceConfigFactory.build(); + const expected = new MediaSourceConfigEmbeddable({ + _id: new ObjectId(configDo.id), + clientId: configDo.getProps().clientId, + clientSecret: configDo.getProps().clientSecret, + authEndpoint: configDo.getProps().authEndpoint, + method: configDo.getProps().method, + }); + + return { configDo, expected }; + }; + + it('should return an instance of config embeddable', async () => { + const { configDo } = await setup(); + + const result = MediaSourceConfigMapper.mapToEntity(configDo); + + expect(result).toBeInstanceOf(MediaSourceConfigEmbeddable); + }); + + it('should return an embeddable with all properties', async () => { + const { configDo, expected } = await setup(); + + const result = MediaSourceConfigMapper.mapToEntity(configDo); + + expect(result).toEqual(expected); + }); + }); + }); +}); diff --git a/apps/server/src/modules/user-license/repo/media-source-config.mapper.ts b/apps/server/src/modules/user-license/repo/media-source-config.mapper.ts new file mode 100644 index 00000000000..5a2f4fcfce1 --- /dev/null +++ b/apps/server/src/modules/user-license/repo/media-source-config.mapper.ts @@ -0,0 +1,25 @@ +import { ObjectId } from '@mikro-orm/mongodb'; +import { MediaSourceOauthConfig } from '../domain/media-source-oauth-config'; +import { MediaSourceConfigEmbeddable } from '../entity/media-source-oauth-config.embeddable'; + +export class MediaSourceConfigMapper { + static mapToEntity(config: MediaSourceOauthConfig): MediaSourceConfigEmbeddable { + const configProps = config.getProps(); + + const configEmbeddable = new MediaSourceConfigEmbeddable({ ...configProps, _id: new ObjectId(configProps.id) }); + + return configEmbeddable; + } + + static mapToDo(embeddable: MediaSourceConfigEmbeddable): MediaSourceOauthConfig { + const config = new MediaSourceOauthConfig({ + id: embeddable._id.toHexString(), + clientId: embeddable.clientId, + clientSecret: embeddable.clientSecret, + method: embeddable.method, + authEndpoint: embeddable.authEndpoint, + }); + + return config; + } +} diff --git a/apps/server/src/modules/user-license/repo/media-source.mapper.ts b/apps/server/src/modules/user-license/repo/media-source.mapper.ts index dd0d5a20915..20b46bdd6cb 100644 --- a/apps/server/src/modules/user-license/repo/media-source.mapper.ts +++ b/apps/server/src/modules/user-license/repo/media-source.mapper.ts @@ -1,13 +1,14 @@ import { EntityData } from '@mikro-orm/core'; import { MediaSource } from '../domain'; import { MediaSourceEntity } from '../entity'; +import { MediaSourceConfigMapper } from './media-source-config.mapper'; export class MediaSourceMapper { public static mapToEntityProperties(entityDO: MediaSource): EntityData { const entityProps: EntityData = { name: entityDO.name, sourceId: entityDO.sourceId, - config: entityDO.config, + config: entityDO.config ? MediaSourceConfigMapper.mapToEntity(entityDO.config) : undefined, format: entityDO.format, }; @@ -19,7 +20,7 @@ export class MediaSourceMapper { id: entity.id, name: entity.name, sourceId: entity.sourceId, - config: entity.config, + config: entity.config ? MediaSourceConfigMapper.mapToDo(entity.config) : undefined, format: entity.format, }); diff --git a/apps/server/src/modules/user-license/repo/media-source.repo.spec.ts b/apps/server/src/modules/user-license/repo/media-source.repo.spec.ts index fdbf9a1dcee..63ae61b3a4c 100644 --- a/apps/server/src/modules/user-license/repo/media-source.repo.spec.ts +++ b/apps/server/src/modules/user-license/repo/media-source.repo.spec.ts @@ -6,6 +6,9 @@ import { MediaSource } from '../domain'; import { MediaSourceEntity } from '../entity'; import { mediaSourceEntityFactory, mediaSourceFactory } from '../testing'; import { MediaSourceRepo } from './media-source.repo'; +import { MediaSourceConfigEmbeddable } from '../entity/media-source-oauth-config.embeddable'; +import { mediaSourceConfigEmbeddableFactory } from '../testing/media-source-config.embeddable.factory'; +import { MediaSourceConfigMapper } from './media-source-config.mapper'; describe(MediaSourceRepo.name, () => { let module: TestingModule; @@ -33,7 +36,9 @@ describe(MediaSourceRepo.name, () => { describe('findBySourceId', () => { describe('when a media source exists', () => { const setup = async () => { - const mediaSource: MediaSourceEntity = mediaSourceEntityFactory.build(); + const config: MediaSourceConfigEmbeddable = mediaSourceConfigEmbeddableFactory.build(); + + const mediaSource: MediaSourceEntity = mediaSourceEntityFactory.build({ config }); await em.persistAndFlush([mediaSource]); @@ -41,11 +46,12 @@ describe(MediaSourceRepo.name, () => { return { mediaSource, + config, }; }; it('should return user licenses for user', async () => { - const { mediaSource } = await setup(); + const { mediaSource, config } = await setup(); const result = await repo.findBySourceId(mediaSource.sourceId); @@ -55,7 +61,7 @@ describe(MediaSourceRepo.name, () => { name: mediaSource.name, sourceId: mediaSource.sourceId, format: mediaSource.format, - config: mediaSource.config, + config: MediaSourceConfigMapper.mapToDo(config), }) ); }); diff --git a/apps/server/src/modules/user-license/repo/media-user-license.repo.spec.ts b/apps/server/src/modules/user-license/repo/media-user-license.repo.spec.ts index 12487458d17..f8a29727842 100644 --- a/apps/server/src/modules/user-license/repo/media-user-license.repo.spec.ts +++ b/apps/server/src/modules/user-license/repo/media-user-license.repo.spec.ts @@ -12,6 +12,9 @@ import { mediaUserLicenseFactory, } from '../testing'; import { MediaUserLicenseRepo } from './media-user-license.repo'; +import { MediaSourceConfigMapper } from './media-source-config.mapper'; +import { MediaSourceConfigEmbeddable } from '../entity/media-source-oauth-config.embeddable'; +import { mediaSourceConfigEmbeddableFactory } from '../testing/media-source-config.embeddable.factory'; describe(MediaUserLicenseRepo.name, () => { let module: TestingModule; @@ -40,7 +43,8 @@ describe(MediaUserLicenseRepo.name, () => { describe('when searching for a users media licences', () => { const setup = async () => { const user: UserEntity = userFactory.build(); - const mediaSource: MediaSourceEntity = mediaSourceEntityFactory.build(); + const config: MediaSourceConfigEmbeddable = mediaSourceConfigEmbeddableFactory.build(); + const mediaSource: MediaSourceEntity = mediaSourceEntityFactory.build({ config }); const mediaUserLicense: MediaUserLicenseEntity = mediaUserLicenseEntityFactory.build({ user, mediaSource }); const otherMediaUserLicense: MediaUserLicenseEntity = mediaUserLicenseEntityFactory.build(); @@ -52,11 +56,12 @@ describe(MediaUserLicenseRepo.name, () => { user, mediaUserLicense, mediaSource, + config, }; }; it('should return user licenses for user', async () => { - const { user, mediaUserLicense, mediaSource } = await setup(); + const { user, mediaUserLicense, mediaSource, config } = await setup(); const result: MediaUserLicense[] = await repo.findMediaUserLicensesForUser(user.id); @@ -71,7 +76,7 @@ describe(MediaUserLicenseRepo.name, () => { name: mediaSource.name, sourceId: mediaSource.sourceId, format: mediaSource.format, - config: mediaSource.config, + config: MediaSourceConfigMapper.mapToDo(config), }), }), ]); diff --git a/apps/server/src/modules/user-license/repo/media-user-license.repo.ts b/apps/server/src/modules/user-license/repo/media-user-license.repo.ts index c24619bb301..004984b07ec 100644 --- a/apps/server/src/modules/user-license/repo/media-user-license.repo.ts +++ b/apps/server/src/modules/user-license/repo/media-user-license.repo.ts @@ -5,6 +5,7 @@ import { EntityId } from '@shared/domain/types'; import { BaseDomainObjectRepo } from '@shared/repo/base-domain-object.repo'; import { MediaSource, MediaUserLicense } from '../domain'; import { MediaSourceEntity, MediaUserLicenseEntity, UserLicenseType } from '../entity'; +import { MediaSourceConfigMapper } from './media-source-config.mapper'; @Injectable() export class MediaUserLicenseRepo extends BaseDomainObjectRepo { @@ -21,7 +22,7 @@ export class MediaUserLicenseRepo extends BaseDomainObjectRepo(MediaSourceConfigEmbeddable, ({ sequence }) => { + return { + _id: new ObjectId(), + clientId: `media-source-client-id-${sequence}`, + clientSecret: `media-source-client-secret-${sequence}`, + authEndpoint: `media-source-auth-endpoint-${sequence}`, + method: MediaSourceAuthMethod.CLIENT_CREDENTIALS, + }; +}); diff --git a/apps/server/src/modules/user-license/testing/media-source-config.factory.ts b/apps/server/src/modules/user-license/testing/media-source-config.factory.ts index 6a742c9764b..d43d288b9b7 100644 --- a/apps/server/src/modules/user-license/testing/media-source-config.factory.ts +++ b/apps/server/src/modules/user-license/testing/media-source-config.factory.ts @@ -1,18 +1,19 @@ +import { ObjectId } from '@mikro-orm/mongodb'; import { BaseFactory } from '@shared/testing'; -import { - MediaSourceConfigEmbeddable, - MediaSourceConfigEmbeddableProps, -} from '../entity/media-source-oauth-config.embeddable'; import { MediaSourceAuthMethod } from '../entity'; +import { MediaSourceOauthConfig, MediaSourceOauthConfigProps } from '../domain/media-source-oauth-config'; -export const mediaSourceConfigFactory = BaseFactory.define< - MediaSourceConfigEmbeddable, - MediaSourceConfigEmbeddableProps ->(MediaSourceConfigEmbeddable, ({ sequence }) => { - return { - clientId: `media-source-client-id${sequence}`, - clientSecret: `media-source-client-secret${sequence}`, - authEndpoint: `media-source-auth-endpoint-${sequence}`, - method: MediaSourceAuthMethod.CLIENT_CREDENTIALS, - }; -}); +export const mediaSourceConfigFactory = BaseFactory.define( + MediaSourceOauthConfig, + ({ sequence }) => { + const config = { + id: new ObjectId().toHexString(), + clientId: `media-source-client-id-${sequence}`, + clientSecret: `media-source-client-secret-${sequence}`, + authEndpoint: `media-source-auth-endpoint-${sequence}`, + method: MediaSourceAuthMethod.CLIENT_CREDENTIALS, + }; + + return config; + } +); diff --git a/apps/server/src/modules/user-license/testing/media-source-entity.factory.ts b/apps/server/src/modules/user-license/testing/media-source-entity.factory.ts index f674f0935af..48013f5364c 100644 --- a/apps/server/src/modules/user-license/testing/media-source-entity.factory.ts +++ b/apps/server/src/modules/user-license/testing/media-source-entity.factory.ts @@ -1,7 +1,7 @@ import { ObjectId } from '@mikro-orm/mongodb'; import { BaseFactory } from '@shared/testing'; import { MediaSourceDataFormat, MediaSourceEntity, MediaSourceEntityProps } from '../entity'; -import { mediaSourceConfigFactory } from './media-source-config.factory'; +import { mediaSourceConfigEmbeddableFactory } from './media-source-config.embeddable.factory'; export const mediaSourceEntityFactory = BaseFactory.define( MediaSourceEntity, @@ -11,7 +11,7 @@ export const mediaSourceEntityFactory = BaseFactory.define Date: Tue, 8 Oct 2024 16:46:01 +0200 Subject: [PATCH 7/7] update bad import --- .../modules/user-license/domain/media-source-oauth-config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts b/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts index 3f51b1b7306..7846c782191 100644 --- a/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts +++ b/apps/server/src/modules/user-license/domain/media-source-oauth-config.ts @@ -1,6 +1,6 @@ import { AuthorizableObject, DomainObject } from '@shared/domain/domain-object'; +import { EntityId } from '@shared/domain/types'; import { MediaSourceAuthMethod } from '../enum'; -import { EntityId } from '../../../shared/domain/types'; export interface MediaSourceOauthConfigProps extends AuthorizableObject { id: EntityId;