[TIL] Work-Flow 프로젝트 - Membership, payment (4/4) 23.08.31

이상훈·2023년 9월 3일
0

[내일배움캠프]

목록 보기
62/68
  • 무료 워크스페이스는 보드를 3개까지만 만들 수 있고 멤버 초대를 5명까지만 할 수 있으므로 기존 로직 수정
 //보드 생성
  async CreateBoard(workspaceId: number, name: string, description: string): Promise<Object> {
    const workspace = await this.workspaceService.getWorkspaceDetail(workspaceId);
    console.log(workspace);
    const boardCount = await this.GetBoards(workspaceId);
    const hasMembership = workspace.memberships.length > 0;

    if (boardCount.length >= 3 && !hasMembership)
      throw new HttpException('무료 워크스페이스는 보드를 3개까지만 생성할 수 있습니다.', HttpStatus.BAD_REQUEST);

    const board = await this.boardRepository.insert({ name, description, workspace });
    const findBoard = await this.boardRepository.findOneBy({ id: board.raw.insertId });
    await this.boardColumnRepository.insert({ name: 'Done', sequence: 1, board: findBoard });
    return board;
  }

워크스페이스가 멤버십을 가지고 있는지 확인 후, 멤버십이 없고 보드의 개수가 3개 이상이라면 오류 출력

마찬가지로 워크스페이스 멤버초대 로직도 수정

// 워크스페이스 멤버초대
  async inviteWorkspaceMember(body: InvitationDto, workspaceId: number, userName: string): Promise<IResult> {
    const existWorkspace = await this.workspaceRepository.findOne({
      where: { id: workspaceId },
      relations: ['memberships'],
    });
    const hasMembership = existWorkspace.memberships.length > 0;
    const entityManager = this.workspaceRepository.manager;

    if (!existWorkspace) throw new HttpException('해당 워크스페이스가 존재하지 않습니다.', HttpStatus.NOT_FOUND);

    const { id } = await this.userService.findUserByEmail(body.email);

    const existMember = await this.workspaceMemberRepository.findOne({
      where: { user: { id }, workspace: { id: workspaceId } },
    });

    const countMember = await this.workspaceMemberRepository.find({ where: { workspace: { id: workspaceId } } });

    if (countMember.length >= 5 && !hasMembership)
      throw new HttpException('무료 워크스페이스는 멤버를 5명까지만 초대 가능합니다.', HttpStatus.UNAUTHORIZED);

    if (existMember) throw new HttpException('이미 초대된 유저입니다.', HttpStatus.CONFLICT);
    try {
      await entityManager.transaction(async (transactionEntityManager: EntityManager) => {
        await this.mailService.inviteProjectMail(body.email, userName, existWorkspace.name, workspaceId);

        await transactionEntityManager.save(Workspace_Member, {
          workspace: { id: workspaceId },
          user: { id },
          role: body.role,
        });
      });
      return { result: true };
    } catch (err) {
      console.error(err);
    }
  }

같은 방식으로 멤버십이 없고 멤버가 이미 5명이상 초대되어 있다면 오류를 출력

출력 결과


정상작동 확인

profile
코린이

0개의 댓글