diff --git a/src/migration/1701141143183-AlterTableRotinaAlterColumndataHoraConcluidos.ts b/src/migration/1701141143183-AlterTableRotinaAlterColumndataHoraConcluidos.ts new file mode 100644 index 0000000..c4cfe70 --- /dev/null +++ b/src/migration/1701141143183-AlterTableRotinaAlterColumndataHoraConcluidos.ts @@ -0,0 +1,31 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AlterTableRotinaAlterColumndataHoraConcluidos1701141143183 + implements MigrationInterface +{ + name = 'AlterTableRotinaAlterColumndataHoraConcluidos1701141143183'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "rotina" RENAME COLUMN "concluido" TO "dataHoraConcluidos"`, + ); + await queryRunner.query( + `ALTER TABLE "rotina" DROP COLUMN "dataHoraConcluidos"`, + ); + await queryRunner.query( + `ALTER TABLE "rotina" ADD "dataHoraConcluidos" TIMESTAMP array NOT NULL DEFAULT '{}'`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "rotina" DROP COLUMN "dataHoraConcluidos"`, + ); + await queryRunner.query( + `ALTER TABLE "rotina" ADD "dataHoraConcluidos" boolean NOT NULL DEFAULT false`, + ); + await queryRunner.query( + `ALTER TABLE "rotina" RENAME COLUMN "dataHoraConcluidos" TO "concluido"`, + ); + } +} diff --git a/src/migration/1701141892826-AlterTableRotinaAlterColumndataHoraConcluidos.ts b/src/migration/1701141892826-AlterTableRotinaAlterColumndataHoraConcluidos.ts new file mode 100644 index 0000000..656b7b5 --- /dev/null +++ b/src/migration/1701141892826-AlterTableRotinaAlterColumndataHoraConcluidos.ts @@ -0,0 +1,25 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AlterTableRotinaAlterColumndataHoraConcluidos1701141892826 + implements MigrationInterface +{ + name = 'AlterTableRotinaAlterColumndataHoraConcluidos1701141892826'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "rotina" DROP COLUMN "dataHoraConcluidos"`, + ); + await queryRunner.query( + `ALTER TABLE "rotina" ADD "dataHoraConcluidos" character varying array NOT NULL DEFAULT '{}'`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "rotina" DROP COLUMN "dataHoraConcluidos"`, + ); + await queryRunner.query( + `ALTER TABLE "rotina" ADD "dataHoraConcluidos" TIMESTAMP array NOT NULL DEFAULT '{}'`, + ); + } +} diff --git a/src/rotina/dto/create-rotina.dto.ts b/src/rotina/dto/create-rotina.dto.ts index ea6c55d..21bfa40 100644 --- a/src/rotina/dto/create-rotina.dto.ts +++ b/src/rotina/dto/create-rotina.dto.ts @@ -1,6 +1,5 @@ import { IsArray, - IsBoolean, IsDateString, IsEnum, IsNotEmpty, @@ -34,9 +33,10 @@ export class CreateRotinaDto { @MaxLength(100) descricao?: string; + @IsArray() @IsOptional() - @IsBoolean() - concluido?: boolean; + @IsString({ each: true }) + dataHoraConcluidos?: string[]; @IsArray() @IsEnum(EDiasSemana, { each: true }) diff --git a/src/rotina/entities/rotina.entity.ts b/src/rotina/entities/rotina.entity.ts index b6e9543..237940c 100644 --- a/src/rotina/entities/rotina.entity.ts +++ b/src/rotina/entities/rotina.entity.ts @@ -31,8 +31,8 @@ export class Rotina { @Column('varchar', { length: 100, nullable: true }) descricao!: string; - @Column({ type: 'boolean', default: false }) - concluido!: boolean; + @Column('varchar', { array: true, default: [] }) + dataHoraConcluidos!: string[]; @ManyToOne(() => Idoso) @JoinColumn({ name: 'idIdoso' }) diff --git a/src/rotina/interfaces/rotina-filter.interface.ts b/src/rotina/interfaces/rotina-filter.interface.ts index 98dc2f0..31a3677 100644 --- a/src/rotina/interfaces/rotina-filter.interface.ts +++ b/src/rotina/interfaces/rotina-filter.interface.ts @@ -1,4 +1,5 @@ export interface IRotinaFilter { id?: number; idIdoso?: number; + dataHora?: string; } diff --git a/src/rotina/rotina.controller.spec.ts b/src/rotina/rotina.controller.spec.ts index 9ec1083..07cee4f 100644 --- a/src/rotina/rotina.controller.spec.ts +++ b/src/rotina/rotina.controller.spec.ts @@ -1,7 +1,7 @@ import { Test, TestingModule } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; import { Filtering } from '../shared/decorators/filtrate.decorator'; -import { Ordering, OrderParams } from '../shared/decorators/ordenate.decorator'; +import { OrderParams, Ordering } from '../shared/decorators/ordenate.decorator'; import { Pagination, PaginationParams, @@ -22,8 +22,8 @@ describe('RotinaController', () => { categoria: ECategoriaRotina.ALIMENTACAO, descricao: 'desc', dataHora: new Date().toISOString() as any, + dataHoraConcluidos: [], dias: [0, 1], - concluido: false, }; const rotina = { @@ -102,6 +102,7 @@ describe('RotinaController', () => { describe('findAll', () => { const filter: IRotinaFilter = { + dataHora: new Date().toISOString(), idIdoso: 1, id: 1, }; @@ -125,7 +126,7 @@ describe('RotinaController', () => { jest.spyOn(service, 'findAll').mockReturnValue(Promise.resolve(expected)); const { data, count, pageSize } = await controller.findAll( - filtering, + filtering as any, pagination, ordering, ); diff --git a/src/rotina/rotina.service.spec.ts b/src/rotina/rotina.service.spec.ts index 1665217..be33fae 100644 --- a/src/rotina/rotina.service.spec.ts +++ b/src/rotina/rotina.service.spec.ts @@ -1,7 +1,7 @@ import { Test, TestingModule } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { Ordering, OrderParams } from '../shared/decorators/ordenate.decorator'; +import { OrderParams, Ordering } from '../shared/decorators/ordenate.decorator'; import { Pagination, PaginationParams, @@ -109,9 +109,17 @@ describe('RotinaService', () => { }), } as any); - const { data, count } = await service.findAll({}, ordering, pagination); + const { data, count } = await service.findAll( + { dataHora: new Date().toISOString() }, + ordering, + pagination, + ); expect(count).toEqual(1); expect((data as Rotina[])[0]).toEqual(rotina); + + const res = await service.findAll({}, ordering, pagination); + expect(res.count).toEqual(1); + expect((res.data as Rotina[])[0]).toEqual(rotina); }); }); }); diff --git a/src/rotina/rotina.service.ts b/src/rotina/rotina.service.ts index 25c9a43..ab1230d 100644 --- a/src/rotina/rotina.service.ts +++ b/src/rotina/rotina.service.ts @@ -67,10 +67,28 @@ export class RotinaService { whereClause += getWhereClauseNumber(filter.id, 'id'); whereClause += getWhereClauseNumber(filter.idIdoso, '"idIdoso"'); + whereClause += this.getWhereClauseDate(filter.dataHora); return whereClause; } + private getWhereClauseDate(value: string | undefined): string { + if (!value || value.length < 1) return ''; + + const date = new Date(value); + const weekday = date.getDay(); + + const start = new Date(value); + start.setUTCHours(0, 0, 0); + const startString = start.toISOString(); + + const end = new Date(value); + end.setUTCHours(23, 59, 59); + const endString = end.toISOString(); + + return ` AND (("dataHora"::date BETWEEN '${startString}'::date AND '${endString}'::date) OR ("dias" && '{${weekday}}'::rotina_dias_enum[]))`; + } + async remove(id: number) { const found = await this._repository.findOneOrFail({ where: { id } }); return this._repository.remove(found);