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

Commit

Permalink
feat: add updateMany, delete and deleteMany in physicalCusotmerQuoteT…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
GabrielDeSouzza committed Jun 9, 2024
1 parent 927dfe8 commit 0e08a3b
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { type AdressesType } from 'domain/entities/QuoteTables/AdressesType';

export abstract class UpdateManyPhysicalCustomerQuoteTableDTO {
id: string;
recipientId?: string;
kindService?: string;
formPayment?: string;
senderId?: string;
who_pays?: string;
adressDestiny?: AdressesType;
adressOrigin?: AdressesType;
typeMerchandise?: string;
amount?: number;
description?: string;
mass?: number;
volume?: number;
nf_value?: number;
icms_id?: string;
nf_serie?: string;
nf_number?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import { HttpStatus, Injectable } from '@nestjs/common';
import { GraphQLError } from 'graphql';

import { type GetPhysicalCustomerQuoteTableDTO } from 'domain/dto/repositories/getDataDtos/GetPhysicalCustomerQuoteTableDto';
import { type FindAllPhysicalCustomerQuoteTableWhereRequestDTO } from 'domain/dto/repositories/whereDtos/PhysicalCustomerQuoteTableRepositoryDto';
import {
type CountAllPhysicalCustomerQuoteTableWhereRequestDTO,
type FindAllPhysicalCustomerQuoteTableWhereRequestDTO,
} from 'domain/dto/repositories/whereDtos/PhysicalCustomerQuoteTableRepositoryDto';
import { PhysicalCustomerQuoteTable } from 'domain/entities/QuoteTables/PhysicalCustomerQuoteTable/PhysicalCustomerQuoteTable';
import { PhysicalCustomerQuoteTableRepository } from 'domain/repositories/PhysicalCustomerQuoteTable.repository';

import { type CreatePhysicalCustomerQuoteTableDTO } from 'app/dtos/PhysicalCustomerQuoteTableDto/CreatePhysicalCustomerQuoteTableDto';
import { type UpdateManyPhysicalCustomerQuoteTableDTO } from 'app/dtos/PhysicalCustomerQuoteTableDto/UpdateManyPhysicalCustomerQuoteTableDto';
import { type UpdatePhysicalCustomerQuoteTableDTO } from 'app/dtos/PhysicalCustomerQuoteTableDto/UpdatePhysicalCustomerQuoteTableDto';
import { generateRandomNumber } from 'app/utils/RandomNumber';

Expand All @@ -21,6 +25,11 @@ export class PhysicalCustomerQuoteTableUseCases {
private senderUseCase: SenderUseCases,
private recipientUseCase: RecipientUseCases,
) {}
async countPhysicalCustomerQuoteTable(
request: CountAllPhysicalCustomerQuoteTableWhereRequestDTO,
) {
return this.physicalCustomerQuoteTableRepository.count(request);
}
async getPhysicalCustomerQuoteTable(
request: GetPhysicalCustomerQuoteTableDTO,
) {
Expand Down Expand Up @@ -140,4 +149,78 @@ export class PhysicalCustomerQuoteTableUseCases {
order,
);
}

async updateManyPhysicalCustomerQuoteTable(
data: UpdateManyPhysicalCustomerQuoteTableDTO[],
updateBy: string,
) {
for (const physicalcustomerquotetable of data)
await this.verifyPhysicalCustomerQuoteTableExist(
physicalcustomerquotetable.id,
);
const physicalcustomerquotetables = data.map(physicalcustomerquotetable => {
const updatePhysicalCustomerQuoteTable = new PhysicalCustomerQuoteTable({
amount: physicalcustomerquotetable.amount,
codQuote: null,
formPayment: physicalcustomerquotetable.formPayment,
kindService: physicalcustomerquotetable.kindService,
description: physicalcustomerquotetable.description,
mass: physicalcustomerquotetable.mass,
nf_value: physicalcustomerquotetable.nf_value,
adressDestiny: physicalcustomerquotetable.adressDestiny,
adressOrigin: physicalcustomerquotetable.adressOrigin,
recipientId: physicalcustomerquotetable.recipientId,
senderId: physicalcustomerquotetable.senderId,
typeMerchandise: physicalcustomerquotetable.typeMerchandise,
volume: physicalcustomerquotetable.volume,
who_pays: physicalcustomerquotetable.who_pays,
created_by: null,
updated_by: updateBy,
id: physicalcustomerquotetable.id,
nf_number: physicalcustomerquotetable.nf_number,
nf_serie: physicalcustomerquotetable.nf_serie,
digital_signature: null,
});

return updatePhysicalCustomerQuoteTable;
});

return this.physicalCustomerQuoteTableRepository.updateManyPhysicalCustomerQuoteTable(
physicalcustomerquotetables,
);
}

async deletePhysicalCustomerQuoteTable(id: string) {
await this.getPhysicalCustomerQuoteTable({ id });

return this.physicalCustomerQuoteTableRepository.deletePhysicalCustomerQuoteTable(
id,
);
}
async deleteManyPhysicalCustomerQuoteTable(ids: string[]) {
for (const physicalcustomerquotetableId of ids)
await this.verifyPhysicalCustomerQuoteTableExist(
physicalcustomerquotetableId,
);

return this.physicalCustomerQuoteTableRepository.deleteManyPhysicalCustomerQuoteTable(
ids,
);
}

private async verifyPhysicalCustomerQuoteTableExist(id: string) {
const exist =
await this.physicalCustomerQuoteTableRepository.findPhysicalCustomerQuoteTable(
{
id,
},
);
if (!exist)
throw new GraphQLError(
`THIS LEGAL CLIENT QUOTETABLE ID ${id} NOT FOUND`,
{
extensions: { code: HttpStatus.NOT_FOUND },
},
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ export class FindAllPhysicalCustomerQuoteTableWhereRequestDTO {
sort?: SortByPhysicalCustomerQuoteTableTypeDTO;
where?: WherePhysicalCustomerQuoteTableTypeDTO;
}

export abstract class CountAllPhysicalCustomerQuoteTableWhereRequestDTO {
where?: WherePhysicalCustomerQuoteTableTypeDTO;
}
14 changes: 14 additions & 0 deletions src/domain/repositories/PhysicalCustomerQuoteTable.repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { type GetPhysicalCustomerQuoteTableDTO } from 'domain/dto/repositories/getDataDtos/GetPhysicalCustomerQuoteTableDto';
import { type FindAllPhysicalCustomerQuoteTableWhereRequestDTO } from 'domain/dto/repositories/whereDtos/PhysicalCustomerQuoteTableRepositoryDto';
import { type CountAllPhysicalCustomersWhereRequestDTO } from 'domain/dto/repositories/whereDtos/PhysicalCustomerRepositoryDto';
import { type PhysicalCustomerQuoteTable } from 'domain/entities/QuoteTables/PhysicalCustomerQuoteTable/PhysicalCustomerQuoteTable';

export abstract class PhysicalCustomerQuoteTableRepository {
abstract count(
parameters: CountAllPhysicalCustomersWhereRequestDTO,
): Promise<number>;
abstract findPhysicalCustomerQuoteTable(
request: GetPhysicalCustomerQuoteTableDTO,
): Promise<PhysicalCustomerQuoteTable>;
Expand All @@ -16,4 +20,14 @@ export abstract class PhysicalCustomerQuoteTableRepository {
abstract findAllPhysicalCustomerQuoteTable(
paraments: FindAllPhysicalCustomerQuoteTableWhereRequestDTO,
): Promise<PhysicalCustomerQuoteTable[]>;

abstract updateManyPhysicalCustomerQuoteTable(
data: PhysicalCustomerQuoteTable[],
): Promise<PhysicalCustomerQuoteTable[]>;
abstract deletePhysicalCustomerQuoteTable(
id: string,
): Promise<PhysicalCustomerQuoteTable>;
abstract deleteManyPhysicalCustomerQuoteTable(
ids: string[],
): Promise<PhysicalCustomerQuoteTable[]>;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Injectable } from '@nestjs/common';

import { type GetPhysicalCustomerQuoteTableDTO } from 'domain/dto/repositories/getDataDtos/GetPhysicalCustomerQuoteTableDto';
import { type FindAllPhysicalCustomerQuoteTableWhereRequestDTO } from 'domain/dto/repositories/whereDtos/PhysicalCustomerQuoteTableRepositoryDto';
import {
type CountAllPhysicalCustomerQuoteTableWhereRequestDTO,
type FindAllPhysicalCustomerQuoteTableWhereRequestDTO,
} from 'domain/dto/repositories/whereDtos/PhysicalCustomerQuoteTableRepositoryDto';
import { type PhysicalCustomerQuoteTable } from 'domain/entities/QuoteTables/PhysicalCustomerQuoteTable/PhysicalCustomerQuoteTable';
import { type PhysicalCustomerQuoteTableRepository } from 'domain/repositories/PhysicalCustomerQuoteTable.repository';

Expand All @@ -13,6 +16,14 @@ export class PhysicalCustomerQuoteTablePrismaService
implements PhysicalCustomerQuoteTableRepository
{
constructor(private prisma: PrismaService) {}

count(
request: CountAllPhysicalCustomerQuoteTableWhereRequestDTO,
): Promise<number> {
return this.prisma.physicalCustomerQuoteTable.count({
where: request.where ?? undefined,
});
}
async findPhysicalCustomerQuoteTable(
request: GetPhysicalCustomerQuoteTableDTO,
): Promise<PhysicalCustomerQuoteTable> {
Expand Down Expand Up @@ -87,4 +98,74 @@ export class PhysicalCustomerQuoteTablePrismaService
),
);
}

updateManyPhysicalCustomerQuoteTable(
data: PhysicalCustomerQuoteTable[],
): Promise<PhysicalCustomerQuoteTable[]> {
console.log(data);
const physicalcustomerquotetableUpdate = this.prisma.$transaction(
async tx => {
const promises = data.map(async physicalcustomerquotetable => {
const physicalcustomerquotetablePrisma =
await tx.physicalCustomerQuoteTable.update({
data: PhysicalCustomerQuoteTablePrismaDTO.EntityToPrismaUpdate(
physicalcustomerquotetable,
),
where: { id: physicalcustomerquotetable.id },
include: { AdressDestiny: true, AdressOrigin: true },
});

return PhysicalCustomerQuoteTablePrismaDTO.PrismaToEntity(
physicalcustomerquotetablePrisma,
physicalcustomerquotetablePrisma.AdressOrigin,
physicalcustomerquotetablePrisma.AdressDestiny,
);
});

return Promise.all(promises);
},
);

return physicalcustomerquotetableUpdate;
}

async deletePhysicalCustomerQuoteTable(
id: string,
): Promise<PhysicalCustomerQuoteTable> {
const data = await this.prisma.physicalCustomerQuoteTable.delete({
where: { id },
include: { AdressDestiny: true, AdressOrigin: true },
});

return PhysicalCustomerQuoteTablePrismaDTO.PrismaToEntity(
data,
data.AdressOrigin,
data.AdressDestiny,
);
}
deleteManyPhysicalCustomerQuoteTable(
ids: string[],
): Promise<PhysicalCustomerQuoteTable[]> {
const physicalcustomerquotetableDeleted = this.prisma.$transaction(
async tx => {
const promises = ids.map(async icmdsId => {
const physicalcustomerquotetablePrisma =
await tx.physicalCustomerQuoteTable.delete({
where: { id: icmdsId },
include: { AdressDestiny: true, AdressOrigin: true },
});

return PhysicalCustomerQuoteTablePrismaDTO.PrismaToEntity(
physicalcustomerquotetablePrisma,
physicalcustomerquotetablePrisma.AdressOrigin,
physicalcustomerquotetablePrisma.AdressDestiny,
);
});

return Promise.all(promises);
},
);

return physicalcustomerquotetableDeleted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ export class PhysicalCustomerQuoteTableWhereArgs {
@IsOptional()
sort?: PhysicalCustomerQuoteTableOrderByWithRelationInput;
}

@ArgsType()
export class PhysicalCustomerQuoteTableCountArgs {
@Field(() => PhysicalCustomerQuoteTableWhereInput, { nullable: true })
@Type(() => PhysicalCustomerQuoteTableWhereInput)
@IsOptional()
where?: PhysicalCustomerQuoteTableWhereInput;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
IsNumber,
IsObject,
IsString,
IsUUID,
} from 'class-validator';

import { type IPhysicalCustomerQuoteTable } from 'domain/entities/QuoteTables/PhysicalCustomerQuoteTable/PhysicalCustomerQuoteTable';
Expand Down Expand Up @@ -109,3 +110,13 @@ export class PhysicalCustomerQuoteTableUpdate extends PartialType(
) {
updated_by: string;
}

@InputType()
export class PhysicalCustomerQuoteTableUpdateManyInput extends PartialType(
PhysicalCustomerQuoteTableInput,
) {
@Field()
@IsUUID()
@IsNotEmpty()
id: string;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UseGuards, UseInterceptors } from '@nestjs/common';
import {
Args,
Int,
Mutation,
Parent,
Query,
Expand All @@ -16,7 +17,10 @@ import { RecipientUseCases } from 'app/useCases/RecipientUseCase /RecipientUseCa
import { SenderUseCases } from 'app/useCases/SenderUseCase /SenderUseCases';
import { UserUseCases } from 'app/useCases/user/UserCases';

import { PhysicalCustomerQuoteTableWhereArgs } from 'infra/graphql/entities/PhysicalCustomerQuoteTableGraphql/Args/WherePhysicalCustomerQuoteTableArgs';
import {
PhysicalCustomerQuoteTableCountArgs,
PhysicalCustomerQuoteTableWhereArgs,
} from 'infra/graphql/entities/PhysicalCustomerQuoteTableGraphql/Args/WherePhysicalCustomerQuoteTableArgs';
import { AcessAllowed } from 'infra/graphql/utilities/decorators/AcessAllowed';
import { CurrentUser } from 'infra/graphql/utilities/decorators/CurrentUser';
import { RoleInterceptor } from 'infra/graphql/utilities/interceptors/RoleInterceptor';
Expand All @@ -30,6 +34,7 @@ import { GetPhysicalCustomerQuoteTableArgs } from './Args/GetPhysicalCustomerQuo
import {
PhysicalCustomerQuoteTableInput,
PhysicalCustomerQuoteTableUpdate,
PhysicalCustomerQuoteTableUpdateManyInput,
} from './PhysicalCustomerQuoteTable.input';
import { PhysicalCustomerQuoteTableModel } from './PhysicalCustomerQuoteTable.model';

Expand All @@ -45,6 +50,14 @@ export class PhysicalCustomerQuoteTableResolver {
private senderUseCase: SenderUseCases,
private icmsUseCase: IcmsUseCases,
) {}
@Query(() => Int)
async countPhysicalCustomerQuoteTable(
@Args() request: PhysicalCustomerQuoteTableCountArgs,
) {
return this.physicalCustomerQuoteTableUseCase.countPhysicalCustomerQuoteTable(
request,
);
}
@Query(() => PhysicalCustomerQuoteTableModel, { nullable: true })
async getPhysicalCustomerQuoteTable(
@Args() request: GetPhysicalCustomerQuoteTableArgs,
Expand Down Expand Up @@ -88,6 +101,36 @@ export class PhysicalCustomerQuoteTableResolver {
physicalCustomerQuoteTableUpdate,
);
}
@Mutation(() => [PhysicalCustomerQuoteTableModel])
async updateManyPhysicalCustomerQuoteTable(
@Args({
name: 'data',
type: () => [PhysicalCustomerQuoteTableUpdateManyInput],
})
data: PhysicalCustomerQuoteTableUpdateManyInput[],
@CurrentUser() user: User,
) {
return this.physicalCustomerQuoteTableUseCase.updateManyPhysicalCustomerQuoteTable(
data,
user.id,
);
}
@Mutation(() => PhysicalCustomerQuoteTableModel)
async deletePhysicalCustomerQuoteTable(@Args('id') id: string) {
return this.physicalCustomerQuoteTableUseCase.deletePhysicalCustomerQuoteTable(
id,
);
}

@Mutation(() => [PhysicalCustomerQuoteTableModel])
async deleteManyPhysicalCustomerQuoteTable(
@Args({ name: 'ids', type: () => [String] })
ids: string[],
) {
return this.physicalCustomerQuoteTableUseCase.deleteManyPhysicalCustomerQuoteTable(
ids,
);
}
@ResolveField(() => UserModelRefereces)
async createdUser(@Parent() user: PhysicalCustomerQuoteTableInput) {
const { created_by: createdBy } = user;
Expand Down
Loading

0 comments on commit 0e08a3b

Please sign in to comment.