Skip to content
This repository was archived by the owner on Jul 14, 2024. It is now read-only.

Commit

Permalink
feat: add update many and delete any in freight expenses
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielDeSouzza committed Apr 24, 2024
1 parent d62737f commit 0a179e4
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 24 deletions.
9 changes: 9 additions & 0 deletions src/app/dtos/FreightExpenseDto/UpdateManyFreightExpenseDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { type IFreightExpense } from 'domain/entities/OrdersEntities/FreightExpense/FreightExpense';

export abstract class UpdateManyFreightExpenseDTO
implements Partial<IFreightExpense>
{
id: string;
expenseName?: string;
value?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { HttpStatus, Injectable } from '@nestjs/common';
import { GraphQLError } from 'graphql';

import { type GetFreightExpenseDTO } from 'domain/dto/repositories/getDataDtos/GetFreightExpenseDto';
import { type FindAllFreightExpenseWhereRequestDTO } from 'domain/dto/repositories/whereDtos/FreightExpenseRepository.Dto';
import {
type CountAllFreightExpenseWhereRequestDTO,
type FindAllFreightExpenseWhereRequestDTO,
} from 'domain/dto/repositories/whereDtos/FreightExpenseRepository.Dto';
import { FreightExpense } from 'domain/entities/OrdersEntities/FreightExpense/FreightExpense';
import { FreightExpenseRepository } from 'domain/repositories/FreightExpenseResitory';

import { type CreateFreightExpenseDTO } from 'app/dtos/FreightExpenseDto/CreateFreightExpenseDto';
import { type UpdateFreightExpenseDTO } from 'app/dtos/FreightExpenseDto/UpdateFreightExpenseDto';
import { type UpdateManyFreightExpenseDTO } from 'app/dtos/FreightExpenseDto/UpdateManyFreightExpenseDto';

import { LegalClientOrderUseCases } from '../LegalClientOrderUseCases/LegalClientOrderUseCases';
import { PhysicalCustomerOrderUseCases } from '../PhysicalCustomerOrderCases/PhysicalCustomerOrderUseCases';
Expand All @@ -20,6 +24,9 @@ export class FreightExpenseUseCases {
private legalClientOrder: LegalClientOrderUseCases,
private physicalCustomeOrder: PhysicalCustomerOrderUseCases,
) {}
async countFreightExpense(request: CountAllFreightExpenseWhereRequestDTO) {
return this.freightExpenseRepository.countFreightExpenseRepositoy(request);
}
async getFreightExpense(request: GetFreightExpenseDTO) {
if (!request.id) {
throw new GraphQLError('IS NECESSARY AN ID', {
Expand Down Expand Up @@ -91,9 +98,26 @@ export class FreightExpenseUseCases {
);
}

async updateManyFreightExpenses(data: UpdateManyFreightExpenseDTO[]) {
const expenses = data.map(
expense =>
new FreightExpense({
expenseName: expense.expenseName,
value: expense.value,
id: expense.id,
}),
);

return this.freightExpenseRepository.updateManyFreightExpense(expenses);
}

async deleteExpense(data: GetFreightExpenseDTO) {
await this.getFreightExpense(data);

return this.freightExpenseRepository.delFreightExpense(data);
}

async deleteManyFreightExpenses(ids: string[]) {
return this.freightExpenseRepository.deleteManyFreightExpenses(ids);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ export class FindAllFreightExpenseWhereRequestDTO {
sort?: SortByFreightExpenseTypeDTO;
where?: WhereFreightExpenseTypeDTO;
}
export abstract class CountAllFreightExpenseWhereRequestDTO {
where?: WhereFreightExpenseTypeDTO;
}
12 changes: 11 additions & 1 deletion src/domain/repositories/FreightExpenseResitory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { type GetFreightExpenseDTO } from 'domain/dto/repositories/getDataDtos/GetFreightExpenseDto';
import { type FindAllFreightExpenseWhereRequestDTO } from 'domain/dto/repositories/whereDtos/FreightExpenseRepository.Dto';
import {
type CountAllFreightExpenseWhereRequestDTO,
type FindAllFreightExpenseWhereRequestDTO,
} from 'domain/dto/repositories/whereDtos/FreightExpenseRepository.Dto';
import { type FreightExpense } from 'domain/entities/OrdersEntities/FreightExpense/FreightExpense';

export abstract class FreightExpenseRepository {
abstract countFreightExpenseRepositoy(
parameters: CountAllFreightExpenseWhereRequestDTO,
): Promise<number>;
abstract getFreightExpense(
request: GetFreightExpenseDTO,
): Promise<FreightExpense>;
Expand All @@ -19,7 +25,11 @@ export abstract class FreightExpenseRepository {
id: string,
expense: FreightExpense,
): Promise<FreightExpense>;
abstract updateManyFreightExpense(
expenses: FreightExpense[],
): Promise<FreightExpense[]>;
abstract delFreightExpense(
data: GetFreightExpenseDTO,
): Promise<FreightExpense>;
abstract deleteManyFreightExpenses(ids: string[]): Promise<FreightExpense[]>;
}
73 changes: 57 additions & 16 deletions src/infra/database/prisma/services/FreightExpense.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Injectable } from '@nestjs/common';

import { type GetFreightExpenseDTO } from 'domain/dto/repositories/getDataDtos/GetFreightExpenseDto';
import { type FindAllFreightExpenseWhereRequestDTO } from 'domain/dto/repositories/whereDtos/FreightExpenseRepository.Dto';
import {
type CountAllFreightExpenseWhereRequestDTO,
type FindAllFreightExpenseWhereRequestDTO,
} from 'domain/dto/repositories/whereDtos/FreightExpenseRepository.Dto';
import { type FreightExpense } from 'domain/entities/OrdersEntities/FreightExpense/FreightExpense';
import { type FreightExpenseRepository } from 'domain/repositories/FreightExpenseResitory';

Expand All @@ -11,11 +14,12 @@ import { FreightExpensePrismaDTO } from './prismaDTO/FreightExpensePrismaDto';
@Injectable()
export class FreightExpensePrismaService implements FreightExpenseRepository {
constructor(private prisma: PrismaService) {}

async delFreightExpense(data: GetFreightExpenseDTO): Promise<FreightExpense> {
return FreightExpensePrismaDTO.PrismaToEntity(
await this.prisma.freightExpenses.delete({ where: { id: data.id } }),
);
countFreightExpenseRepositoy(
parameters: CountAllFreightExpenseWhereRequestDTO,
): Promise<number> {
return this.prisma.freightExpenses.count({
where: parameters.where ?? undefined,
});
}

async getFreightExpense(
Expand All @@ -29,6 +33,21 @@ export class FreightExpensePrismaService implements FreightExpenseRepository {
}),
);
}

async findAllFreightExpense(
parameters: FindAllFreightExpenseWhereRequestDTO,
): Promise<FreightExpense[]> {
const contracts = await this.prisma.freightExpenses.findMany({
take: parameters.limit,
skip: parameters.offset,
where: parameters.where,
orderBy: parameters.sort,
});

return contracts.map(contract =>
FreightExpensePrismaDTO.PrismaToEntity(contract),
);
}
async createFreightExpense(
contract: FreightExpense,
): Promise<FreightExpense> {
Expand Down Expand Up @@ -66,18 +85,40 @@ export class FreightExpensePrismaService implements FreightExpenseRepository {
}),
);
}
async findAllFreightExpense(
parameters: FindAllFreightExpenseWhereRequestDTO,
): Promise<FreightExpense[]> {
const contracts = await this.prisma.freightExpenses.findMany({
take: parameters.limit,
skip: parameters.offset,
where: parameters.where,
orderBy: parameters.sort,
updateManyFreightExpense(data: FreightExpense[]): Promise<FreightExpense[]> {
const expensesUpdated = this.prisma.$transaction(async tx => {
const promises = data.map(async expense => {
const expensePrisma = await tx.freightExpenses.update({
data: FreightExpensePrismaDTO.EntityToPrismaUpdate(expense),
where: { id: expense.id },
});

return FreightExpensePrismaDTO.PrismaToEntity(expensePrisma);
});

return Promise.all(promises);
});

return contracts.map(contract =>
FreightExpensePrismaDTO.PrismaToEntity(contract),
return expensesUpdated;
}
async delFreightExpense(data: GetFreightExpenseDTO): Promise<FreightExpense> {
return FreightExpensePrismaDTO.PrismaToEntity(
await this.prisma.freightExpenses.delete({ where: { id: data.id } }),
);
}
deleteManyFreightExpenses(ids: string[]): Promise<FreightExpense[]> {
const expensesDeleted = this.prisma.$transaction(async tx => {
const promises = ids.map(async expenseId => {
const expensePrisma = await tx.freightExpenses.delete({
where: { id: expenseId },
});

return FreightExpensePrismaDTO.PrismaToEntity(expensePrisma);
});

return Promise.all(promises);
});

return expensesDeleted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export class ContractOutsourcedDriverPrismaService
async updateManyContractOutsourcedDriver(
data: ContractOutsourcedDriver[],
): Promise<ContractOutsourcedDriver[]> {
console.log(data);
const contractsUpdated = await this.prisma.$transaction(async tx => {
const promises = data.map(async contract => {
const contractPrisma = await tx.contractOutsourcedDriver.update({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ export class FreightExpenseWhereArgs {
@IsOptional()
sort?: FreightExpensesOrderByWithRelationInput;
}

@ArgsType()
export class FreightExpenseCountArgs {
@Field(() => FreightExpensesWhereInput, { nullable: true })
@Type(() => FreightExpenseWhereArgs)
@IsOptional()
where?: FreightExpensesWhereInput;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Field, Float, InputType, PartialType } from '@nestjs/graphql';

import { IsNotEmpty, IsNumber, IsOptional, IsString } from 'class-validator';
import {
IsNotEmpty,
IsNumber,
IsOptional,
IsString,
IsUUID,
} from 'class-validator';

import { type IFreightExpense } from 'domain/entities/OrdersEntities/FreightExpense/FreightExpense';

Expand Down Expand Up @@ -30,3 +36,13 @@ export class FreightExpenseInput
export class FreightExpenseUpdateInput extends PartialType(
FreightExpenseInput,
) {}

@InputType()
export class FreightExpenseUpdateManyInput extends PartialType(
FreightExpenseInput,
) {
@Field()
@IsUUID()
@IsNotEmpty()
id: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ import { GraphQLAuthGuard } from 'infra/guard/GraphQlAuthGuard';

import { DeletFreightExpenseInput } from './Args/DeleteFreightExpenseInput';
import { GetFreightExpenseArgs } from './Args/GetFreightExpenseArgs';
import { FreightExpenseWhereArgs } from './Args/WhereFreightExpenseArgs';
import {
FreightExpenseCountArgs,
FreightExpenseWhereArgs,
} from './Args/WhereFreightExpenseArgs';
import {
FreightExpenseUpdateManyInput,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
FreightExpenseInput,
FreightExpenseUpdateInput,
Expand All @@ -25,10 +29,13 @@ import { FreightExpenseModel } from './FreightExpense.model';
@Resolver(() => FreightExpenseModel)
export class FreightExpenseResolver {
constructor(private freightExpenseUseCase: FreightExpenseUseCases) {}

@Query(() => Number)
async countFreightExpenses(@Args() args: FreightExpenseCountArgs) {
return this.freightExpenseUseCase.countFreightExpense(args);
}
@Query(() => FreightExpenseModel)
async getFreightExpense(@Args() request: GetFreightExpenseArgs) {
console.log(request);

return this.freightExpenseUseCase.getFreightExpense({
id: request.id,
});
Expand Down Expand Up @@ -58,11 +65,23 @@ export class FreightExpenseResolver {
freightExpenseUpInput,
);
}

@Mutation(() => [FreightExpenseModel])
async updateManyFreightExpenses(
@Args({ name: 'Data', type: () => [FreightExpenseUpdateManyInput] })
data: FreightExpenseUpdateManyInput[],
) {
return this.freightExpenseUseCase.updateManyFreightExpenses(data);
}
@Mutation(() => FreightExpenseModel)
async deleteFreightExpense(
@Args('delData') request: DeletFreightExpenseInput,
) {
return this.freightExpenseUseCase.deleteExpense(request);
}
@Mutation(() => [FreightExpenseModel])
async deleteManyFreightExpenses(
@Args({ name: 'ids', type: () => [String] }) ids: string[],
) {
return this.freightExpenseUseCase.deleteManyFreightExpenses(ids);
}
}
11 changes: 11 additions & 0 deletions src/infra/graphql/generated/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,14 @@ input FreightExpenseUpdateInput {
value: Float
}

input FreightExpenseUpdateManyInput {
expenseName: String
id: String!
legalClientOrderId: String
physicalCustomerOrderId: String
value: Float
}

input FreightExpensesListRelationFilter {
every: FreightExpensesWhereInput
none: FreightExpensesWhereInput
Expand Down Expand Up @@ -1393,6 +1401,7 @@ type Mutation {
deleteFreightExpense(delData: DeletFreightExpenseInput!): FreightExpenseModel!
deleteManyCarrierCompanies(deleteManyCarrierCompanies: [String!]!): [CarrierCompanyModel!]!
deleteManyContractOutsourcedDriver(ids: [String!]!): [ContractOutsourcedDriverModel!]!
deleteManyFreightExpenses(ids: [String!]!): [FreightExpenseModel!]!
deleteManyUsers(deleteManyUsers: [String!]!): [UserModel!]!
deleteUser(id: String!): UserModel!
login(loginData: AuthInput!): AuthModel!
Expand All @@ -1406,6 +1415,7 @@ type Mutation {
updateMaintenance(data: MaintenanceUpdateInput!, id: String!): MaintenanceModel!
updateMaintenanceCompany(id: String!, maintenancecompanyInput: MaintenanceCompanyUpdateInput!): MaintenanceCompanyModel!
updateManyCarrierCompanies(updateManyCarrierCompanies: [CarrierCompanyUpdateManyInput!]!): [CarrierCompanyModel!]!
updateManyFreightExpenses(Data: [FreightExpenseUpdateManyInput!]!): [FreightExpenseModel!]!
updateManyUsers(updateManyUsers: [UserUpdateManyInput!]!): [UserModel!]!
updateOrderProcessing(data: OrderProcessingUpdateInput!, id: String!): OrderProcessingModel!
updateOutsourcedDriver(id: String!, outsourcedDriver: OutsourcedDriverUpdateInput!): OutsourcedDriverModel!
Expand Down Expand Up @@ -2579,6 +2589,7 @@ input PhysicalCustomerWhereInput {
}

type Query {
countFreightExpenses(where: FreightExpensesWhereInput): Float!
generateLegalClientCte(request: CtePdfLegalClientInput!): CtePDfModel!
generatePhysicalCustomerCte(request: CtePdfPhysicalCustomerInput!): CtePDfModel!
getAllCarrierCompany(limit: Int! = 25, offset: Int! = 0, sort: CarrierCompanyOrderByWithRelationInput, where: CarrierCompanyWhereInput): [CarrierCompanyModel!]
Expand Down

0 comments on commit 0a179e4

Please sign in to comment.