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

Commit

Permalink
fix: add delete many freight expenses in physical customer oder
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielDeSouzza committed Jun 25, 2024
1 parent bec2249 commit aeedefe
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export abstract class UpdatePhysicalCustomerOrderDTO {
updated_by?: string;
carrier_id?: string;
expenses?: IExpense[];
deleted_expenses?: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class PhysicalCustomerOrderUseCases {
await this.physicalCustomerOrderRepository.findPhysicalCustomerOrder({
id,
});

console.log(orderExist);
const order = new PhysicalCustomerOrder({
created_by: null,
physicalCustomerId: data.physicalCustomerId,
Expand All @@ -127,7 +127,9 @@ export class PhysicalCustomerOrderUseCases {
order: null,
quote_table_id: data.quote_table_id,
expenses: data.expenses,
deleted_expenses: data.deleted_expenses,
});
console.log('ORDER', order);

return this.physicalCustomerOrderRepository.updatePhysicalCustomerOrder(
id,
Expand Down
2 changes: 2 additions & 0 deletions src/app/useCases/user/UserCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class UserUseCases {
}

async getUser(search: getUserDto): Promise<User> {
console.log(search);

if (!search.id && !search.email) {
throw new GraphQLError('Is necessary to provide an ID or EMAIL');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface IPhysicalCustomerOrder {
updated_by: string;
created_by: string;
expenses?: IExpense[];
deleted_expenses?: string[];
}

export class PhysicalCustomerOrder extends Entity {
Expand Down Expand Up @@ -56,7 +57,7 @@ export class PhysicalCustomerOrder extends Entity {
throw new NotificationError(errors);
}

this.totolExpense = props?.expenses?.reduce(
this.totolExpense = props.expenses?.reduce(
(total, expense) => total + expense.value,
0,
);
Expand Down Expand Up @@ -155,6 +156,15 @@ export class PhysicalCustomerOrder extends Entity {
this.props.order = order;
}

get deleted_expenses(): string[] {
return this.props.deleted_expenses;
}

set deleted_expenses(deleted_expenses: string[]) {
console.log('SSS', deleted_expenses);
this.props.deleted_expenses = deleted_expenses;
}

get physicalCustomerId(): string {
return this.props.physicalCustomerId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type FreightExpenses as FreightExpensePrisma,
} from '@prisma/client';

import { type IExpense } from 'domain/entities/LegalClientEntities/LegalClientOrder/LegaClientOrder';
import { PhysicalCustomerOrder } from 'domain/entities/PhysicalClientEntities/physicalCustomerOrder/PhysicalCustomerOrder';

export class PhysicalCustomerOrderPrismaDTO {
Expand Down Expand Up @@ -38,7 +39,6 @@ export class PhysicalCustomerOrderPrismaDTO {
public static EntityToCreatePrisma(
physicalCustomerOrder: PhysicalCustomerOrder,
) {
console.log(physicalCustomerOrder.expenses);
const physicalCustomerOrderPrisma: Prisma.PhysicalCustomerOrderCreateInput =
{
CreatedBy: { connect: { id: physicalCustomerOrder.created_by } },
Expand Down Expand Up @@ -71,6 +71,11 @@ export class PhysicalCustomerOrderPrismaDTO {
public static EntityToPrismaUpdate(
physicalCustomerOrder: PhysicalCustomerOrder,
) {
const x = this.freightExpensesQuery(
physicalCustomerOrder.expenses,
physicalCustomerOrder.deleted_expenses,
);
console.log(physicalCustomerOrder);
const physicalCustomerOrderPrisma: Prisma.PhysicalCustomerOrderUpdateInput =
{
PhysicalCustomer: physicalCustomerOrder.physicalCustomerId
Expand All @@ -89,23 +94,52 @@ export class PhysicalCustomerOrderPrismaDTO {
total_receivable: physicalCustomerOrder.total_receivable,
total_shipping_cost: physicalCustomerOrder.total_shipping_cost,
total_tax_payable: physicalCustomerOrder.total_tax_payable,
FreightExpenses: physicalCustomerOrder.expenses
? {
upsert: physicalCustomerOrder.expenses.map(expense => ({
create: {
expense_name: expense.expenseName,
value: expense.value,
},
update: {
expense_name: expense.expenseName,
value: expense.value,
},
where: { id: expense.id ?? '' },
})),
}
: undefined,
FreightExpenses: x,
};

return physicalCustomerOrderPrisma;
}
private static freightExpensesQuery(
expenses?: IExpense[],
deleted?: string[],
): Prisma.FreightExpensesUpdateManyWithoutPhysicalCustomerOrderNestedInput {
if (expenses && deleted) {
return {
disconnect: deleted.map(expenseId => ({
id: expenseId,
})),
upsert: expenses.map(expense => ({
create: {
expense_name: expense.expenseName,
value: expense.value,
},
update: {
expense_name: expense.expenseName,
value: expense.value,
},
where: { id: expense.id ?? '' },
})),
};
} else if (!expenses && deleted) {
return {
disconnect: deleted.map(expenseId => ({
id: expenseId,
})),
};
} else if (expenses && !deleted) {
return {
upsert: expenses.map(expense => ({
create: {
expense_name: expense.expenseName,
value: expense.value,
},
update: {
expense_name: expense.expenseName,
value: expense.value,
},
where: { id: expense.id ?? '' },
})),
};
} else return undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export class PhysicalCustomerOrderUpdateInput extends PartialType(
@IsOptional()
@Type(() => FreightExpenseUpdateOrderInput)
expenses?: FreightExpenseUpdateOrderInput[];
@Field(() => [String], { nullable: true })
@Allow()
deleted_expenses: string[];
}
@InputType()
export class PhysicalCustomerOrderUpdateManyInput extends PartialType(
Expand Down
1 change: 1 addition & 0 deletions src/infra/graphql/generated/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -2773,6 +2773,7 @@ input PhysicalCustomerOrderOrderByWithRelationInput {

input PhysicalCustomerOrderUpdateInput {
carrier_id: String
deleted_expenses: [String!]
expenses: [FreightExpenseUpdateOrderInput!]
physicalCustomerId: String
quote_table_id: String
Expand Down

0 comments on commit aeedefe

Please sign in to comment.