41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { BaseCrudService } from '../common/base-crud.service.js';
|
|
import { PrismaService } from '../prisma/prisma.service.js';
|
|
import { getRequestContext } from '../common/context/request-context.js';
|
|
import { AssignResourcesDto, CreateRoleDto, UpdateRoleDto } from './dto.js';
|
|
|
|
@Injectable()
|
|
export class RolesService extends BaseCrudService {
|
|
constructor(prisma: PrismaService) {
|
|
super(prisma, 'role');
|
|
}
|
|
|
|
createRole(dto: CreateRoleDto) {
|
|
return this.create({ ...dto });
|
|
}
|
|
|
|
updateRole(id: string, dto: UpdateRoleDto) {
|
|
return this.update(id, { ...dto });
|
|
}
|
|
|
|
async assignedResources(roleCode: string) {
|
|
const ctx = getRequestContext()!;
|
|
const rows = await this.prisma.client.roleResource.findMany({
|
|
where: { tenantCode: ctx.tenantCode, roleCode },
|
|
select: { resourceCode: true },
|
|
});
|
|
return rows.map((r) => r.resourceCode);
|
|
}
|
|
|
|
async assignResources(roleCode: string, dto: AssignResourcesDto) {
|
|
const ctx = getRequestContext()!;
|
|
await this.prisma.client.roleResource.deleteMany({ where: { tenantCode: ctx.tenantCode, roleCode } });
|
|
if (dto.resourceCodes.length > 0) {
|
|
await this.prisma.client.roleResource.createMany({
|
|
data: dto.resourceCodes.map((resourceCode) => ({ tenantCode: ctx.tenantCode, roleCode, resourceCode })),
|
|
});
|
|
}
|
|
return null;
|
|
}
|
|
}
|