import { Injectable } from '@nestjs/common' ; import { InjectModel } from '@nestjs/sequelize' ; import { Skill , SkillType } from './skills.model' ; import { GroupedSkills } from './dto/getGroupedSkills.dto' ; import { AddSkillRequest } from './dto/addSkill.dto' ; import { UpdateSkillRequest } from './dto/updateSkill.dto' ; @ Injectable ( ) export class SkillsService { constructor ( @ InjectModel ( Skill ) private skillModel ) { } async add ( input ) { try { await this . skillModel . create ( input ) ; return { ok : true } ; } catch ( error ) { return { ok : false , error } ; } } async delete ( id ) { try { await this . skillModel . destroy ( { where : { id } } ) ; return { ok : true } ; } catch ( error ) { return { ok : false , error } ; } } async update ( { id , ... updateInfo } ) { try { await this . skillModel . update ( updateInfo , { where : { id } } ) ; return { ok : true } ; } catch ( error ) { return { ok : false , error } ; } } async getByType ( type ? ) { try { const skills = await this . skillModel . findAll ( { where : type ? { type } : undefined , order : [ [ 'order' , 'ASC' ] ] , } ) ; return { skills } ; } catch ( error ) { return { error } ; } } async getGroupeds ( ) { try { const total = await this . skillModel . findAll ( { order : [ [ 'order' , 'ASC' ] ] } ) ; const skills = ( total . reduce ( ( groupedSkill , skill ) => { if ( skill . type === SkillType . FRONT_END ) { groupedSkill . front . push ( skill ) ; return groupedSkill ; } if ( skill . type === SkillType . BACK_END ) { groupedSkill . back . push ( skill ) ; return groupedSkill ; } groupedSkill . devops . push ( skill ) ; return groupedSkill ; } , { front : [ ] , back : [ ] , devops : [ ] } as { [ key ] : Skill [ ] ; } ) as unknown ) as GroupedSkills ; return { skills } ; } catch ( error ) { return { error } ; } } }