Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/order/dto/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { BaseDTO } from 'src/utils/dto/base.dto';
import { ResponseOrderProductPresentationDetailDTO } from 'src/products/dto/product-presentation.dto';
import { ResponseBranchDTO } from 'src/branch/dto/branch.dto';
import { OrderDeliveryEmployeeDTO } from './order-delivery.dto';
import { PaymentMethod } from 'src/payments/entities/payment-information.entity';

export class CreateOrderDetailDTO {
@ApiProperty({ description: 'ID of the product presentation' })
Expand Down Expand Up @@ -71,6 +72,14 @@ export class CreateOrderDTO {
@Type(() => CreateOrderDetailDTO)
products: CreateOrderDetailDTO[];

@ApiProperty({
description: 'Payment method (CASH, CARD, etc.)',
enum: PaymentMethod,
})
@IsEnum(PaymentMethod)
@IsOptional()
paymentMethod: PaymentMethod;

constructor(
type: OrderType,
products: CreateOrderDetailDTO[],
Expand Down Expand Up @@ -122,6 +131,15 @@ export class ResponseOrderDTO extends BaseDTO {
@Expose()
@ApiProperty({ description: 'Total price of the order' })
totalPrice: number;

@Expose()
@ApiProperty({
description: 'Payment method (CASH, CARD, etc.)',
enum: PaymentMethod,
})
@IsEnum(PaymentMethod)
@IsOptional()
paymentMethod: PaymentMethod;
}

export class ResponseOrderDetailedDTO extends ResponseOrderDTO {
Expand Down
16 changes: 16 additions & 0 deletions src/order/entities/order.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { User } from 'src/user/entities/user.entity';
import { BaseModel, UUIDModel } from 'src/utils/entity';
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
import { OrderDelivery, OrderDetailDelivery } from './order_delivery.entity';
import { PaymentMethod } from 'src/payments/entities/payment-information.entity';
import { PaymentConfirmation } from 'src/payments/entities/payment-confirmation.entity';

export enum OrderType {
PICKUP = 'pickup',
Expand Down Expand Up @@ -47,6 +49,20 @@ export class Order extends BaseModel {

@OneToMany(() => OrderDelivery, (orderDelivery) => orderDelivery.order)
orderDeliveries: OrderDelivery[];

@Column({
type: 'enum',
enum: PaymentMethod,
name: 'payment_method',
default: PaymentMethod.CASH,
})
paymentMethod: PaymentMethod;

@OneToMany(
() => PaymentConfirmation,
(paymentConfirmation) => paymentConfirmation.order,
)
paymentConfirmations: PaymentConfirmation[];
}

@Entity()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddPaymentMethodOrderMigration1746555455789
implements MigrationInterface
{
name = 'AddPaymentMethodOrderMigration1746555455789';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "cart_item" DROP CONSTRAINT "FK_67a2e8406e01ffa24ff9026944e"`,
);
await queryRunner.query(
`ALTER TABLE "payment_confirmation" ADD "order_id" uuid`,
);
await queryRunner.query(
`CREATE TYPE "public"."order_payment_method_enum" AS ENUM('card', 'mobile_payment', 'bank_transfer', 'cash')`,
);
await queryRunner.query(
`ALTER TABLE "order" ADD "payment_method" "public"."order_payment_method_enum" NOT NULL DEFAULT 'cash'`,
);
await queryRunner.query(
`ALTER TABLE "payment_confirmation" ADD CONSTRAINT "FK_de3ea608b9f32c2184b551c554b" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE RESTRICT ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "cart_item" ADD CONSTRAINT "FK_7a462d104c801f5e9bb63806c1e" FOREIGN KEY ("product_presentation_id") REFERENCES "product_presentation"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "cart_item" DROP CONSTRAINT "FK_7a462d104c801f5e9bb63806c1e"`,
);
await queryRunner.query(
`ALTER TABLE "payment_confirmation" DROP CONSTRAINT "FK_de3ea608b9f32c2184b551c554b"`,
);
await queryRunner.query(`ALTER TABLE "order" DROP COLUMN "payment_method"`);
await queryRunner.query(`DROP TYPE "public"."order_payment_method_enum"`);
await queryRunner.query(
`ALTER TABLE "payment_confirmation" DROP COLUMN "order_id"`,
);
await queryRunner.query(
`ALTER TABLE "cart_item" ADD CONSTRAINT "FK_67a2e8406e01ffa24ff9026944e" FOREIGN KEY ("product_presentation_id") REFERENCES "product_presentation"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}
}
1 change: 1 addition & 0 deletions src/order/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class OrderService {
branch,
type: createOrderDTO.type,
totalPrice: totalPrice,
paymentMethod: createOrderDTO.paymentMethod,
});
const order = await this.orderRepository.save(orderToCreate);
const orderDetails = productsWithQuantity.map((product) => {
Expand Down
13 changes: 10 additions & 3 deletions src/payments/dto/payment-confirmation.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ApiProperty, IntersectionType } from '@nestjs/swagger';
import { IsString } from 'class-validator';
import { IsString, IsUUID } from 'class-validator';
import { BaseDTO } from 'src/utils/dto/base.dto';

export class CreatePaymentConfirmationDTO {
class PaymentConfirmationDTO {
@ApiProperty()
@IsString()
bank: string;
Expand All @@ -20,7 +20,14 @@ export class CreatePaymentConfirmationDTO {
phoneNumber: string;
}

export class CreatePaymentConfirmationDTO {
@ApiProperty()
@IsString()
@IsUUID()
orderId: string;
}

export class ResponsePaymentConfirmationDTO extends IntersectionType(
CreatePaymentConfirmationDTO,
PaymentConfirmationDTO,
BaseDTO,
) {}
9 changes: 7 additions & 2 deletions src/payments/entities/payment-confirmation.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Order } from 'src/order/entities/order.entity';
import { BaseModel } from 'src/utils/entity';
import { Column, Entity } from 'typeorm';
import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';

@Entity('payment_confirmation')
export class PaymentConfirmation extends BaseModel {
Expand All @@ -15,5 +16,9 @@ export class PaymentConfirmation extends BaseModel {
@Column({ type: 'character varying', name: 'phone_number' })
phoneNumber: string;

//order: Order;
@ManyToOne(() => Order, (order) => order.paymentConfirmations, {
onDelete: 'RESTRICT',
})
@JoinColumn({ name: 'order_id' })
order: Order;
}
2 changes: 2 additions & 0 deletions src/payments/payment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { PaymentConfirmationController } from './controllers/payment-confirmatio
import { PaymentInformationService } from './services/payment-information.service';
import { PaymentConfirmationService } from './services/payment-confirmation.service';
import { AuthModule } from 'src/auth/auth.module';
import { OrderModule } from 'src/order/order.module';

@Module({
imports: [
TypeOrmModule.forFeature([PaymentInformation, PaymentConfirmation]),
AuthModule,
OrderModule,
],
controllers: [PaymentInformationController, PaymentConfirmationController],
providers: [PaymentInformationService, PaymentConfirmationService],
Expand Down
10 changes: 8 additions & 2 deletions src/payments/services/payment-confirmation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { PaymentConfirmation } from '../entities/payment-confirmation.entity';
import { CreatePaymentConfirmationDTO } from '../dto/payment-confirmation.dto';
import { OrderService } from 'src/order/order.service';

@Injectable()
export class PaymentConfirmationService {
constructor(
@InjectRepository(PaymentConfirmation)
private paymentConfirmationRepository: Repository<PaymentConfirmation>,
private orderService: OrderService,
) {}

async create(
createPaymentConfirmationDto: CreatePaymentConfirmationDTO,
): Promise<PaymentConfirmation> {
const confirmation = this.paymentConfirmationRepository.create(
createPaymentConfirmationDto,
const order = await this.orderService.findOne(
createPaymentConfirmationDto.orderId,
);
const confirmation = this.paymentConfirmationRepository.create({
...createPaymentConfirmationDto,
order,
});
return this.paymentConfirmationRepository.save(confirmation);
}
}
7 changes: 7 additions & 0 deletions src/products/dto/product.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ export class ProductPresentationDTO extends BaseDTO {
}

export class ProductQueryDTO extends PaginationQueryDTO {
@IsOptional()
@Transform(({ value }: { value: string }) => (value ? value.split(',') : []))
@IsUUID(undefined, { each: true })
id: string[];

@IsOptional()
@Transform(({ value }: { value: string }) => (value ? value.split(',') : []))
@IsUUID(undefined, { each: true })
Expand Down Expand Up @@ -141,6 +146,7 @@ export class ProductQueryDTO extends PaginationQueryDTO {
genericProductId?: string[],
priceRange?: number[],
isVisible?: boolean,
id?: string[],
) {
super(page, limit);
this.q = q ? q : '';
Expand All @@ -151,5 +157,6 @@ export class ProductQueryDTO extends PaginationQueryDTO {
this.genericProductId = genericProductId ? genericProductId : [];
this.priceRange = priceRange ? priceRange : [];
this.isVisible = isVisible;
this.id = id ? id : [];
}
}
8 changes: 8 additions & 0 deletions src/products/products.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export class ProductsController {
type: Boolean,
example: true,
})
@ApiQuery({
name: 'id',
required: false,
description: 'Filter by product presentation ID',
type: String,
})
@ApiOkResponse({
description: 'Products obtained correctly.',
schema: {
Expand Down Expand Up @@ -132,6 +138,7 @@ export class ProductsController {
genericProductId,
priceRange,
isVisible,
id,
} = pagination;
const { products, total } = await this.productsServices.getProducts(
page,
Expand All @@ -144,6 +151,7 @@ export class ProductsController {
genericProductId,
priceRange,
isVisible,
id,
);

return {
Expand Down
7 changes: 7 additions & 0 deletions src/products/products.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class ProductsService {
genericProductIds?: string[],
priceRange?: number[],
isVisible?: boolean,
ids?: string[],
) {
let where = {};
if (searchQuery) {
Expand All @@ -32,6 +33,12 @@ export class ProductsService {
],
};
}
if (ids && ids.length > 0) {
where = {
...where,
id: In(ids),
};
}
if (categoryIds && categoryIds.length > 0) {
where = {
...where,
Expand Down
Loading