Skip to content

Commit

Permalink
Refactor entity services to receive the request (#117)
Browse files Browse the repository at this point in the history
* Refactor entity services to receive the request

* Fix authz admin E2E test
  • Loading branch information
wcalderipe authored Feb 19, 2024
1 parent e1620af commit 93baabc
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 60 deletions.
15 changes: 0 additions & 15 deletions apps/authz/src/app/__test__/e2e/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { readFileSync, unlinkSync } from 'fs'
import { mock } from 'jest-mock-extended'
import request from 'supertest'
import { AppModule } from '../../../app/app.module'
import { PersistenceModule } from '../../../shared/module/persistence/persistence.module'
import { TestPrismaService } from '../../../shared/module/persistence/service/test-prisma.service'
import { Organization } from '../../../shared/types/entities.types'
import { Criterion, Then, TimeWindow } from '../../../shared/types/policy.type'
import { load } from '../../app.config'
Expand All @@ -18,7 +16,6 @@ const REQUEST_HEADER_ORG_ID = 'x-org-id'
describe('Admin Endpoints', () => {
let app: INestApplication
let module: TestingModule
let testPrismaService: TestPrismaService

// TODO: Real sigs; these will NOT match the test data.
const authentication: Signature = {
Expand Down Expand Up @@ -54,35 +51,23 @@ describe('Admin Endpoints', () => {
load: [load],
isGlobal: true
}),
PersistenceModule,
AppModule
]
})
.overrideProvider(EntityRepository)
.useValue(entityRepositoryMock)
.compile()

testPrismaService = module.get<TestPrismaService>(TestPrismaService)

app = module.createNestApplication()

await app.init()
})

afterAll(async () => {
await testPrismaService.truncateAll()
await module.close()
await app.close()
})

beforeEach(async () => {
await testPrismaService.getClient().organization.create({ data: org })
})

afterEach(async () => {
await testPrismaService.truncateAll()
})

