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 legal client quote t…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
GabrielDeSouzza committed Apr 27, 2024
1 parent 59bb13c commit 9fe840d
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { type ILegalClientQuoteTable } from 'domain/entities/QuoteTables/LegalClientQuoteTable/LegalClientQuoteTable';

export abstract class UpdateManyLegalClientQuoteTableDTO
implements Partial<ILegalClientQuoteTable>
{
id: string;
recipientId?: string;
senderId?: string;
who_pays?: string;
postalCodOrigin?: string;
postalCodDestiny?: string;
typeMerchandise?: string;
amount?: number;
description?: string;
mass?: number;
volume?: number;
nf_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 GetLegalClientQuoteTableDTO } from 'domain/dto/repositories/getDataDtos/GetLegalClientQuoteTableDto';
import { type FindAllLegalClientQuoteTableWhereRequestDTO } from 'domain/dto/repositories/whereDtos/LegalClientQuoteTableRepositoryDto';
import {
type CountLegalClientQuoteTableRequestDTO,
type FindAllLegalClientQuoteTableWhereRequestDTO,
} from 'domain/dto/repositories/whereDtos/LegalClientQuoteTableRepositoryDto';
import { LegalClientQuoteTable } from 'domain/entities/QuoteTables/LegalClientQuoteTable/LegalClientQuoteTable';
import { LegalClientQuoteTableRepository } from 'domain/repositories/LegalClientQuoteTable.repository';

import { type CreateLegalClientQuoteTableDTO } from 'app/dtos/LegalClientQuoteTableDto/CreateLegalClientQuoteTableDto';
import { type UpdateLegalClientQuoteTableDTO } from 'app/dtos/LegalClientQuoteTableDto/UpdateLegalClientQuoteTableDto';
import { type UpdateManyLegalClientQuoteTableDTO } from 'app/dtos/LegalClientQuoteTableDto/UpdateManyLegalClientQuoteTableDto';
import { generateRandomNumber } from 'app/utils/RandomNumber';

