import { Injectable } from '@nestjs/common' ; import { InjectConnection , InjectRepository } from '@nestjs/typeorm' ; import { Connection , Repository } from 'typeorm' ; import { ID } from '../../common/shared-types' ; import { User } from '../../entity' ; import { UserDto } from '../../restApi/dtos/user.dto' ; import { patchEntity } from '../helpers/utils/patch-entity' ; @ Injectable ( ) export class UserService { constructor ( @ InjectConnection ( ) private readonly connection ) { } public create ( newUser ) { return this . connection . getRepository ( User ) . save ( newUser ) . then ( user => { return user ; } ) ; } async createOrUpdate ( input ) { let user ; const existing = await this . connection . getRepository ( User ) . findOne ( { where : { identifier : input . identifier , } , } ) ; if ( existing ) { user = patchEntity ( existing , input ) ; } else { user = new User ( input ) ; } return this . connection . getRepository ( User ) . save ( user ) ; } async updateUser ( user ) { return await this . connection . getRepository ( User ) . save ( user ) ; } public getDetailById ( id ) { return this . connection . getRepository ( User ) . findOne ( { relations : [ 'metas' ] , where : { id , } , } ) ; } param ids getUsersDetailByIds ( ids ) { return this . connection . getRepository ( User ) . findByIds ( ids ) ; } param identifier findByIdentifier ( identifier ) { return this . connection . getRepository ( User ) . findOne ( { relations : [ 'metas' ] , where : { identifier , } , } ) ; } }