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!

added some dummy routes

Blue Mouse20009cf16242
diff --git a/src/project/dto/share.dto.ts b/src/project/dto/share.dto.ts
new file mode 100644
index 000000000..e8ac6209e
--- /dev/null
+++ b/src/project/dto/share.dto.ts
@@ -0,0 +1,20 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsNotEmpty } from 'class-validator'
+
+export class ShareDTO {
+ @ApiProperty({
+ example: '[email protected]',
+ required: true,
+ description: 'User you want to invite to your project',
+ })
+ @IsNotEmpty()
+ email: string
+
+ @ApiProperty({
+ example: 'viewer',
+ required: true,
+ description: 'Users role in your project',
+ })
+ @IsNotEmpty()
+ role: string
+}
diff --git a/src/project/entity/project-share.entity.ts b/src/project/entity/project-share.entity.ts
index 82688cc57..227186a53 100644
--- a/src/project/entity/project-share.entity.ts
+++ b/src/project/entity/project-share.entity.ts
@@ -4,6 +4,11 @@ import { ApiProperty } from '@nestjs/swagger'
import { User } from '../../user/entities/user.entity'
import { Project } from './project.entity'
+export enum PlanCode {
+ viewer = 'viewer',
+ admin = 'admin',
+}
+
@Entity()
export class ProjectShare {
@PrimaryGeneratedColumn('uuid')
@@ -22,6 +27,12 @@ export class ProjectShare {
})
confirmed: boolean
+ @Column({
+ type: 'enum',
+ enum: PlanCode,
+ })
+ role: PlanCode
+
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
created: Date
diff --git a/src/project/project.controller.ts b/src/project/project.controller.ts
index 84d565ff9..cae424b67 100644
--- a/src/project/project.controller.ts
+++ b/src/project/project.controller.ts
@@ -30,6 +30,7 @@ import { Project } from './entity/project.entity'
import { CurrentUserId } from '../common/decorators/current-user-id.decorator'
import { UserService } from '../user/user.service'
import { ProjectDTO } from './dto/project.dto'
+import { ShareDTO } from './dto/share.dto'
import { AppLoggerService } from '../logger/logger.service'
import {
redis,
@@ -421,4 +422,59 @@ export class ProjectController {
}
}
}
+
+ // The routes related to sharing projects feature
+ @Delete('/:pid/:userId')
+ @HttpCode(204)
+ @UseGuards(SelfhostedGuard)
+ @UseGuards(RolesGuard)
+ @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ @ApiResponse({ status: 204, description: 'Empty body' })
+ async deleteShare(
+ @Param('pid') pid: string,
+ @Param('userId') userId: string,
+ @CurrentUserId() uid: string,
+ ): Promise<any> {
+ this.logger.log({ uid, pid, userId }, 'DELETE /project/:pid/:userId')
+
+ if (!isValidPID(pid)) {
+ throw new BadRequestException(
+ 'The provided Project ID (pid) is incorrect',
+ )
+ }
+
+ const user = await this.userService.findOne(uid)
+ const project = await this.projectService.findOneWhere(
+ { id: pid },
+ {
+ relations: ['admin', 'share'],
+ select: ['id', 'admin', 'share'],
+ },
+ )
+
+ if (_isEmpty(project)) {
+ throw new NotFoundException(`Project with ID ${pid} does not exist`)
+ }
+
+ this.projectService.allowedToManage(project, uid, user.roles)
+
+ console.log(project)
+ // TODO
+ }
+
+ @Post('/:pid/share')
+ @HttpCode(200)
+ @UseGuards(SelfhostedGuard)
+ @UseGuards(RolesGuard)
+ @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ @ApiResponse({ status: 200, type: Project })
+ async share(
+ @Param('pid') pid: string,
+ @Body() shareDTO: ShareDTO,
+ @CurrentUserId() uid: string,
+ ): Promise<any> {
+ this.logger.log({ uid, pid, shareDTO }, 'POST /project/:pid/share')
+
+ // TODO
+ }
}
diff --git a/src/project/project.module.ts b/src/project/project.module.ts
index 9d3acaa96..54d1bab49 100644
--- a/src/project/project.module.ts
+++ b/src/project/project.module.ts
@@ -2,6 +2,7 @@ import { Module, forwardRef } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { Project } from './entity/project.entity'
+import { ProjectShare } from './entity/project-share.entity'
import { ProjectService } from './project.service'
import { ProjectController } from './project.controller'
import { UserModule } from '../user/user.module'
@@ -10,8 +11,9 @@ import { AppLoggerModule } from '../logger/logger.module'
@Module({
imports: [
TypeOrmModule.forFeature([Project]),
+ TypeOrmModule.forFeature([ProjectShare]),
forwardRef(() => UserModule),
- AppLoggerModule
+ AppLoggerModule,
],
providers: [ProjectService],
exports: [ProjectService],
diff --git a/src/project/project.service.ts b/src/project/project.service.ts
index 6d44203d0..8ac97a806 100644
--- a/src/project/project.service.ts
+++ b/src/project/project.service.ts
@@ -5,22 +5,21 @@ import * as _isEmpty from 'lodash/isEmpty'
import * as _isString from 'lodash/isString'
import * as _isArray from 'lodash/isArray'
import * as _size from 'lodash/size'
-import * as _split from 'lodash/split'
import * as _join from 'lodash/join'
import * as _find from 'lodash/find'
import * as _map from 'lodash/map'
+import * as _includes from 'lodash/includes'
import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
-import * as _includes from 'lodash/includes'
import { Pagination, PaginationOptionsInterface } from '../common/pagination'
import { Project } from './entity/project.entity'
+import { ProjectShare } from './entity/project-share.entity'
import { ProjectDTO } from './dto/project.dto'
import { UserType } from './../user/entities/user.entity'
import {
isValidPID, redisProjectCountCacheTimeout, getRedisUserCountKey, redis, clickhouse, isSelfhosted,
} from '../common/constants'
-import { getProjectsClickhouse } from '../common/utils'
dayjs.extend(utc)
@@ -28,7 +27,9 @@ dayjs.extend(utc)
export class ProjectService {
constructor(
@InjectRepository(Project)
- private projectsRepository: Repository<Project>
+ private projectsRepository: Repository<Project>,
+ @InjectRepository(ProjectShare)
+ private projectShareRepository: Repository<ProjectShare>,
) {}
async paginate(options: PaginationOptionsInterface, where: Record<string, unknown> | undefined): Promise<Pagination<Project>> {
@@ -70,6 +71,22 @@ export class ProjectService {
.execute()
}
+ async createShare(share: ProjectShare): Promise<ProjectShare> {
+ return this.projectShareRepository.save(share)
+ }
+
+ async deleteShare(id: string): Promise<any> {
+ return this.projectShareRepository.delete(id)
+ }
+
+ async updateShare(id: string, share: ProjectShare): Promise<any> {
+ return this.projectShareRepository.update(id, share)
+ }
+
+ async findShare(params: object): Promise<ProjectShare[]> {
+ return this.projectShareRepository.find(params)
+ }
+
findOneWithRelations(id: string): Promise<Project | null> {
return this.projectsRepository.findOne(id, { relations: ['admin'] })
}