-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(Event): Add video uploaded event
- Loading branch information
1 parent
7bffeba
commit 2c5044d
Showing
9 changed files
with
142 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/domain/course-management/enterprise/events/video-uploaded.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { type UniqueEntityID } from '@/core/entities/unique-entity-id' | ||
import { type DomainEvent } from '@/core/events/domain-event' | ||
import { type Video } from '../entities/video' | ||
|
||
export class VideoUploadedEvent implements DomainEvent { | ||
public video: Video | ||
public ocurredAt: Date | ||
|
||
constructor(video: Video) { | ||
this.video = video | ||
this.ocurredAt = new Date() | ||
} | ||
|
||
getAggregateId(): UniqueEntityID { | ||
return this.video.id | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/domain/storage/application/subscribers/on-video-uploaded.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { makeVideo } from '../../../../../test/factories/make-video' | ||
import { InMemoryFilesRepository } from '../../../../../test/repositories/in-memory-files-repository' | ||
import { InMemoryVideosRepository } from '../../../../../test/repositories/in-memory-videos-repository' | ||
import { FakeUploader } from '../../../../../test/storage/fake-uploader' | ||
|
||
let inMemoryFilesRepository: InMemoryFilesRepository | ||
let inMemoryVideosRepository: InMemoryVideosRepository | ||
let fakeUploader: FakeUploader | ||
|
||
describe('On video uploaded', () => { | ||
beforeEach(() => { | ||
inMemoryFilesRepository = new InMemoryFilesRepository() | ||
inMemoryVideosRepository = new InMemoryVideosRepository() | ||
fakeUploader = new FakeUploader() | ||
}) | ||
|
||
it('should be able to upload a video', async () => { | ||
const video = makeVideo() | ||
await inMemoryVideosRepository.create(video) | ||
|
||
expect(inMemoryFilesRepository.items[0]).toBe({ | ||
name: video.videoName | ||
}) | ||
expect(fakeUploader.files[0]).toBe({ | ||
name: video.videoName | ||
}) | ||
}) | ||
}) |
31 changes: 31 additions & 0 deletions
31
src/domain/storage/application/subscribers/on-video-uploaded.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { DomainEvents } from '@/core/events/domain-events' | ||
import { type EventHandler } from '@/core/events/event-handler' | ||
import { VideoUploadedEvent } from '@/domain/course-management/enterprise/events/video-uploaded' | ||
import { type UploadFileUseCase } from '../use-cases/upload-file' | ||
|
||
export class OnVideoUploaded implements EventHandler { | ||
constructor( | ||
private readonly uploadFileUseCase: UploadFileUseCase | ||
) { | ||
this.setupSubscriptions() | ||
} | ||
|
||
setupSubscriptions(): void { | ||
DomainEvents.register( | ||
this.uploadVideo.bind(this) as (event: unknown) => void, | ||
VideoUploadedEvent.name | ||
) | ||
} | ||
|
||
private async uploadVideo({ video }: VideoUploadedEvent) { | ||
console.log('olá') | ||
|
||
await this.uploadFileUseCase.exec({ | ||
fileName: video.videoName, | ||
fileType: video.videoType, | ||
body: video.body, | ||
size: video.size, | ||
storedAt: video.storedAt | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* This function loops through a function rerunning all assertions | ||
* inside of it until it gets a truthy result. | ||
* | ||
* If the maximum duration is reached, it then rejects. | ||
* | ||
* @param expectations A function containing all tests assertions | ||
* @param maxDuration Maximum wait time before rejecting | ||
*/ | ||
export async function waitFor( | ||
assertions: () => void | Promise<void>, | ||
maxDuration = 1000 | ||
): Promise<void> { | ||
await new Promise<void>((resolve, reject) => { | ||
let elapsedTime = 0 | ||
|
||
const interval = setInterval(async () => { | ||
elapsedTime += 10 | ||
|
||
try { | ||
await assertions() | ||
clearInterval(interval) | ||
resolve() | ||
} catch (err) { | ||
if (elapsedTime >= maxDuration) { | ||
reject(err) | ||
} | ||
} | ||
}, 10) | ||
}) | ||
} |