Skip to content

Commit

Permalink
Add autocomplete query
Browse files Browse the repository at this point in the history
Add limit when no 'autocomplete' provided
  • Loading branch information
manelcecs committed Oct 31, 2023
1 parent 28ed326 commit 0d1e044
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 44 deletions.
9 changes: 6 additions & 3 deletions src/domain-services/projects/graphql/resolver.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -10,7 +10,10 @@ export default class ProjectsResolver {
constructor(private projectService: ProjectService) {}

@Query(() => [Project])
async getProjects(@Ctx() context: Context): Promise<Project[]> {
return await this.projectService.getProjects(context.models);
async getProjects(
@Ctx() context: Context,
@Arg('autocomplete', { nullable: true }) autocomplete?: string
): Promise<Project[]> {
return await this.projectService.getProjects(context.models, autocomplete);
}
}
99 changes: 58 additions & 41 deletions src/domain-services/projects/project-service.ts
Original file line number Diff line number Diff line change
@@ -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<Project[]> {
const projects = await models.project.find();
public async getProjects(
models: Database,
autocomplete?: string
): Promise<Project[]> {
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;
}
}

0 comments on commit 0d1e044

Please sign in to comment.