From bc5d782abd8af5264b3f4b8430dbe97ab8c4e387 Mon Sep 17 00:00:00 2001 From: William Calderipe Date: Mon, 19 Feb 2024 13:58:40 +0100 Subject: [PATCH] Refactor entity services to receive the request --- .../core/service/address-book.service.ts | 6 ++--- .../entity/core/service/credential.service.ts | 4 ++-- .../core/service/organization.service.ts | 22 ++++++++++++------- .../entity/core/service/user-group.service.ts | 4 +++- .../store/entity/core/service/user.service.ts | 8 ++++--- .../entity/core/service/wallet.service.ts | 15 +++++++++---- .../controller/address-book.controller.ts | 6 ++--- .../rest/controller/credential.controller.ts | 2 +- .../controller/organization.controller.ts | 7 ++---- .../rest/controller/user-group.controller.ts | 4 ++-- .../rest/controller/user-wallet.controller.ts | 2 +- .../http/rest/controller/user.controller.ts | 4 ++-- .../controller/wallet-group.controller.ts | 8 ++----- .../http/rest/controller/wallet.controller.ts | 4 ++-- .../persistence/repository/user.repository.ts | 3 ++- 15 files changed, 54 insertions(+), 45 deletions(-) diff --git a/apps/orchestration/src/store/entity/core/service/address-book.service.ts b/apps/orchestration/src/store/entity/core/service/address-book.service.ts index cf893230c..1b4d7f96a 100644 --- a/apps/orchestration/src/store/entity/core/service/address-book.service.ts +++ b/apps/orchestration/src/store/entity/core/service/address-book.service.ts @@ -1,4 +1,4 @@ -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' @@ -6,7 +6,7 @@ import { AddressBookRepository } from '../../persistence/repository/address-book export class AddressBookService { constructor(private addressBookRepository: AddressBookRepository) {} - create(orgId: string, account: AddressBookAccountEntity): Promise { - return this.addressBookRepository.create(orgId, account) + create(orgId: string, data: CreateAddressBookAccountRequest): Promise { + return this.addressBookRepository.create(orgId, data.request.account) } } diff --git a/apps/orchestration/src/store/entity/core/service/credential.service.ts b/apps/orchestration/src/store/entity/core/service/credential.service.ts index bd7fdd96f..21bb80015 100644 --- a/apps/orchestration/src/store/entity/core/service/credential.service.ts +++ b/apps/orchestration/src/store/entity/core/service/credential.service.ts @@ -6,7 +6,7 @@ import { CredentialRepository } from '../../persistence/repository/credential.re export class CredentialService { constructor(private credentialRepository: CredentialRepository) {} - create(orgId: string, request: CreateCredentialRequest): Promise { - return this.credentialRepository.create(orgId, request.request.credential) + create(orgId: string, data: CreateCredentialRequest): Promise { + return this.credentialRepository.create(orgId, data.request.credential) } } diff --git a/apps/orchestration/src/store/entity/core/service/organization.service.ts b/apps/orchestration/src/store/entity/core/service/organization.service.ts index 0672df1db..d15b19260 100644 --- a/apps/orchestration/src/store/entity/core/service/organization.service.ts +++ b/apps/orchestration/src/store/entity/core/service/organization.service.ts @@ -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 } } } diff --git a/apps/orchestration/src/store/entity/core/service/user-group.service.ts b/apps/orchestration/src/store/entity/core/service/user-group.service.ts index b6e099d76..40959ad52 100644 --- a/apps/orchestration/src/store/entity/core/service/user-group.service.ts +++ b/apps/orchestration/src/store/entity/core/service/user-group.service.ts @@ -1,3 +1,4 @@ +import { AssignUserGroupRequest } from '@narval/authz-shared' import { Injectable } from '@nestjs/common' import { UserGroupRepository } from '../../persistence/repository/user-group.repository' @@ -5,7 +6,8 @@ import { UserGroupRepository } from '../../persistence/repository/user-group.rep export class UserGroupService { constructor(private userGroupRepository: UserGroupRepository) {} - async assign(orgId: string, userId: string, groupId: string): Promise { + async assign(orgId: string, input: AssignUserGroupRequest): Promise { + const { groupId, userId } = input.request.data const group = await this.userGroupRepository.findById(groupId) if (group) { diff --git a/apps/orchestration/src/store/entity/core/service/user.service.ts b/apps/orchestration/src/store/entity/core/service/user.service.ts index 7a60b353b..745ac46c0 100644 --- a/apps/orchestration/src/store/entity/core/service/user.service.ts +++ b/apps/orchestration/src/store/entity/core/service/user.service.ts @@ -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' @@ -10,8 +10,10 @@ export class UserService { private userWalletRepository: UserWalletRepository ) {} - create(orgId: string, user: UserEntity, credential?: CredentialEntity): Promise { - return this.userRepository.create(orgId, user, credential) + create(orgId: string, input: CreateUserRequest): Promise { + const { user } = input.request + + return this.userRepository.create(orgId, user, user.credential) } delete(uid: string): Promise { diff --git a/apps/orchestration/src/store/entity/core/service/wallet.service.ts b/apps/orchestration/src/store/entity/core/service/wallet.service.ts index 51bc76957..5c1eb06dc 100644 --- a/apps/orchestration/src/store/entity/core/service/wallet.service.ts +++ b/apps/orchestration/src/store/entity/core/service/wallet.service.ts @@ -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' @@ -10,11 +15,13 @@ export class WalletService { private walletGroupRepository: WalletGroupRepository ) {} - async create(orgId: string, wallet: WalletEntity): Promise { - return this.walletRepository.create(orgId, wallet) + async create(orgId: string, input: RegisterWalletRequest): Promise { + return this.walletRepository.create(orgId, input.request.wallet) } - async assignGroup(orgId: string, walletId: string, groupId: string): Promise { + async assignGroup(orgId: string, input: AssignWalletGroupRequest): Promise { + const { groupId, walletId } = input.request.data + await this.walletGroupRepository.create(orgId, { uid: groupId, wallets: [walletId] diff --git a/apps/orchestration/src/store/entity/http/rest/controller/address-book.controller.ts b/apps/orchestration/src/store/entity/http/rest/controller/address-book.controller.ts index fe8483a2b..875b3fb5e 100644 --- a/apps/orchestration/src/store/entity/http/rest/controller/address-book.controller.ts +++ b/apps/orchestration/src/store/entity/http/rest/controller/address-book.controller.ts @@ -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 @@ -27,9 +27,7 @@ export class AddressBookController { @OrgId() orgId: string, @Body() body: CreateAddressBookAccountRequestDto ): Promise { - const { account } = body.request - - await this.addressBookService.create(orgId, account) + const account = await this.addressBookService.create(orgId, body) return new CreateAddressBookAccountResponseDto({ account }) } diff --git a/apps/orchestration/src/store/entity/http/rest/controller/credential.controller.ts b/apps/orchestration/src/store/entity/http/rest/controller/credential.controller.ts index 364fe8ade..b6631b367 100644 --- a/apps/orchestration/src/store/entity/http/rest/controller/credential.controller.ts +++ b/apps/orchestration/src/store/entity/http/rest/controller/credential.controller.ts @@ -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 diff --git a/apps/orchestration/src/store/entity/http/rest/controller/organization.controller.ts b/apps/orchestration/src/store/entity/http/rest/controller/organization.controller.ts index 5e72763d0..ef2eebcc3 100644 --- a/apps/orchestration/src/store/entity/http/rest/controller/organization.controller.ts +++ b/apps/orchestration/src/store/entity/http/rest/controller/organization.controller.ts @@ -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 { - 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, diff --git a/apps/orchestration/src/store/entity/http/rest/controller/user-group.controller.ts b/apps/orchestration/src/store/entity/http/rest/controller/user-group.controller.ts index 771cba295..b3d53e470 100644 --- a/apps/orchestration/src/store/entity/http/rest/controller/user-group.controller.ts +++ b/apps/orchestration/src/store/entity/http/rest/controller/user-group.controller.ts @@ -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 @@ -26,7 +26,7 @@ export class UserGroupController { async assign(@OrgId() orgId: string, @Body() body: AssignUserGroupRequestDto): Promise { 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 } diff --git a/apps/orchestration/src/store/entity/http/rest/controller/user-wallet.controller.ts b/apps/orchestration/src/store/entity/http/rest/controller/user-wallet.controller.ts index 3b4f56be2..b9a1f758f 100644 --- a/apps/orchestration/src/store/entity/http/rest/controller/user-wallet.controller.ts +++ b/apps/orchestration/src/store/entity/http/rest/controller/user-wallet.controller.ts @@ -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 diff --git a/apps/orchestration/src/store/entity/http/rest/controller/user.controller.ts b/apps/orchestration/src/store/entity/http/rest/controller/user.controller.ts index 13d6b196b..84d599c90 100644 --- a/apps/orchestration/src/store/entity/http/rest/controller/user.controller.ts +++ b/apps/orchestration/src/store/entity/http/rest/controller/user.controller.ts @@ -26,9 +26,9 @@ export class UserController { type: CreateUserResponseDto }) async create(@OrgId() orgId: string, @Body() body: CreateUserRequestDto): Promise { - 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 } diff --git a/apps/orchestration/src/store/entity/http/rest/controller/wallet-group.controller.ts b/apps/orchestration/src/store/entity/http/rest/controller/wallet-group.controller.ts index 51fc4f329..d6a5716be 100644 --- a/apps/orchestration/src/store/entity/http/rest/controller/wallet-group.controller.ts +++ b/apps/orchestration/src/store/entity/http/rest/controller/wallet-group.controller.ts @@ -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 @@ -27,11 +27,7 @@ export class WalletGroupController { @OrgId() orgId: string, @Body() body: AssignWalletGroupRequestDto ): Promise { - 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 }) } diff --git a/apps/orchestration/src/store/entity/http/rest/controller/wallet.controller.ts b/apps/orchestration/src/store/entity/http/rest/controller/wallet.controller.ts index 7698e0c58..96f286184 100644 --- a/apps/orchestration/src/store/entity/http/rest/controller/wallet.controller.ts +++ b/apps/orchestration/src/store/entity/http/rest/controller/wallet.controller.ts @@ -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 @@ -24,7 +24,7 @@ export class WalletController { type: RegisterWalletResponseDto }) async register(@OrgId() orgId: string, @Body() body: RegisterWalletRequestDto): Promise { - const wallet = await this.walletService.create(orgId, body.request.wallet) + const wallet = await this.walletService.create(orgId, body) return new RegisterWalletResponseDto({ wallet }) } diff --git a/apps/orchestration/src/store/entity/persistence/repository/user.repository.ts b/apps/orchestration/src/store/entity/persistence/repository/user.repository.ts index 38da706ce..d156a7adc 100644 --- a/apps/orchestration/src/store/entity/persistence/repository/user.repository.ts +++ b/apps/orchestration/src/store/entity/persistence/repository/user.repository.ts @@ -15,7 +15,8 @@ export class UserRepository { const entity: UserEntity = await tx.userEntity .create({ data: { - ...user, + uid: user.uid, + role: user.role, orgId } })