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

Commit

Permalink
feat: add delete, delete many and update many in incident
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielDeSouzza committed Apr 25, 2024
1 parent 6b45874 commit 59bb13c
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 20 deletions.
9 changes: 9 additions & 0 deletions src/app/dtos/IncidentDto/UpdateManyIncidentDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { type IIncident } from 'domain/entities/OrdersEntities/IncidentEntity/Incident';

export abstract class UpdateManyIncidentDTO implements Partial<IIncident> {
id: string;
date_incident?: Date;
date_resolved?: Date;
order_process_id?: string;
description?: string;
}
44 changes: 43 additions & 1 deletion src/app/useCases/IncidentUseCases/IncidentUseCases.ts
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 GetIncidentDTO } from 'domain/dto/repositories/getDataDtos/GetIncidentDto';
import { type FindAllIncidentWhereRequestDTO } from 'domain/dto/repositories/whereDtos/FreightIncidentRepository.Dto';
import {
type CountIncidentRequestDTO,
type FindAllIncidentWhereRequestDTO,
} from 'domain/dto/repositories/whereDtos/FreightIncidentRepository.Dto';
import { Incident } from 'domain/entities/OrdersEntities/IncidentEntity/Incident';
import { IncidentRepository } from 'domain/repositories/IncidentResitory';

import { type CreateIncidentDTO } from 'app/dtos/IncidentDto/CreateIncidentDto';
import { type UpdateIncidentDTO } from 'app/dtos/IncidentDto/UpdateIncidentDto';
import { type UpdateManyIncidentDTO } from 'app/dtos/IncidentDto/UpdateManyIncidentDto';

import { OrderProcessingUseCases } from '../OrderProcessingUseCases/OrderProcessingUseCases';

