Skip to content

Commit

Permalink
Merge pull request #17 from fga-eps-mds/feature/#116-CRUD-metricas
Browse files Browse the repository at this point in the history
Feature/#116 crud metricas
  • Loading branch information
west7 authored Dec 10, 2023
2 parents 8f2e68f + bc2a0a8 commit e187306
Show file tree
Hide file tree
Showing 23 changed files with 991 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { DbModule } from './config/db/db.module';
import { DbService } from './config/db/db.service';
import { CronModule } from './cron/cron.module';
import { IdosoModule } from './idoso/idoso.module';
import { MetricaModule } from './metrica/metrica.module';
import { RotinaModule } from './rotina/rotina.module';
import { ValorMetricaModule } from './valorMetrica/valorMetrica.module';

const ENV = process.env.NODE_ENV;

Expand Down Expand Up @@ -41,6 +43,8 @@ const ENV = process.env.NODE_ENV;
DbModule,
IdosoModule,
RotinaModule,
MetricaModule,
ValorMetricaModule,
CronModule,
],
controllers: [],
Expand Down
11 changes: 11 additions & 0 deletions src/metrica/classes/tipo-metrica.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export enum ECategoriaMetrica {
FREQUENCIA_CARDIACA = 'Frequência Cardíaca',
PRESSAO_SANGUINEA = 'Pressão',
TEMPERATURA = 'Temperatura',
PESO = 'Peso',
GLICEMIA = 'Glicemia',
SATURACAO = 'Saturação',
HORAS_DORMIDAS = 'Horas Dormidas',
ALTURA = 'Altura',
IMC = 'IMC',
}
12 changes: 12 additions & 0 deletions src/metrica/dto/create-metrica-dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IsEnum, IsNotEmpty, IsNumber } from "class-validator";
import { ECategoriaMetrica } from "../classes/tipo-metrica.enum";

export class CreateMetricaDto {
@IsNotEmpty()
@IsNumber()
idIdoso!: number;

@IsNotEmpty()
@IsEnum(ECategoriaMetrica)
categoria?: ECategoriaMetrica;
}
4 changes: 4 additions & 0 deletions src/metrica/dto/update-metrica-dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from "@nestjs/mapped-types";
import { CreateMetricaDto } from "./create-metrica-dto";

export class UpdateMetricaDto extends PartialType(CreateMetricaDto) { }
22 changes: 22 additions & 0 deletions src/metrica/entities/metrica.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { Idoso } from "../../idoso/entities/idoso.entity";
import { ECategoriaMetrica } from "../classes/tipo-metrica.enum";
import { CreateMetricaDto } from "../dto/create-metrica-dto";
import { UpdateMetricaDto } from "../dto/update-metrica-dto";

@Entity({ name: 'metrica' })
export class Metrica {
@PrimaryGeneratedColumn()
id!: number;

@ManyToOne(() => Idoso)
@JoinColumn({ name: 'idIdoso' })
idIdoso!: number;

@Column('enum', { enum: ECategoriaMetrica })
categoria!: ECategoriaMetrica;

constructor(createMetricaDto: CreateMetricaDto | UpdateMetricaDto) {
Object.assign(this, createMetricaDto);
}
}
3 changes: 3 additions & 0 deletions src/metrica/interfaces/metrica-filter.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IMetricaFilter {
idIdoso?: number;
}
132 changes: 132 additions & 0 deletions src/metrica/metrica.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Filtering } from '../shared/decorators/filtrate.decorator';
import { OrderParams, Ordering } from '../shared/decorators/ordenate.decorator';
import {
Pagination,
PaginationParams,
} from '../shared/decorators/paginate.decorator';
import { ECategoriaMetrica } from './classes/tipo-metrica.enum';
import { Metrica } from './entities/metrica.entity';
import { IMetricaFilter } from './interfaces/metrica-filter.interface';
import { MetricaController } from './metrica.controller';
import { MetricaService } from './metrica.service';

