Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class AdminRoleController {
@RequireAdminPermission('ManageRoles')
async createAdminRole(
@Body(new ZodValidationPipe(CreateAdminRoleBodySchema)) body: CreateAdminRoleBody,
): Promise<AdminRole> {
): Promise<AdminRole[]> {
return this.adminRoleService.create(body)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('adminRoleService', () => {
eventEmitter.emitAsync.mockResolvedValue([])
})

it('creates a role at the next position and returns the created role', async () => {
it('creates a role at the next position and returns the refreshed role list', async () => {
const existingRole = makeAdminRole({
position: 5,
permissions: 4n,
Expand All @@ -39,20 +39,21 @@ describe('adminRoleService', () => {
prisma.adminRole.findFirst.mockResolvedValue(existingRole)
prisma.adminRole.create.mockResolvedValue(createdRole)
prisma.adminRole.findUnique.mockResolvedValue(createdRole)
prisma.adminRole.findMany.mockResolvedValue([createdRole])
prisma.user.findMany.mockResolvedValue([])
prisma.$transaction.mockImplementation(async callback => callback(prisma))

const createBody = makeCreateAdminRoleBody({ name: 'New role' })
const service = new AdminRoleService(prisma, eventEmitter)
const result = await service.create(createBody)

expect(result).toEqual(
expect(result).toEqual([
expect.objectContaining({
id: existingRole.id,
permissions: '0',
position: 6,
}),
)
])
expect(prisma.adminRole.create).toHaveBeenCalledWith({
data: {
name: 'New role',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getAdminRoleMemberCounts,
getRoles,
} from './admin-role-queries.utils'
import { toAdminRole, toAdminRoles } from './admin-role.utils'
import { toAdminRoles } from './admin-role.utils'

@Injectable()
export class AdminRoleService {
Expand All @@ -34,7 +34,7 @@ export class AdminRoleService {
}

@StartActiveSpan()
async create(body: CreateAdminRoleBody): Promise<AdminRole> {
async create(body: CreateAdminRoleBody): Promise<AdminRole[]> {
const span = trace.getActiveSpan()
this.logger.log(`adminRole.create started (name=${body.name})`)

Expand All @@ -54,7 +54,9 @@ export class AdminRoleService {

span?.setAttribute('admin_role.id', createdRole.id)
this.logger.log(`adminRole.create completed (id=${createdRole.id})`)
return toAdminRole(createdRole)
const roles = await getRoles(this.prisma)
span?.setAttribute('admin_role.count', roles.length)
return toAdminRoles(roles)
}

@StartActiveSpan()
Expand Down
6 changes: 3 additions & 3 deletions apps/server/src/resources/admin-role/router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ describe('test adminRoleContract', () => {
describe('createAdminRole', () => {
it('should create a role for authorized users', async () => {
const user = getUserMockInfos(ADMIN_PERMS.MANAGE_ROLES)
const newRole = { id: 'newRole', name: 'New Role' }
const newRoles = [{ id: faker.string.uuid(), name: 'New Role', oidcGroup: '', position: 0, permissions: '0', type: 'managed' }]
const roleData = { name: 'New Role' }

authUserMock.mockResolvedValueOnce(user)
businessCreateRoleMock.mockResolvedValueOnce(newRole)
businessCreateRoleMock.mockResolvedValueOnce(newRoles)

const response = await app.inject()
.post(adminRoleContract.createAdminRole.path)
.body(roleData)
.end()

expect(businessCreateRoleMock).toHaveBeenCalledWith(roleData, expect.any(String))
expect(response.json()).toEqual(newRole)
expect(response.json()).toEqual(newRoles)
expect(response.statusCode).toEqual(201)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/contracts/admin-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const adminRoleContract = contractInstance.router({
path: '',
body: AdminRoleSchema.pick({ name: true }),
responses: {
200: AdminRoleSchema,
201: AdminRoleSchema.array(),
Comment thread
shikanime marked this conversation as resolved.
400: ErrorSchema,
401: ErrorSchema,
403: ErrorSchema,
Expand Down
Loading