Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/xd 193 #199

Merged
merged 12 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@
},
{
"sourceTag": "domain:facilities",
"onlyDependOnLibsWithTags": ["domain:facilities", "domain:common"]
"onlyDependOnLibsWithTags": [
"domain:facilities",
"domain:common",
"domain:cases"
]
},
{
"sourceTag": "domain:common",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { faker } from '@faker-js/faker';
import { Test, TestingModule } from '@nestjs/testing';
import {
ECasePriority,
ECaseStatus,
ECaseType,
ICaseResponse,
ICreateCaseBody,
} from '@frontend/cases/shared/models';
import { Test, TestingModule } from '@nestjs/testing';
} from 'cases-shared-models';
import { firstValueFrom, of } from 'rxjs';

import { XdCaseService } from '../services/case.service';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ICaseResponse } from '@frontend/cases/shared/models';
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common';
import { ApiAcceptedResponse, ApiCreatedResponse, ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { ICaseResponse } from 'cases-shared-models';
import { ESwaggerTag } from 'common-backend-swagger';
import { Observable } from 'rxjs';

Expand Down
22 changes: 18 additions & 4 deletions libs/cases/backend/case-management/src/lib/dto/body.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ECasePriority, ECaseStatus, ECaseType, ICreateCaseBody, IUpdateCaseBody } from '@frontend/cases/shared/models';
import {
ECasePriority,
ECaseStatus,
ECaseType,
ICreateCaseBody,
IUpdateCaseBody,
} from 'cases-shared-models';
import { Type } from 'class-transformer';
import { IsDate, IsEmail, IsEnum, IsNotEmpty, IsOptional, IsString } from 'class-validator';

