Skip to content

Commit

Permalink
Add customevents fir views
Browse files Browse the repository at this point in the history
  • Loading branch information
pro1code1hack committed Jun 26, 2024
1 parent 4f33a03 commit 13324a1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
9 changes: 3 additions & 6 deletions apps/production/src/analytics/analytics.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,13 @@ export class AnalyticsController {
await this.analyticsService.checkBillingAccess(pid)

// TODO make it smarter
if (
viewId &&
(period || timeBucket || from || to || filters || timezone || mode)
) {
if (viewId && filters) {
throw new ConflictException('Cannot specify both viewId and filters.')
}

const view = await this.projectsViewsRepository.findView(viewId)
const view = await this.projectsViewsRepository.findProjectView(pid, viewId)

if (pid !== view.projectId) {
if (!view) {
throw new NotFoundException('View not found.')
}

Expand Down
8 changes: 8 additions & 0 deletions apps/production/src/project/dto/project-view-ids.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IntersectionType } from '@nestjs/swagger'
import { ProjectIdDto } from './project-id.dto'
import { ProjectViewIdDto } from './project-view-id.dto'

export class ProjectViewIdsDto extends IntersectionType(
ProjectIdDto,
ProjectViewIdDto,
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ export class ProjectViewCustomEventEntity {
@UpdateDateColumn()
updatedAt: Date

@ManyToOne(() => ProjectViewEntity, projectView => projectView.id, {
onDelete: 'CASCADE',
})
@ManyToOne(() => ProjectViewEntity, projectView => projectView.customEvents)
view: ProjectViewEntity
}
3 changes: 2 additions & 1 deletion apps/production/src/project/entity/project-view.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ export class ProjectViewEntity {
@ManyToOne(() => Project, project => project.views, { onDelete: 'CASCADE' })
project: Project

@ApiProperty({ type: ProjectViewCustomEventEntity, isArray: true })
@OneToMany(
() => ProjectViewCustomEventEntity,
projectViewCustomEvent => projectViewCustomEvent.id,
projectViewCustomEvent => projectViewCustomEvent.view,
)
customEvents: ProjectViewCustomEventEntity[]
}
6 changes: 3 additions & 3 deletions apps/production/src/project/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ import { ProjectViewEntity } from './entity/project-view.entity'
import { ProjectIdDto } from './dto/project-id.dto'
import { CreateProjectViewDto } from './dto/create-project-view.dto'
import { UpdateProjectViewDto } from './dto/update-project-view.dto'
import { ProjectViewIdDto } from './dto/project-view-id.dto'
import { ProjectViewIdsDto } from './dto/project-view-ids.dto'

const PROJECTS_MAXIMUM = 50

Expand Down Expand Up @@ -1955,7 +1955,7 @@ export class ProjectController {
@UseGuards(JwtAccessTokenGuard, RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
async updateProjectView(
@Param() params: ProjectIdDto & ProjectViewIdDto,
@Param() params: ProjectViewIdsDto,
@Body() body: UpdateProjectViewDto,
@CurrentUserId() userId: string,
) {
Expand Down Expand Up @@ -1998,7 +1998,7 @@ export class ProjectController {
@UseGuards(JwtAccessTokenGuard, RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
async deleteProjectView(
@Param() params: ProjectIdDto & ProjectViewIdDto,
@Param() params: ProjectViewIdsDto,
@CurrentUserId() userId: string,
) {
const project = await this.projectService.findProject(params.projectId, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,47 @@ import { Repository } from 'typeorm'
import { ProjectViewEntity } from '../entity/project-view.entity'
import { CreateProjectViewDto } from '../dto/create-project-view.dto'
import { UpdateProjectViewDto } from '../dto/update-project-view.dto'
import { ProjectViewCustomEventEntity } from '../entity/project-view-custom-event.entity'

@Injectable()
export class ProjectsViewsRepository {
constructor(
@InjectRepository(ProjectViewEntity)
private viewsRepository: Repository<ProjectViewEntity>,
@InjectRepository(ProjectViewCustomEventEntity)
private projectViewCustomEventsRepository: Repository<ProjectViewCustomEventEntity>,
) {}

async findViews(projectId: string) {
return this.viewsRepository.find({ projectId })
return this.viewsRepository.find({
where: { projectId },
relations: ['customEvents'],
})
}

async createProjectView(projectId: string, data: CreateProjectViewDto) {
return this.viewsRepository.save({
const view = await this.viewsRepository.save({
project: { id: projectId },
...data,
customEvents: data.customEvents,
})

const customEventPromises = data.customEvents.map(customEvent =>
this.projectViewCustomEventsRepository.save({
viewId: view.id,
...customEvent,
}),
)
await Promise.all(customEventPromises)

return view
}

async findView(id: string) {
return this.viewsRepository.findOne({ where: { id } })
return this.viewsRepository.findOne({
where: { id },
relations: ['customEvents'],
})
}

async findProjectView(projectId: string, viewId: string) {
Expand Down

0 comments on commit 13324a1

Please sign in to comment.