Search code examples
typescripttypeorm

The function getAllTelephones strange behavior


Objective

I have three tables called clients, contacts, and telephones. I have one route that to return all telephones depending on the client.id or contact.id if I pass one client.id I hope that returns me all telephones from this client and at the same to contacts. But does not working this. I pass the client.id returns me all telephones but if is contact.id returns me an empty array. If I change the if place, returns me the contacts telephones, but client telephones come an empty.

Sequence

telephones.routes.ts

telephonesRouter.get('/:id', telephonesController.index);

TelephonesController.ts

public async index(request: Request, response: Response): Promise<Response> {
    const { id } = request.params;

    try {
      const telephoneService = container.resolve(GetTelephoneService);

      const telephones = await telephoneService.execute({ id });

      return response.status(200).json({ telephones });
    } catch (err) {
      return response.status(400).json({ error: err.message });
    }
  }

GetTelephoneService.ts

import { injectable, inject } from 'tsyringe';

import AppError from '@shared/errors/AppError';

/* Interface */
import IClientsRepository from '@modules/clients/repositories/IClientsRepository';
import IContactsRepository from '@modules/contacts/repositories/IContactsRepository';
import ITelephonesRepository from '../repositories/ITelephonesRepository';
import Telephone from '../infra/typeorm/entities/Telephone';

interface IRequest {
  id: string;
}

@injectable()
class GetTelephoneService {
  constructor(
    @inject('TelephonesRepository')
    private telephonesRepository: ITelephonesRepository,

    @inject('ClientsRepository')
    private clientsRepository: IClientsRepository,

    @inject('ContactsRepository')
    private contactsRepository: IContactsRepository,
  ) {}

  public async execute({ id }: IRequest): Promise<Telephone[] | undefined> {
    const isContact = await this.contactsRepository.findContact(id);

    if (isContact !== undefined) {
      const telephones = await this.telephonesRepository.getAllTelephones(id);
      return telephones;
    }

    const isClient = await this.clientsRepository.findClient(id);

    if (isClient !== undefined) {
      const telephones = await this.telephonesRepository.getAllTelephones(id);
      return telephones;
    }
    if (!isContact && !isClient) {
      throw new AppError("We can't found the id", 400);
    }

    return undefined;
  }
}

export default GetTelephoneService;

TelephonesRepository.ts

public async getAllTelephones(
    owner_id: string,
  ): Promise<Telephone[] | undefined> {
    const getTelephoneClient = await this.ormRepository.find({
      where: { client_id: owner_id },
    });

    console.log('getTelephoneClient', getTelephoneClient);

    const getTelephoneContact = await this.ormRepository.find({
      where: { contact_id: owner_id, client_id: null },
    });

    console.log('getTelephoneContact', getTelephoneContact);

    if (getTelephoneClient !== undefined) {
      return getTelephoneClient;
    }

    if (getTelephoneContact !== undefined) {
      return getTelephoneContact;
    }

    return undefined;
  }

At the TelephonesRepository.ts if I pass getTelephoneClient first if. Will returns the telephones that the client has. But if I pass contact.id the getTelephoneContact come empty. If I invert, invert the behavior.


Solution

  • The solution that I did was at the if's I saw that they will never be undefined they will contain data or will be an empty array because the return always be Telephone[] | undefined. So I verified if the array length is different 0.