-
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
Showing
52 changed files
with
2,226 additions
and
15 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
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 @@ | ||
{ | ||
"extends": ["../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
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,17 @@ | ||
# api-protocol | ||
|
||
This library is responsible for providing the API and business logic for the | ||
protocol feature. The protocol is a collection of entries documenting the | ||
overall events and communication both manually added by the users as well as | ||
automatically added by transactions like starting an operation or logging in a | ||
station. | ||
|
||
Currently the protocol documents events with the following entry types: | ||
|
||
- [`CommunicationMessage`](./src/lib/core/entity/protocol-entries/communication-message.entity.ts): | ||
Documenting the communication between any two units | ||
|
||
## Running unit tests | ||
|
||
Run `nx test api-protocol` to execute the unit tests via | ||
[Jest](https://jestjs.io). |
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,11 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'api-protocol', | ||
preset: '../../../jest.preset.js', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../../coverage/libs/api/protocol', | ||
}; |
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,20 @@ | ||
{ | ||
"name": "api-protocol", | ||
"$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/api/protocol/src", | ||
"projectType": "library", | ||
"targets": { | ||
"lint": { | ||
"executor": "@nx/eslint:lint", | ||
"outputs": ["{options.outputFile}"] | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/api/protocol/jest.config.ts" | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
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 @@ | ||
export * from './lib/protocol.module'; |
88 changes: 88 additions & 0 deletions
88
libs/api/protocol/src/lib/core/command/create-communication-message.command.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,88 @@ | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { EventBus } from '@nestjs/cqrs'; | ||
import { plainToInstance } from 'class-transformer'; | ||
import { after, before } from 'node:test'; | ||
|
||
import { AuthUser } from '@kordis/shared/model'; | ||
|
||
import { UserProducer } from '../entity/partials/producer-partial.entity'; | ||
import { UnknownUnit } from '../entity/partials/unit-partial.entity'; | ||
import { | ||
CommunicationMessage, | ||
CommunicationMessagePayload, | ||
} from '../entity/protocol-entries/communication-message.entity'; | ||
import { ProtocolEntryCreatedEvent } from '../event/protocol-entry-created.event'; | ||
import { ProtocolEntryRepository } from '../repository/protocol-entry.repository'; | ||
import { | ||
CreateCommunicationMessageCommand, | ||
CreateCommunicationMessageHandler, | ||
} from './create-communication-message.command'; | ||
|
||
describe('CreateCommunicationMessageCommand', () => { | ||
let handler: CreateCommunicationMessageHandler; | ||
const repositoryMock = createMock<ProtocolEntryRepository>(); | ||
const eventBusMock = createMock<EventBus>(); | ||
|
||
before(() => { | ||
jest.useFakeTimers({ now: new Date(0) }); | ||
}); | ||
|
||
beforeEach(() => { | ||
handler = new CreateCommunicationMessageHandler( | ||
repositoryMock, | ||
eventBusMock, | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should create communication message protocol entry and emit event', async () => { | ||
const sender = new UnknownUnit(); | ||
sender.name = 'Bob'; | ||
const recipient = new UnknownUnit(); | ||
recipient.name = 'Alice'; | ||
const time = new Date('1913-10-19T00:00:00'); | ||
const message = '🛥️'; | ||
const channel = 'D'; | ||
const authUser = { | ||
id: 'user-id', | ||
organizationId: 'org-id', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
} as unknown as AuthUser; | ||
|
||
const command = new CreateCommunicationMessageCommand( | ||
time, | ||
sender, | ||
recipient, | ||
message, | ||
channel, | ||
authUser, | ||
); | ||
|
||
const expectedCommMsg = plainToInstance(CommunicationMessage, { | ||
sender, | ||
recipient, | ||
time, | ||
searchableText: message, | ||
channel, | ||
payload: plainToInstance(CommunicationMessagePayload, { message }), | ||
producer: plainToInstance(UserProducer, { | ||
userId: authUser.id, | ||
firstName: authUser.firstName, | ||
lastName: authUser.lastName, | ||
}), | ||
orgId: authUser.organizationId, | ||
}); | ||
repositoryMock.create.mockResolvedValueOnce(expectedCommMsg); | ||
|
||
await handler.execute(command); | ||
|
||
expect(repositoryMock.create).toHaveBeenCalledWith(expectedCommMsg); | ||
expect(eventBusMock.publish).toHaveBeenCalledWith( | ||
new ProtocolEntryCreatedEvent('org-id', expectedCommMsg), | ||
); | ||
}); | ||
}); |
96 changes: 96 additions & 0 deletions
96
libs/api/protocol/src/lib/core/command/create-communication-message.command.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,96 @@ | ||
import { Inject, Logger } from '@nestjs/common'; | ||
import { CommandHandler, EventBus, ICommandHandler } from '@nestjs/cqrs'; | ||
|
||
import type { KordisLogger } from '@kordis/api/observability'; | ||
import { AuthUser } from '@kordis/shared/model'; | ||
|
||
import { UserProducer } from '../entity/partials/producer-partial.entity'; | ||
import { | ||
RegisteredUnit, | ||
UnknownUnit, | ||
} from '../entity/partials/unit-partial.entity'; | ||
import { | ||
CommunicationMessage, | ||
CommunicationMessagePayload, | ||
} from '../entity/protocol-entries/communication-message.entity'; | ||
import { ProtocolEntryCreatedEvent } from '../event/protocol-entry-created.event'; | ||
import { | ||
PROTOCOL_ENTRY_REPOSITORY, | ||
ProtocolEntryRepository, | ||
} from '../repository/protocol-entry.repository'; | ||
|
||
export class CreateCommunicationMessageCommand { | ||
constructor( | ||
public readonly time: Date, | ||
public readonly sender: RegisteredUnit | UnknownUnit, | ||
public readonly recipient: RegisteredUnit | UnknownUnit, | ||
public readonly message: string, | ||
public readonly channel: string, | ||
public readonly requestUser: AuthUser, | ||
) {} | ||
} | ||
|
||
@CommandHandler(CreateCommunicationMessageCommand) | ||
export class CreateCommunicationMessageHandler | ||
implements ICommandHandler<CreateCommunicationMessageCommand> | ||
{ | ||
private readonly logger: KordisLogger = new Logger( | ||
CreateCommunicationMessageHandler.name, | ||
); | ||
|
||
constructor( | ||
@Inject(PROTOCOL_ENTRY_REPOSITORY) | ||
private readonly repository: ProtocolEntryRepository, | ||
private readonly eventBus: EventBus, | ||
) {} | ||
|
||
async execute( | ||
command: CreateCommunicationMessageCommand, | ||
): Promise<CommunicationMessage> { | ||
let commMsg = this.createCommunicationMessageFromCommand(command); | ||
|
||
commMsg.validOrThrow(); | ||
|
||
commMsg = await this.repository.create(commMsg); | ||
|
||
this.logger.log('Communication message created', { commMsgId: commMsg.id }); | ||
|
||
this.eventBus.publish( | ||
new ProtocolEntryCreatedEvent( | ||
command.requestUser.organizationId, | ||
commMsg, | ||
), | ||
); | ||
|
||
return commMsg; | ||
} | ||
|
||
private createCommunicationMessageFromCommand({ | ||
time, | ||
sender, | ||
recipient, | ||
message, | ||
channel, | ||
requestUser, | ||
}: CreateCommunicationMessageCommand): CommunicationMessage { | ||
const msgPayload = new CommunicationMessagePayload(); | ||
msgPayload.message = message; | ||
|
||
const producer = new UserProducer(); | ||
producer.userId = requestUser.id; | ||
producer.firstName = requestUser.firstName; | ||
producer.lastName = requestUser.lastName; | ||
|
||
const commMsg = new CommunicationMessage(); | ||
commMsg.time = time; | ||
commMsg.sender = sender; | ||
commMsg.recipient = recipient; | ||
commMsg.payload = msgPayload; | ||
commMsg.producer = producer; | ||
commMsg.channel = channel; | ||
commMsg.searchableText = message; | ||
commMsg.orgId = requestUser.organizationId; | ||
|
||
return commMsg; | ||
} | ||
} |
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,6 @@ | ||
export class Page<T> { | ||
nodes: T[]; | ||
totalEdges: number; | ||
hasNext: boolean; | ||
hasPrevious: boolean; | ||
} |
32 changes: 32 additions & 0 deletions
32
libs/api/protocol/src/lib/core/entity/partials/producer-partial.entity.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,32 @@ | ||
import { AutoMap } from '@automapper/classes'; | ||
import { Field, ObjectType, createUnionType } from '@nestjs/graphql'; | ||
|
||
@ObjectType({ isAbstract: true }) | ||
export abstract class Producer {} | ||
|
||
@ObjectType() | ||
export class UserProducer extends Producer { | ||
@Field() | ||
@AutoMap() | ||
userId: string; | ||
|
||
@Field() | ||
@AutoMap() | ||
firstName: string; | ||
|
||
@Field() | ||
@AutoMap() | ||
lastName: string; | ||
} | ||
|
||
@ObjectType() | ||
export class SystemProducer extends Producer { | ||
@Field() | ||
@AutoMap() | ||
name: string; | ||
} | ||
|
||
export const ProducerUnion = createUnionType({ | ||
name: 'ProducerUnion', | ||
types: () => [SystemProducer, UserProducer] as const, | ||
}); |
28 changes: 28 additions & 0 deletions
28
libs/api/protocol/src/lib/core/entity/partials/unit-partial.entity.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,28 @@ | ||
import { AutoMap } from '@automapper/classes'; | ||
import { Field, InputType, ObjectType, createUnionType } from '@nestjs/graphql'; | ||
|
||
import { UnitViewModel } from '@kordis/api/unit'; | ||
|
||
@ObjectType({ isAbstract: true }) | ||
export abstract class Unit {} | ||
|
||
@ObjectType() | ||
@InputType('RegisteredUnitInput') | ||
export class RegisteredUnit extends Unit { | ||
@Field(() => UnitViewModel) | ||
@AutoMap() | ||
unit: { id: string }; | ||
} | ||
|
||
@ObjectType() | ||
@InputType('UnknownUnitInput') | ||
export class UnknownUnit extends Unit { | ||
@Field() | ||
@AutoMap() | ||
name: string; | ||
} | ||
|
||
export const UnitUnion = createUnionType({ | ||
name: 'UnitUnion', | ||
types: () => [RegisteredUnit, UnknownUnit] as const, | ||
}); |
21 changes: 21 additions & 0 deletions
21
libs/api/protocol/src/lib/core/entity/protocol-entries/communication-message.entity.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,21 @@ | ||
import { AutoMap } from '@automapper/classes'; | ||
import { Field, ObjectType } from '@nestjs/graphql'; | ||
import { IsNotEmpty, IsString } from 'class-validator'; | ||
|
||
import { ProtocolCommunicationEntryBase } from './protocol-entry-base.entity'; | ||
|
||
@ObjectType() | ||
export class CommunicationMessagePayload { | ||
@Field() | ||
@AutoMap() | ||
@IsString() | ||
@IsNotEmpty() | ||
message: string; | ||
} | ||
|
||
@ObjectType() | ||
export class CommunicationMessage extends ProtocolCommunicationEntryBase { | ||
@Field(() => CommunicationMessagePayload) | ||
@AutoMap() | ||
payload: CommunicationMessagePayload; | ||
} |
Oops, something went wrong.