feat: get more data for admin and user about payment
This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
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, authority: string, session: any): Promise<Payment> {
|
||||
const paymentDoc = await Models.Payment.create([{ userId, amount, courseId, authority }], { session });
|
||||
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;
|
||||
@@ -20,13 +26,73 @@ export class PaymentDataAccess {
|
||||
console.log(payment, "ssssss")
|
||||
return payment;
|
||||
}
|
||||
|
||||
async getPaymentsByUserId(userId: string): Promise<Payment[]> {
|
||||
const payments = await Models.Payment.find({ userId });
|
||||
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.find();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user