Skip to content

Commit e0bf8d0

Browse files
committed
device - location relation
1 parent 0439ecb commit e0bf8d0

22 files changed

Lines changed: 451 additions & 197 deletions

backend/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@
9393
"^.+\\.(t|j)s$": "ts-jest"
9494
},
9595
"collectCoverageFrom": [
96-
"**/*.(t|j)s"
96+
"**/*.(t|j)s",
97+
"!**/*.(module|entity|dto).(t|j)s",
98+
"!src/core/configuration.ts",
99+
"!src/main.ts"
97100
],
98101
"coverageDirectory": "../coverage",
99102
"testEnvironment": "node"

backend/src/base/location/location.entity.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import {
22
Column,
3-
Entity,
3+
Entity, OneToMany,
44
PrimaryGeneratedColumn,
55
Tree,
66
TreeChildren,
77
TreeParent,
88
} from 'typeorm';
9+
import {DeviceEntity} from "../../inventory/device/device.entity";
910

1011
export enum LocationType {
1112
NONE = 0, // Keine Angabe
@@ -52,4 +53,7 @@ export class LocationEntity {
5253
parentId?: number;
5354
@TreeParent({ onDelete: 'SET NULL' })
5455
parent?: LocationEntity | null;
56+
57+
@OneToMany(() => DeviceEntity, (x) => x.location)
58+
devices: DeviceEntity[];
5559
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class KeycloakGroupCountDto {
2+
count: number;
3+
}

backend/src/core/services/dto/keycloak/keycloak-group-count.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

backend/src/core/services/keycloak.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { AxiosError } from 'axios';
2323
import { KeycloakErrorDto } from './dto/keycloak/keycloak-error.dto';
2424
import { InternalErrorDto } from '../../shared/dto/internal-error.dto';
2525
import { Role } from '../auth/role/role';
26-
import { KeycloakGroupCount } from './dto/keycloak/keycloak-group-count';
26+
import { KeycloakGroupCountDto } from './dto/keycloak/keycloak-group-count.dto';
2727
import { KeycloakGroupDto } from './dto/keycloak/keycloak-group.dto';
2828
import { KeycloakRoleDto } from './dto/keycloak/keycloak-role.dto';
2929

@@ -524,7 +524,7 @@ export class KeycloakService {
524524
public getGroupCount(): Observable<number> {
525525
return from(this.getToken()).pipe(
526526
mergeMap((t) =>
527-
this.httpService.get<KeycloakGroupCount>(
527+
this.httpService.get<KeycloakGroupCountDto>(
528528
`${this.keycloakUrl}/admin/realms/${this.realm}/groups/count`,
529529
this.generateHeader(t),
530530
),

backend/src/inventory/device/device-db.service.spec.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('DeviceDbService', () => {
4747
expect(repoMock.createQueryBuilder).toHaveBeenCalled();
4848
expect(
4949
repoMock.createQueryBuilder().leftJoinAndSelect,
50-
).toHaveBeenCalledTimes(2);
50+
).toHaveBeenCalledTimes(4);
5151
expect(repoMock.createQueryBuilder().limit).toHaveBeenCalledWith(10);
5252
expect(repoMock.createQueryBuilder().offset).toHaveBeenCalledWith(0);
5353
expect(repoMock.createQueryBuilder().orderBy).toHaveBeenCalled();
@@ -65,7 +65,9 @@ describe('DeviceDbService', () => {
6565
where: jest.fn().mockReturnThis(),
6666
});
6767

68-
expect(await service.findAll(0, 10, 1)).toEqual(devices);
68+
expect(await service.findAll(0, 10, 1, undefined, undefined)).toEqual(
69+
devices,
70+
);
6971
expect(repoMock.createQueryBuilder).toHaveBeenCalled();
7072
expect(repoMock.createQueryBuilder().where).toHaveBeenCalled();
7173
expect(repoMock.createQueryBuilder().limit).toHaveBeenCalledWith(10);
@@ -85,7 +87,31 @@ describe('DeviceDbService', () => {
8587
where: jest.fn().mockReturnThis(),
8688
});
8789

88-
expect(await service.findAll(0, 10, undefined, 1)).toEqual(devices);
90+
expect(await service.findAll(0, 10, undefined, 1, undefined)).toEqual(
91+
devices,
92+
);
93+
expect(repoMock.createQueryBuilder).toHaveBeenCalled();
94+
expect(repoMock.createQueryBuilder().where).toHaveBeenCalled();
95+
expect(repoMock.createQueryBuilder().limit).toHaveBeenCalledWith(10);
96+
expect(repoMock.createQueryBuilder().offset).toHaveBeenCalledWith(0);
97+
expect(repoMock.createQueryBuilder().orderBy).toHaveBeenCalled();
98+
expect(repoMock.createQueryBuilder().getMany).toHaveBeenCalled();
99+
});
100+
101+
it('should return all devices by location', async () => {
102+
const devices = [new DeviceEntity(), new DeviceEntity()];
103+
repoMock.createQueryBuilder = jest.fn().mockReturnValue({
104+
leftJoinAndSelect: jest.fn().mockReturnThis(),
105+
limit: jest.fn().mockReturnThis(),
106+
offset: jest.fn().mockReturnThis(),
107+
orderBy: jest.fn().mockReturnThis(),
108+
getMany: jest.fn().mockResolvedValue(devices),
109+
where: jest.fn().mockReturnThis(),
110+
});
111+
112+
expect(await service.findAll(0, 10, undefined, undefined, 1)).toEqual(
113+
devices,
114+
);
89115
expect(repoMock.createQueryBuilder).toHaveBeenCalled();
90116
expect(repoMock.createQueryBuilder().where).toHaveBeenCalled();
91117
expect(repoMock.createQueryBuilder().limit).toHaveBeenCalledWith(10);

backend/src/inventory/device/device-db.service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,26 @@ export class DeviceDbService {
2020
limit?: number,
2121
typeId?: number,
2222
groupId?: number,
23+
locationId?: number,
2324
) {
2425
let query = this.repo
2526
.createQueryBuilder('d')
2627
.limit(limit ?? 100)
2728
.offset(offset ?? 0)
2829
.leftJoinAndSelect('d.type', 'dt')
29-
.leftJoinAndSelect('d.group', 'dg');
30+
.leftJoinAndSelect('d.group', 'dg')
31+
.leftJoinAndSelect('d.location', 'l')
32+
.leftJoinAndSelect('l.parent', 'lp');
3033

3134
if (typeId) {
3235
query = query.where('d.typeId = :typeId', { typeId });
3336
}
3437
if (groupId) {
3538
query = query.where('d.groupId = :groupId', { groupId });
3639
}
40+
if (locationId) {
41+
query = query.where('d.locationId = :locationId', { locationId });
42+
}
3743

3844
return query.orderBy('d.name').getMany();
3945
}
@@ -43,6 +49,8 @@ export class DeviceDbService {
4349
.createQueryBuilder('d')
4450
.leftJoinAndSelect('d.type', 'dt')
4551
.leftJoinAndSelect('d.group', 'dg')
52+
.leftJoinAndSelect('d.location', 'l')
53+
.leftJoinAndSelect('l.parent', 'lp')
4654
.where('d.id = :id', { id });
4755

4856
return query.getOne();

backend/src/inventory/device/device.controller.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('DeviceController', () => {
4141

4242
const result = await controller.getAll({ offset: 0, limit: 10 });
4343

44-
expect(service.findAll).toHaveBeenCalledWith(0, 10, undefined, undefined);
44+
expect(service.findAll).toHaveBeenCalledWith(0, 10, undefined, undefined, undefined);
4545
expect(result).toEqual(mockDevices);
4646
});
4747

backend/src/inventory/device/device.controller.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ export class DeviceController {
3535
public async getAll(
3636
@Query() querys: DeviceGetQueryDto,
3737
): Promise<DeviceDto[]> {
38-
return this.service.findAll(querys.offset, querys.limit, querys.typeId, querys.groupId);
38+
return this.service.findAll(
39+
querys.offset,
40+
querys.limit,
41+
querys.typeId,
42+
querys.groupId,
43+
querys.locationId,
44+
);
3945
}
4046

4147
@Endpoint(EndpointType.GET, {

backend/src/inventory/device/device.entity.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
22
import { DeviceTypeEntity } from '../device-type/device-type.entity';
33
import {DeviceGroupEntity} from "../device-group/device-group.entity";
4+
import {LocationEntity} from "../../base/location/location.entity";
45

56
export enum EquipmentState {
67
ACTIVE = 0,
@@ -67,5 +68,12 @@ export class DeviceEntity {
6768
})
6869
group?: DeviceGroupEntity;
6970

70-
// TODO location
71+
@Column({ nullable: true })
72+
locationId?: number;
73+
@ManyToOne(() => LocationEntity, (x) => x.devices, {
74+
nullable: true,
75+
onDelete: 'SET NULL',
76+
onUpdate: 'CASCADE',
77+
})
78+
location?: LocationEntity;
7179
}

0 commit comments

Comments
 (0)