forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.js
More file actions
137 lines (126 loc) · 3.09 KB
/
Copy pathTransaction.js
File metadata and controls
137 lines (126 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// models/Transaction.js
import mongoose from "mongoose";
const transactionSchema = new mongoose.Schema(
{
// Transaction identification
stellarTxHash: {
type: String,
required: true,
unique: true,
index: true,
},
stellarLedger: {
type: Number,
},
// Transaction kind: item purchase or sadaqah donation
type: {
type: String,
enum: ["purchase", "donation"],
default: "purchase",
index: true,
},
// Parties involved
buyer: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true,
index: true,
},
buyerWallet: {
type: String,
required: true,
},
creator: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: function () {
return this.type !== "donation";
},
index: true,
},
creatorWallet: {
type: String,
required: true,
},
// Item being purchased (not applicable to donations)
itemType: {
type: String,
enum: ["book", "course"],
required: function () {
return this.type !== "donation";
},
},
itemId: {
type: mongoose.Schema.Types.ObjectId,
required: function () {
return this.type !== "donation";
},
refPath: "itemTypeModel",
},
itemTypeModel: {
type: String,
enum: ["Book", "Course"],
required: function () {
return this.type !== "donation";
},
},
itemTitle: {
type: String,
required: function () {
return this.type !== "donation";
},
},
// Payment details
amount: {
type: String, // Store as string to preserve precision
required: true,
},
currency: {
type: String,
default: "USDC",
enum: ["USDC"],
},
network: {
type: String,
enum: ["testnet", "mainnet"],
required: true,
},
// Platform fee split (only set when a fee was applied at build time)
platformFee: {
feePercent: Number,
platformWallet: String,
platformAmount: String, // Stored as string to preserve precision
creatorAmount: String,
},
// Status tracking
status: {
type: String,
enum: ["pending", "submitted", "confirmed", "failed", "expired"],
default: "pending",
index: true,
},
// Error handling
failureReason: {
type: String,
},
retryCount: {
type: Number,
default: 0,
},
// Timestamps
submittedAt: Date,
confirmedAt: Date,
expiresAt: {
type: Date,
default: () => new Date(Date.now() + 30 * 60 * 1000), // 30 minutes
},
},
{ timestamps: true }
);
// Indexes for efficient queries
transactionSchema.index({ buyer: 1, status: 1 });
transactionSchema.index({ creator: 1, status: 1 });
transactionSchema.index({ itemType: 1, itemId: 1 });
transactionSchema.index({ type: 1, status: 1, createdAt: -1 }); // Donation stats
transactionSchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 }); // TTL for expired pending
export default mongoose.model("Transaction", transactionSchema);