Skip to content

Commit

Permalink
Refactor(Upload, Video, Image, File): Refactor all upload flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-Poffo committed Feb 23, 2024
1 parent 88bb885 commit 0cc96f9
Show file tree
Hide file tree
Showing 40 changed files with 206 additions and 511 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { OnFileUploaded } from '@/domain/course-management/application/subscribers/on-file-uploaded'
import { InMemoryVideosRepository } from '../../../../../test/repositories/in-memory-videos-repository'
import { waitFor } from '../../../../../test/utils/wait-for'
import { InMemoryFilesRepository } from './../../../../../test/repositories/in-memory-files-repository'
import { InMemoryImagesRepository } from './../../../../../test/repositories/in-memory-images-repository'
import { FakeUploader } from './../../../../../test/storage/fake-uploader'
import { UploadFileUseCase } from './../../../storage/application/use-cases/upload-file'

let inMemoryFilesRepository: InMemoryFilesRepository
let inMemoryImagesRepository: InMemoryImagesRepository
let inMemoryVideosRepository: InMemoryVideosRepository
let fakeUploader: FakeUploader
let uploadFileUseCase: UploadFileUseCase

describe('On file uploaded event', () => {
beforeEach(() => {
inMemoryFilesRepository = new InMemoryFilesRepository()
inMemoryImagesRepository = new InMemoryImagesRepository()
inMemoryVideosRepository = new InMemoryVideosRepository()
fakeUploader = new FakeUploader()
uploadFileUseCase = new UploadFileUseCase(
inMemoryFilesRepository,
fakeUploader
)

new OnFileUploaded(
inMemoryImagesRepository,
inMemoryVideosRepository
)
})

it('should be able to upload a file and persist your respective entity, image or video', async () => {
const result = await uploadFileUseCase.exec({
fileName: 'file.jpg',
body: Buffer.from('image body'),
size: 1024,
fileType: 'image/jpeg'
})

expect(result.isRight()).toBe(true)
expect(inMemoryFilesRepository.items[0]).toMatchObject({
fileName: 'file.jpg'
})
expect(fakeUploader.files[0]).toMatchObject({
fileName: 'file.jpg'
})
await waitFor(() => {
expect(inMemoryImagesRepository.items[0]).toMatchObject({
imageName: 'file.jpg'
})
}, 5000)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { DomainEvents } from '@/core/events/domain-events'
import { type EventHandler } from '@/core/events/event-handler'
import { FileUploadedEvent } from '@/domain/storage/enterprise/events/file-uploaded'
import { Image } from '../../enterprise/entities/image'
import { Video } from '../../enterprise/entities/video'
import { type ImagesRepository } from '../repositories/images-repository'
import { type VideosRepository } from '../repositories/videos-repository'

export class OnFileUploaded implements EventHandler {
constructor(
private readonly imagesRepository: ImagesRepository,
private readonly videosRepository: VideosRepository
) {
this.setupSubscriptions()
}

setupSubscriptions(): void {
DomainEvents.register(
this.createRespectiveEntity.bind(this) as (event: unknown) => void,
FileUploadedEvent.name
)
}

private async createRespectiveEntity({ file }: FileUploadedEvent) {
if (/image\/(jpeg|png)/.test(file.fileType)) {
const image = Image.create({
body: file.body,
imageName: file.fileName,
size: file.size,
imageKey: file.fileKey,
imageType: file.fileType as 'image/jpeg' | 'image/png',
storedAt: file.storedAt
})

await this.imagesRepository.create(image)
} else if (/video\/(mp4|avi)/.test(file.fileType)) {
const video = Video.create({
body: file.body,
videoName: file.fileName,
size: file.size,
videoKey: file.fileKey,
videoType: file.fileType as 'video/mp4' | 'video/avi',
duration: 24,
storedAt: file.storedAt
})

await this.videosRepository.create(video)
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

11 changes: 2 additions & 9 deletions src/domain/course-management/enterprise/entities/image.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AggregateRoot } from '@/core/entities/aggregate-root'
import { Entity } from '@/core/entities/entity'
import { type UniqueEntityID } from '@/core/entities/unique-entity-id'
import { type Optional } from '@/core/types/optional'
import { ImageUploadedEvent } from '../events/image-uploaded'

export interface ImageProps {
imageName: string
Expand All @@ -12,7 +11,7 @@ export interface ImageProps {
storedAt: Date
}

export class Image extends AggregateRoot<ImageProps> {
export class Image extends Entity<ImageProps> {
get imageName() {
return this.props.imageName
}
Expand Down Expand Up @@ -54,12 +53,6 @@ export class Image extends AggregateRoot<ImageProps> {
id
)

const isNewImage = !id

if (isNewImage) {
image.addDomainEvent(new ImageUploadedEvent(image))
}

return image
}
}
11 changes: 2 additions & 9 deletions src/domain/course-management/enterprise/entities/video.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AggregateRoot } from '@/core/entities/aggregate-root'
import { Entity } from '@/core/entities/entity'
import { type UniqueEntityID } from '@/core/entities/unique-entity-id'
import { type Optional } from '@/core/types/optional'
import { VideoUploadedEvent } from '../events/video-uploaded'

export interface VideoProps {
videoName: string
Expand All @@ -13,7 +12,7 @@ export interface VideoProps {
storedAt: Date
}

export class Video extends AggregateRoot<VideoProps> {
export class Video extends Entity<VideoProps> {
get videoName() {
return this.props.videoName
}
Expand Down Expand Up @@ -59,12 +58,6 @@ export class Video extends AggregateRoot<VideoProps> {
id
)

const isNewVideo = !id

if (isNewVideo) {
video.addDomainEvent(new VideoUploadedEvent(video))
}

return video
}
}
17 changes: 0 additions & 17 deletions src/domain/course-management/enterprise/events/image-uploaded.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/domain/course-management/enterprise/events/video-uploaded.ts

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0cc96f9

Please sign in to comment.