push
Browse files
src/controllers/chatController.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Request, Response } from 'express';
|
|
| 2 |
import { AppDataSource } from '../config/database';
|
| 3 |
import { Agent, AgentStatus } from '../entities/Agent';
|
| 4 |
import { Subscription, SubscriptionStatus } from '../entities/Subscription';
|
|
|
|
| 5 |
import { ChatProtocolService } from '../services/chatProtocol';
|
| 6 |
import { WalletService } from '../services/walletService';
|
| 7 |
|
|
@@ -39,17 +40,27 @@ export class ChatController {
|
|
| 39 |
});
|
| 40 |
}
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
const agent = await agentRepository.findOne({
|
| 44 |
-
where: {
|
|
|
|
|
|
|
|
|
|
| 45 |
});
|
| 46 |
|
| 47 |
if (!agent) {
|
| 48 |
-
return res.status(404).json({ error: 'Agent not found
|
| 49 |
}
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
// Check wallet balance and charge points (first 2 tasks free)
|
| 52 |
-
if (
|
| 53 |
const chargeResult = await walletService.chargeForTask(userId, agentId, agent);
|
| 54 |
|
| 55 |
if (!chargeResult.success) {
|
|
@@ -69,6 +80,27 @@ export class ChatController {
|
|
| 69 |
},
|
| 70 |
});
|
| 71 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
|
| 74 |
// Convert multer files to chat protocol format
|
|
@@ -105,15 +137,20 @@ export class ChatController {
|
|
| 105 |
try {
|
| 106 |
const { id: agentId } = req.params;
|
| 107 |
const { conversationId, limit } = req.query;
|
| 108 |
-
const
|
|
|
|
|
|
|
| 109 |
|
| 110 |
if (!userId) {
|
| 111 |
return res.status(401).json({ error: 'Authentication required' });
|
| 112 |
}
|
| 113 |
|
| 114 |
-
//
|
| 115 |
const agent = await agentRepository.findOne({
|
| 116 |
-
where: {
|
|
|
|
|
|
|
|
|
|
| 117 |
});
|
| 118 |
|
| 119 |
if (!agent) {
|
|
|
|
| 2 |
import { AppDataSource } from '../config/database';
|
| 3 |
import { Agent, AgentStatus } from '../entities/Agent';
|
| 4 |
import { Subscription, SubscriptionStatus } from '../entities/Subscription';
|
| 5 |
+
import { Transaction, TransactionType, TransactionStatus } from '../entities/Transaction';
|
| 6 |
import { ChatProtocolService } from '../services/chatProtocol';
|
| 7 |
import { WalletService } from '../services/walletService';
|
| 8 |
|
|
|
|
| 40 |
});
|
| 41 |
}
|
| 42 |
|
| 43 |
+
const user = (req as any).user;
|
| 44 |
+
const isAdmin = user?.isAdmin || false;
|
| 45 |
+
|
| 46 |
+
// Get agent - admins can test pending/rejected agents, regular users only approved
|
| 47 |
const agent = await agentRepository.findOne({
|
| 48 |
+
where: {
|
| 49 |
+
id: agentId,
|
| 50 |
+
...(isAdmin ? {} : { status: AgentStatus.APPROVED })
|
| 51 |
+
},
|
| 52 |
});
|
| 53 |
|
| 54 |
if (!agent) {
|
| 55 |
+
return res.status(404).json({ error: 'Agent not found' });
|
| 56 |
}
|
| 57 |
|
| 58 |
+
// Admins don't pay for unapproved agents (pending/rejected) - they're testing quality
|
| 59 |
+
const isAdminTestingUnapproved = isAdmin && agent.status !== AgentStatus.APPROVED;
|
| 60 |
+
const shouldCharge = userId && !isAdminTestingUnapproved;
|
| 61 |
+
|
| 62 |
// Check wallet balance and charge points (first 2 tasks free)
|
| 63 |
+
if (shouldCharge) {
|
| 64 |
const chargeResult = await walletService.chargeForTask(userId, agentId, agent);
|
| 65 |
|
| 66 |
if (!chargeResult.success) {
|
|
|
|
| 80 |
},
|
| 81 |
});
|
| 82 |
}
|
| 83 |
+
} else if (isAdminTestingUnapproved) {
|
| 84 |
+
// Create a free transaction for admin testing (for tracking purposes)
|
| 85 |
+
const wallet = await walletService.getOrCreateWallet(userId);
|
| 86 |
+
const transactionRepository = AppDataSource.getRepository(Transaction);
|
| 87 |
+
const transaction = transactionRepository.create({
|
| 88 |
+
walletId: wallet.id,
|
| 89 |
+
userId,
|
| 90 |
+
agentId,
|
| 91 |
+
type: TransactionType.ADMIN_TEST,
|
| 92 |
+
status: TransactionStatus.COMPLETED,
|
| 93 |
+
amount: 0,
|
| 94 |
+
balanceAfter: Number(wallet.balance),
|
| 95 |
+
description: `Admin test of ${agent.name} (${agent.status}) - no charge`,
|
| 96 |
+
metadata: {
|
| 97 |
+
agentId,
|
| 98 |
+
agentName: agent.name,
|
| 99 |
+
agentStatus: agent.status,
|
| 100 |
+
isAdminTest: true
|
| 101 |
+
},
|
| 102 |
+
});
|
| 103 |
+
await transactionRepository.save(transaction);
|
| 104 |
}
|
| 105 |
|
| 106 |
// Convert multer files to chat protocol format
|
|
|
|
| 137 |
try {
|
| 138 |
const { id: agentId } = req.params;
|
| 139 |
const { conversationId, limit } = req.query;
|
| 140 |
+
const user = (req as any).user;
|
| 141 |
+
const userId = user?.id;
|
| 142 |
+
const isAdmin = user?.isAdmin || false;
|
| 143 |
|
| 144 |
if (!userId) {
|
| 145 |
return res.status(401).json({ error: 'Authentication required' });
|
| 146 |
}
|
| 147 |
|
| 148 |
+
// Admins can view history for any agent, regular users only approved agents
|
| 149 |
const agent = await agentRepository.findOne({
|
| 150 |
+
where: {
|
| 151 |
+
id: agentId,
|
| 152 |
+
...(isAdmin ? {} : { status: AgentStatus.APPROVED })
|
| 153 |
+
},
|
| 154 |
});
|
| 155 |
|
| 156 |
if (!agent) {
|
src/entities/Transaction.ts
CHANGED
|
@@ -15,6 +15,7 @@ export enum TransactionType {
|
|
| 15 |
CHARGE = 'charge', // Charged for agent task
|
| 16 |
REFUND = 'refund', // Refunded points
|
| 17 |
FREE = 'free', // Free task
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
export enum TransactionStatus {
|
|
|
|
| 15 |
CHARGE = 'charge', // Charged for agent task
|
| 16 |
REFUND = 'refund', // Refunded points
|
| 17 |
FREE = 'free', // Free task
|
| 18 |
+
ADMIN_TEST = 'admin_test', // Admin testing unapproved agents (no charge)
|
| 19 |
}
|
| 20 |
|
| 21 |
export enum TransactionStatus {
|