File

src/businessUnit/business-unit.service.ts

Index

Properties
Methods

Constructor

constructor(prismaService: PrismaService, userService: UserService, roleService: RoleService, notificationService: NotificationService)
Parameters :
Name Type Optional
prismaService PrismaService No
userService UserService No
roleService RoleService No
notificationService NotificationService No

Methods

Async bulkDeleteBUUser
bulkDeleteBUUser(userId: string, ids: number[], removedBy: string)
Parameters :
Name Type Optional
userId string No
ids number[] No
removedBy string No
Returns : any
Async deleteBusinessUnitAdmins
deleteBusinessUnitAdmins(id: number, userId: string, removedBy: string)
Parameters :
Name Type Optional
id number No
userId string No
removedBy string No
Returns : any
findAll
findAll()
Returns : Promise<BusinessUnit[]>
findManyById
findManyById(ids)
Parameters :
Name Optional
ids No
Returns : Promise<BusinessUnit[]>
Async getApproversForBusinessUnit
getApproversForBusinessUnit(businessUnitId: number)
Parameters :
Name Type Optional
businessUnitId number No
Returns : Promise<string[]>
getBUdefaultRoles
getBUdefaultRoles()
Returns : any
getBUForUser
getBUForUser(userId: string)
Parameters :
Name Type Optional
userId string No
Returns : any
getBURoles
getBURoles()
Returns : any
Async getBUUsers
getBUUsers(id: number)
Parameters :
Name Type Optional
id number No
Returns : unknown
Async isBUAdmin
isBUAdmin(userId: string)
Parameters :
Name Type Optional
userId string No
Returns : unknown
Async setBusinessUnitUser
setBusinessUnitUser(buId: number, roleId: number, userId: string, updatedBy: string)
Parameters :
Name Type Optional
buId number No
roleId number No
userId string No
updatedBy string No
Returns : any

Properties

Private BU_ADMIN_ROLE_NAME
Default value : Roles.BU_ADMIN
import { Roles, RoleService, UserService } from "../iam";
import { Injectable } from "@nestjs/common";
import { BusinessUnit } from "@prisma/client";
import { PrismaService } from "../common/prisma/prisma.service";
import { NotificationService } from "../notification/notification.service";
import { formatURLQueryParams } from "../common/helper";
import {
  BusinessUnitSettingsEvents,
  businessUnitSettingsEventsConfig,
} from "./business-unit.events";
import { EventType } from "../common/events";
import { NotificationEntityType } from "../notification/types/notification.enums";

@Injectable()
export class BusinessUnitService {
  private BU_ADMIN_ROLE_NAME = Roles.BU_ADMIN;

  constructor(
    private readonly prismaService: PrismaService,
    private readonly userService: UserService,
    private readonly roleService: RoleService,
    private readonly notificationService: NotificationService,
  ) {}

  async getBUUsers(id: number) {
    const admins = await this.prismaService.userBURole.findMany({
      where: {
        bu: {
          id: {
            equals: id,
          },
        },
      },
      include: {
        role: true,
      },
    });
    return await Promise.all(
      admins.map(async (user) => {
        return this.userService.getUserDetails(user.userId).then((response) => {
          return {
            ...response,
            role: user.role,
          };
        });
      }),
    );
  }

  getBUForUser(userId: string) {
    const _userId = userId.toLowerCase();
    return this.prismaService.businessUnit.findMany({
      where: {
        users: {
          some: {
            userId: _userId,
          },
        },
      },
    });
  }

  async deleteBusinessUnitAdmins(
    id: number,
    userId: string,
    removedBy: string,
  ) {
    const _userId = userId.toLowerCase();
    const response = await this.prismaService.userBURole.delete({
      where: {
        userBUIdentifier: {
          userId: _userId,
          buId: id,
        },
      },
      include: {
        role: true,
        bu: true,
      },
    });

    const [updatedBy, sendToName] = await Promise.all([
      this.userService.getUserDetails(removedBy),
      this.userService.getUserDetails(_userId),
    ]);

    this.notificationService.triggerNotification(
      businessUnitSettingsEventsConfig,
      0,
      0,
      EventType.BUSINESS_UNIT,
      NotificationEntityType.BUSINESS_UNIT,
      BusinessUnitSettingsEvents.DELETE_USER_BU_ROLE,
      {
        removedBy: updatedBy.name,
        role: response.role,
        sendToName: sendToName.name,
        bu: response.bu,
      },
      [_userId],
    );
  }

