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

Feature/#203 calendario #13

Merged
merged 4 commits into from
Dec 2, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

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

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
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"`,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

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

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
await queryRunner.query(
`ALTER TABLE "rotina" DROP COLUMN "dataHoraConcluidos"`,
);
await queryRunner.query(
`ALTER TABLE "rotina" ADD "dataHoraConcluidos" TIMESTAMP array NOT NULL DEFAULT '{}'`,
);
}
}
6 changes: 3 additions & 3 deletions src/rotina/dto/create-rotina.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
IsArray,
IsBoolean,
IsDateString,
IsEnum,
IsNotEmpty,
Expand Down Expand Up @@ -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 })
Expand Down
4 changes: 2 additions & 2 deletions src/rotina/entities/rotina.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
1 change: 1 addition & 0 deletions src/rotina/interfaces/rotina-filter.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface IRotinaFilter {
id?: number;
idIdoso?: number;
dataHora?: string;
}
7 changes: 4 additions & 3 deletions src/rotina/rotina.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = {
Expand Down Expand Up @@ -102,6 +102,7 @@ describe('RotinaController', () => {

describe('findAll', () => {
const filter: IRotinaFilter = {
dataHora: new Date().toISOString(),
idIdoso: 1,
id: 1,
};
Expand All @@ -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,
);
Expand Down
12 changes: 10 additions & 2 deletions src/rotina/rotina.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
});
});
});
18 changes: 18 additions & 0 deletions src/rotina/rotina.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading