import { Injectable } from '@nestjs/common' ; import { InjectModel } from '@nestjs/sequelize' ; import { Op } from 'sequelize' ; import { Skill } from '@skills/skills.model' ; import { Project , ProjectType , ProjectWithMethod } from './projects.model' ; import { AddProjectRequest } from './dto/addProject.dto' ; import { UpdateProjectRequest } from './dto/updateProject.dto' ; import { FixRequest } from '../common/dto/inputFix.dto' ; @ Injectable ( ) export class ProjectsService { constructor ( @ InjectModel ( Project ) private projectModel ) { } async add ( { skillIds , ... addInfo } ) { try { const project = ( await this . projectModel . create ( addInfo ) ) as ProjectWithMethod ; if ( skillIds ) { await project . addSkills ( skillIds ) ; } return { ok : true } ; } catch ( error ) { return { ok : false , error } ; } } async update ( { id , deleteSkills , addSkills , ... updateInfo } ) { try { const project = ( await this . projectModel . findOne ( { where : { id } } ) ) as ProjectWithMethod ; if ( ! project ) throw new Error ( 'no project' ) ; await project . update ( updateInfo ) ; if ( deleteSkills ) await project . removeSkill ( deleteSkills ) ; if ( addSkills ) await project . addSkill ( addSkills ) ; return { ok : true } ; } catch ( error ) { return { ok : false , error } ; } } async delete ( id ) { try { const project = ( await this . projectModel . findOne ( { where : { id } , include : [ { model : Skill } ] , } ) ) as ProjectWithMethod ; await project ?. removeSkill ( project . skills ?. map ( ( { id : skillId } ) => skillId ) || [ ] ) ; await project ?. destroy ( ) ; return { ok : true } ; } catch ( error ) { return { ok : false , error } ; } } async fix ( { id , fix } ) { try { await this . projectModel . update ( { picked : fix ? Date . now ( ) : null } , { where : { id } } ) ; return { ok : true } ; } catch ( error ) { return { ok : false , error } ; } } async getById ( id ) { try { const project = await this . projectModel . findOne ( { where : { id } , include : [ { model : Skill } ] , } ) ; return { project } ; } catch ( error ) { return { error } ; } } async getAllByType ( type ) { try { const projects = await this . projectModel . findAll ( { where : { type } , include : [ { model : Skill } ] , order : [ [ 'id' , 'DESC' ] ] , } ) ; return { projects } ; } catch ( error ) { return { error } ; } } async getPickeds ( ) { try { const projects = await this . projectModel . findAll ( { where : { [ Op . not ] : { picked : null } } , include : [ { model : Skill } ] , order : [ [ 'picked' , 'DESC' ] ] , } ) ; return { projects } ; } catch ( error ) { return { error } ; } } }