universo-platform-3d
159 строк · 4.4 Кб
1import {2BadRequestException,3ForbiddenException,4Inject,5Injectable,6NotFoundException,7forwardRef
8} from '@nestjs/common'9import { InjectModel } from '@nestjs/mongoose'10import { Environment, EnvironmentDocument } from './environment.schema'11import { Model, Types } from 'mongoose'12import { UpdateEnvironmentDto } from './dto/update-environment.dto'13import { ObjectId } from 'mongodb'14import { Space, SpaceDocument } from '../space/space.schema'15import {16SpaceService,17SpaceServiceType,18SpaceWithStandardPopulatedProperties
19} from '../space/space.service'20import { UserId } from '../util/mongo-object-id-helpers'21
22@Injectable()23export class EnvironmentService {24constructor(25@InjectModel(Environment.name)26private readonly environmentModel: Model<EnvironmentDocument>,27@InjectModel(Space.name)28private spaceModel: Model<SpaceDocument>,29@Inject(forwardRef(() => SpaceService))30private readonly spaceService: SpaceServiceType31) {}32
33public create() {34const createdEnvironment = new this.environmentModel()35return createdEnvironment.save()36}37
38public findOne(id: string) {39return this.environmentModel.findById(id).exec()40}41
42public async findOneWithRolesCheck(id: string, userId: UserId) {43const environment = await this.environmentModel.findById(id).exec()44if (!environment) {45throw new NotFoundException()46}47
48// find the space that is associated with this environment49const space = await this.spaceModel50.findOne({ environment: new ObjectId(id) })51.exec()52
53if (!space) {54throw new NotFoundException()55}56
57// get the space with standard populated properties58// this is necessary to check if the user has access to the space59const spaceWithStandardPopulatedProperties =60await this.spaceService.getSpace(space._id)61
62// check if the user has access to the space63if (64this.spaceService.canFindWithRolesCheck(65userId,66spaceWithStandardPopulatedProperties
67)68) {69return environment70} else {71throw new ForbiddenException()72}73}74
75public update(id: string, dto: UpdateEnvironmentDto) {76return this.environmentModel77.findByIdAndUpdate(id, dto, { new: true })78.exec()79}80
81public async updateWithRolesCheck(82id: string,83dto: UpdateEnvironmentDto,84userId: UserId85) {86const environment = await this.environmentModel.findById(id).exec()87if (!environment) {88throw new NotFoundException()89}90
91// find the space that is associated with this environment92const space = await this.spaceModel93.findOne({ environment: new ObjectId(id) })94.exec()95
96if (!space) {97throw new NotFoundException()98}99
100// get the space with standard populated properties101// this is necessary to check if the user has access to the space102const spaceWithStandardPopulatedProperties =103await this.spaceService.getSpace(space._id)104
105// check if the user has access to the space106if (107this.spaceService.canUpdateWithRolesCheck(108userId,109spaceWithStandardPopulatedProperties
110)111) {112return this.environmentModel113.findByIdAndUpdate(id, dto, { new: true })114.exec()115} else {116throw new ForbiddenException()117}118}119
120public async copyFromEnvironment(environmentId: string) {121let environment: EnvironmentDocument122
123if (!environmentId) {124environment = new this.environmentModel()125} else {126environment = await this.environmentModel.findById(environmentId).exec()127environment._id = new ObjectId()128environment.isNew = true129environment.updatedAt = new Date()130environment.createdAt = new Date()131}132
133return environment.save()134}135
136public remove(id: string) {137if (!Types.ObjectId.isValid(id)) {138throw new BadRequestException('ID is not a valid Mongo ObjectID')139}140return this.environmentModel141.findOneAndDelete({ _id: id }, { new: true })142.exec()143.then((data) => {144if (data) {145return data146} else {147throw new NotFoundException()148}149})150}151
152public async restoreEnvironment(environment: EnvironmentDocument) {153environment._id = new ObjectId()154const newEnvironment = new this.environmentModel(environment)155newEnvironment.updatedAt = new Date()156newEnvironment.createdAt = new Date()157return await newEnvironment.save()158}159}
160