File

src/settings/settings.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

getDefaultRole
getDefaultRole()
Returns : any
Async getTuroAdmins
getTuroAdmins()
Returns : unknown
getTuroRoles
getTuroRoles()
Returns : any
Async getTuroUsers
getTuroUsers()
Returns : unknown
Async removeUser
removeUser(userId: string, removedBy: string)
Parameters :
Name Type Optional
userId string No
removedBy string No
Returns : any
Async setTuroUser
setTuroUser(roleId: number, userId: string, updatedBy: any)
Parameters :
Name Type Optional
roleId number No
userId string No
updatedBy any No
Returns : any

Properties

Private TURO_ADMIN_ROLE_NAME
Default value : Roles.TURO_ADMIN
Private TURO_READONLY_ROLE_NAME
Default value : Roles.TURO_READONLY
import { PrismaService } from "../common/prisma/prisma.service";
import { Roles, RoleService, UserService } from "../iam";
import { Injectable } from "@nestjs/common";
import { NotificationService } from "../notification/notification.service";
import {
  TuroSettingsEvents,
  turoSettingsEventsConfig,
} from "./settings.events";
import { EventType } from "../common/events";
import { NotificationEntityType } from "../notification/types/notification.enums";
import { formatURLQueryParams } from "../common/helper";

@Injectable()
export class SettingsService {
  private TURO_ADMIN_ROLE_NAME = Roles.TURO_ADMIN;
  private TURO_READONLY_ROLE_NAME = Roles.TURO_READONLY;

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

  async getTuroUsers() {
    const admins = await this.prismaService.userTuroRole.findMany({
      include: {
        role: true,
      },
    });
    return Promise.all(
      admins.map(async (user) => {
        const { userId, updatedBy, role, updatedAt } = user;

        // Fetch user details and updatedBy details in parallel
        const [userDetails, updatedByDetails] = await Promise.all([
          this.userService.getUserDetails(userId),
          this.userService.getUserDetails(updatedBy),
        ]);

        return {
          ...userDetails,
          role,
          updatedBy: updatedByDetails.name,
          updatedAt,
        };
      }),
    );
  }

  async getTuroAdmins() {
    const adminIds = await this.prismaService.userTuroRole.findMany({
      where: {
        role: { name: this.TURO_ADMIN_ROLE_NAME },
      },
    });
    return adminIds;
  }

  getTuroRoles() {
    return this.prismaService.role.findMany({
      where: {
        name: {
          in: [this.TURO_ADMIN_ROLE_NAME, this.TURO_READONLY_ROLE_NAME],
        },
      },
    });
  }

  async setTuroUser(roleId: number, userId: string, updatedBy: any) {
    const _userId = userId.toLowerCase();
    const _updatedBy = updatedBy.toLowerCase();

    await this.prismaService.userTuroRole.upsert({
      where: {
        userId: _userId,
      },
      create: {
        userId: _userId,
        roleId: roleId,
        updatedBy: _updatedBy,
      },
      update: {
        roleId: roleId,
        updatedBy: _updatedBy,
      },
    });

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

    this.notificationService.triggerNotification(
      turoSettingsEventsConfig,
      0,
      0,
      EventType.SETTINGS,
      NotificationEntityType.SETTINGS,
      TuroSettingsEvents.ASSIGN_USER_TURO_ROLE,
      {
        assigner: assigner.name,
        role: role,
        sendToName: sendToName.name,
        overviewURL: formatURLQueryParams(
          process.env.TURO_BASE_URL,
          `${process.env.TURO_PROJECT_BASE_URL}`,
          {
            overviewSearch: "",
          },
        ),
      },
      [_userId],
    );
  }

  getDefaultRole() {
    return this.prismaService.role.findUnique({
      where: {
        name: this.TURO_ADMIN_ROLE_NAME,
      },
    });
  }

  async removeUser(userId: string, removedBy: string) {
    const _userId = userId.toLowerCase();
    const _removedBy = removedBy.toLowerCase();
    const response = await this.prismaService.userTuroRole.delete({
      where: {
        userId: _userId,
      },
      include: {
        role: true,
      },
    });

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

    this.notificationService.triggerNotification(
      turoSettingsEventsConfig,
      0,
      0,
      EventType.SETTINGS,
      NotificationEntityType.SETTINGS,
      TuroSettingsEvents.DELETE_USER_TURO_ROLE,
      {
        removedBy: updatedBy.name,
        role: response.role,
        sendToName: sendToName.name,
      },
      [_userId],
    );
  }
}

results matching ""

    No results matching ""