Skip to content

Commit 21b60ba

Browse files
committed
Enhance CanUpdateMediaUserMetadataEvent to allow it to be able to inject dependencies
Not sure if this is the best pattern, but playing it forward for now.
1 parent 0b0dddd commit 21b60ba

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

src/common/create-and-inject.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ModuleRef } from '@nestjs/core';
2+
import { pickBy } from 'lodash';
3+
import { Class } from 'type-fest';
4+
5+
/**
6+
* A helper to create an instance of a class and inject dependencies.
7+
*/
8+
export async function createAndInject<T extends Class<any>>(
9+
moduleRef: ModuleRef,
10+
type: T,
11+
...input: ConstructorParameters<T>
12+
): Promise<InstanceType<T>> {
13+
const injection = await moduleRef.resolve(type);
14+
const injectionProps = pickBy(injection);
15+
const object = new type(...input);
16+
Object.assign(object, injectionProps);
17+
return object;
18+
}

src/common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export { Many, many, maybeMany, JsonSet, ArrayItem } from '@seedcompany/common';
33
export * from './temporal';
44
export * from './calculated.decorator';
55
export * from './context.type';
6+
export * from './create-and-inject';
67
export * from './data-object';
78
export * from './date-filter.input';
89
export { DbLabel } from './db-label.decorator';
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import { Injectable, Optional, Scope } from '@nestjs/common';
12
import { PollVoter } from '~/common';
23
import { AnyMedia, MediaUserMetadata } from '../media.dto';
34

45
/**
56
* An attempt to update the media metadata.
67
* Vote with `allowUpdate` to control whether the update is allowed.
78
*/
9+
@Injectable({ scope: Scope.TRANSIENT })
810
export class CanUpdateMediaUserMetadataEvent {
911
constructor(
10-
readonly media: AnyMedia,
11-
readonly input: MediaUserMetadata,
12-
readonly allowUpdate: PollVoter<boolean>,
12+
@Optional() readonly media: AnyMedia,
13+
@Optional() readonly input: MediaUserMetadata,
14+
@Optional() readonly allowUpdate: PollVoter<boolean>,
1315
) {}
1416
}

src/components/file/media/media.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { forwardRef, Module } from '@nestjs/common';
22
import { FileModule } from '../file.module';
33
import { DetectExistingMediaMigration } from './detect-existing-media.migration';
44
import { DimensionsResolver } from './dimensions.resolver';
5+
import { CanUpdateMediaUserMetadataEvent } from './events/can-update-event';
56
import { MediaByFileVersionLoader } from './media-by-file-version.loader';
67
import { MediaDetector } from './media-detector.service';
78
import { MediaLoader } from './media.loader';
@@ -20,6 +21,7 @@ import { MediaService } from './media.service';
2021
MediaResolver,
2122
MediaService,
2223
DetectExistingMediaMigration,
24+
CanUpdateMediaUserMetadataEvent,
2325
],
2426
exports: [MediaService],
2527
})

src/components/file/media/media.service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Injectable } from '@nestjs/common';
2+
import { ModuleRef } from '@nestjs/core';
23
import { RequireAtLeastOne } from 'type-fest';
34
import {
5+
createAndInject,
46
IdOf,
57
NotFoundException,
68
Poll,
@@ -20,6 +22,7 @@ export class MediaService {
2022
private readonly detector: MediaDetector,
2123
private readonly repo: MediaRepository,
2224
private readonly eventBus: IEventBus,
25+
private readonly moduleRef: ModuleRef,
2326
) {}
2427

2528
async detectAndSave(file: FileVersion, metadata?: MediaUserMetadata) {
@@ -40,7 +43,13 @@ export class MediaService {
4043
) {
4144
const media = await this.repo.readOne(input);
4245
const poll = new Poll();
43-
const event = new CanUpdateMediaUserMetadataEvent(media, input, poll);
46+
const event = await createAndInject(
47+
this.moduleRef,
48+
CanUpdateMediaUserMetadataEvent,
49+
media,
50+
input,
51+
poll,
52+
);
4453
await this.eventBus.publish(event);
4554
if (!(poll.plurality && !poll.vetoed)) {
4655
throw new UnauthorizedException(

0 commit comments

Comments
 (0)