Skip to content

Commit

Permalink
(feature) project search (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
kruzhambus authored Oct 6, 2023
1 parent 277d8be commit 05dc675
Showing 1 changed file with 53 additions and 8 deletions.
61 changes: 53 additions & 8 deletions apps/production/src/project/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '@nestjs/common'
import { Response } from 'express'
import { ApiTags, ApiQuery, ApiResponse } from '@nestjs/swagger'
import { ILike } from 'typeorm'
import * as _isEmpty from 'lodash/isEmpty'
import * as _map from 'lodash/map'
import * as _trim from 'lodash/trim'
Expand Down Expand Up @@ -111,24 +112,48 @@ export class ProjectController {
@ApiQuery({ name: 'skip', required: false })
@ApiQuery({ name: 'isCaptcha', required: false, type: Boolean })
@ApiQuery({ name: 'relatedonly', required: false, type: Boolean })
@ApiQuery({ name: 'search', required: false, type: String })
@ApiResponse({ status: 200, type: [Project] })
@Auth([UserType.CUSTOMER, UserType.ADMIN], true)
async get(
@CurrentUserId() userId: string,
@Query('take') take: number | undefined,
@Query('skip') skip: number | undefined,
@Query('isCaptcha') isCaptchaStr: string | undefined,
@Query('search') search: string | undefined,
): Promise<Pagination<Project> | Project[] | object> {
this.logger.log({ userId, take, skip }, 'GET /project')
const isCaptcha = isCaptchaStr === 'true'

const where = Object()
where.admin = userId
let where: any

if (isCaptcha) {
where.isCaptchaProject = true
if (search) {
where = [
{
admin: userId,
isCaptchaProject: isCaptcha,
isAnalyticsProject: !isCaptcha,
name: ILike(`%${search}%`),
// name: ILike(`%${mysql.escape(search).slice(1, 0).slice(0, -1)}%`),
},
{
admin: userId,
isCaptchaProject: isCaptcha,
isAnalyticsProject: !isCaptcha,
id: ILike(`%${search}%`),
// id: ILike(`%${mysql.escape(search).slice(1, 0).slice(0, -1)}%`),
},
]
} else {
where.isAnalyticsProject = true
where = {
admin: userId,
}

if (isCaptcha) {
where.isCaptchaProject = true
} else {
where.isAnalyticsProject = true
}
}

const paginated = await this.projectService.paginate({ take, skip }, where)
Expand Down Expand Up @@ -168,17 +193,37 @@ export class ProjectController {
@ApiQuery({ name: 'take', required: false })
@ApiQuery({ name: 'skip', required: false })
@ApiQuery({ name: 'relatedonly', required: false, type: Boolean })
@ApiQuery({ name: 'search', required: false, type: String })
@ApiResponse({ status: 200, type: [Project] })
@Auth([UserType.CUSTOMER, UserType.ADMIN], true)
async getShared(
@CurrentUserId() userId: string,
@Query('take') take: number | undefined,
@Query('skip') skip: number | undefined,
@Query('search') search: string | undefined,
): Promise<Pagination<ProjectShare> | ProjectShare[] | object> {
this.logger.log({ userId, take, skip }, 'GET /project/shared')
this.logger.log({ userId, take, skip, search }, 'GET /project/shared')

const where = Object()
where.user = userId
let where = Object()

if (search) {
where = [
{
user: userId,
project: {
name: ILike(`%${search}%`),
},
},
{
user: userId,
project: {
id: ILike(`%${search}%`),
},
},
]
} else {
where.user = userId
}

const paginated = await this.projectService.paginateShared(
{ take, skip },
Expand Down

0 comments on commit 05dc675

Please sign in to comment.