import * as _ from "lodash" ; import { Inject , Injectable } from "@nestjs/common" ; import { ContactDto } from "./contact.dto" ; import { BaseService } from "../../base.service" ; import { UpdateOptions } from "sequelize" ; import { DbDiToken , Contact } from "../db" ; import { EmailExistsException } from "./exceptions/email-exists.exception" ; import { EntityNotFoundException , InvalidArgumentException } from "../../http/exceptions" ; import { TypeMapperDiToken , TypeMapper } from "../shared" ; @ Injectable ( ) export class ContactService extends BaseService { public constructor ( @ Inject ( DbDiToken . CONTACT_REPOSITORY ) private readonly repository , @ Inject ( TypeMapperDiToken . MAPPER ) private readonly typeMapper ) { super ( ) ; } public async create ( contact ) { const createdContact = ( await this . repository . create ( contact ) ) . toJSON ( ) ; return this . typeMapper . map ( Contact , ContactDto , createdContact ) ; } public async validateEmail ( value ) { if ( _ . isEmpty ( value ) ) { throw new InvalidArgumentException ( "value" , value ) ; } const exists = ! ! ( await this . repository . findOne ( { where : { email : value } } ) ) ; if ( exists ) { throw new EmailExistsException ( ) ; } } public async delete ( id ) { return super . destroyBy ( this . repository , id ) ; } public async multipleDelete ( ids ) { return super . multipleDestroyBy ( this . repository , ids ) ; } public async getById ( id ) { const contact = ( await this . repository . findById ( id ) ) . toJSON ( ) ; return this . typeMapper . map ( Contact , ContactDto , contact ) ; } public async update ( contact ) { const result = await super . updateBy < Contact > ( this . repository , Contact , contact , "id" , true ) as Contact ; return this . typeMapper . map ( Contact , ContactDto , result ) ; } }