Skip to content

Commit

Permalink
Merge pull request #8 from fga-eps-mds/feature/#170-crud-idoso-backend
Browse files Browse the repository at this point in the history
Feature/#170 crud idoso backend
  • Loading branch information
pedro-cella authored Nov 15, 2023
2 parents 3007196 + d9a44e3 commit cada4ad
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 39 deletions.
8 changes: 4 additions & 4 deletions e2e/idoso.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ describe('E2E - Idoso', () => {
dataNascimento: new Date().toISOString() as any,
tipoSanguineo: ETipoSanguineo.AB_Negativo,
telefoneResponsavel: '123456789',
descricao: 'desc',
descricao: 'Nova descricao',
dataHora: new Date().toISOString() as any,
};

beforeAll(async () => {
Expand Down Expand Up @@ -96,6 +97,7 @@ describe('E2E - Idoso', () => {
const res = await request(app.getHttpServer())
.post('/idoso')
.set('Content-Type', 'application/json')
.set('Authorization', 'bearer ' + token)
.send(idoso);

expect(res.statusCode).toEqual(201);
Expand All @@ -113,6 +115,7 @@ describe('E2E - Idoso', () => {
const res = await request(app.getHttpServer())
.post('/idoso')
.set('Content-Type', 'application/json')
.set('Authorization', 'bearer ' + token)
.send({});

expect(res.statusCode).toEqual(400);
Expand All @@ -130,9 +133,6 @@ describe('E2E - Idoso', () => {
'telefoneResponsavel must be longer than or equal to 9 characters',
'telefoneResponsavel should not be empty',
'telefoneResponsavel must be a string',
'descricao should not be empty',
'descricao must be shorter than or equal to 500 characters',
'descricao must be a string',
]);
expect(res.body.data).toBeNull();
});
Expand Down
6 changes: 5 additions & 1 deletion src/idoso/dto/create-idoso-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export class CreateIdosoDto {

@IsString()
@MaxLength(500)
@IsNotEmpty()
@IsOptional()
descricao?: string;

@IsDateString()
@IsOptional()
dataHora!: Date;
}
11 changes: 7 additions & 4 deletions src/idoso/entities/idoso.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ export class Idoso {
@Column('timestamp')
dataNascimento!: Date;

@Column('enum', { enum: ETipoSanguineo })
tipoSanguineo?: ETipoSanguineo;
@Column('enum', { enum: ETipoSanguineo, nullable: true })
tipoSanguineo!: ETipoSanguineo;

@Column('varchar', { length: 11 })
telefoneResponsavel!: string;

@Column('varchar', { length: 500 })
descricao?: string;
@Column('varchar', { length: 500, nullable: true })
descricao!: string;

@Column('timestamp', { nullable: true })
dataHora!: Date;

constructor(createIdosoDto: CreateIdosoDto | UpdateIdosoDto) {
Object.assign(this, createIdosoDto);
Expand Down
19 changes: 14 additions & 5 deletions src/idoso/idoso.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 { OrderParams, Ordering } from '../shared/decorators/ordenate.decorator';
import { Ordering, OrderParams } from '../shared/decorators/ordenate.decorator';
import {
Pagination,
PaginationParams,
Expand All @@ -24,6 +24,7 @@ describe('IdosoController', () => {
tipoSanguineo: ETipoSanguineo.AB_Negativo,
telefoneResponsavel: '123456789',
descricao: 'desc',
dataHora: new Date().toISOString() as any,
};

const idoso = {
Expand Down Expand Up @@ -63,30 +64,38 @@ describe('IdosoController', () => {
});

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

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

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

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

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

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

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

const response = await controller.update({ id: 1 }, { nome: 'Henrique' });
expect(response.data).toEqual(idoso);
Expand Down
2 changes: 2 additions & 0 deletions src/idoso/idoso.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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 { IdValidator } from '../shared/validators/id.validator';
Expand All @@ -25,6 +26,7 @@ export class IdosoController {
constructor(private readonly _service: IdosoService) {}

@Get()
@PublicRoute()
async findAll(
@Filtrate() queryParam: Filtering<IIdosoFilter>,
@Paginate() pagination: Pagination,
Expand Down
12 changes: 1 addition & 11 deletions src/idoso/idoso.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ describe('IdosoService', () => {
expect(found.id).toEqual(1);
});

it('should find Idoso with foto', async () => {
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({
id: 1,
foto: Buffer.from('/9j/4AAQSkZJRgABAQAAAQABAAD', 'utf-8'),
} as any);

const found = await service.findOne(1, true);
expect(found.id).toEqual(1);
});

it('should remove Idoso', async () => {
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({ id: 1 } as any);
jest.spyOn(repository, 'remove').mockReturnValue({ id: 1 } as any);
Expand Down Expand Up @@ -98,7 +88,7 @@ describe('IdosoService', () => {
expect(found).toEqual({
id: 1,
nome: 'Henrique',
foto: 'data:image/png;base64,1',
foto: '1',
});
});

Expand Down
17 changes: 3 additions & 14 deletions src/idoso/idoso.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Ordering } from '../shared/decorators/ordenate.decorator';
import { Pagination } from '../shared/decorators/paginate.decorator';
import { getImageUri } from '../shared/helpers/buffer-to-image';
import {
getWhereClauseNumber,
getWhereClauseString,
Expand All @@ -27,25 +26,15 @@ export class IdosoService {
return this._repository.save(idoso);
}

async findOne(id: number, transformImage = false) {
const idoso = await this._repository.findOneOrFail({ where: { id } });
if (transformImage && idoso.foto) {
idoso.foto = getImageUri(idoso.foto) as unknown as Buffer;
}
return idoso;
async findOne(id: number) {
return this._repository.findOneOrFail({ where: { id } });
}

async update(id: number, body: UpdateIdosoDto): Promise<Idoso> {
const found = await this.findOne(id);
const merged = Object.assign(found, body);

const updated = await this._repository.save(merged);

if (updated.foto) {
updated.foto = getImageUri(updated.foto) as unknown as Buffer & string;
}

return updated;
return this._repository.save(merged);
}

async findAll(
Expand Down
16 changes: 16 additions & 0 deletions src/migration/1699572747760-AlterTableIdoso.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AlterTableIdoso1699572747760 implements MigrationInterface {
name = 'AlterTableIdoso1699572747760'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "idoso" ALTER COLUMN "tipoSanguineo" DROP NOT NULL`);
await queryRunner.query(`ALTER TABLE "idoso" ALTER COLUMN "descricao" DROP NOT NULL`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "idoso" ALTER COLUMN "descricao" SET NOT NULL`);
await queryRunner.query(`ALTER TABLE "idoso" ALTER COLUMN "tipoSanguineo" SET NOT NULL`);
}

}
14 changes: 14 additions & 0 deletions src/migration/1699904482169-AddDataHoraTableIdoso.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddDataHoraTableIdoso1699904482169 implements MigrationInterface {
name = 'AddDataHoraTableIdoso1699904482169'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "idoso" ADD "dataHora" TIMESTAMP`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "idoso" DROP COLUMN "dataHora"`);
}

}

0 comments on commit cada4ad

Please sign in to comment.