Skip to content

Commit

Permalink
Merge pull request #18 from fga-eps-mds/hotfix/#225-CardMetrica-nao-a…
Browse files Browse the repository at this point in the history
…tualiza

Hotfix/#225 card metrica nao atualiza
  • Loading branch information
HenriqueAmorim20 authored Dec 13, 2023
2 parents e187306 + 214f89b commit 6af7f14
Show file tree
Hide file tree
Showing 19 changed files with 266 additions and 141 deletions.
19 changes: 10 additions & 9 deletions src/metrica/classes/tipo-metrica.enum.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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',
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',
HIDRATACAO = 'Hidratação',
}
28 changes: 19 additions & 9 deletions src/metrica/dto/create-metrica-dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { IsEnum, IsNotEmpty, IsNumber } from "class-validator";
import { ECategoriaMetrica } from "../classes/tipo-metrica.enum";
import {
IsEnum,
IsNotEmpty,
IsNumber,
IsOptional,
IsString,
} from 'class-validator';
import { ECategoriaMetrica } from '../classes/tipo-metrica.enum';

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

@IsNotEmpty()
@IsEnum(ECategoriaMetrica)
categoria?: ECategoriaMetrica;
}
@IsNotEmpty()
@IsEnum(ECategoriaMetrica)
categoria?: ECategoriaMetrica;

@IsOptional()
@IsString()
valorMaximo?: string;
}
6 changes: 3 additions & 3 deletions src/metrica/dto/update-metrica-dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PartialType } from "@nestjs/mapped-types";
import { CreateMetricaDto } from "./create-metrica-dto";
import { PartialType } from '@nestjs/mapped-types';
import { CreateMetricaDto } from './create-metrica-dto';

export class UpdateMetricaDto extends PartialType(CreateMetricaDto) { }
export class UpdateMetricaDto extends PartialType(CreateMetricaDto) {}
46 changes: 30 additions & 16 deletions src/metrica/entities/metrica.entity.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
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";
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Idoso } from '../../idoso/entities/idoso.entity';
import { ValorMetrica } from '../../valorMetrica/entities/valorMetrica.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;
@PrimaryGeneratedColumn()
id!: number;

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

@Column('enum', { enum: ECategoriaMetrica })
categoria!: ECategoriaMetrica;
@OneToMany(() => ValorMetrica, (valorMetrica) => valorMetrica.idMetrica)
valoresMetricas!: ValorMetrica[];

constructor(createMetricaDto: CreateMetricaDto | UpdateMetricaDto) {
Object.assign(this, createMetricaDto);
}
}
@Column('enum', { enum: ECategoriaMetrica })
categoria!: ECategoriaMetrica;

@Column('varchar', { length: 20, nullable: true })
valorMaximo?: string;

