import { HttpStatus, UnprocessableEntityException } from '@nestjs/common';
import { STATUS } from 'src/core/strings/constants/base.constants';
import { In } from 'typeorm';

export async function validateRelation(dataService, id) {
  const haveRelation = await dataService.getOneByOptions({
    where: {
      status: In([STATUS.ACTIVE]),
      bundling_items: {
        id: id,
      },
    },
  });

  if (haveRelation) {
    throw new UnprocessableEntityException({
      statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
      message: `Gagal! data telah digunakan untuk bundling`,
      error: 'Unprocessable Entity',
    });
  }
}