  async setBusinessUnitUser(
    buId: number,
    roleId: number,
    userId: string,
    updatedBy: string,
  ) {
    const _userId = userId.toLowerCase();
    const _updatedBy = updatedBy.toLowerCase();
    const response = await this.prismaService.userBURole.upsert({
      where: {
        userBUIdentifier: {
          userId: _userId,
          buId: buId,
        },
      },
      create: {
        userId: _userId,
        bu: {
          connect: {
            id: buId,
          },
        },
        role: {
          connect: {
            id: roleId,
          },
        },
        updatedBy: _updatedBy,
      },
      update: {
        role: {
          connect: {
            id: roleId,
          },
        },
        updatedBy: _updatedBy,
      },
      include: {
        bu: true,
      },
    });

    const [assigner, sendToName, role] = await Promise.all([
      this.userService.getUserDetails(_updatedBy),
      this.userService.getUserDetails(_userId),
      this.roleService.role(roleId),
    ]);

    this.notificationService.triggerNotification(
      businessUnitSettingsEventsConfig,
      0,
      0,
      EventType.BUSINESS_UNIT,
      NotificationEntityType.BUSINESS_UNIT,
      BusinessUnitSettingsEvents.ASSIGN_USER_BU_ROLE,
      {
        assigner: assigner.name,
        role: role,
        sendToName: sendToName.name,
        bu: response.bu,
        overviewURL: formatURLQueryParams(
          process.env.TURO_BASE_URL,
          `${process.env.TURO_PROJECT_BASE_URL}`,
          {
            overviewSearch: "",
          },
        ),
      },
      [_userId],
    );
  }

  findAll(): Promise<BusinessUnit[]> {
    return this.prismaService.businessUnit.findMany();
  }

  findManyById(ids): Promise<BusinessUnit[]> {
    return this.prismaService.businessUnit.findMany({
      where: {
        id: {
          in: ids,
        },
      },
    });
  }

  async getApproversForBusinessUnit(businessUnitId: number): Promise<string[]> {
    const users = await this.prismaService.userBURole.findMany({
      where: {
        buId: businessUnitId,
        role: {
          name: {
            equals: this.BU_ADMIN_ROLE_NAME,
          },
        },
      },
    });
    return users.map((user) => {
      return user.userId;
    });
  }

  getBUdefaultRoles() {
    return this.prismaService.role.findUnique({
      where: {
        name: this.BU_ADMIN_ROLE_NAME,
      },
    });
  }
  getBURoles() {
    return this.prismaService.role.findMany({
      where: {
        name: {
          in: [this.BU_ADMIN_ROLE_NAME],
        },
      },
    });
  }

  async bulkDeleteBUUser(userId: string, ids: number[], removedBy: string) {
    const _userId = userId.toLowerCase();
    const _removedBy = removedBy.toLowerCase();
    const buRoles = await this.prismaService.userBURole.findMany({
      where: {
        userId: _userId,
        buId: {
          in: ids,
        },
      },
      include: {
        role: true,
        bu: true,
      },
    });

    await this.prismaService.userBURole.deleteMany({
      where: {
        userId: _userId,
        buId: {
          in: ids,
        },
      },
    });

    const [updatedBy, sendToName] = await Promise.all([
      this.userService.getUserDetails(_removedBy),
      this.userService.getUserDetails(_userId),
    ]);

    buRoles.forEach((buRole) => {
      this.notificationService.triggerNotification(
        businessUnitSettingsEventsConfig,
        0,
        0,
        EventType.BUSINESS_UNIT,
        NotificationEntityType.BUSINESS_UNIT,
        BusinessUnitSettingsEvents.DELETE_USER_BU_ROLE,
        {
          removedBy: updatedBy.name,
          role: buRole.role,
          sendToName: sendToName.name,
          bu: buRole.bu,
        },
        [_userId],
      );
    });
  }

  async isBUAdmin(userId: string) {
    const _userId = userId.toLowerCase();
    const response = await this.prismaService.userBURole.count({
      where: {
        userId: _userId,
      },
    });
    return response > 0 ? true : false;
  }
}

results matching ""

    No results matching ""