diff --git a/src/repository/equipamentRepository.ts b/src/repository/equipamentRepository.ts index ab6d1c1..65ffe40 100644 --- a/src/repository/equipamentRepository.ts +++ b/src/repository/equipamentRepository.ts @@ -1,5 +1,5 @@ /* eslint-disable */ -import { MoreThanOrEqual, ILike } from 'typeorm' +import { MoreThanOrEqual, ILike, LessThanOrEqual, Between } from 'typeorm' import { dataSource } from '../db/config' import { Equipment } from '../db/entities/equipment' import { EquipmentRepositoryProtocol, Query } from './protocol/equipmentRepositoryProtocol' @@ -34,30 +34,60 @@ export class EquipmentRepository implements EquipmentRepositoryProtocol { }) return equipment } - + async genericFind(query: Query): Promise { const { type, + storageType, + ram_size, + processador, + screenSize, + screenType, + power, unit, situation, updatedAt, brand, + model, + acquisition, search, searchTipping, + initialDate, + finalDate, take, skip, + acquisitionYear, } = query; const defaultConditions= { - type: type, + type: type ? { id: type } : undefined, + storageType: storageType, situacao: situation, + screenSize: screenSize, + screenType: screenType, + processor: processador, + power: power, unit: unit? {id: unit} : undefined, brand: brand ? { id: brand } : undefined, + ram_size: ram_size, + model: model, + acquisition: acquisition ? {name: acquisition}: undefined, updatedAt: updatedAt ? MoreThanOrEqual(updatedAt) : undefined, + createdAt: undefined, + acquisitionDate: acquisitionYear + ? Between(new Date(Number(acquisitionYear), 0, 1), new Date(Number(acquisitionYear), 11, 31)) + : undefined, }; - - let searchConditions; + if(initialDate && finalDate) { + defaultConditions.createdAt = Between(initialDate, finalDate) + } else if (initialDate) { + defaultConditions.createdAt = MoreThanOrEqual(initialDate) + } else if (finalDate) { + defaultConditions.createdAt = LessThanOrEqual(finalDate) + } + + let searchConditions; if(typeof search !== 'undefined') { searchConditions = [ { @@ -80,9 +110,8 @@ export class EquipmentRepository implements EquipmentRepositoryProtocol { } ] } else - searchConditions = defaultConditions; - - + searchConditions = defaultConditions; + const queryResult = await this.equipmentRepository.find({ relations: { brand: true, @@ -95,11 +124,9 @@ export class EquipmentRepository implements EquipmentRepositoryProtocol { take: take, skip: skip }); - return queryResult; } - - + async findByTippingNumberOrSerialNumber( id: string ): Promise { diff --git a/src/repository/protocol/equipmentRepositoryProtocol.ts b/src/repository/protocol/equipmentRepositoryProtocol.ts index cefecdd..8abfab2 100644 --- a/src/repository/protocol/equipmentRepositoryProtocol.ts +++ b/src/repository/protocol/equipmentRepositoryProtocol.ts @@ -1,6 +1,11 @@ import { Equipment } from '../../db/entities/equipment' export type Query = { + userId?: string + id?: string + tippingNumber?: string + serialNumber?: string + acquisition?: string type?: string unit?: string situation?: string @@ -9,15 +14,26 @@ export type Query = { search?: string model?: string searchTipping?: string + screenSize?: string + power?: string + screenType?: string + processador?: string + storageType?: string + ram_size?: string + storageAmount?: string + createdAt?: Date + initialDate?: Date + finalDate?: Date take?: number skip?: number + acquisitionYear?: string } export interface EquipmentRepositoryProtocol { create(equipment: Equipment): Promise updateOne(equipmentData: any): Promise findOne(equipmentId: string): Promise - genericFind(query: any): Promise + genericFind(query: Query): Promise findByTippingNumberOrSerialNumber(id: string): Promise findByTippingNumber(tippingNumber: string): Promise deleteOne(id: string): Promise diff --git a/src/useCases/getEquipment/getEquipmentUseCase.ts b/src/useCases/getEquipment/getEquipmentUseCase.ts index b7f067d..7c556cf 100644 --- a/src/useCases/getEquipment/getEquipmentUseCase.ts +++ b/src/useCases/getEquipment/getEquipmentUseCase.ts @@ -40,6 +40,10 @@ export interface GetEquipmentInput { createdAt?: Date + initialDate?: Date + + finalDate?: Date + take?: number skip?: number