constructor(createMetricaDto: CreateMetricaDto | UpdateMetricaDto) {
Object.assign(this, createMetricaDto);
}
}
1 change: 1 addition & 0 deletions src/metrica/metrica.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('MetricaController', () => {
const metricaDto = {
idIdoso: 1,
categoria: ECategoriaMetrica.FREQUENCIA_CARDIACA,
valoresMetricas: [],
};

const metrica = {
Expand Down
24 changes: 15 additions & 9 deletions src/metrica/metrica.controller.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import {
Body,
Controller,
Delete,
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 { HttpResponse } from '../shared/classes/http-response';
import { Filtering, Filtrate } from '../shared/decorators/filtrate.decorator';
import { Ordenate, Ordering } from '../shared/decorators/ordenate.decorator';
import { Paginate, Pagination } from '../shared/decorators/paginate.decorator';
import { PublicRoute } from '../shared/decorators/public-route.decorator';
import { Response } from '../shared/interceptors/data-transform.interceptor';
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 { UpdateMetricaDto } from './dto/update-metrica-dto';
import { Metrica } from './entities/metrica.entity';
import { IMetricaFilter } from './interfaces/metrica-filter.interface';
import { MetricaService } from './metrica.service';

@Controller('metrica')
export class MetricaController {
Expand All @@ -38,6 +38,12 @@ export class MetricaController {
async findOne(@Param() param: IdValidator): Promise<Metrica> {
return this._service.findOne(param.id);
}

@Get('soma-hidratacao/:id')
async getSomaHidratacao(@Param() param: IdValidator): Promise<number> {
return this._service.getSomaHidratacao(param.id);
}

@Patch(':id')
async update(
@Param() param: IdValidator,
Expand Down
10 changes: 5 additions & 5 deletions src/metrica/metrica.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { MetricaController } from './metrica.controller';
import { MetricaService } from './metrica.service';

@Module({
imports: [TypeOrmModule.forFeature([Metrica])],
controllers: [MetricaController],
providers: [MetricaService, Repository],
exports: [MetricaService],
imports: [TypeOrmModule.forFeature([Metrica])],
controllers: [MetricaController],
providers: [MetricaService, Repository],
exports: [MetricaService],
})
export class MetricaModule { }
export class MetricaModule {}
29 changes: 26 additions & 3 deletions src/metrica/metrica.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Pagination } from '../shared/decorators/paginate.decorator';
import { getWhereClauseNumber } from '../shared/helpers/sql-query-helper';
import { ResponsePaginate } from '../shared/interfaces/response-paginate.interface';
import { CreateMetricaDto } from './dto/create-metrica-dto';
import { Metrica } from './entities/metrica.entity';
import { UpdateMetricaDto } from './dto/update-metrica-dto';
import { Metrica } from './entities/metrica.entity';
import { IMetricaFilter } from './interfaces/metrica-filter.interface';

@Injectable()
Expand All @@ -23,8 +23,31 @@ export class MetricaService {
}

async findOne(id: number) {
const metrica = await this._repository.findOneOrFail({ where: { id } });
return metrica;
return this._repository.findOneOrFail({ where: { id } });
}

async getSomaHidratacao(id: number) {
const start = new Date();
start.setUTCHours(0, 0, 0);
const startString = start.toISOString();

const end = new Date();
end.setUTCHours(23, 59, 59);
const endString = end.toISOString();

const result = (await this._repository
.createQueryBuilder('metrica')
.leftJoinAndSelect('metrica.valoresMetricas', 'valoresMetricas')
.select('valoresMetricas.valor AS valor')
.where('metrica.id = :id', { id })
.andWhere(
`"valoresMetricas"."dataHora"::date BETWEEN '${startString}'::date AND '${endString}'::date`,
)
.getRawMany()) as { valor: string }[];

return result.reduce((accumulator, valorMetrica) => {
return accumulator + Number(valorMetrica.valor);
}, 0);
}

async update(id: number, body: UpdateMetricaDto): Promise<Metrica> {
Expand Down
33 changes: 20 additions & 13 deletions src/migration/1701549606356-CreateTableMetrica.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { MigrationInterface, QueryRunner } from 'typeorm';

export class CreateTableMetrica1701549606356 implements MigrationInterface {
name = 'CreateTableMetrica1701549606356'
name = 'CreateTableMetrica1701549606356';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TYPE "public"."metrica_categoria_enum" AS ENUM('Frequência Cardíaca', 'Pressão', 'Temperatura', 'Peso', 'Glicemia', 'Saturação')`);
await queryRunner.query(`CREATE TABLE "metrica" ("id" SERIAL NOT NULL, "categoria" "public"."metrica_categoria_enum" NOT NULL, "idIdoso" integer, CONSTRAINT "PK_37eda6d5162b9305738916e1712" PRIMARY KEY ("id"))`);
await queryRunner.query(`ALTER TABLE "metrica" ADD CONSTRAINT "FK_574d03daab9657eaa6cc7d5d726" FOREIGN KEY ("idIdoso") REFERENCES "idoso"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "metrica" DROP CONSTRAINT "FK_574d03daab9657eaa6cc7d5d726"`);
await queryRunner.query(`DROP TABLE "metrica"`);
await queryRunner.query(`DROP TYPE "public"."metrica_categoria_enum"`);
}
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TYPE "public"."metrica_categoria_enum" AS ENUM('Frequência Cardíaca', 'Pressão', 'Temperatura', 'Peso', 'Glicemia', 'Saturação')`,
);
await queryRunner.query(
`CREATE TABLE "metrica" ("id" SERIAL NOT NULL, "categoria" "public"."metrica_categoria_enum" NOT NULL, "idIdoso" integer, CONSTRAINT "PK_37eda6d5162b9305738916e1712" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`ALTER TABLE "metrica" ADD CONSTRAINT "FK_574d03daab9657eaa6cc7d5d726" FOREIGN KEY ("idIdoso") REFERENCES "idoso"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "metrica" DROP CONSTRAINT "FK_574d03daab9657eaa6cc7d5d726"`,
);
await queryRunner.query(`DROP TABLE "metrica"`);
await queryRunner.query(`DROP TYPE "public"."metrica_categoria_enum"`);
}
}
31 changes: 19 additions & 12 deletions src/migration/1701559083238-CreateTableValorMetrica.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { MigrationInterface, QueryRunner } from 'typeorm';

export class CreateTableValorMetrica1701559083238 implements MigrationInterface {
name = 'CreateTableValorMetrica1701559083238'
export class CreateTableValorMetrica1701559083238
implements MigrationInterface
{
name = 'CreateTableValorMetrica1701559083238';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "valorMetrica" ("id" SERIAL NOT NULL, "valor" double precision NOT NULL, "dataHora" TIMESTAMP NOT NULL, "idMetrica" integer, CONSTRAINT "PK_9e8ca5a7cfc9169850aa35d4fe2" PRIMARY KEY ("id"))`);
await queryRunner.query(`ALTER TABLE "valorMetrica" ADD CONSTRAINT "FK_80f274382900aafd3a667034d46" FOREIGN KEY ("idMetrica") REFERENCES "metrica"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "valorMetrica" DROP CONSTRAINT "FK_80f274382900aafd3a667034d46"`);
await queryRunner.query(`DROP TABLE "valorMetrica"`);
}
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "valorMetrica" ("id" SERIAL NOT NULL, "valor" double precision NOT NULL, "dataHora" TIMESTAMP NOT NULL, "idMetrica" integer, CONSTRAINT "PK_9e8ca5a7cfc9169850aa35d4fe2" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`ALTER TABLE "valorMetrica" ADD CONSTRAINT "FK_80f274382900aafd3a667034d46" FOREIGN KEY ("idMetrica") REFERENCES "metrica"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "valorMetrica" DROP CONSTRAINT "FK_80f274382900aafd3a667034d46"`,
);
await queryRunner.query(`DROP TABLE "valorMetrica"`);
}
}
53 changes: 34 additions & 19 deletions src/migration/1701996752336-AlterTableValorMetrica.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AlterTableValorMetrica1701996752336 implements MigrationInterface {
name = 'AlterTableValorMetrica1701996752336'
name = 'AlterTableValorMetrica1701996752336';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TYPE "public"."metrica_categoria_enum" RENAME TO "metrica_categoria_enum_old"`);
await queryRunner.query(`CREATE TYPE "public"."metrica_categoria_enum" AS ENUM('Frequência Cardíaca', 'Pressão', 'Temperatura', 'Peso', 'Glicemia', 'Saturação', 'Horas Dormidas', 'Altura', 'IMC')`);
await queryRunner.query(`ALTER TABLE "metrica" ALTER COLUMN "categoria" TYPE "public"."metrica_categoria_enum" USING "categoria"::"text"::"public"."metrica_categoria_enum"`);
await queryRunner.query(`DROP TYPE "public"."metrica_categoria_enum_old"`);
await queryRunner.query(`ALTER TABLE "valorMetrica" DROP COLUMN "valor"`);
await queryRunner.query(`ALTER TABLE "valorMetrica" ADD "valor" character varying(10) NOT NULL`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "valorMetrica" DROP COLUMN "valor"`);
await queryRunner.query(`ALTER TABLE "valorMetrica" ADD "valor" double precision NOT NULL`);
await queryRunner.query(`CREATE TYPE "public"."metrica_categoria_enum_old" AS ENUM('Frequência Cardíaca', 'Pressão', 'Temperatura', 'Peso', 'Glicemia', 'Saturação')`);
await queryRunner.query(`ALTER TABLE "metrica" ALTER COLUMN "categoria" TYPE "public"."metrica_categoria_enum_old" USING "categoria"::"text"::"public"."metrica_categoria_enum_old"`);
await queryRunner.query(`DROP TYPE "public"."metrica_categoria_enum"`);
await queryRunner.query(`ALTER TYPE "public"."metrica_categoria_enum_old" RENAME TO "metrica_categoria_enum"`);
}
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TYPE "public"."metrica_categoria_enum" RENAME TO "metrica_categoria_enum_old"`,
);
await queryRunner.query(
`CREATE TYPE "public"."metrica_categoria_enum" AS ENUM('Frequência Cardíaca', 'Pressão', 'Temperatura', 'Peso', 'Glicemia', 'Saturação', 'Horas Dormidas', 'Altura', 'IMC')`,
);
await queryRunner.query(
`ALTER TABLE "metrica" ALTER COLUMN "categoria" TYPE "public"."metrica_categoria_enum" USING "categoria"::"text"::"public"."metrica_categoria_enum"`,
);
await queryRunner.query(`DROP TYPE "public"."metrica_categoria_enum_old"`);
await queryRunner.query(`ALTER TABLE "valorMetrica" DROP COLUMN "valor"`);
await queryRunner.query(
`ALTER TABLE "valorMetrica" ADD "valor" character varying(10) NOT NULL`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "valorMetrica" DROP COLUMN "valor"`);
await queryRunner.query(
`ALTER TABLE "valorMetrica" ADD "valor" double precision NOT NULL`,
);
await queryRunner.query(
`CREATE TYPE "public"."metrica_categoria_enum_old" AS ENUM('Frequência Cardíaca', 'Pressão', 'Temperatura', 'Peso', 'Glicemia', 'Saturação')`,
);
await queryRunner.query(
`ALTER TABLE "metrica" ALTER COLUMN "categoria" TYPE "public"."metrica_categoria_enum_old" USING "categoria"::"text"::"public"."metrica_categoria_enum_old"`,
);
await queryRunner.query(`DROP TYPE "public"."metrica_categoria_enum"`);
await queryRunner.query(
`ALTER TYPE "public"."metrica_categoria_enum_old" RENAME TO "metrica_categoria_enum"`,
);
}
}
Loading

0 comments on commit 6af7f14

Please sign in to comment.