src/settings/settings.service.ts
Properties |
|
Methods |
|
constructor(prismaService: PrismaService, userService: UserService, roleService: RoleService, notificationService: NotificationService)
|
|||||||||||||||
|
Defined in src/settings/settings.service.ts:16
|
|||||||||||||||
|
Parameters :
|
| getDefaultRole |
getDefaultRole()
|
|
Defined in src/settings/settings.service.ts:118
|
|
Returns :
any
|
| Async getTuroAdmins |
getTuroAdmins()
|
|
Defined in src/settings/settings.service.ts:51
|
|
Returns :
unknown
|
| getTuroRoles |
getTuroRoles()
|
|
Defined in src/settings/settings.service.ts:60
|
|
Returns :
any
|
| Async getTuroUsers |
getTuroUsers()
|
|
Defined in src/settings/settings.service.ts:25
|
|
Returns :
unknown
|
| Async removeUser |
removeUser(userId: string, removedBy: string)
|
|
Defined in src/settings/settings.service.ts:126
|
|
Returns :
any
|
| Async setTuroUser |
setTuroUser(roleId: number, userId: string, updatedBy: any)
|
|
Defined in src/settings/settings.service.ts:70
|
|
Returns :
any
|
| Private TURO_ADMIN_ROLE_NAME |
Default value : Roles.TURO_ADMIN
|
|
Defined in src/settings/settings.service.ts:15
|
| Private TURO_READONLY_ROLE_NAME |
Default value : Roles.TURO_READONLY
|
|
Defined in src/settings/settings.service.ts:16
|
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],
);
}
}