-
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.
feat(api-radio-communication-system): introduce TETRA SDS, Call Out a…
…nd Status updates (#629) Co-authored-by: Jasper Herzberg <jhrzbrg@outlook.com>
- Loading branch information
1 parent
3a284b6
commit 9e3efef
Showing
38 changed files
with
1,212 additions
and
26 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
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 @@ | ||
# Radio Communication System | ||
|
||
This module covers the radio communication currently via tetra control. The | ||
`TetraControlService` can send Call Outs, SDS and Status changes. Also, a | ||
webhook handler for incoming status changes is implemented. | ||
|
||
The configuraton for Tetra (credentials for sending and receiving data, and the | ||
API URL of the Tetra server) are stored in the database per tenant. When the | ||
incoming webhook is called, Kordis retrieves the Tetra settings by the provided | ||
API key. The webhook for incoming requests is | ||
`<kordis-api-url>/webhooks/tetra-control?key=<safe-key>`. | ||
|
||
Upon receiving a new status change, a `NewTetraStatusEvent` is emitted. | ||
|
||
## Running unit tests | ||
|
||
Run `nx test api-tetra` 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-tetra', | ||
preset: '../../../jest.preset.js', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../../coverage/libs/api/tetra', | ||
}; |
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,30 @@ | ||
{ | ||
"name": "api-tetra", | ||
"$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/api/tetra/src", | ||
"projectType": "library", | ||
"targets": { | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["libs/api/tetra/**/*.ts"] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/api/tetra/jest.config.ts", | ||
"passWithNoTests": true | ||
}, | ||
"configurations": { | ||
"ci": { | ||
"ci": true, | ||
"codeCoverage": true | ||
} | ||
} | ||
} | ||
}, | ||
"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,2 @@ | ||
export * from './lib/infra/tetra.module'; | ||
export * from './lib/core/event/new-tetra-status.event'; |
95 changes: 95 additions & 0 deletions
95
libs/api/tetra/src/lib/core/command/handle-tetra-control-webhook.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,95 @@ | ||
import { DeepMocked, createMock } from '@golevelup/ts-jest'; | ||
import { EventBus } from '@nestjs/cqrs'; | ||
import { Test } from '@nestjs/testing'; | ||
|
||
import { TetraConfig } from '../entity/tetra-config.entitiy'; | ||
import { NewTetraStatusEvent } from '../event/new-tetra-status.event'; | ||
import { UnknownTetraControlWebhookKeyException } from '../exception/unknown-tetra-control-webhook-key.exception'; | ||
import { TetraControlStatusPayload } from '../model/tetra-control-status-payload.model'; | ||
import { | ||
TETRA_CONFIG_REPOSITORY, | ||
TetraConfigRepository, | ||
} from '../repository/tetra-config.repository'; | ||
import { TETRA_SERVICE, TetraService } from '../service/tetra.service'; | ||
import { HandleTetraControlWebhookHandler } from './handle-tetra-control-webhook.command'; | ||
|
||
describe('HandleTetraControlWebhookHandler', () => { | ||
let handler: HandleTetraControlWebhookHandler; | ||
let tetraConfigRepository: DeepMocked<TetraConfigRepository>; | ||
let eventBus: DeepMocked<EventBus>; | ||
|
||
beforeEach(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
providers: [ | ||
HandleTetraControlWebhookHandler, | ||
{ provide: TETRA_SERVICE, useValue: createMock<TetraService>() }, | ||
{ | ||
provide: TETRA_CONFIG_REPOSITORY, | ||
useValue: createMock<TetraConfigRepository>(), | ||
}, | ||
{ provide: EventBus, useValue: createMock<EventBus>() }, | ||
], | ||
}).compile(); | ||
|
||
handler = moduleRef.get<HandleTetraControlWebhookHandler>( | ||
HandleTetraControlWebhookHandler, | ||
); | ||
tetraConfigRepository = moduleRef.get<DeepMocked<TetraConfigRepository>>( | ||
TETRA_CONFIG_REPOSITORY, | ||
); | ||
eventBus = moduleRef.get<DeepMocked<EventBus>>(EventBus); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should throw UnknownTetraControlWebhookKeyException when key not found', async () => { | ||
tetraConfigRepository.findByWebhookAccessKey.mockResolvedValueOnce(null); | ||
|
||
await expect( | ||
handler.execute({ | ||
payload: { data: { type: 'status' } } as TetraControlStatusPayload, | ||
key: 'test', | ||
}), | ||
).rejects.toThrow(UnknownTetraControlWebhookKeyException); | ||
}); | ||
|
||
it('should dispatch new status', async () => { | ||
tetraConfigRepository.findByWebhookAccessKey.mockResolvedValueOnce({ | ||
orgId: 'orgId', | ||
} as TetraConfig); | ||
const date = new Date('1998-09-16'); | ||
await handler.execute({ | ||
payload: { | ||
data: { type: 'status', status: '1' }, | ||
sender: 'sender', | ||
timestamp: `/Date(${date.getTime()})/`, | ||
} as TetraControlStatusPayload, | ||
key: 'test', | ||
}); | ||
|
||
expect(eventBus.publish).toHaveBeenCalledWith( | ||
new NewTetraStatusEvent('orgId', 'sender', 1, date, 'tetracontrol'), | ||
); | ||
}); | ||
|
||
it('should not dispatch new status when no', async () => { | ||
tetraConfigRepository.findByWebhookAccessKey.mockResolvedValueOnce({ | ||
orgId: 'orgId', | ||
} as TetraConfig); | ||
const date = new Date('1998-09-16'); | ||
await handler.execute({ | ||
payload: { | ||
data: { type: 'status', status: '1' }, | ||
sender: 'sender', | ||
timestamp: `/Date(${date.getTime()})/`, | ||
} as TetraControlStatusPayload, | ||
key: 'test', | ||
}); | ||
|
||
expect(eventBus.publish).toHaveBeenCalledWith( | ||
new NewTetraStatusEvent('orgId', 'sender', 1, date, 'tetracontrol'), | ||
); | ||
}); | ||
}); |
67 changes: 67 additions & 0 deletions
67
libs/api/tetra/src/lib/core/command/handle-tetra-control-webhook.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,67 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { CommandHandler, EventBus, ICommandHandler } from '@nestjs/cqrs'; | ||
|
||
import { NewTetraStatusEvent } from '../event/new-tetra-status.event'; | ||
import { UnhandledTetraControlWebhookTypeException } from '../exception/unhandled-tetra-control-webhook-type.exception'; | ||
import { UnknownTetraControlWebhookKeyException } from '../exception/unknown-tetra-control-webhook-key.exception'; | ||
import { TetraControlStatusPayload } from '../model/tetra-control-status-payload.model'; | ||
import { | ||
TETRA_CONFIG_REPOSITORY, | ||
TetraConfigRepository, | ||
} from '../repository/tetra-config.repository'; | ||
import { TETRA_SERVICE, TetraService } from '../service/tetra.service'; | ||
|
||
export class HandleTetraControlWebhookCommand { | ||
constructor( | ||
readonly payload: TetraControlStatusPayload, | ||
readonly key: string, | ||
) {} | ||
} | ||
|
||
@CommandHandler(HandleTetraControlWebhookCommand) | ||
export class HandleTetraControlWebhookHandler | ||
implements ICommandHandler<HandleTetraControlWebhookCommand> | ||
{ | ||
constructor( | ||
@Inject(TETRA_SERVICE) private readonly tetraService: TetraService, | ||
@Inject(TETRA_CONFIG_REPOSITORY) | ||
private readonly tetraConfigRepository: TetraConfigRepository, | ||
private readonly eventBus: EventBus, | ||
) {} | ||
|
||
async execute({ | ||
payload, | ||
key, | ||
}: HandleTetraControlWebhookCommand): Promise<void> { | ||
const tetraConfig = | ||
await this.tetraConfigRepository.findByWebhookAccessKey(key); | ||
if (!tetraConfig) { | ||
throw new UnknownTetraControlWebhookKeyException(); | ||
} | ||
|
||
switch (payload.data.type) { | ||
case 'status': | ||
this.eventBus.publish( | ||
new NewTetraStatusEvent( | ||
tetraConfig.orgId, | ||
payload.sender, | ||
parseInt(payload.data.status), | ||
this.getSanitizedTimestamp(payload.timestamp), | ||
'tetracontrol', | ||
), | ||
); | ||
break; | ||
default: | ||
throw new UnhandledTetraControlWebhookTypeException(payload.data.type); | ||
} | ||
} | ||
|
||
private getSanitizedTimestamp(timestamp: string): Date { | ||
const parsedTimestamp = /\/Date\((-?\d+)\)\//.exec(timestamp); | ||
if (parsedTimestamp && parsedTimestamp.length > 1) { | ||
return new Date(parseInt(parsedTimestamp[1])); | ||
} | ||
|
||
return new Date(); | ||
} | ||
} |
Oops, something went wrong.