From 0d1e044c96372de6f0b423f9d0842b76f2d1aaf6 Mon Sep 17 00:00:00 2001 From: manelcecs Date: Mon, 30 Oct 2023 17:45:48 +0100 Subject: [PATCH] Add autocomplete query Add limit when no 'autocomplete' provided --- .../projects/graphql/resolver.ts | 9 +- .../projects/project-service.ts | 99 +++++++++++-------- 2 files changed, 64 insertions(+), 44 deletions(-) diff --git a/src/domain-services/projects/graphql/resolver.ts b/src/domain-services/projects/graphql/resolver.ts index d7be6d99..bffbc8b1 100644 --- a/src/domain-services/projects/graphql/resolver.ts +++ b/src/domain-services/projects/graphql/resolver.ts @@ -1,5 +1,5 @@ import { Service } from 'typedi'; -import { Ctx, Query, Resolver } from 'type-graphql'; +import { Arg, Ctx, Query, Resolver } from 'type-graphql'; import { ProjectService } from '../project-service'; import Project from './types'; import Context from '../../Context'; @@ -10,7 +10,10 @@ export default class ProjectsResolver { constructor(private projectService: ProjectService) {} @Query(() => [Project]) - async getProjects(@Ctx() context: Context): Promise { - return await this.projectService.getProjects(context.models); + async getProjects( + @Ctx() context: Context, + @Arg('autocomplete', { nullable: true }) autocomplete?: string + ): Promise { + return await this.projectService.getProjects(context.models, autocomplete); } } diff --git a/src/domain-services/projects/project-service.ts b/src/domain-services/projects/project-service.ts index cac96e9c..7ed50e74 100644 --- a/src/domain-services/projects/project-service.ts +++ b/src/domain-services/projects/project-service.ts @@ -1,56 +1,73 @@ import { Database } from '@unocha/hpc-api-core/src/db'; import { Service } from 'typedi'; import Project, { Pdf, mapPdfModelToType } from './graphql/types'; -import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types'; +import { Op } from '@unocha/hpc-api-core/src/db/util/conditions'; @Service() export class ProjectService { - public async getProjects(models: Database): Promise { - const projects = await models.project.find(); + public async getProjects( + models: Database, + autocomplete?: string + ): Promise { + let whereClause = {}; + let limitClause; + if (autocomplete) { + whereClause = { + name: { + [Op.LIKE]: `%${autocomplete}%`, + }, + }; + } else { + limitClause = 100; + } - return await Promise.all( - projects.map(async (project) => { - let projectVersion = null; + const findOptions = { + where: whereClause, + order: [['name', 'ASC']], + limit: limitClause, + }; + const projectsVersion = await models.projectVersion.find(findOptions); - if (project.sourceProjectId) { - const projectVersionIdBranded = createBrandedValue( - project.sourceProjectId?.valueOf() - ); + const projects: Project[] = []; + await Promise.all( + projectsVersion.map(async (projectVersion) => { + const projectDB = await models.project.findOne({ + where: { + id: projectVersion.projectId, + }, + }); - projectVersion = await models.projectVersion.findOne({ - where: { - id: projectVersionIdBranded, - }, - }); - } + if (projectDB) { + const pdf: Pdf | null = mapPdfModelToType(projectDB.pdf); - const pdf: Pdf | null = mapPdfModelToType(project.pdf); + const project: Project = { + id: projectDB.id.valueOf(), + createdAt: projectDB.createdAt.toString(), + updatedAt: projectDB.updatedAt.toString(), + code: projectDB.code ?? '', + currentPublishedVersionId: projectDB.currentPublishedVersionId + ? projectDB.currentPublishedVersionId.valueOf() + : 0, + creatorParticipantId: projectDB.creatorParticipantId + ? projectDB.creatorParticipantId.valueOf() + : 0, + latestVersionId: projectDB.latestVersionId + ? projectDB.latestVersionId.valueOf() + : 0, + implementationStatus: projectDB.implementationStatus, + pdf: pdf, + sourceProjectId: projectVersion.id.valueOf(), + name: projectVersion.name, + version: projectVersion.version, + projectVersionCode: projectVersion.code, + visible: projectDB.currentPublishedVersionId ? true : false, + }; - return { - id: project.id.valueOf(), - createdAt: project.createdAt.toString(), - updatedAt: project.updatedAt.toString(), - code: project.code ?? '', - currentPublishedVersionId: project.currentPublishedVersionId - ? project.currentPublishedVersionId.valueOf() - : 0, - creatorParticipantId: project.creatorParticipantId - ? project.creatorParticipantId.valueOf() - : 0, - latestVersionId: project.latestVersionId - ? project.latestVersionId.valueOf() - : 0, - implementationStatus: project.implementationStatus, - pdf: pdf, - sourceProjectId: project.sourceProjectId - ? project.sourceProjectId.valueOf() - : 0, - name: projectVersion ? projectVersion.name : 'none', - version: projectVersion ? projectVersion.version : 0, - projectVersionCode: projectVersion ? projectVersion.code : 'none', - visible: project.currentPublishedVersionId ? true : false, - }; + projects.push(project); + } }) ); + + return projects; } }