Skip to content

Commit

Permalink
paginacion-endpoint-listar-proyectos (#45)
Browse files Browse the repository at this point in the history
* paginacion-endpoint-listar-proyectos

* Se unifica endpoint para busqueda y paginado de proyectos

* Correccion de atributo de busqueda

* Update project.service.ts

* Fix code scanning alert no. 5156: Disallow unused variables

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

---------

Co-authored-by: Juani Schuhmann <76596375+jschuhmann47@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 23, 2024
1 parent 1d5220c commit 9001b16
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
26 changes: 17 additions & 9 deletions src/project/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,25 @@ export class ProjectController {
}

@Get('')
async getAllUserProjects(@Req() req: { user: { id: string } }) {
async getAllUserProjects(
@Req() req: { user: { id: string } },
@Query('limit') limit: number,
@Query('offset') offset: number,
@Query('search') search: string
) {
const { id } = req.user
const projects = await this.projectService.findUserProjects(id)
return projects
}

@Get('search')
async searchProjects(@Query() query: { name: string }) {
const name = query.name
const projects = await this.projectService.findProjectsByName(name)
return projects
const [projects, total] = await this.projectService.findUserProjects(
id,
limit,
offset,
search
)

return {
items: projects,
total,
}
}

@Get(':id')
Expand Down
51 changes: 35 additions & 16 deletions src/project/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ import {
NotFoundException,
} from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { FilterQuery } from 'mongoose'
import mongoose, { Model } from 'mongoose'
import { UserService } from '../user/user.service'
import { ProjectDto, toParticipant, UpdateUserRolesDto } from './project.dto'
import { Project } from './project.schema'
import { defaultStages, Permission, Stage } from './stage.schema'
import { insensitiveRegExp } from './utils/escape_string'
import { User } from 'src/user/user.schema'
import { getParentsFromNode, OrganizationChart } from './orgChart'
import { OkrService } from 'src/herramientas/okr/okr.service'

type ProjectQuery = FilterQuery<{
'participants.user'?: string
coordinators?: string
name?: { $regex: RegExp }
}>
@Injectable()
export class ProjectService {
constructor(
Expand Down Expand Up @@ -49,24 +54,38 @@ export class ProjectService {
return this.projectModel.create(projectToCreate)
}

async findUserProjects(requestorId: string) {
async findUserProjects(
requestorId: string,
limit: number,
offset: number,
search: string
) {
const isAdmin = await this.userService.isAdmin(requestorId)
if (isAdmin) {
return this.projectModel.find({})
} else {
return this.projectModel.find({
$or: [
{ 'participants.user': requestorId },
{ coordinators: requestorId },
],
})

let query: ProjectQuery = isAdmin
? {}
: {
$or: [
{ 'participants.user': requestorId },
{ coordinators: requestorId },
],
}

if (search) {
query = {
$and: [query, { name: { $regex: new RegExp(search, 'i') } }],
} as ProjectQuery
}
}

async findProjectsByName(name: string) {
return this.projectModel.find({
name: insensitiveRegExp(name),
})
const total = await this.projectModel.countDocuments(query)

const projects = await this.projectModel
.find(query)
.skip(offset)
.limit(limit)
.exec()

return [projects, total]
}

async update(id: string, updated: ProjectDto) {
Expand Down

0 comments on commit 9001b16

Please sign in to comment.