Skip to content

Commit

Permalink
feat: fromGetSharedResource
Browse files Browse the repository at this point in the history
  • Loading branch information
mlajkim committed Dec 1, 2023
1 parent d49bea8 commit e567f54
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/domains/shared-resource/shared-resource.domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DataNotObjectError } from '@/errors/400/data-not-object.error'
import { PostSharedResourceDTO } from '@/dto/post-shared-resource.dto'
import { WordService } from '@/services/word.service'
import { NotExistOrNoPermissionError } from '@/errors/400/not-exist-or-no-permission.error'
import { GetSharedResourcesQueryDTO } from '@/dto/get-shared-resources-query.dto'

export class SharedResourceDomain {
readonly props: ISharedResource
Expand All @@ -21,6 +22,10 @@ export class SharedResourceDomain {
this.props = props
}

get isExpired() {
return Date.now() < this.props.expireInSecs
}

static fromMdb(props: SharedResourceDoc): SharedResourceDomain {
if (typeof props !== 'object') throw new DataNotObjectError()

Expand Down Expand Up @@ -75,6 +80,23 @@ export class SharedResourceDomain {
}
}

/** Get the shared resource with given id */

static async fromGetSharedResource(
id: string,
dto: GetSharedResourcesQueryDTO,
model: SharedResourceModel,
): Promise<SharedResourceDomain> {
const doc = await model.findById(id)
if (!doc) throw new NotExistOrNoPermissionError()

const domain = SharedResourceDomain.fromMdb(doc)
if (dto.isExpired && domain.isExpired)
throw new NotExistOrNoPermissionError()

return domain
}

/** Returns props of the SharedResourceDomain */
toResDTO(): Partial<ISharedResource> {
return this.props
Expand Down

0 comments on commit e567f54

Please sign in to comment.