Expand All @@ -18,6 +22,9 @@ export class IncidentUseCases {
private incidentRepository: IncidentRepository,
private orderProcessing: OrderProcessingUseCases,
) {}
async countIncident(request: CountIncidentRequestDTO) {
return this.incidentRepository.countIncident(request);
}
async getIncident(request: GetIncidentDTO) {
if (!request.id) {
throw new GraphQLError('IS NECESSARY AN ID', {
Expand Down Expand Up @@ -62,4 +69,39 @@ export class IncidentUseCases {

return this.incidentRepository.updateIncident(id, updateExpense);
}
async updateManyIncident(data: UpdateManyIncidentDTO[], updateBy: string) {
for (const incident of data) await this.verifyIncidentExist(incident.id);
const incidents = data.map(incident => {
const updateIncident = new Incident({
created_by: null,
date_incident: incident.date_incident,
date_resolved: incident.date_resolved,
description: incident.description,
order_process_id: incident.order_process_id,
updated_by: updateBy,
id: incident.id,
});

return updateIncident;
});

return this.incidentRepository.updateManyIncident(incidents);
}
async deleteIncident(id: string) {
await this.getIncident({ id });

return this.incidentRepository.deleteIncident(id);
}
async deleteManyIncident(ids: string[]) {
for (const incidentId of ids) await this.verifyIncidentExist(incidentId);

return this.incidentRepository.deleteManyIncident(ids);
}
private async verifyIncidentExist(id: string) {
const exist = await this.incidentRepository.getIncident({ id });
if (!exist)
throw new GraphQLError(`THIS INCIDENT ID ${id} NOT FOUND`, {
extensions: { code: HttpStatus.NOT_FOUND },
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ export class FindAllIncidentWhereRequestDTO {
sort?: SortByIncidentTypeDTO;
where?: WhereIncidentTypeDTO;
}

export abstract class CountIncidentRequestDTO {
where?: WhereIncidentTypeDTO;
}
9 changes: 8 additions & 1 deletion src/domain/repositories/IncidentResitory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { type GetIncidentDTO } from 'domain/dto/repositories/getDataDtos/GetIncidentDto';
import { type FindAllIncidentWhereRequestDTO } from 'domain/dto/repositories/whereDtos/FreightIncidentRepository.Dto';
import {
type CountIncidentRequestDTO,
type FindAllIncidentWhereRequestDTO,
} from 'domain/dto/repositories/whereDtos/FreightIncidentRepository.Dto';
import { type Incident } from 'domain/entities/OrdersEntities/IncidentEntity/Incident';

export abstract class IncidentRepository {
abstract countIncident(request: CountIncidentRequestDTO): Promise<number>;
abstract getIncident(request: GetIncidentDTO): Promise<Incident>;
abstract findAllIncident(
parameters: FindAllIncidentWhereRequestDTO,
): Promise<Incident[]>;
abstract createIncident(expense: Incident): Promise<Incident>;
abstract updateIncident(id: string, expense: Incident): Promise<Incident>;
abstract updateManyIncident(data: Incident[]): Promise<Incident[]>;
abstract deleteIncident(id: string): Promise<Incident>;
abstract deleteManyIncident(ids: string[]): Promise<Incident[]>;
}
70 changes: 58 additions & 12 deletions src/infra/database/prisma/services/Incident.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Injectable } from '@nestjs/common';

import { type GetIncidentDTO } from 'domain/dto/repositories/getDataDtos/GetIncidentDto';
import { type FindAllIncidentWhereRequestDTO } from 'domain/dto/repositories/whereDtos/FreightIncidentRepository.Dto';
import {
type CountIncidentRequestDTO,
type FindAllIncidentWhereRequestDTO,
} from 'domain/dto/repositories/whereDtos/FreightIncidentRepository.Dto';
import { type Incident } from 'domain/entities/OrdersEntities/IncidentEntity/Incident';
import { type IncidentRepository } from 'domain/repositories/IncidentResitory';

Expand All @@ -11,7 +14,11 @@ import { IncidentPrismaDTO } from './prismaDTO/IncidentPrismaDto';
@Injectable()
export class IncidentPrismaService implements IncidentRepository {
constructor(private prisma: PrismaService) {}

countIncident(request: CountIncidentRequestDTO): Promise<number> {
return this.prisma.incident.count({
where: request.where ?? undefined,
});
}
async getIncident(request: GetIncidentDTO): Promise<Incident> {
return IncidentPrismaDTO.PrismaToEntity(
await this.prisma.incident.findFirst({
Expand All @@ -21,6 +28,20 @@ export class IncidentPrismaService implements IncidentRepository {
}),
);
}
async findAllIncident(
parameters: FindAllIncidentWhereRequestDTO,
): Promise<Incident[]> {
const incidents = await this.prisma.incident.findMany({
take: parameters.limit,
skip: parameters.offset,
where: parameters.where,
orderBy: parameters.sort,
});

return incidents.map(contract =>
IncidentPrismaDTO.PrismaToEntity(contract),
);
}
async createIncident(contract: Incident): Promise<Incident> {
return IncidentPrismaDTO.PrismaToEntity(
await this.prisma.incident.create({
Expand All @@ -37,18 +58,43 @@ export class IncidentPrismaService implements IncidentRepository {
}),
);
}
async findAllIncident(
parameters: FindAllIncidentWhereRequestDTO,
): Promise<Incident[]> {
const incidents = await this.prisma.incident.findMany({
take: parameters.limit,
skip: parameters.offset,
where: parameters.where,
orderBy: parameters.sort,

updateManyIncident(data: Incident[]): Promise<Incident[]> {
console.log(data);
const incidentUpdate = this.prisma.$transaction(async tx => {
const promises = data.map(async incident => {
const incidentPrisma = await tx.incident.update({
data: IncidentPrismaDTO.EntityToPrismaUpdate(incident),
where: { id: incident.id },
});

return IncidentPrismaDTO.PrismaToEntity(incidentPrisma);
});

return Promise.all(promises);
});

return incidents.map(contract =>
IncidentPrismaDTO.PrismaToEntity(contract),
return incidentUpdate;
}

async deleteIncident(id: string): Promise<Incident> {
return IncidentPrismaDTO.PrismaToEntity(
await this.prisma.incident.delete({ where: { id } }),
);
}
deleteManyIncident(ids: string[]): Promise<Incident[]> {
const incidentDeleted = this.prisma.$transaction(async tx => {
const promises = ids.map(async icmdsId => {
const incidentPrisma = await tx.incident.delete({
where: { id: icmdsId },
});

return IncidentPrismaDTO.PrismaToEntity(incidentPrisma);
});

return Promise.all(promises);
});

return incidentDeleted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export class IncidentPrismaDTO {
const incidentUptade: Prisma.IncidentUpdateInput = {
date_incident: incident.date_incident,
description: incident.description,
OrderProcess: { connect: { id: incident.order_process_id } },
OrderProcess: incident.order_process_id
? { connect: { id: incident.order_process_id } }
: undefined,
updated_at: incident.updated_at,
date_resolved: incident.date_resolved,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ export class IncidentWhereArgs {
@IsOptional()
sort?: IncidentOrderByWithRelationInput;
}

@ArgsType()
export class IncidentCountArgs {
@Field(() => IncidentWhereInput, { nullable: true })
@Type(() => IncidentWhereInput)
@IsOptional()
where?: IncidentWhereInput;
}
7 changes: 7 additions & 0 deletions src/infra/graphql/entities/IncidentGraphql/Incident.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ export class IncidentInput

@InputType()
export class IncidentUpdateInput extends PartialType(IncidentInput) {}
@InputType()
export class IncidentUpdateManyInput extends PartialType(IncidentInput) {
@Field()
@IsUUID()
@IsNotEmpty()
id: string;
}
46 changes: 41 additions & 5 deletions src/infra/graphql/entities/IncidentGraphql/Incident.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@ import {
Resolver,
} from '@nestjs/graphql';

import { ROLE } from 'domain/entities/User/User';
import { ROLE, User } from 'domain/entities/User/User';

import { IncidentUseCases } from 'app/useCases/IncidentUseCases/IncidentUseCases';
import { OrderProcessingUseCases } from 'app/useCases/OrderProcessingUseCases/OrderProcessingUseCases';

import { AcessAllowed } from 'infra/graphql/utilities/decorators/AcessAllowed';
import { CurrentUser } from 'infra/graphql/utilities/decorators/CurrentUser';
import { RoleInterceptor } from 'infra/graphql/utilities/interceptors/RoleInterceptor';
import { GraphQLAuthGuard } from 'infra/guard/GraphQlAuthGuard';

import { OrderProcessingModel } from '../OrderProcessingGraphql/OrderProcessing.model';
import { GetIncidentArgs } from './Args/GetIncidentArgs';
import { IncidentWhereArgs } from './Args/WhereIncidentArgs';
import { IncidentInput, IncidentUpdateInput } from './Incident.input';
import { IncidentCountArgs, IncidentWhereArgs } from './Args/WhereIncidentArgs';
import {
IncidentInput,
IncidentUpdateInput,
IncidentUpdateManyInput,
} from './Incident.input';
import { IncidentModel } from './Incident.model';

@UseGuards(GraphQLAuthGuard)
Expand All @@ -32,6 +37,10 @@ export class IncidentResolver {
private incidentUseCase: IncidentUseCases,
private orderProcessingUseCase: OrderProcessingUseCases,
) {}
@Query(() => Number)
async countIncident(@Args() request: IncidentCountArgs) {
return this.incidentUseCase.countIncident(request);
}
@Query(() => IncidentModel)
async getIncident(@Args() request: GetIncidentArgs) {
console.log(request);
Expand All @@ -49,18 +58,45 @@ export class IncidentResolver {
@Mutation(() => IncidentModel)
async createIncident(
@Args('data')
IncidentInput: IncidentInput,
incidentInput: IncidentInput,
@CurrentUser() user: User,
) {
return this.incidentUseCase.createIncident(IncidentInput);
incidentInput.created_by = user.id;
incidentInput.updated_by = user.id;

return this.incidentUseCase.createIncident(incidentInput);
}
@Mutation(() => IncidentModel)
async updateIncident(
@Args('id') id: string,
@Args('upData')
incidentUpInput: IncidentUpdateInput,
@CurrentUser() user: User,
) {
incidentUpInput.updated_by = user.id;

return this.incidentUseCase.updateIncident(id, incidentUpInput);
}
@Mutation(() => [IncidentModel])
async updateManyIncident(
@Args({ name: 'data', type: () => [IncidentUpdateManyInput] })
data: IncidentUpdateManyInput[],
@CurrentUser() user: User,
) {
return this.incidentUseCase.updateManyIncident(data, user.id);
}
@Mutation(() => IncidentModel)
async deleteIncident(@Args('id') id: string) {
return this.incidentUseCase.deleteIncident(id);
}

@Mutation(() => [IncidentModel])
async deleteManyIncident(
@Args({ name: 'ids', type: () => [String] })
ids: string[],
) {
return this.incidentUseCase.deleteManyIncident(ids);
}
@ResolveField(() => OrderProcessingModel)
OrderProcessing(@Parent() incident: IncidentInput) {
return this.orderProcessingUseCase.getOrderProcessing({
Expand Down
12 changes: 12 additions & 0 deletions src/infra/graphql/generated/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,14 @@ input IncidentUpdateInput {
order_process_id: String
}

input IncidentUpdateManyInput {
date_incident: Timestamp
date_resolved: Timestamp
description: String
id: String!
order_process_id: String
}

input IncidentWhereInput {
AND: [IncidentWhereInput!]
CreatedBy: UserWhereInput
Expand Down Expand Up @@ -1408,10 +1416,12 @@ type Mutation {
deleteContractOutsourcedDriver(id: String!): ContractOutsourcedDriverModel!
deleteFreightExpense(delData: DeletFreightExpenseInput!): FreightExpenseModel!
deleteIcms(id: String!): IcmsModel!
deleteIncident(id: String!): IncidentModel!
deleteManyCarrierCompanies(deleteManyCarrierCompanies: [String!]!): [CarrierCompanyModel!]!
deleteManyContractOutsourcedDriver(ids: [String!]!): [ContractOutsourcedDriverModel!]!
deleteManyFreightExpenses(ids: [String!]!): [FreightExpenseModel!]!
deleteManyIcms(ids: [String!]!): [IcmsModel!]!
deleteManyIncident(ids: [String!]!): [IncidentModel!]!
deleteManyUsers(deleteManyUsers: [String!]!): [UserModel!]!
deleteUser(id: String!): UserModel!
login(loginData: AuthInput!): AuthModel!
Expand All @@ -1427,6 +1437,7 @@ type Mutation {
updateManyCarrierCompanies(updateManyCarrierCompanies: [CarrierCompanyUpdateManyInput!]!): [CarrierCompanyModel!]!
updateManyFreightExpenses(Data: [FreightExpenseUpdateManyInput!]!): [FreightExpenseModel!]!
updateManyIcms(data: [IcmsUpdateManyInput!]!): [IcmsModel!]!
updateManyIncident(data: [IncidentUpdateManyInput!]!): [IncidentModel!]!
updateManyUsers(updateManyUsers: [UserUpdateManyInput!]!): [UserModel!]!
updateOrderProcessing(data: OrderProcessingUpdateInput!, id: String!): OrderProcessingModel!
updateOutsourcedDriver(id: String!, outsourcedDriver: OutsourcedDriverUpdateInput!): OutsourcedDriverModel!
Expand Down Expand Up @@ -2602,6 +2613,7 @@ input PhysicalCustomerWhereInput {
type Query {
countFreightExpenses(where: FreightExpensesWhereInput): Float!
countIcms(where: IcmsWhereInput): Float!
countIncident(where: IncidentWhereInput): 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 59bb13c

Please sign in to comment.