Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Return owned, shared & captcha projects from one endpoints

Blue Mouse893e64d9d33a
diff --git a/backend/apps/cloud/src/project/project.controller.ts b/backend/apps/cloud/src/project/project.controller.ts
index ef81f5217..173e18ed4 100644
--- a/backend/apps/cloud/src/project/project.controller.ts
+++ b/backend/apps/cloud/src/project/project.controller.ts
@@ -21,8 +21,6 @@ import {
ConflictException,
Res,
UnauthorizedException,
- ParseBoolPipe,
- DefaultValuePipe,
} from '@nestjs/common'
import { Response } from 'express'
import {
@@ -34,7 +32,7 @@ import {
ApiOkResponse,
ApiNoContentResponse,
} from '@nestjs/swagger'
-import { Equal, FindOptionsWhere, ILike, In } from 'typeorm'
+import { Equal, In } from 'typeorm'
import _isEmpty from 'lodash/isEmpty'
import _map from 'lodash/map'
import _trim from 'lodash/trim'
@@ -153,57 +151,11 @@ export class ProjectController {
@CurrentUserId() userId: string,
@Query('take') take: number | undefined,
@Query('skip') skip: number | undefined,
- @Query('isCaptcha') isCaptchaStr: string | undefined,
@Query('search') search: string | undefined,
- @Query('showArchived', new DefaultValuePipe(false), ParseBoolPipe)
- showArchived?: boolean,
): Promise<Pagination<Project> | Project[] | object> {
this.logger.log({ userId, take, skip }, 'GET /project')
- const isCaptcha = isCaptchaStr === 'true'
-
- let where: FindOptionsWhere<Project> | FindOptionsWhere<Project>[]
-
- if (search) {
- where = [
- {
- admin: {
- id: userId,
- },
- isCaptchaProject: isCaptcha,
- isAnalyticsProject: !isCaptcha,
- name: ILike(`%${search}%`),
- isArchived: showArchived,
- },
- {
- admin: {
- id: userId,
- },
- isCaptchaProject: isCaptcha,
- isAnalyticsProject: !isCaptcha,
- id: ILike(`%${search}%`),
- isArchived: showArchived,
- },
- ] as FindOptionsWhere<Project>[]
- } else {
- where = {
- admin: {
- id: userId,
- },
- } as FindOptionsWhere<Project>
-
- if (isCaptcha) {
- where.isCaptchaProject = true
- } else {
- where.isAnalyticsProject = true
- }
-
- if (showArchived) {
- where.isArchived = true
- }
- }
-
const [paginated, totalMonthlyEvents, user] = await Promise.all([
- this.projectService.paginate({ take, skip }, where),
+ this.projectService.paginate({ take, skip }, userId, search),
this.projectService.getRedisCount(userId),
this.userService.findOne({ where: { id: userId } }),
])
@@ -218,13 +170,18 @@ export class ProjectController {
_map(paginated.results, ({ id }) => id),
)
- paginated.results = _map(paginated.results, p => ({
- ...p,
- isOwner: true,
+ console.log('paginated.results:', paginated.results)
+
+ paginated.results = _map(paginated.results, project => ({
+ ...project,
+ isOwner: project.admin.id === userId,
+ isShareConfirmed: true,
isLocked: !!user?.dashboardBlockReason,
- isDataExists: _includes(pidsWithData, p?.id),
- isErrorDataExists: _includes(pidsWithErrorData, p?.id),
- organisationId: p?.organisation?.id,
+ isDataExists: _includes(pidsWithData, project?.id),
+ isErrorDataExists: _includes(pidsWithErrorData, project?.id),
+ organisationId: project?.organisation?.id,
+ passwordHash: undefined,
+ admin: undefined,
}))
return {
@@ -233,123 +190,6 @@ export class ProjectController {
}
}
- @Get('/shared')
- @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, search }, 'GET /project/shared')
-
- let where: FindOptionsWhere<ProjectShare> | FindOptionsWhere<ProjectShare>[]
-
- if (search) {
- where = [
- {
- user: { id: userId },
- project: {
- name: ILike(`%${search}%`),
- },
- },
- {
- user: { id: userId },
- project: {
- id: ILike(`%${search}%`),
- },
- },
- ] as FindOptionsWhere<ProjectShare>[]
- } else {
- where = {
- user: {
- id: userId,
- },
- } as FindOptionsWhere<ProjectShare>
- }
-
- const paginated = await this.projectService.paginateShared(
- { take, skip },
- where,
- )
-
- const pidsWithData =
- await this.projectService.getPIDsWhereAnalyticsDataExists(
- _map(paginated.results, ({ project }) => project.id),
- )
-
- const pidsWithErrorData =
- await this.projectService.getPIDsWhereErrorsDataExists(
- _map(paginated.results, ({ project }) => project.id),
- )
-
- // @ts-expect-error
- paginated.results = _map(paginated.results, share => {
- const project = processProjectUser(share.project)
-
- return {
- ...share,
- project: {
- ...project,
- admin: undefined,
- passwordHash: undefined,
- isLocked: !!share?.project?.admin?.dashboardBlockReason,
- isDataExists: _includes(pidsWithData, share?.project?.id),
- isErrorDataExists: _includes(pidsWithErrorData, share?.project?.id),
- },
- }
- })
-
- return paginated
- }
-
- @Get('/all')
- @ApiQuery({ name: 'take', required: false })
- @ApiQuery({ name: 'skip', required: false })
- @UseGuards(JwtAccessTokenGuard, RolesGuard)
- @Auth([UserType.ADMIN])
- @ApiResponse({ status: 200, type: Project })
- async getAllProjects(
- @Query('take') take: number | undefined,
- @Query('skip') skip: number | undefined,
- ): Promise<Project | object> {
- this.logger.log({ take, skip }, 'GET /all')
-
- return this.projectService.paginate({ take, skip })
- }
-
- @ApiBearerAuth()
- @Get('/user/:id')
- @ApiQuery({ name: 'take', required: false })
- @ApiQuery({ name: 'skip', required: false })
- @ApiQuery({ name: 'relatedonly', required: false, type: Boolean })
- @ApiResponse({ status: 200, type: [Project] })
- @UseGuards(JwtAccessTokenGuard, RolesGuard)
- @Roles(UserType.ADMIN)
- async getUserProject(
- @Param('id') userId: string,
- @Query('take') take: number | undefined,
- @Query('skip') skip: number | undefined,
- ): Promise<Pagination<Project> | Project[] | object> {
- this.logger.log({ userId, take, skip }, 'GET /user/:id')
-
- const where = Object()
- where.admin = userId
-
- const paginated = await this.projectService.paginate({ take, skip }, where)
- const totalMonthlyEvents = await this.projectService.getRedisCount(userId)
-
- return {
- ...paginated,
- totalMonthlyEvents,
- }
- }
-
@ApiBearerAuth()
@Post('/admin/:id')
@ApiResponse({ status: 201, type: Project })
diff --git a/backend/apps/cloud/src/project/project.service.ts b/backend/apps/cloud/src/project/project.service.ts
index c08720947..b783deb7a 100644
--- a/backend/apps/cloud/src/project/project.service.ts
+++ b/backend/apps/cloud/src/project/project.service.ts
@@ -12,7 +12,6 @@ import { InjectRepository } from '@nestjs/typeorm'
import {
FindManyOptions,
FindOneOptions,
- FindOptionsWhere,
Repository,
DeepPartial,
} from 'typeorm'
@@ -376,87 +375,40 @@ export class ProjectService {
async paginate(
options: PaginationOptionsInterface,
- where?: FindOptionsWhere<Project> | FindOptionsWhere<Project>[],
+ userId: string,
+ search?: string,
): Promise<Pagination<Project>> {
- const [results, total] = await this.projectsRepository.findAndCount({
- take: options.take || 100,
- skip: options.skip || 0,
- where,
- order: {
- name: 'ASC',
- },
- relations: ['share', 'share.user', 'funnels', 'organisation'],
- })
+ // Create query builder starting from Project
+ const queryBuilder = this.projectsRepository
+ .createQueryBuilder('project')
+ .leftJoinAndSelect('project.admin', 'admin')
+ .leftJoinAndSelect('project.share', 'share')
+ .leftJoinAndSelect('share.user', 'sharedUser')
+ .leftJoinAndSelect('project.funnels', 'funnels')
+ .where('admin.id = :userId', { userId })
+ .orWhere('share.user.id = :userId', { userId })
+
+ // Add search condition if search term provided
+ if (search?.trim()) {
+ queryBuilder.andWhere('(project.name ILIKE :search)', {
+ search: `%${search.trim()}%`,
+ })
+ }
- return new Pagination<Project>({
- results: processProjectsUser(results),
- total,
- })
- }
+ queryBuilder
+ .orderBy('project.name', 'ASC')
+ .skip(options.skip || 0)
+ .take(options.take || 100)
- async paginateShared(
- options: PaginationOptionsInterface,
- where: FindOptionsWhere<ProjectShare> | FindOptionsWhere<ProjectShare>[],
- ): Promise<Pagination<ProjectShare>> {
- const [results, total] = await this.projectShareRepository.findAndCount({
- take: options.take || 100,
- skip: options.skip || 0,
- where,
- order: {
- project: {
- name: 'ASC',
- },
- },
- relations: [
- 'project',
- 'project.admin',
- 'project.funnels',
- 'project.share',
- 'project.share.user',
- ],
- })
+ // Execute count and results queries
+ const [results, total] = await queryBuilder.getManyAndCount()
- return new Pagination<ProjectShare>({
- results,
+ return new Pagination<Project>({
+ results: processProjectsUser(results),
total,
})
}
- // async paginateAll(
- // options: PaginationOptionsInterface,
- // where: FindOptionsWhere<Project> | FindOptionsWhere<Project>[],
- // ): Promise<Pagination<Project>> {
- // // Create query builder starting from Project
- // const queryBuilder = this.projectsRepository
- // .createQueryBuilder('project')
- // .leftJoinAndSelect('project.admin', 'admin')
- // .leftJoinAndSelect('project.share', 'share')
- // .leftJoinAndSelect('share.user', 'sharedUser')
- // .leftJoinAndSelect('project.funnels', 'funnels')
- // // Add isOwner flag using CASE expression
- // .addSelect(
- // `
- // CASE
- // WHEN admin.id = :userId THEN true
- // ELSE false
- // END`,
- // 'project_isOwner',
- // )
- // .where('admin.id = :userId', { userId })
- // .orWhere('share.user.id = :userId', { userId })
- // .orderBy('project.name', 'ASC')
- // .skip(options.skip || 0)
- // .take(options.take || 100)
-
- // // Execute count and results queries
- // const [results, total] = await queryBuilder.getManyAndCount()
-
- // return new Pagination<Project>({
- // results: processProjectsUser(results),
- // total,
- // })
- // }
-
async count(): Promise<number> {
return this.projectsRepository.count()
}