|
@@ -1,34 +1,51 @@
|
|
|
import { Injectable, NotFoundException } from '@nestjs/common'
|
|
import { Injectable, NotFoundException } from '@nestjs/common'
|
|
|
import { ProjectsRepository } from './projects.repository'
|
|
import { ProjectsRepository } from './projects.repository'
|
|
|
import { CreateProjectDto, UpdateProjectDto } from './dto/project.dto'
|
|
import { CreateProjectDto, UpdateProjectDto } from './dto/project.dto'
|
|
|
|
|
+import { OrgsService } from '../orgs/orgs.service'
|
|
|
|
|
|
|
|
@Injectable()
|
|
@Injectable()
|
|
|
export class ProjectsService {
|
|
export class ProjectsService {
|
|
|
- constructor(private readonly repo: ProjectsRepository) {}
|
|
|
|
|
|
|
+ constructor(
|
|
|
|
|
+ private readonly repo: ProjectsRepository,
|
|
|
|
|
+ private readonly orgsService: OrgsService,
|
|
|
|
|
+ ) {}
|
|
|
|
|
|
|
|
- create(dto: CreateProjectDto) {
|
|
|
|
|
|
|
+ async create(dto: CreateProjectDto, userId: string) {
|
|
|
|
|
+ await this.orgsService.ensureUserInOrg(userId, dto.orgId)
|
|
|
return this.repo.create(dto)
|
|
return this.repo.create(dto)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- findAll(orgId?: string) {
|
|
|
|
|
- return this.repo.findAll(orgId)
|
|
|
|
|
|
|
+ async findAll(userId: string, orgId?: string) {
|
|
|
|
|
+ if (orgId) {
|
|
|
|
|
+ await this.orgsService.ensureUserInOrg(userId, orgId)
|
|
|
|
|
+ return this.repo.findAll(orgId)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const allowedOrgIds = await this.orgsService.getUserOrgIds(userId)
|
|
|
|
|
+ if (allowedOrgIds.length === 0) return []
|
|
|
|
|
+
|
|
|
|
|
+ const all = await this.repo.findAll()
|
|
|
|
|
+ return all.filter((p) => allowedOrgIds.includes(p.orgId))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async findOne(id: string) {
|
|
|
|
|
|
|
+ async findOne(id: string, userId: string) {
|
|
|
const project = await this.repo.findById(id)
|
|
const project = await this.repo.findById(id)
|
|
|
if (!project) throw new NotFoundException(`Project ${id} not found`)
|
|
if (!project) throw new NotFoundException(`Project ${id} not found`)
|
|
|
|
|
+ await this.orgsService.ensureUserInOrg(userId, project.orgId)
|
|
|
return project
|
|
return project
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async update(id: string, dto: UpdateProjectDto) {
|
|
|
|
|
|
|
+ async update(id: string, dto: UpdateProjectDto, userId: string) {
|
|
|
const exists = await this.repo.findById(id)
|
|
const exists = await this.repo.findById(id)
|
|
|
if (!exists) throw new NotFoundException(`Project ${id} not found`)
|
|
if (!exists) throw new NotFoundException(`Project ${id} not found`)
|
|
|
|
|
+ await this.orgsService.ensureUserInOrg(userId, exists.orgId)
|
|
|
return this.repo.update(id, dto)
|
|
return this.repo.update(id, dto)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async remove(id: string) {
|
|
|
|
|
|
|
+ async remove(id: string, userId: string) {
|
|
|
const exists = await this.repo.findById(id)
|
|
const exists = await this.repo.findById(id)
|
|
|
if (!exists) throw new NotFoundException(`Project ${id} not found`)
|
|
if (!exists) throw new NotFoundException(`Project ${id} not found`)
|
|
|
|
|
+ await this.orgsService.ensureUserInOrg(userId, exists.orgId)
|
|
|
await this.repo.remove(id)
|
|
await this.repo.remove(id)
|
|
|
return { success: true }
|
|
return { success: true }
|
|
|
}
|
|
}
|