forked from MyZubster-Ecosystem/myzubster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaiForwardController.js
More file actions
199 lines (174 loc) · 6.01 KB
/
Copy pathaiForwardController.js
File metadata and controls
199 lines (174 loc) · 6.01 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const AIContract = require('../models/AIContract');
const githubController = require('./githubWebhookController');
const users = githubController.users || {};
const DISCOUNT_RATES = {
1: 0.10,
2: 0.15,
3: 0.20,
6: 0.30
};
const BASE_PRICE_MYZ = 10;
module.exports = {
getQuote: (req, res) => {
const { model, tokens, deliveryMonths } = req.body;
if (!model || !tokens || !deliveryMonths) {
return res.status(400).json({ error: 'Mancano parametri' });
}
const discount = DISCOUNT_RATES[deliveryMonths] || 0;
const pricePerM = BASE_PRICE_MYZ * (1 - discount);
const totalPrice = pricePerM * (tokens / 1_000_000);
res.json({
model,
tokens,
deliveryMonths,
discount: discount * 100,
pricePerM,
totalPrice: Math.round(totalPrice * 100) / 100,
currency: 'MYZ'
});
},
purchaseContract: async (req, res) => {
try {
const { userId, model, tokens, deliveryMonths, paymentMethod } = req.body;
if (!userId || !model || !tokens || !deliveryMonths) {
return res.status(400).json({ error: 'Dati mancanti' });
}
const user = users[userId];
if (!user) return res.status(404).json({ error: 'Utente non trovato' });
const discount = DISCOUNT_RATES[deliveryMonths] || 0;
const pricePerM = BASE_PRICE_MYZ * (1 - discount);
const totalPrice = Math.round((pricePerM * (tokens / 1_000_000)) * 100) / 100;
if (paymentMethod === 'MYZ') {
if (user.myzBalance < totalPrice) {
return res.status(400).json({ error: 'MYZ insufficienti' });
}
user.myzBalance -= totalPrice;
} else {
return res.status(501).json({ error: 'Pagamento XMR non ancora implementato' });
}
const deliveryDate = new Date();
deliveryDate.setMonth(deliveryDate.getMonth() + deliveryMonths);
deliveryDate.setDate(1);
const contract = new AIContract({
userId,
model,
tokens,
priceMYZ: totalPrice,
deliveryMonth: deliveryDate,
discount: discount * 100,
expiresAt: new Date(deliveryDate.getFullYear(), deliveryDate.getMonth() + 1, 0)
});
await contract.save();
res.status(201).json({
success: true,
contract: {
id: contract._id,
model,
tokens,
priceMYZ: totalPrice,
discount: discount * 100,
deliveryMonth: deliveryDate,
expiresAt: contract.expiresAt
},
newBalance: user.myzBalance
});
} catch (error) {
console.error('❌ Errore acquisto contratto:', error);
res.status(500).json({ error: 'Errore interno', details: error.message });
}
},
getUserContracts: async (req, res) => {
try {
const { userId } = req.params;
const contracts = await AIContract.find({ userId }).sort({ deliveryMonth: 1 });
res.json({ success: true, contracts });
} catch (error) {
console.error('❌ Errore lista contratti:', error);
res.status(500).json({ error: 'Errore interno' });
}
},
consumeTokens: async (req, res) => {
try {
const { userId, model, tokensUsed } = req.body;
if (!userId || !model || !tokensUsed) {
return res.status(400).json({ error: 'userId, model e tokensUsed sono obbligatori' });
}
const contract = await AIContract.findOne({
userId,
model,
status: 'active',
expiresAt: { $gte: new Date() }
}).sort({ deliveryMonth: 1 });
if (!contract) {
return res.status(404).json({ error: 'Nessun contratto attivo disponibile' });
}
const remaining = contract.tokens - contract.consumedTokens;
if (remaining < tokensUsed) {
return res.status(400).json({ error: `Token insufficienti. Rimasti: ${remaining}` });
}
contract.consumedTokens += tokensUsed;
if (contract.consumedTokens >= contract.tokens) {
contract.status = 'consumed';
}
await contract.save();
res.json({
success: true,
contractId: contract._id,
remaining: contract.tokens - contract.consumedTokens,
totalConsumed: contract.consumedTokens
});
} catch (error) {
console.error('❌ Errore consumo token:', error);
res.status(500).json({ error: 'Errore interno', details: error.message });
}
},
listForResale: async (req, res) => {
try {
const { contractId, resalePrice } = req.body;
if (!contractId) return res.status(400).json({ error: 'contractId obbligatorio' });
const contract = await AIContract.findById(contractId);
if (!contract) return res.status(404).json({ error: 'Contratto non trovato' });
if (contract.status !== 'active') return res.status(400).json({ error: 'Contratto non attivo' });
contract.status = 'listed_for_sale';
contract.resalePrice = resalePrice || contract.priceMYZ * 1.1;
await contract.save();
res.json({ success: true, contract });
} catch (error) {
console.error('❌ Errore rivendita:', error);
res.status(500).json({ error: 'Errore interno' });
}
},
// Nuovo: Ottieni il saldo token rimanente per un utente/modello
getBalance: async (req, res) => {
try {
const { userId, model } = req.params;
if (!userId || !model) {
return res.status(400).json({ error: 'userId e model sono obbligatori' });
}
const contracts = await AIContract.find({
userId,
model,
status: 'active',
expiresAt: { $gte: new Date() }
});
let totalRemaining = 0;
for (const c of contracts) {
totalRemaining += (c.tokens - c.consumedTokens);
}
res.json({
success: true,
userId,
model,
totalRemaining,
contracts: contracts.map(c => ({
id: c._id,
remaining: c.tokens - c.consumedTokens,
expiresAt: c.expiresAt
}))
});
} catch (error) {
console.error('❌ Errore saldo token:', error);
res.status(500).json({ error: 'Errore interno' });
}
}
};