-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd1fbd6
commit bc1d38a
Showing
14 changed files
with
336 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
apps/orchestration/src/store/entity/__test__/e2e/wallet-group.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { Action, OrganizationEntity, Signature } from '@narval/authz-shared' | ||
import { HttpStatus, INestApplication } from '@nestjs/common' | ||
import { ConfigModule } from '@nestjs/config' | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import request from 'supertest' | ||
import { generateSignature } from '../../../../__test__/fixture/authorization-request.fixture' | ||
import { load } from '../../../../orchestration.config' | ||
import { REQUEST_HEADER_ORG_ID } from '../../../../orchestration.constant' | ||
import { PolicyEngineModule } from '../../../../policy-engine/policy-engine.module' | ||
import { PersistenceModule } from '../../../../shared/module/persistence/persistence.module' | ||
import { TestPrismaService } from '../../../../shared/module/persistence/service/test-prisma.service' | ||
import { QueueModule } from '../../../../shared/module/queue/queue.module' | ||
import { EntityStoreModule } from '../../entity-store.module' | ||
import { OrganizationRepository } from '../../persistence/repository/organization.repository' | ||
import { WalletGroupRepository } from '../../persistence/repository/wallet-group.repository' | ||
|
||
const API_RESOURCE_USER_ENTITY = '/store/wallet-groups' | ||
|
||
describe('Wallet Group Store', () => { | ||
let app: INestApplication | ||
let module: TestingModule | ||
let testPrismaService: TestPrismaService | ||
let orgRepository: OrganizationRepository | ||
let walletGroupRepository: WalletGroupRepository | ||
|
||
const organization: OrganizationEntity = { | ||
uid: 'ac1374c2-fd62-4b6e-bd49-a4afcdcb91cc' | ||
} | ||
|
||
const authentication: Signature = generateSignature() | ||
|
||
const approvals: Signature[] = [generateSignature(), generateSignature()] | ||
|
||
const nonce = 'b6d826b4-72cb-4c14-a6ca-235a2d8e9060' | ||
|
||
const walletId = 'c7eac7d1-7572-4756-b52e-0caebe208364' | ||
|
||
const groupId = '2a1509ad-ea87-422e-bebd-974547cd4fee' | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
load: [load], | ||
isGlobal: true | ||
}), | ||
PersistenceModule, | ||
QueueModule.forRoot(), | ||
PolicyEngineModule, | ||
EntityStoreModule | ||
] | ||
}).compile() | ||
|
||
testPrismaService = module.get<TestPrismaService>(TestPrismaService) | ||
orgRepository = module.get<OrganizationRepository>(OrganizationRepository) | ||
walletGroupRepository = module.get<WalletGroupRepository>(WalletGroupRepository) | ||
|
||
app = module.createNestApplication() | ||
|
||
await app.init() | ||
}) | ||
|
||
afterAll(async () => { | ||
await testPrismaService.truncateAll() | ||
await module.close() | ||
await app.close() | ||
}) | ||
|
||
beforeEach(async () => { | ||
await orgRepository.create(organization.uid) | ||
}) | ||
|
||
afterEach(async () => { | ||
await testPrismaService.truncateAll() | ||
}) | ||
|
||
describe(`POST ${API_RESOURCE_USER_ENTITY}`, () => { | ||
it('assigns a wallet to a group', async () => { | ||
const payload = { | ||
authentication, | ||
approvals, | ||
request: { | ||
action: Action.ASSIGN_WALLET_GROUP, | ||
nonce, | ||
data: { walletId, groupId } | ||
} | ||
} | ||
|
||
const { status, body } = await request(app.getHttpServer()) | ||
.post(API_RESOURCE_USER_ENTITY) | ||
.set(REQUEST_HEADER_ORG_ID, organization.uid) | ||
.send(payload) | ||
|
||
const actualGroup = await walletGroupRepository.findById(groupId) | ||
|
||
expect(body).toEqual({ | ||
data: { | ||
walletId, | ||
groupId | ||
} | ||
}) | ||
expect(status).toEqual(HttpStatus.CREATED) | ||
|
||
expect(actualGroup).toEqual({ | ||
uid: groupId, | ||
wallets: [walletId] | ||
}) | ||
}) | ||
}) | ||
}) |
14 changes: 12 additions & 2 deletions
14
apps/orchestration/src/store/entity/core/service/wallet.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
import { WalletEntity } from '@narval/authz-shared' | ||
import { WalletEntity, WalletGroupMembership } from '@narval/authz-shared' | ||
import { Injectable } from '@nestjs/common' | ||
import { WalletGroupRepository } from '../../persistence/repository/wallet-group.repository' | ||
import { WalletRepository } from '../../persistence/repository/wallet.repository' | ||
|
||
@Injectable() | ||
export class WalletService { | ||
constructor(private walletRepository: WalletRepository) {} | ||
constructor(private walletRepository: WalletRepository, private walletGroupRepository: WalletGroupRepository) {} | ||
|
||
async create(orgId: string, wallet: WalletEntity): Promise<WalletEntity> { | ||
return this.walletRepository.create(orgId, wallet) | ||
} | ||
|
||
async assignGroup(orgId: string, walletId: string, groupId: string): Promise<WalletGroupMembership> { | ||
await this.walletGroupRepository.maybeCreate(orgId, { | ||
uid: groupId, | ||
wallets: [walletId] | ||
}) | ||
|
||
return { groupId, walletId } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
apps/orchestration/src/store/entity/http/rest/controller/wallet-group.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Body, Controller, HttpStatus, Post } from '@nestjs/common' | ||
import { ApiHeader, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger' | ||
import { REQUEST_HEADER_ORG_ID } from '../../../../../orchestration.constant' | ||
import { OrgId } from '../../../../../shared/decorator/org-id.decorator' | ||
import { WalletService } from '../../../core/service/wallet.service' | ||
import { AssignWalletGroupRequestDto } from '../dto/assign-wallet-group-request.dto' | ||
import { AssignWalletGroupResponseDto } from '../dto/assign-wallet-group-response.dto' | ||
|
||
@Controller('/store/wallet-groups') | ||
@ApiTags('Entity Store') | ||
export class WalletGroupController { | ||
constructor(private walletService: WalletService) {} | ||
|
||
@Post() | ||
@ApiOperation({ | ||
summary: "Assigns a wallet to a group. If the group doesn't exist, creates it first." | ||
}) | ||
@ApiHeader({ | ||
name: REQUEST_HEADER_ORG_ID | ||
}) | ||
@ApiResponse({ | ||
status: HttpStatus.CREATED, | ||
type: AssignWalletGroupResponseDto | ||
}) | ||
async assign( | ||
@OrgId() orgId: string, | ||
@Body() body: AssignWalletGroupRequestDto | ||
): Promise<AssignWalletGroupResponseDto> { | ||
const membership = await this.walletService.assignGroup( | ||
orgId, | ||
body.request.data.walletId, | ||
body.request.data.groupId | ||
) | ||
|
||
return new AssignWalletGroupResponseDto({ data: membership }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
apps/orchestration/src/store/entity/http/rest/dto/assign-wallet-group-request.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Action, BaseAdminRequestPayloadDto } from '@narval/authz-shared' | ||
import { ApiProperty } from '@nestjs/swagger' | ||
import { Type } from 'class-transformer' | ||
import { IsDefined, IsEnum, ValidateNested } from 'class-validator' | ||
import { BaseActionDto } from './base-action.dto' | ||
import { WalletGroupMembershipDto } from './wallet-group-membership.dto' | ||
|
||
class AssignWalletGroupActionDto extends BaseActionDto { | ||
@IsEnum(Action) | ||
@IsDefined() | ||
@ApiProperty({ | ||
enum: Object.values(Action), | ||
default: Action.ASSIGN_WALLET_GROUP | ||
}) | ||
action: typeof Action.ASSIGN_WALLET_GROUP | ||
|
||
@IsDefined() | ||
@Type(() => WalletGroupMembershipDto) | ||
@ValidateNested() | ||
@ApiProperty({ | ||
type: WalletGroupMembershipDto | ||
}) | ||
data: WalletGroupMembershipDto | ||
} | ||
|
||
export class AssignWalletGroupRequestDto extends BaseAdminRequestPayloadDto { | ||
@IsDefined() | ||
@Type(() => AssignWalletGroupActionDto) | ||
@ValidateNested() | ||
@ApiProperty({ | ||
type: AssignWalletGroupActionDto | ||
}) | ||
request: AssignWalletGroupActionDto | ||
} |
18 changes: 18 additions & 0 deletions
18
apps/orchestration/src/store/entity/http/rest/dto/assign-wallet-group-response.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiProperty } from '@nestjs/swagger' | ||
import { Type } from 'class-transformer' | ||
import { IsDefined, ValidateNested } from 'class-validator' | ||
import { WalletGroupMembershipDto } from './wallet-group-membership.dto' | ||
|
||
export class AssignWalletGroupResponseDto { | ||
@IsDefined() | ||
@Type(() => WalletGroupMembershipDto) | ||
@ValidateNested() | ||
@ApiProperty({ | ||
type: WalletGroupMembershipDto | ||
}) | ||
data: WalletGroupMembershipDto | ||
|
||
constructor(partial: Partial<AssignWalletGroupResponseDto>) { | ||
Object.assign(this, partial) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
apps/orchestration/src/store/entity/http/rest/dto/wallet-group-membership.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiProperty } from '@nestjs/swagger' | ||
import { IsNotEmpty, IsString } from 'class-validator' | ||
|
||
export class WalletGroupMembershipDto { | ||
@IsString() | ||
@IsNotEmpty() | ||
@ApiProperty() | ||
walletId: string | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
@ApiProperty() | ||
groupId: string | ||
|
||
constructor(partial: Partial<WalletGroupMembershipDto>) { | ||
Object.assign(this, partial) | ||
} | ||
} |
Oops, something went wrong.