Files
dlearn-api/src/dataAccess/payment.dataAccess.ts
T

100 lines
2.5 KiB
TypeScript

import mongoose from "mongoose";
import Models from "../models";
import Payment from "../models/payment.model";
export class PaymentDataAccess {
async createPayment(userId: string, amount: number, courseId: string,transactionNumber:string ,authority: string, session: any): Promise<Payment> {
const paymentDoc = await Models.Payment.create([{ userId, amount, courseId, transactionNumber, authority }], { session });
console.log({ paymentDoc });
const payment = paymentDoc.map(doc => doc.toObject())[0];
return payment;
}
async updatePaymentStatus(paymentId: string,status: string) {
const updatedPayment = await Models.Payment.findByIdAndUpdate(paymentId, { paymentStatus: status }, { new: true });
return updatedPayment;
}
async getPayment(paymentId: string): Promise<Payment | null> {
const payment = await Models.Payment.findById(paymentId);
return payment;
}
async getPaymentByAuthority(authority: string): Promise<Payment | null> {
console.log({ authority })
const payment = await Models.Payment.findOne({ authority });
console.log(payment, "ssssss")
return payment;
}
async getPaymentsByUserId(userId: string): Promise<Payment[]> {
const payments = await Models.Payment.aggregate([
{
$match: { userId: new mongoose.Types.ObjectId(userId) }
},
{
$lookup: {
from: "courses",
localField: "courseId",
foreignField: "_id",
as: "course",
}
},
{
$unwind: "$course",
},
{
$sort: { createdAt: -1 }
},
{
$project: {
_id:1,
authority: 1,
amount: 1,
transactionNumber: 1,
paymentStatus: 1,
course: {
name: 1,
slug: 1,
image: 1,
price: 1
}
}
}
]);
return payments;
}
async getPayments(): Promise<Payment[]> {
const payments = await Models.Payment.aggregate([
{
$lookup: {
from: "users",
localField: "userId",
foreignField: "_id",
as: "user",
},
},
{
$unwind: "$user",
},
{
$lookup: {
from: "courses",
localField: "courseId",
foreignField: "_id",
as: "course",
}
},
{
$unwind: "$course",
},
{
$sort: { createdAt: -1 }
}
]);
return payments;
}
}