Expand Down Expand Up @@ -39,14 +45,14 @@ export class createCaseBodyDto implements ICreateCaseBody {
*/
@IsString()
@IsEnum(ECaseType, {
message: 'Type must be one of these values: ' + Object.values(ECaseType).join(', '),
message: 'type must be one of these values: ' + Object.values(ECaseType).join(', '),
})
type: ECaseType;

@IsString()
@IsNotEmpty()
@IsEnum(ECaseStatus, {
message: 'Status must be one of these values: ' + Object.values(ECaseStatus).join(', '),
message: 'status must be one of these values: ' + Object.values(ECaseStatus).join(', '),
})
status: ECaseStatus;

Expand All @@ -73,7 +79,7 @@ export class createCaseBodyDto implements ICreateCaseBody {
@IsString()
@IsNotEmpty()
@IsEnum(ECasePriority, {
message: 'Priority must be one of these values: ' + Object.values(ECasePriority).join(', '),
message: 'priority must be one of these values: ' + Object.values(ECasePriority).join(', '),
})
priority: ECasePriority;

Expand All @@ -93,6 +99,14 @@ export class createCaseBodyDto implements ICreateCaseBody {
@IsString()
@IsNotEmpty()
eTag: string;

/**
* The asset which the case belongs to
* @example "1702540787672"
*/
@IsString()
@IsNotEmpty()
assetId: string;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ICaseParams } from '@frontend/cases/shared/models';
import { ICaseParams } from 'cases-shared-models';
import { Type } from 'class-transformer';
import { IsNotEmpty, IsNumber } from 'class-validator';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { faker } from '@faker-js/faker';
import {
ECasePriority,
ECaseStatus,
ECaseType,
ICreateCaseBody,
} from '@frontend/cases/shared/models';
import { EPumpStatus } from '@frontend/facilities/shared/models';
import { Test, TestingModule } from '@nestjs/testing';
import { ECasePriority, ECaseStatus, ECaseType, ICreateCaseBody } from 'cases-shared-models';
import { PrismaService } from 'common-backend-prisma';
import { omit } from 'lodash';
import { firstValueFrom } from 'rxjs';

import { XdCaseController } from '../controller/case.controller';
Expand All @@ -33,6 +30,10 @@ describe('CaseController', () => {
const prismaServiceMock = {
onModuleInit: jest.fn(),

asset: {
findUnique: jest.fn(),
},

case: {
create: jest.fn(),
findMany: jest.fn(),
Expand Down Expand Up @@ -65,45 +66,80 @@ describe('CaseController', () => {
const createResult = {
...createCaseModel,
id: faker.number.int(),
assetAssetId: faker.string.uuid(),
modifiedBy: '',
createdAt: new Date(),
updatedAt: new Date(),
};

const spy = jest.spyOn(prisma.case, 'create').mockResolvedValue(createResult);
const findUniqeSpy = jest.spyOn(prisma.asset, 'findUnique').mockResolvedValue({
assetId: createResult.assetId,
description: faker.lorem.sentence(),
name: faker.lorem.sentence(),
typeId: faker.string.uuid(),
createdAt: new Date(),
updatedAt: new Date(),
variables: {},
status: faker.helpers.enumValue(EPumpStatus),
indicatorMsg: faker.string.sample(),
});

// act
const result = await firstValueFrom(service.createCase(createCaseModel));

// assert
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith({ data: createCaseModel });
expect(result).toEqual({ ...createResult, overdue: false });
expect(spy).toHaveBeenCalledWith({
data: { assetId: createCaseModel.assetId, ...omit(createCaseModel, 'assetId') },
});

const expectedResult = {
...omit(createResult, 'assetAssetId'),
assetId: createResult.assetAssetId,
overdue: false,
};

expect(result).toEqual({ ...expectedResult, overdue: false });
});

it('createCase => should create a new case by given data with overdue false', async () => {
const createResult = {
id: faker.number.int(),
...createCaseModel,
assetAssetId: faker.string.uuid(),
modifiedBy: '',
createdAt: new Date(),
updatedAt: new Date(),
};

jest.spyOn(prisma.case, 'create').mockResolvedValue(createResult);

jest.spyOn(prisma.asset, 'findUnique').mockResolvedValue({
assetId: createResult.assetId,
description: faker.lorem.sentence(),
name: faker.lorem.sentence(),
typeId: faker.string.uuid(),
createdAt: new Date(),
updatedAt: new Date(),
variables: {},
status: faker.helpers.enumValue(EPumpStatus),
indicatorMsg: faker.string.sample(),
});

// act
const result = await firstValueFrom(service.createCase(createCaseModel));

// assert
expect(result).toEqual({ ...createResult, overdue: false });
expect(result).toEqual(expect.objectContaining({ overdue: false }));
});

it('getAllCases => should return an array of cases', async () => {
const getResult = {
id: faker.number.int(),
...createCaseModel,
modifiedBy: '',
assetAssetId: faker.string.uuid(),
createdAt: new Date(),
updatedAt: new Date(),
};
Expand All @@ -122,7 +158,11 @@ describe('CaseController', () => {
// assert
expect(spy).toHaveBeenCalledTimes(1);
expect(result).toEqual(
getResultMany.map((item, index) => ({ ...item, overdue: index !== 2 })),
getResultMany.map((item) => ({
...omit(item, 'assetAssetId'),
assetId: item.assetAssetId,
overdue: item.dueDate < new Date(),
})),
);
});

Expand All @@ -131,6 +171,7 @@ describe('CaseController', () => {
id: faker.number.int(),
...createCaseModel,
modifiedBy: '',
assetAssetId: faker.string.uuid(),
createdAt: new Date(),
updatedAt: new Date(),
};
Expand All @@ -140,16 +181,23 @@ describe('CaseController', () => {
// act
const result = await firstValueFrom(service.getCaseById(getResult.id));

const expectedResult = {
...omit(getResult, 'assetAssetId'),
assetId: getResult.assetAssetId,
overdue: false,
};

// assert
expect(spy).toHaveBeenCalledTimes(1);
expect(result).toEqual({ ...getResult, overdue: false });
expect(result).toEqual(expectedResult);
});

it('updateCaseById => should find a case by ID and update it', async () => {
const updateResult = {
id: faker.number.int(),
...createCaseModel,
modifiedBy: '',
assetAssetId: faker.string.uuid(),
createdAt: new Date(),
updatedAt: new Date(),
};
Expand All @@ -159,15 +207,23 @@ describe('CaseController', () => {
// act
const result = await firstValueFrom(service.updateCaseById(updateResult.id, updateResult));

console.log('result', result);

const expectedResult = {
...omit(updateResult, 'assetAssetId'),
assetId: updateResult.assetAssetId,
overdue: false,
};
// assert
expect(spy).toHaveBeenCalledTimes(1);
expect(result).toEqual({ ...updateResult, overdue: false });
expect(result).toEqual(expectedResult);
});

it('deleteCaseById => should find a case by ID and delete it', async () => {
const deleteResult = {
id: faker.number.int(),
...createCaseModel,
assetAssetId: faker.string.uuid(),
modifiedBy: '',
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -178,8 +234,13 @@ describe('CaseController', () => {
// act
const result = await firstValueFrom(service.deleteCaseById(deleteResult.id));

const expectedResult = {
...omit(deleteResult, 'assetAssetId'),
assetId: deleteResult.assetAssetId,
overdue: false,
};
// assert
expect(spy).toHaveBeenCalledTimes(1);
expect(result).toEqual({ ...deleteResult, overdue: false });
expect(result).toEqual(expectedResult);
});
});
Loading