diff --git a/src/domains/shared-resource/shared-resource.domain.ts b/src/domains/shared-resource/shared-resource.domain.ts index b8a51f7..dfa2448 100644 --- a/src/domains/shared-resource/shared-resource.domain.ts +++ b/src/domains/shared-resource/shared-resource.domain.ts @@ -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 @@ -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() @@ -75,6 +80,23 @@ export class SharedResourceDomain { } } + /** Get the shared resource with given id */ + + static async fromGetSharedResource( + id: string, + dto: GetSharedResourcesQueryDTO, + model: SharedResourceModel, + ): Promise { + 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 { return this.props