describe('MetricaController', () => {
let controller: MetricaController;
let service: MetricaService;

const metricaDto = {
idIdoso: 1,
categoria: ECategoriaMetrica.FREQUENCIA_CARDIACA,
};

const metrica = {
...metricaDto,
id: 1,
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [],
controllers: [MetricaController],
providers: [
{
provide: MetricaService,
useValue: {
create: jest.fn(),
findOne: jest.fn(),
remove: jest.fn(),
update: jest.fn(),
findAll: jest.fn(),
},
},
{
provide: getRepositoryToken(Metrica),
useValue: {},
},
],
}).compile();

controller = module.get<MetricaController>(MetricaController);
service = module.get<MetricaService>(MetricaService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

it('should create IMetrica', async () => {
jest
.spyOn(service, 'create')
.mockReturnValue(Promise.resolve(metrica as Metrica));

const response = await controller.create(metricaDto);
expect(response.data).toEqual(metrica);
expect(response.message).toEqual('Salvo com sucesso!');
});

it('should find IMetrica', async () => {
jest
.spyOn(service, 'findOne')
.mockReturnValue(Promise.resolve(metrica as Metrica));

const response = await controller.findOne({ id: 1 });
expect(response).toEqual(metrica);
});

it('should remove IMetrica', async () => {
jest
.spyOn(service, 'remove')
.mockReturnValue(Promise.resolve(metrica as Metrica));

const response = await controller.remove({ id: 1 });
expect(response.data).toEqual(metrica);
expect(response.message).toEqual('Excluído com sucesso!');
});

it('should update IMetrica', async () => {
jest
.spyOn(service, 'update')
.mockReturnValue(Promise.resolve(metrica as Metrica));

const response = await controller.update({ id: 1 }, { idIdoso: 2 });
expect(response.data).toEqual(metrica);
expect(response.message).toEqual('Atualizado com sucesso!');
});

describe('findAll', () => {
const filter: IMetricaFilter = {
idIdoso: 1,
};
const filtering = new Filtering<Metrica>(JSON.stringify(filter));

const order: OrderParams = {
column: 'id',
dir: 'ASC',
};
const ordering: Ordering = new Ordering(JSON.stringify(order));

const paginate: PaginationParams = {
limit: 10,
offset: 0,
};
const pagination: Pagination = new Pagination(paginate);

it('should findAll Metrica', async () => {
const expected = { data: [metrica], count: 1, pageSize: 1 };

jest.spyOn(service, 'findAll').mockReturnValue(Promise.resolve(expected));

const { data, count, pageSize } = await controller.findAll(
filtering as any,
pagination,
ordering,
);

expect(count).toEqual(1);
expect(pageSize).toEqual(1);
expect(data).toEqual([metrica]);
});
});
});
60 changes: 60 additions & 0 deletions src/metrica/metrica.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
Body,
Controller,
Get,
Param,
Patch,
Post,
Delete,
} from '@nestjs/common';
import { MetricaService } from './metrica.service';
import { Response } from '../shared/interceptors/data-transform.interceptor';
import { Paginate, Pagination } from '../shared/decorators/paginate.decorator';
import { Ordenate, Ordering } from '../shared/decorators/ordenate.decorator';
import { ResponsePaginate } from '../shared/interfaces/response-paginate.interface';
import { Metrica } from './entities/metrica.entity';
import { IdValidator } from '../shared/validators/id.validator';
import { UpdateMetricaDto } from './dto/update-metrica-dto';
import { HttpResponse } from '../shared/classes/http-response';
import { CreateMetricaDto } from './dto/create-metrica-dto';
import { PublicRoute } from '../shared/decorators/public-route.decorator';
import { Filtering, Filtrate } from '../shared/decorators/filtrate.decorator';
import { IMetricaFilter } from './interfaces/metrica-filter.interface';

@Controller('metrica')
export class MetricaController {
constructor(private readonly _service: MetricaService) {}

@Get()
@PublicRoute()
async findAll(
@Filtrate() queryParam: Filtering<IMetricaFilter>,
@Paginate() pagination: Pagination,
@Ordenate() ordering: Ordering,
): Promise<ResponsePaginate<Metrica[]>> {
return this._service.findAll(queryParam.filter, ordering, pagination);
}
@Get(':id')
async findOne(@Param() param: IdValidator): Promise<Metrica> {
return this._service.findOne(param.id);
}
@Patch(':id')
async update(
@Param() param: IdValidator,
@Body() body: UpdateMetricaDto,
): Promise<Response<Metrica>> {
const updated = await this._service.update(param.id, body);
return new HttpResponse<Metrica>(updated).onUpdated();
}
@Post()
async create(@Body() body: CreateMetricaDto) {
const created = await this._service.create(body);
return new HttpResponse<Metrica>(created).onCreated();
}

@Delete(':id')
async remove(@Param() param: IdValidator): Promise<Response<unknown>> {
const deleted = await this._service.remove(param.id);
return new HttpResponse(deleted).onDeleted();
}
}
14 changes: 14 additions & 0 deletions src/metrica/metrica.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Metrica } from './entities/metrica.entity';
import { MetricaController } from './metrica.controller';
import { MetricaService } from './metrica.service';

@Module({
imports: [TypeOrmModule.forFeature([Metrica])],
controllers: [MetricaController],
providers: [MetricaService, Repository],
exports: [MetricaService],
})
export class MetricaModule { }
Loading

0 comments on commit e187306

Please sign in to comment.