import { RecipientUseCases } from '../RecipientUseCase /RecipientUseCases';
Expand All @@ -21,6 +25,13 @@ export class LegalClientQuoteTableUseCases {
private senderUseCase: SenderUseCases,
private recipientUseCase: RecipientUseCases,
) {}
async countLegalClientQuoteTable(
request: CountLegalClientQuoteTableRequestDTO,
) {
return this.legalClientQuoteTableRepository.countLegalClientQuoteTable(
request,
);
}
async getLegalClientQuoteTable(request: GetLegalClientQuoteTableDTO) {
if (!request.id && !request.codQuote) {
throw new GraphQLError('IS NECESSARY AN ID OR COD QUOTE', {
Expand Down Expand Up @@ -120,4 +131,59 @@ export class LegalClientQuoteTableUseCases {
order,
);
}
async updateManyLegalClientQuoteTable(
data: UpdateManyLegalClientQuoteTableDTO[],
updateBy: string,
) {
for (const legalclientquotetable of data)
await this.verifyLegalClientQuoteTableExist(legalclientquotetable.id);
const legalclientquotetables = data.map(legalclientquotetable => {
const updateLegalClientQuoteTable = new LegalClientQuoteTable({
amount: legalclientquotetable.amount,
codQuote: null,
description: legalclientquotetable.description,
mass: legalclientquotetable.mass,
nf_value: legalclientquotetable.nf_value,
postalCodDestiny: legalclientquotetable.postalCodDestiny,
postalCodOrigin: legalclientquotetable.postalCodOrigin,
recipientId: legalclientquotetable.recipientId,
senderId: legalclientquotetable.senderId,
typeMerchandise: legalclientquotetable.typeMerchandise,
volume: legalclientquotetable.volume,
who_pays: legalclientquotetable.who_pays,
created_by: null,
updated_by: updateBy,
id: legalclientquotetable.id,
});

return updateLegalClientQuoteTable;
});

return this.legalClientQuoteTableRepository.updateManyLegalClientQuoteTable(
legalclientquotetables,
);
}
async deleteLegalClientQuoteTable(id: string) {
await this.getLegalClientQuoteTable({ id });

return this.legalClientQuoteTableRepository.deleteLegalClientQuoteTable(id);
}
async deleteManyLegalClientQuoteTable(ids: string[]) {
for (const legalclientquotetableId of ids)
await this.verifyLegalClientQuoteTableExist(legalclientquotetableId);

return this.legalClientQuoteTableRepository.deleteManyLegalClientQuoteTable(
ids,
);
}
private async verifyLegalClientQuoteTableExist(id: string) {
const exist =
await this.legalClientQuoteTableRepository.findLegalClientQuoteTable({
id,
});
if (!exist)
throw new GraphQLError(`THIS LEGALCLIENTQUOTETABLE 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 FindAllLegalClientQuoteTableWhereRequestDTO {
sort?: SortByLegalClientQuoteTableTypeDTO;
where?: WhereLegalClientQuoteTableTypeDTO;
}

export abstract class CountLegalClientQuoteTableRequestDTO {
where?: WhereLegalClientQuoteTableTypeDTO;
}
17 changes: 16 additions & 1 deletion src/domain/repositories/LegalClientQuoteTable.repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { type GetLegalClientQuoteTableDTO } from 'domain/dto/repositories/getDataDtos/GetLegalClientQuoteTableDto';
import { type FindAllLegalClientQuoteTableWhereRequestDTO } from 'domain/dto/repositories/whereDtos/LegalClientQuoteTableRepositoryDto';
import {
type CountLegalClientQuoteTableRequestDTO,
type FindAllLegalClientQuoteTableWhereRequestDTO,
} from 'domain/dto/repositories/whereDtos/LegalClientQuoteTableRepositoryDto';
import { type LegalClientQuoteTable } from 'domain/entities/QuoteTables/LegalClientQuoteTable/LegalClientQuoteTable';

export abstract class LegalClientQuoteTableRepository {
abstract countLegalClientQuoteTable(
request: CountLegalClientQuoteTableRequestDTO,
): Promise<number>;
abstract findLegalClientQuoteTable(
request: GetLegalClientQuoteTableDTO,
): Promise<LegalClientQuoteTable>;
Expand All @@ -16,4 +22,13 @@ export abstract class LegalClientQuoteTableRepository {
abstract findAllLegalClientQuoteTable(
paraments: FindAllLegalClientQuoteTableWhereRequestDTO,
): Promise<LegalClientQuoteTable[]>;
abstract updateManyLegalClientQuoteTable(
data: LegalClientQuoteTable[],
): Promise<LegalClientQuoteTable[]>;
abstract deleteLegalClientQuoteTable(
id: string,
): Promise<LegalClientQuoteTable>;
abstract deleteManyLegalClientQuoteTable(
ids: string[],
): Promise<LegalClientQuoteTable[]>;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Injectable } from '@nestjs/common';

import { type GetLegalClientQuoteTableDTO } from 'domain/dto/repositories/getDataDtos/GetLegalClientQuoteTableDto';
import { type FindAllLegalClientQuoteTableWhereRequestDTO } from 'domain/dto/repositories/whereDtos/LegalClientQuoteTableRepositoryDto';
import {
type CountLegalClientQuoteTableRequestDTO,
type FindAllLegalClientQuoteTableWhereRequestDTO,
} from 'domain/dto/repositories/whereDtos/LegalClientQuoteTableRepositoryDto';
import { type LegalClientQuoteTable } from 'domain/entities/QuoteTables/LegalClientQuoteTable/LegalClientQuoteTable';
import { type LegalClientQuoteTableRepository } from 'domain/repositories/LegalClientQuoteTable.repository';

Expand All @@ -13,6 +16,13 @@ export class LegalClientQuoteTablePrismaService
implements LegalClientQuoteTableRepository
{
constructor(private prisma: PrismaService) {}
countLegalClientQuoteTable(
request: CountLegalClientQuoteTableRequestDTO,
): Promise<number> {
return this.prisma.legalClientQuoteTable.count({
where: request.where ?? undefined,
});
}
async findLegalClientQuoteTable(
request: GetLegalClientQuoteTableDTO,
): Promise<LegalClientQuoteTable> {
Expand Down Expand Up @@ -71,4 +81,56 @@ export class LegalClientQuoteTablePrismaService
LegalClientQuoteTablePrismaDTO.PrismaToEntity(legalclientquotetable),
);
}
updateManyLegalClientQuoteTable(
data: LegalClientQuoteTable[],
): Promise<LegalClientQuoteTable[]> {
console.log(data);
const legalclientquotetableUpdate = this.prisma.$transaction(async tx => {
const promises = data.map(async legalclientquotetable => {
const legalclientquotetablePrisma =
await tx.legalClientQuoteTable.update({
data: LegalClientQuoteTablePrismaDTO.EntityToPrismaUpdate(
legalclientquotetable,
),
where: { id: legalclientquotetable.id },
});

return LegalClientQuoteTablePrismaDTO.PrismaToEntity(
legalclientquotetablePrisma,
);
});

return Promise.all(promises);
});

return legalclientquotetableUpdate;
}

async deleteLegalClientQuoteTable(
id: string,
): Promise<LegalClientQuoteTable> {
return LegalClientQuoteTablePrismaDTO.PrismaToEntity(
await this.prisma.legalClientQuoteTable.delete({ where: { id } }),
);
}
deleteManyLegalClientQuoteTable(
ids: string[],
): Promise<LegalClientQuoteTable[]> {
const legalclientquotetableDeleted = this.prisma.$transaction(async tx => {
const promises = ids.map(async icmdsId => {
const legalclientquotetablePrisma =
await tx.legalClientQuoteTable.delete({
where: { id: icmdsId },
});

return LegalClientQuoteTablePrismaDTO.PrismaToEntity(
legalclientquotetablePrisma,
);
});

return Promise.all(promises);
});

return legalclientquotetableDeleted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ export class LegalClientQuoteTableWhereArgs {
@IsOptional()
sort?: LegalClientQuoteTableOrderByWithRelationInput;
}
@ArgsType()
export class LegalClientQuoteTableCountArgs {
@Field(() => LegalClientQuoteTableWhereInput, { nullable: true })
@Type(() => LegalClientQuoteTableWhereInput)
@IsOptional()
where?: LegalClientQuoteTableWhereInput;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import {
Field,
HideField,
InputType,
Int,
OmitType,
PartialType,
} from '@nestjs/graphql';

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

import { type ILegalClientQuoteTable } from 'domain/entities/QuoteTables/LegalClientQuoteTable/LegalClientQuoteTable';

Expand Down Expand Up @@ -41,7 +42,7 @@ export class LegalClientQuoteTableInput
@IsString()
@IsNotEmpty()
typeMerchandise: string;
@Field()
@Field(() => Int)
@IsNumber()
@IsNotEmpty()
amount: number;
Expand Down Expand Up @@ -75,3 +76,13 @@ export class LegalClientQuoteTableUpdate extends PartialType(
) {
updated_by: string;
}

@InputType()
export class LegalClientQuoteTableUpdateManyInput extends PartialType(
LegalClientQuoteTableInput,
) {
@Field()
@IsUUID()
@IsNotEmpty()
id: string;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Field, ObjectType } from '@nestjs/graphql';
import { Field, Int, ObjectType } from '@nestjs/graphql';

import { type ILegalClientQuoteTable } from 'domain/entities/QuoteTables/LegalClientQuoteTable/LegalClientQuoteTable';

Expand All @@ -23,7 +23,7 @@ export class LegalClientQuoteTableModel implements ILegalClientQuoteTable {
postalCodDestiny: string;
@Field()
typeMerchandise: string;
@Field()
@Field(() => Int)
amount: number;
@Field()
description: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import { RecipientUseCases } from 'app/useCases/RecipientUseCase /RecipientUseCa
import { SenderUseCases } from 'app/useCases/SenderUseCase /SenderUseCases';
import { UserUseCases } from 'app/useCases/user/UserCases';

import { LegalClientQuoteTableWhereArgs } from 'infra/graphql/entities/LegalClientQuoteTableGraphql/Args/WhereLegalClientQuoteTableArgs';
import {
LegalClientQuoteTableCountArgs,
LegalClientQuoteTableWhereArgs,
} from 'infra/graphql/entities/LegalClientQuoteTableGraphql/Args/WhereLegalClientQuoteTableArgs';
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 @@ -28,6 +31,7 @@ import { GetLegalClientQuoteTableArgs } from './Args/GetLegalClientQuoteTableArg
import {
LegalClientQuoteTableInput,
LegalClientQuoteTableUpdate,
LegalClientQuoteTableUpdateManyInput,
} from './LegalClientQuoteTable.input';
import { LegalClientQuoteTableModel } from './LegalClientQuoteTable.model';

Expand All @@ -42,6 +46,14 @@ export class LegalClientQuoteTableResolver {
private recipientUseCase: RecipientUseCases,
private senderUseCase: SenderUseCases,
) {}
@Query(() => Number)
async countLegalClientQuoteTable(
@Args() request: LegalClientQuoteTableCountArgs,
) {
return this.legalClientQuoteTableUseCase.countLegalClientQuoteTable(
request,
);
}
@Query(() => LegalClientQuoteTableModel, { nullable: true })
async getLegalClientQuoteTable(
@Args() request: GetLegalClientQuoteTableArgs,
Expand Down Expand Up @@ -85,6 +97,32 @@ export class LegalClientQuoteTableResolver {
legalClientQuoteTableUpdate,
);
}

@Mutation(() => [LegalClientQuoteTableModel])
async updateManyLegalClientQuoteTable(
@Args({ name: 'data', type: () => [LegalClientQuoteTableUpdateManyInput] })
data: LegalClientQuoteTableUpdateManyInput[],
@CurrentUser() user: User,
) {
return this.legalClientQuoteTableUseCase.updateManyLegalClientQuoteTable(
data,
user.id,
);
}
@Mutation(() => LegalClientQuoteTableModel)
async deleteLegalClientQuoteTable(@Args('id') id: string) {
return this.legalClientQuoteTableUseCase.deleteLegalClientQuoteTable(id);
}

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

0 comments on commit 9fe840d

Please sign in to comment.