Skip to content
Open
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
2 changes: 1 addition & 1 deletion backend/api-inventory/src/dto/pay.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class CreatePaymentDto {

export class RefundPaymentDto {
paymentId: string;
refundAmount: number; // Partial refunds may require an amount
amount: number; // Partial refunds may require an amount
}

export class PaymentIdDto {
Expand Down
104 changes: 38 additions & 66 deletions backend/api-inventory/src/payment-processing/pay.service.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,35 @@
import { Injectable, BadRequestException, BadGatewayException, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { Injectable, BadRequestException, BadGatewayException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service'; // Assuming you're using Prisma
import { CreatePaymentDto, RefundPaymentDto } from 'src/dto/pay.dto';


@Injectable()
export class PaymentService {
constructor(private readonly prismaService: PrismaService) {}

async createPayment(createPaymentDto: CreatePaymentDto) {
if (!createPaymentDto) {
throw new BadRequestException('Please provide valid payment data.');
}

try {
const payment = await this.prismaService.payment.create({
data: {
...createPaymentDto,
status: 'completed',
createdAt: new Date(),
},
});

return {
success: true,
message: 'Payment processed successfully',
data: payment,
};
} catch (error) {
throw new BadGatewayException('Failed to process payment. Please try again later.');
}
// Logic to process the payment
const payment = await this.prismaService.payment.create({
data: {
...createPaymentDto,
status: 'completed',
createdAt: new Date(),
},
});
return {
success: true,
message: 'Payment processed successfully',
data: payment,
};
}

async getPaymentDetails(paymentId: string) {
if (!paymentId) {
throw new BadRequestException('Please provide a valid payment ID.');
}

const payment = await this.prismaService.payment.findUnique({
where: { id: paymentId },
});

if (!payment) {
throw new NotFoundException('Payment not found.');
throw new BadRequestException('Payment not found');
}

return {
success: true,
message: 'Payment details retrieved successfully',
Expand All @@ -51,44 +38,29 @@ export class PaymentService {
}

async processRefund(refundPaymentDto: RefundPaymentDto) {
if (!refundPaymentDto) {
throw new BadRequestException('Please provide valid refund data.');
// Logic to process the refund
const obj = await this.prismaService.payment.findUnique({where : {id : refundPaymentDto.paymentId}})
if(!obj.id){
throw new BadGatewayException({
success : false,
message : 'id is not valid'
})
}

const payment = await this.prismaService.payment.findUnique({
where: { id: refundPaymentDto.paymentId },
});

if (!payment) {
throw new NotFoundException('Payment not found with the provided ID.');
}

if (refundPaymentDto.refundAmount > payment.amount) {
throw new BadRequestException('Refund amount exceeds the original payment amount.');
}

try {
const refund = await this.prismaService.refund.create({
data: {
paymentId: refundPaymentDto.paymentId,
amount: refundPaymentDto.refundAmount, // Use 'amount' instead of 'refundAmount'
status: 'refunded',
createdAt: new Date(),
},
});
const actualAmt = obj.amount;
// case bnakr smabhal liyo pls

return {
success: true,
message: 'Refund processed successfully',
data: refund,
};
} catch (error) {
throw new BadRequestException({
success: false,
message: 'Error processing refund',
error: error.message,
});
}

const refund = await this.prismaService.refund.create({
data: {
...refundPaymentDto,
status: 'refunded',
createdAt: new Date(),
},
});
return {
success: true,
message: 'Refund processed successfully',
data: refund,
};
}
}
4 changes: 2 additions & 2 deletions backend/api-inventory/src/product-management/pm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export class PmService {
updatedAt: new Date(),
...product,
};

console.log("----------------------")
const res = await this.prismaService.product.create({ data: newProduct })

console.log("----------------------")
try {
if (res) {
return {
Expand Down