Skip to content

Commit 8e6edc4

Browse files
authored
Merge pull request #96 from catenax-ng/TRACEFOSS-582-investigation-mock-update
TRACEFOSS-582 - prepare investigation mock for feature implementation
2 parents 9f53fda + dc7b5fa commit 8e6edc4

File tree

24 files changed

+158
-98
lines changed

24 files changed

+158
-98
lines changed

src/app/mocks/services/investigations-mock/investigations.handler.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,29 @@
1818
********************************************************************************/
1919

2020
import { environment } from '@env';
21+
import { InvestigationStatus } from '@shared/model/investigations.model';
2122
import { rest } from 'msw';
2223
import { applyPagination, extractPagination } from '../pagination.helper';
23-
import { buildMockInvestigations } from './investigations.model';
24+
import { buildMockInvestigations, getInvestigationById, InvestigationIdPrefix } from './investigations.model';
2425

2526
export const investigationsHandlers = [
26-
rest.get(`${environment.apiUrl}/investigations/received`, (req, res, ctx) => {
27+
rest.get(`${environment.apiUrl}/investigations`, (req, res, ctx) => {
2728
const pagination = extractPagination(req);
28-
return res(ctx.status(200), ctx.json(applyPagination(buildMockInvestigations(['received']), pagination)));
29-
}),
29+
const status = req.url.searchParams.get('status') ?? '';
3030

31-
rest.get(`${environment.apiUrl}/investigations/queued-and-requested`, (req, res, ctx) => {
32-
const pagination = extractPagination(req);
33-
return res(
34-
ctx.status(200),
35-
ctx.json(applyPagination(buildMockInvestigations(['requested', 'queued']), pagination)),
36-
);
31+
const currentStatus = status.split(',') as InvestigationStatus[];
32+
return res(ctx.status(200), ctx.json(applyPagination(buildMockInvestigations(currentStatus), pagination)));
3733
}),
3834

3935
rest.post(`${environment.apiUrl}/investigations`, (_, res, ctx) => {
40-
const investigations = buildMockInvestigations(['queued']);
36+
return res(ctx.status(200), ctx.json({ id: InvestigationIdPrefix + 1 }));
37+
}),
38+
39+
rest.put(`${environment.apiUrl}/investigations/:investigationId/status`, (req, res, ctx) => {
40+
const { investigationId } = req.params;
41+
const { status } = req.body as Record<string, unknown>;
4142

42-
const response = { investigationId: investigations[0].id };
43-
return res(ctx.status(200), ctx.json(response));
43+
const investigation = getInvestigationById(investigationId as string);
44+
return res(ctx.status(200), ctx.json({ ...investigation, status }));
4445
}),
4546
];

src/app/mocks/services/investigations-mock/investigations.model.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,32 @@
1818
********************************************************************************/
1919

2020
import type { InvestigationResponse } from '@shared/model/investigations.model';
21+
import { InvestigationStatus } from '@shared/model/investigations.model';
22+
import { getRandomAsset } from '../parts-mock/parts.model';
2123

22-
export const buildMockInvestigations = (statuses: string[]): InvestigationResponse[] =>
24+
export const InvestigationIdPrefix = 'id-';
25+
export const buildMockInvestigations = (statuses: InvestigationStatus[]): InvestigationResponse[] =>
2326
new Array(25).fill(null).map((_, index) => {
2427
const status = statuses[index % statuses.length];
2528
return {
26-
id: `id-${index + 1}`,
29+
id: `${InvestigationIdPrefix}${index + 1}`,
2730
description: `Investigation No ${index + 1}`,
28-
createDate: `2022-05-${(index + 1).toString().padStart(2, '0')}T12:34:12`,
2931
status,
32+
createdBy: 'OEM A',
33+
createDate: `2022-05-${(index + 1).toString().padStart(2, '0')}T12:34:12`,
34+
parts: [getRandomAsset().id, getRandomAsset().id, getRandomAsset().id],
3035
};
3136
});
37+
38+
const MockEmptyInvestigation: InvestigationResponse = {
39+
id: `${InvestigationIdPrefix}000`,
40+
description: `Investigation No 000`,
41+
status: InvestigationStatus.CREATED,
42+
createdBy: 'OEM A',
43+
createDate: `2022-05-01T12:34:12`,
44+
parts: [getRandomAsset().id],
45+
};
46+
47+
export const getInvestigationById = (id: string) => {
48+
return [].find(investigation => investigation.id === id) || { ...MockEmptyInvestigation, id };
49+
};

src/app/mocks/services/parts-mock/parts.handler.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export const partsHandlers = [
3434
return res(ctx.status(200), ctx.json(mockAssetsCountriesMap));
3535
}),
3636

37+
rest.get(`${environment.apiUrl}/assets/my`, (req, res, ctx) => {
38+
const pagination = extractPagination(req);
39+
40+
return res(ctx.status(200), ctx.json(applyPagination(mockBmwAssets, pagination)));
41+
}),
42+
3743
rest.get(`${environment.apiUrl}/assets/:partId`, (req, res, ctx) => {
3844
const { partId } = req.params;
3945
const currentAsset = getAssetById(partId as string);
@@ -62,6 +68,10 @@ export const partsHandlersTest = [
6268
return res(ctx.status(200), ctx.json(mockAssetsCountriesMap));
6369
}),
6470

71+
rest.get(`${environment.apiUrl}/assets/my`, (req, res, ctx) => {
72+
return res(ctx.status(200), ctx.json(mockAssets));
73+
}),
74+
6575
rest.get(`${environment.apiUrl}/assets/:partId`, (req, res, ctx) => {
6676
const { partId } = req.params;
6777
const currentAsset = mockAssetList[partId as string];

src/app/mocks/services/parts-mock/parts.model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,8 @@ const MockEmptyPart: PartResponse = {
335335
export const getAssetById = (id: string) => {
336336
return [...mockBmwAssets, ...otherPartsAssets].find(asset => asset.id === id) || { ...MockEmptyPart, id };
337337
};
338+
339+
export const getRandomAsset = () => {
340+
const parts = [...mockBmwAssets, ...otherPartsAssets];
341+
return parts[Math.floor(Math.random() * parts.length)];
342+
};

src/app/modules/core/model/pagination.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ export interface PaginationResponse<T> {
3232
totalItems: number;
3333
content: T[];
3434
}
35+
36+
export const EmptyPagination = { page: 0, pageCount: 0, pageSize: 0, totalItems: 0, content: [] };

src/app/modules/core/pagination/pagination.assembler.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
* SPDX-License-Identifier: Apache-2.0
1818
********************************************************************************/
1919

20-
import { Pagination, PaginationResponse } from '@core/model/pagination.model';
20+
import { EmptyPagination, Pagination, PaginationResponse } from '@core/model/pagination.model';
2121

2222
export class PaginationAssembler {
2323
public static assemblePagination<ResponseItem, Item>(
24-
{ page, pageCount, pageSize, totalItems, content }: PaginationResponse<ResponseItem>,
24+
response: PaginationResponse<ResponseItem>,
2525
contentMapper: (item: ResponseItem) => Item,
2626
): Pagination<Item> {
27+
if (!response || !response.content.length) {
28+
return EmptyPagination;
29+
}
30+
31+
const { page, pageCount, pageSize, totalItems, content } = response;
2732
return { page, pageCount, pageSize, totalItems, content: content.map(contentMapper) };
2833
}
2934
}

src/app/modules/page/dashboard/abstraction/dashboard.facade.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import { Injectable } from '@angular/core';
2121
import { RoleService } from '@core/user/role.service';
2222
import { CountryLocationMap, PartsCoordinates } from '@page/dashboard/presentation/map/map.model';
23-
import { Investigations, InvestigationStatusGroup } from '@shared/model/investigations.model';
23+
import { Investigations, InvestigationStatus } from '@shared/model/investigations.model';
2424
import { View } from '@shared/model/view.model';
2525
import { InvestigationsService } from '@shared/service/investigations.service';
2626
import { PartsService } from '@shared/service/parts.service';
@@ -122,7 +122,7 @@ export class DashboardFacade {
122122
private setInvestigations(): void {
123123
this.investigationSubscription?.unsubscribe();
124124
this.investigationSubscription = this.investigationsService
125-
.getInvestigationsByType(InvestigationStatusGroup.RECEIVED, 0, 5)
125+
.getInvestigationsByType([InvestigationStatus.RECEIVED], 0, 5)
126126
.subscribe({
127127
next: data => this.dashboardState.setInvestigation({ data }),
128128
error: (error: Error) => this.dashboardState.setInvestigation({ error }),

src/app/modules/page/investigations/core/investigations.facade.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
********************************************************************************/
1919

2020
import { Injectable } from '@angular/core';
21-
import { Investigations, InvestigationStatusGroup } from '@shared/model/investigations.model';
21+
import { Investigations, InvestigationStatus } from '@shared/model/investigations.model';
2222
import { View } from '@shared/model/view.model';
2323
import { InvestigationsService } from '@shared/service/investigations.service';
2424
import { Observable, Subscription } from 'rxjs';
@@ -45,7 +45,7 @@ export class InvestigationsFacade {
4545
public setReceivedInvestigation(page = 0, pageSize = 5): void {
4646
this.investigationReceivedSubscription?.unsubscribe();
4747
this.investigationReceivedSubscription = this.investigationsService
48-
.getInvestigationsByType(InvestigationStatusGroup.RECEIVED, page, pageSize)
48+
.getInvestigationsByType([InvestigationStatus.RECEIVED], page, pageSize)
4949
.subscribe({
5050
next: data => (this.investigationsState.investigationsReceived = { data }),
5151
error: (error: Error) => (this.investigationsState.investigationsReceived = { error }),
@@ -55,7 +55,7 @@ export class InvestigationsFacade {
5555
public setQueuedAndRequestedInvestigations(page = 0, pageSize = 5): void {
5656
this.investigationQueuedAndRequestedSubscription?.unsubscribe();
5757
this.investigationQueuedAndRequestedSubscription = this.investigationsService
58-
.getInvestigationsByType(InvestigationStatusGroup.QUEUED_AND_REQUESTED, page, pageSize)
58+
.getInvestigationsByType([InvestigationStatus.CREATED, InvestigationStatus.SENT], page, pageSize)
5959
.subscribe({
6060
next: data => (this.investigationsState.investigationsQueuedAndRequested = { data }),
6161
error: (error: Error) => (this.investigationsState.investigationsQueuedAndRequested = { error }),

src/app/modules/page/parts/core/parts.facade.spec.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ describe('Parts facade', () => {
3939
beforeEach(() => {
4040
partsServiceMok = {
4141
getPart: id => new BehaviorSubject(mockAssetList[id]).pipe(map(part => PartsAssembler.assemblePart(part))),
42-
getParts: (_page, _pageSize, _sorting) => of(mockAssets).pipe(map(parts => PartsAssembler.assembleParts(parts))),
42+
getMyParts: (_page, _pageSize, _sorting) =>
43+
of(mockAssets).pipe(map(parts => PartsAssembler.assembleParts(parts))),
4344
} as PartsService;
4445

4546
partsState = new PartsState();
@@ -48,24 +49,24 @@ describe('Parts facade', () => {
4849

4950
describe('setParts', () => {
5051
it('should set parts if request is successful', async () => {
51-
const serviceSpy = jest.spyOn(partsServiceMok, 'getParts');
52-
partsFacade.setParts(0, 10);
52+
const serviceSpy = jest.spyOn(partsServiceMok, 'getMyParts');
53+
partsFacade.setMyParts(0, 10);
5354

5455
await waitFor(() => expect(serviceSpy).toHaveBeenCalledTimes(1));
5556
await waitFor(() => expect(serviceSpy).toHaveBeenCalledWith(0, 10, null));
5657

57-
const parts = await firstValueFrom(partsState.parts$);
58+
const parts = await firstValueFrom(partsState.myParts$);
5859
await waitFor(() => expect(parts).toEqual({ data: PartsAssembler.assembleParts(mockAssets) }));
5960
});
6061

6162
it('should not set parts if request fails', async () => {
62-
const serviceSpy = jest.spyOn(partsServiceMok, 'getParts');
63+
const serviceSpy = jest.spyOn(partsServiceMok, 'getMyParts');
6364
const spyData = new BehaviorSubject(null).pipe(switchMap(_ => throwError(() => new Error('error'))));
6465

6566
serviceSpy.mockReturnValue(spyData);
66-
partsFacade.setParts(0, 10);
67+
partsFacade.setMyParts(0, 10);
6768

68-
const parts = await firstValueFrom(partsState.parts$);
69+
const parts = await firstValueFrom(partsState.myParts$);
6970
await waitFor(() => expect(parts).toEqual({ error: new Error('error') }));
7071
});
7172
});

src/app/modules/page/parts/core/parts.facade.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,25 @@ import { catchError, delay, map, takeUntil } from 'rxjs/operators';
3030
@Injectable()
3131
export class PartsFacade {
3232
private subjectList: Record<string, Subject<void>> = {};
33-
private partsSubscription: Subscription;
33+
private myPartsSubscription: Subscription;
3434
private readonly unsubscribeTrigger = new Subject<void>();
3535

3636
constructor(private readonly partsService: PartsService, private readonly partsState: PartsState) {}
3737

3838
public get parts$(): Observable<View<Pagination<Part>>> {
39-
return this.partsState.parts$;
39+
return this.partsState.myParts$;
4040
}
4141

42-
public setParts(page = 0, pageSize = 5, sorting: TableHeaderSort = null): void {
43-
this.partsSubscription?.unsubscribe();
44-
this.partsSubscription = this.partsService.getParts(page, pageSize, sorting).subscribe({
45-
next: data => (this.partsState.parts = { data }),
46-
error: error => (this.partsState.parts = { error }),
42+
public setMyParts(page = 0, pageSize = 5, sorting: TableHeaderSort = null): void {
43+
this.myPartsSubscription?.unsubscribe();
44+
this.myPartsSubscription = this.partsService.getMyParts(page, pageSize, sorting).subscribe({
45+
next: data => (this.partsState.myParts = { data }),
46+
error: error => (this.partsState.myParts = { error }),
4747
});
4848
}
4949

5050
public unsubscribeParts(): void {
51-
this.partsSubscription?.unsubscribe();
51+
this.myPartsSubscription?.unsubscribe();
5252
this.unsubscribeTrigger.next();
5353
}
5454

0 commit comments

Comments
 (0)