import { Injectable } from '@nestjs/common';
import { DataSource, IsNull } from 'typeorm';
import { UserGroup } from './entity/user_group.entity';

@Injectable()
export class UserGroupsRepositoryService {
  constructor(private dataSource: DataSource) {}

  async getUserGroups(account_id: number): Promise<UserGroup[]> {
    const value = await this.dataSource.transaction(async (entityManager) => {
      const userGroupRepo = entityManager.getRepository(UserGroup);

      const userGroups = await userGroupRepo.find({
        // 論理削除されていないレコードを取得
        where: { account_id: account_id, deleted_at: IsNull() },
      });

      return userGroups;
    });
    return value;
  }
}
