import { BadRequestException , Injectable , Logger , OnModuleInit } from '@nestjs/common' ; import { InjectRepository } from '@nestjs/typeorm' ; import { CrudService , IPagination , RequestContext } from '../core' ; import { DeepPartial , FindConditions , Repository , UpdateResult } from 'typeorm' ; import { Project } from './project.entity' ; import { KubernetesService } from './kubernetes/kubernetes.service' ; import { ClusterService } from './cluster/cluster.service' ; import { CreateProjectDto } from './dto/create-project.dto' ; import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity' ; import { FindOwnProjectsDto } from './dto/find-own-projects.dto' ; import { FindProjectsDto } from './dto/find-projects.dto' ; import { User } from '@ngx-starter-kit/models' ; import { KubeContext } from './interfaces/kube-context' ; @ Injectable ( ) export class ProjectService extends CrudService < Project > implements OnModuleInit { private readonly logger = new Logger ( ProjectService . name ) ; constructor ( @ InjectRepository ( Project ) private readonly projectRepository , private readonly clusterService , private readonly kservice ) { super ( projectRepository ) ; } async onModuleInit ( ) { this . logger . log ( 'Initialization... ProjectService' ) ; } async findAll ( { take , skip , order , ... where } ) { return super . findAll ( { where , take , skip , order } ) ; } async searchAll ( query ) { console . log ( query ) ; return super . findAll ( query ) ; } async hasNamespace ( cluster , namespace ) { return ( await this . projectRepository . count ( { where : { cluster , namespace } } ) ) > 0 ; } async findOwn ( { take , skip , order } , actor ) { const criteria = new FindProjectsDto ( { createdBy : actor , take , skip , order } ) ; console . log ( 'findOwn criteria' , criteria ) ; return super . findAll ( criteria ) ; } async create ( entity , user ) { const cluster = await this . clusterService . findOne ( { name : ( entity as CreateProjectDto ) . clusterName } ) ; if ( cluster === undefined ) { throw new BadRequestException ( 'cluster not found' ) ; } entity . cluster = cluster ; if ( ! entity . username ) { entity . username = user . username ; } if ( ! entity . email ) { entity . email = user . email ; } return await super . create ( entity ) ; } /**
     * FIXME: workaround as @BeforeUpdate don't work with repository.update().
     */ async update ( id , partialEntity , ... options ) { try { const user = RequestContext . currentUser ( ) ; if ( user ) { partialEntity . updatedBy = user ; } return await this . repository . update ( id , partialEntity ) ; } catch ( err ) { throw new BadRequestException ( err ) ; } } }