diff --git a/src/domain/template/template/template.service.ts b/src/domain/template/template/template.service.ts index 551bf1060c..6bad82f5ba 100644 --- a/src/domain/template/template/template.service.ts +++ b/src/domain/template/template/template.service.ts @@ -380,31 +380,22 @@ export class TemplateService { }); } - async getTemplateInTemplatesSetOrFail( + async getTemplateByNameIDInTemplatesSetOrFail( templatesSetID: string, - templateIdOrNameId: string + templateNameId: string ): Promise { - let template = await this.templateRepository.findOne({ + const template = await this.templateRepository.findOne({ where: { templatesSet: { id: templatesSetID, }, - nameID: templateIdOrNameId, + id: templateNameId, }, }); - if (!template) { - template = await this.templateRepository.findOne({ - where: { - templatesSet: { - id: templatesSetID, - }, - id: templateIdOrNameId, - }, - }); - } + if (!template) { throw new EntityNotFoundException( - `Templates with ID/NameId(${templatesSetID}) not found!`, + `Templates with NameID (${templateNameId}) not found in templatesSet with ID: ${templatesSetID}!`, LogContext.TEMPLATES ); } diff --git a/src/library/innovation-pack/innovaton.pack.service.ts b/src/library/innovation-pack/innovaton.pack.service.ts index 8819879937..7daf3d1024 100644 --- a/src/library/innovation-pack/innovaton.pack.service.ts +++ b/src/library/innovation-pack/innovaton.pack.service.ts @@ -1,4 +1,3 @@ -import { UUID_LENGTH } from '@common/constants'; import { LogContext, ProfileType } from '@common/enums'; import { EntityNotFoundException, @@ -148,20 +147,11 @@ export class InnovationPackService { innovationPackID: string, options?: FindOneOptions ): Promise { - let innovationPack: IInnovationPack | null = null; - if (innovationPackID.length === UUID_LENGTH) { - innovationPack = await this.innovationPackRepository.findOne({ - where: { id: innovationPackID }, - ...options, - }); - } - if (!innovationPack) { - // look up based on nameID - innovationPack = await this.innovationPackRepository.findOne({ - where: { nameID: innovationPackID }, - ...options, - }); - } + const innovationPack = await this.innovationPackRepository.findOne({ + where: { id: innovationPackID }, + ...options, + }); + if (!innovationPack) throw new EntityNotFoundException( `Unable to find InnovationPack with ID: ${innovationPackID}`, @@ -170,6 +160,23 @@ export class InnovationPackService { return innovationPack; } + async getInnovationPackByNameIdOrFail( + innovationPackNameID: string, + options?: FindOneOptions + ): Promise { + const innovationPack = await this.innovationPackRepository.findOne({ + where: { nameID: innovationPackNameID }, + ...options, + }); + + if (!innovationPack) + throw new EntityNotFoundException( + `Unable to find InnovationPack using NameID: ${innovationPackNameID}`, + LogContext.LIBRARY + ); + return innovationPack; + } + public async getProfile( innovationPackInput: IInnovationPack, relations?: FindOptionsRelations diff --git a/src/services/api/lookup-by-name/lookup.by.name.resolver.fields.ts b/src/services/api/lookup-by-name/lookup.by.name.resolver.fields.ts index c011908be3..1283aefc0b 100644 --- a/src/services/api/lookup-by-name/lookup.by.name.resolver.fields.ts +++ b/src/services/api/lookup-by-name/lookup.by.name.resolver.fields.ts @@ -31,7 +31,7 @@ export class LookupByNameResolverFields { @Args('NAMEID', { type: () => NameID }) nameid: string ): Promise { const innovationPack = - await this.innovationPackService.getInnovationPackOrFail(nameid); + await this.innovationPackService.getInnovationPackByNameIdOrFail(nameid); this.authorizationService.grantAccessOrFail( agentInfo, innovationPack.authorization, @@ -46,23 +46,24 @@ export class LookupByNameResolverFields { @ResolveField(() => ITemplate, { nullable: true, description: - 'Lookup the specified Template using a templatesSetId and NameID', + 'Lookup the specified Template using a templatesSetId and the template NameID', }) async template( @CurrentUser() agentInfo: AgentInfo, @Args('templatesSetID', { type: () => UUID }) ID: string, @Args('NAMEID', { type: () => NameID }) nameID: string ): Promise { - const template = await this.templateService.getTemplateInTemplatesSetOrFail( - ID, - nameID - ); + const template = + await this.templateService.getTemplateByNameIDInTemplatesSetOrFail( + ID, + nameID + ); this.authorizationService.grantAccessOrFail( agentInfo, template.authorization, AuthorizationPrivilege.READ, - `lookup InnovationPack by NameID: ${template.id}` + `lookup template by NameID: ${template.id}` ); return template;