describe('POST /policies', () => {
it('sets the organization policies', async () => {
const payload = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { AddressBookAccountEntity } from '@narval/authz-shared'
import { AddressBookAccountEntity, CreateAddressBookAccountRequest } from '@narval/authz-shared'
import { Injectable } from '@nestjs/common'
import { AddressBookRepository } from '../../persistence/repository/address-book.repository'

@Injectable()
export class AddressBookService {
constructor(private addressBookRepository: AddressBookRepository) {}

create(orgId: string, account: AddressBookAccountEntity): Promise<AddressBookAccountEntity> {
return this.addressBookRepository.create(orgId, account)
create(orgId: string, data: CreateAddressBookAccountRequest): Promise<AddressBookAccountEntity> {
return this.addressBookRepository.create(orgId, data.request.account)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CredentialRepository } from '../../persistence/repository/credential.re
export class CredentialService {
constructor(private credentialRepository: CredentialRepository) {}

create(orgId: string, request: CreateCredentialRequest): Promise<CredentialEntity> {
return this.credentialRepository.create(orgId, request.request.credential)
create(orgId: string, data: CreateCredentialRequest): Promise<CredentialEntity> {
return this.credentialRepository.create(orgId, data.request.credential)
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
import { CredentialEntity, OrganizationEntity, UserEntity, UserRole } from '@narval/authz-shared'
import {
CreateOrganizationRequest,
CredentialEntity,
OrganizationEntity,
UserEntity,
UserRole
} from '@narval/authz-shared'
import { Injectable } from '@nestjs/common'
import { OrganizationRepository } from '../../persistence/repository/organization.repository'
import { UserService } from './user.service'
import { UserRepository } from '../../persistence/repository/user.repository'

@Injectable()
export class OrganizationService {
constructor(
private orgRepository: OrganizationRepository,
private userService: UserService
private userRepository: UserRepository
) {}

async create(input: { uid: string; rootCredential: CredentialEntity }): Promise<{
async create(input: CreateOrganizationRequest): Promise<{
organization: OrganizationEntity
rootUser: UserEntity
rootCredential: CredentialEntity
}> {
const { uid, rootCredential } = input
const { uid, credential } = input.request.organization

const rootUser: UserEntity = {
uid: input.rootCredential.userId,
uid: credential.userId,
role: UserRole.ROOT
}

await this.userService.create(uid, rootUser, input.rootCredential)
await this.userRepository.create(uid, rootUser, credential)

const organization = await this.orgRepository.create(uid)

return {
organization,
rootUser,
rootCredential
rootCredential: credential
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { AssignUserGroupRequest } from '@narval/authz-shared'
import { Injectable } from '@nestjs/common'
import { UserGroupRepository } from '../../persistence/repository/user-group.repository'

@Injectable()
export class UserGroupService {
constructor(private userGroupRepository: UserGroupRepository) {}

async assign(orgId: string, userId: string, groupId: string): Promise<boolean> {
async assign(orgId: string, input: AssignUserGroupRequest): Promise<boolean> {
const { groupId, userId } = input.request.data
const group = await this.userGroupRepository.findById(groupId)

if (group) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CredentialEntity, UserEntity, UserRole, UserWalletEntity } from '@narval/authz-shared'
import { CreateUserRequest, UserEntity, UserRole, UserWalletEntity } from '@narval/authz-shared'
import { Injectable } from '@nestjs/common'
import { UserWalletRepository } from '../../persistence/repository/user-wallet.repository'
import { UserRepository } from '../../persistence/repository/user.repository'
Expand All @@ -10,8 +10,10 @@ export class UserService {
private userWalletRepository: UserWalletRepository
) {}

create(orgId: string, user: UserEntity, credential?: CredentialEntity): Promise<UserEntity> {
return this.userRepository.create(orgId, user, credential)
create(orgId: string, input: CreateUserRequest): Promise<UserEntity> {
const { user } = input.request

return this.userRepository.create(orgId, user, user.credential)
}

delete(uid: string): Promise<boolean> {
Expand Down
15 changes: 11 additions & 4 deletions apps/orchestration/src/store/entity/core/service/wallet.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { WalletEntity, WalletGroupMemberEntity } from '@narval/authz-shared'
import {
AssignWalletGroupRequest,
RegisterWalletRequest,
WalletEntity,
WalletGroupMemberEntity
} from '@narval/authz-shared'
import { Injectable } from '@nestjs/common'
import { WalletGroupRepository } from '../../persistence/repository/wallet-group.repository'
import { WalletRepository } from '../../persistence/repository/wallet.repository'
Expand All @@ -10,11 +15,13 @@ export class WalletService {
private walletGroupRepository: WalletGroupRepository
) {}

async create(orgId: string, wallet: WalletEntity): Promise<WalletEntity> {
return this.walletRepository.create(orgId, wallet)
async create(orgId: string, input: RegisterWalletRequest): Promise<WalletEntity> {
return this.walletRepository.create(orgId, input.request.wallet)
}

async assignGroup(orgId: string, walletId: string, groupId: string): Promise<WalletGroupMemberEntity> {
async assignGroup(orgId: string, input: AssignWalletGroupRequest): Promise<WalletGroupMemberEntity> {
const { groupId, walletId } = input.request.data

await this.walletGroupRepository.create(orgId, {
uid: groupId,
wallets: [walletId]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class AddressBookController {

@Post()
@ApiOperation({
summary: 'Registers an account in the address book entity.'
summary: 'Registers an account in the address book entity'
})
@ApiHeader({
name: REQUEST_HEADER_ORG_ID
Expand All @@ -27,9 +27,7 @@ export class AddressBookController {
@OrgId() orgId: string,
@Body() body: CreateAddressBookAccountRequestDto
): Promise<CreateAddressBookAccountResponseDto> {
const { account } = body.request

await this.addressBookService.create(orgId, account)
const account = await this.addressBookService.create(orgId, body)

return new CreateAddressBookAccountResponseDto({ account })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class CredentialController {

@Post()
@ApiOperation({
summary: 'Registers a new user credential.'
summary: 'Registers a new user credential'
})
@ApiHeader({
name: REQUEST_HEADER_ORG_ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ export class OrganizationController {

@Post()
@ApiOperation({
summary: 'Creates a new organization and root user.'
summary: 'Creates a new organization and root user'
})
@ApiResponse({
status: HttpStatus.CREATED,
type: CreateOrganizationResponseDto
})
async create(@Body() body: CreateOrganizationRequestDto): Promise<CreateOrganizationResponseDto> {
const { organization, rootCredential, rootUser } = await this.orgService.create({
uid: body.request.organization.uid,
rootCredential: body.request.organization.credential
})
const { organization, rootCredential, rootUser } = await this.orgService.create(body)

return new CreateOrganizationResponseDto({
organization,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class UserGroupController {

@Post()
@ApiOperation({
summary: "Assigns a user to a group. If the group doesn't exist, creates it first."
summary: "Assigns a user to a group. If the group doesn't exist, creates it first"
})
@ApiHeader({
name: REQUEST_HEADER_ORG_ID
Expand All @@ -26,7 +26,7 @@ export class UserGroupController {
async assign(@OrgId() orgId: string, @Body() body: AssignUserGroupRequestDto): Promise<AssignUserGroupResponseDto> {
const { userId, groupId } = body.request.data

await this.userGroupService.assign(orgId, userId, groupId)
await this.userGroupService.assign(orgId, body)

return new AssignUserGroupResponseDto({
data: { userId, groupId }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class UserWalletController {

@Post()
@ApiOperation({
summary: 'Assigns a wallet to a user.'
summary: 'Assigns a wallet to a user'
})
@ApiHeader({
name: REQUEST_HEADER_ORG_ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export class UserController {
type: CreateUserResponseDto
})
async create(@OrgId() orgId: string, @Body() body: CreateUserRequestDto): Promise<CreateUserResponseDto> {
const { uid, role, credential } = body.request.user
const { uid, role } = body.request.user

await this.userService.create(orgId, { uid, role }, credential)
await this.userService.create(orgId, body)

return new CreateUserResponseDto({
user: { uid, role }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class WalletGroupController {

@Post()
@ApiOperation({
summary: "Assigns a wallet to a group. If the group doesn't exist, creates it first."
summary: "Assigns a wallet to a group. If the group doesn't exist, creates it first"
})
@ApiHeader({
name: REQUEST_HEADER_ORG_ID
Expand All @@ -27,11 +27,7 @@ export class WalletGroupController {
@OrgId() orgId: string,
@Body() body: AssignWalletGroupRequestDto
): Promise<AssignWalletGroupResponseDto> {
const membership = await this.walletService.assignGroup(
orgId,
body.request.data.walletId,
body.request.data.groupId
)
const membership = await this.walletService.assignGroup(orgId, body)

return new AssignWalletGroupResponseDto({ data: membership })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class WalletController {

@Post()
@ApiOperation({
summary: 'Registers wallet as an entity.'
summary: 'Registers wallet as an entity'
})
@ApiHeader({
name: REQUEST_HEADER_ORG_ID
Expand All @@ -24,7 +24,7 @@ export class WalletController {
type: RegisterWalletResponseDto
})
async register(@OrgId() orgId: string, @Body() body: RegisterWalletRequestDto): Promise<RegisterWalletResponseDto> {
const wallet = await this.walletService.create(orgId, body.request.wallet)
const wallet = await this.walletService.create(orgId, body)

return new RegisterWalletResponseDto({ wallet })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export class UserRepository {
const entity: UserEntity = await tx.userEntity
.create({
data: {
...user,
uid: user.uid,
role: user.role,
orgId
}
})
Expand Down

0 comments on commit 93baabc

Please sign in to comment.