CognxSafeTrack commited on
Commit Β·
30d60ea
1
Parent(s): c51c0db
feat: implement inbound audio/image transcription, secure media proxy, and client-side bulk contact import
Browse files- apps/admin/package.json +2 -1
- apps/admin/src/components/crm/CrmAIAssistant.tsx +30 -9
- apps/admin/src/components/crm/CrmInbox.tsx +27 -1
- apps/admin/src/pages/CrmConversationalDashboard.tsx +116 -30
- apps/api/src/index.ts +7 -5
- apps/api/src/routes/admin.ts +12 -8
- apps/api/src/routes/ai.ts +1 -3
- apps/api/src/routes/auth.ts +2 -1
- apps/api/src/routes/campaigns.ts +23 -1
- apps/api/src/routes/organizations.ts +121 -0
- apps/api/src/routes/payments.ts +8 -4
- apps/api/src/routes/whatsapp.ts +2 -1
- apps/api/src/scripts/migrate-json-to-sql.ts +4 -36
- apps/api/src/scripts/test-e2e-journey.ts +5 -2
- apps/api/src/services/ai/index.ts +18 -3
- apps/api/src/services/whatsapp-utils.ts +35 -0
- apps/api/src/types/fastify.d.ts +15 -21
- apps/whatsapp-worker/src/handlers/InboundHandler.ts +60 -4
- apps/whatsapp-worker/src/handlers/MediaHandler.ts +10 -3
- apps/whatsapp-worker/src/index.ts +20 -3
- apps/whatsapp-worker/src/services/ai.ts +38 -0
- apps/whatsapp-worker/src/services/whatsapp-logic.ts +11 -1
- packages/database/prisma/schema.prisma +18 -13
- packages/database/seed.ts +5 -4
- pnpm-lock.yaml +3 -0
apps/admin/package.json
CHANGED
|
@@ -19,7 +19,8 @@
|
|
| 19 |
"react-dom": "^18.2.0",
|
| 20 |
"react-i18next": "^17.0.4",
|
| 21 |
"react-router-dom": "^6.20.0",
|
| 22 |
-
"recharts": "^3.8.1"
|
|
|
|
| 23 |
},
|
| 24 |
"devDependencies": {
|
| 25 |
"@repo/tsconfig": "workspace:*",
|
|
|
|
| 19 |
"react-dom": "^18.2.0",
|
| 20 |
"react-i18next": "^17.0.4",
|
| 21 |
"react-router-dom": "^6.20.0",
|
| 22 |
+
"recharts": "^3.8.1",
|
| 23 |
+
"xlsx": "^0.18.5"
|
| 24 |
},
|
| 25 |
"devDependencies": {
|
| 26 |
"@repo/tsconfig": "workspace:*",
|
apps/admin/src/components/crm/CrmAIAssistant.tsx
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
import { useRef, useEffect } from 'react';
|
| 2 |
-
import { Upload, Send, Bot, User, Loader2, FileText, CheckCircle2 } from 'lucide-react';
|
| 3 |
import { motion } from 'framer-motion';
|
| 4 |
|
| 5 |
interface Message {
|
| 6 |
id: string;
|
| 7 |
role: 'user' | 'assistant';
|
| 8 |
content: string;
|
|
|
|
| 9 |
timestamp: Date;
|
| 10 |
}
|
| 11 |
|
|
@@ -22,6 +23,9 @@ interface CrmAIAssistantProps {
|
|
| 22 |
onDrop: (e: React.DragEvent) => void;
|
| 23 |
onSendMessage: (e?: React.FormEvent) => void;
|
| 24 |
onValidateAndSend: (message: string) => void;
|
|
|
|
|
|
|
|
|
|
| 25 |
suggestions?: Array<{
|
| 26 |
id: string;
|
| 27 |
label: string;
|
|
@@ -45,6 +49,9 @@ export default function CrmAIAssistant({
|
|
| 45 |
onDrop,
|
| 46 |
onSendMessage,
|
| 47 |
onValidateAndSend,
|
|
|
|
|
|
|
|
|
|
| 48 |
suggestions = []
|
| 49 |
}: CrmAIAssistantProps) {
|
| 50 |
const messagesEndRef = useRef<HTMLDivElement>(null);
|
|
@@ -144,7 +151,7 @@ export default function CrmAIAssistant({
|
|
| 144 |
}`}>
|
| 145 |
{msg.content}
|
| 146 |
</div>
|
| 147 |
-
{msg.role === 'assistant' && msg.id !== '1' && !isGenerating && messages[messages.length - 1].id === msg.id && (
|
| 148 |
<motion.button
|
| 149 |
initial={{ opacity: 0, scale: 0.9 }}
|
| 150 |
animate={{ opacity: 1, scale: 1 }}
|
|
@@ -216,13 +223,27 @@ export default function CrmAIAssistant({
|
|
| 216 |
value={input}
|
| 217 |
onChange={(e) => setInput(e.target.value)}
|
| 218 |
/>
|
| 219 |
-
<
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
</div>
|
| 227 |
</form>
|
| 228 |
</div>
|
|
|
|
| 1 |
import { useRef, useEffect } from 'react';
|
| 2 |
+
import { Upload, Send, Bot, User, Loader2, FileText, CheckCircle2, Mic } from 'lucide-react';
|
| 3 |
import { motion } from 'framer-motion';
|
| 4 |
|
| 5 |
interface Message {
|
| 6 |
id: string;
|
| 7 |
role: 'user' | 'assistant';
|
| 8 |
content: string;
|
| 9 |
+
type?: 'campaign' | 'support';
|
| 10 |
timestamp: Date;
|
| 11 |
}
|
| 12 |
|
|
|
|
| 23 |
onDrop: (e: React.DragEvent) => void;
|
| 24 |
onSendMessage: (e?: React.FormEvent) => void;
|
| 25 |
onValidateAndSend: (message: string) => void;
|
| 26 |
+
isRecording: boolean;
|
| 27 |
+
onStartRecording: () => void;
|
| 28 |
+
onStopRecording: () => void;
|
| 29 |
suggestions?: Array<{
|
| 30 |
id: string;
|
| 31 |
label: string;
|
|
|
|
| 49 |
onDrop,
|
| 50 |
onSendMessage,
|
| 51 |
onValidateAndSend,
|
| 52 |
+
isRecording,
|
| 53 |
+
onStartRecording,
|
| 54 |
+
onStopRecording,
|
| 55 |
suggestions = []
|
| 56 |
}: CrmAIAssistantProps) {
|
| 57 |
const messagesEndRef = useRef<HTMLDivElement>(null);
|
|
|
|
| 151 |
}`}>
|
| 152 |
{msg.content}
|
| 153 |
</div>
|
| 154 |
+
{msg.role === 'assistant' && msg.id !== '1' && !isGenerating && messages[messages.length - 1].id === msg.id && msg.type === 'campaign' && (
|
| 155 |
<motion.button
|
| 156 |
initial={{ opacity: 0, scale: 0.9 }}
|
| 157 |
animate={{ opacity: 1, scale: 1 }}
|
|
|
|
| 223 |
value={input}
|
| 224 |
onChange={(e) => setInput(e.target.value)}
|
| 225 |
/>
|
| 226 |
+
<div className="flex items-center gap-2 pr-2">
|
| 227 |
+
<button
|
| 228 |
+
type="button"
|
| 229 |
+
onClick={isRecording ? onStopRecording : onStartRecording}
|
| 230 |
+
className={`p-3 rounded-2xl transition-all ${
|
| 231 |
+
isRecording
|
| 232 |
+
? 'bg-red-50 text-red-500 animate-pulse shadow-inner'
|
| 233 |
+
: 'text-slate-400 hover:text-indigo-600 hover:bg-indigo-50'
|
| 234 |
+
}`}
|
| 235 |
+
title={isRecording ? "ArrΓͺter l'enregistrement" : "DictΓ©e vocale"}
|
| 236 |
+
>
|
| 237 |
+
<Mic className={`w-5 h-5 ${isRecording ? 'fill-current' : ''}`} />
|
| 238 |
+
</button>
|
| 239 |
+
<button
|
| 240 |
+
type="submit"
|
| 241 |
+
className="w-14 h-14 bg-indigo-600 text-white rounded-[1.8rem] flex items-center justify-center hover:bg-indigo-700 hover:rotate-6 transition-all shadow-xl shadow-indigo-100 disabled:opacity-50 disabled:rotate-0"
|
| 242 |
+
disabled={!input.trim() || isRecording}
|
| 243 |
+
>
|
| 244 |
+
<Send className="w-6 h-6" />
|
| 245 |
+
</button>
|
| 246 |
+
</div>
|
| 247 |
</div>
|
| 248 |
</form>
|
| 249 |
</div>
|
apps/admin/src/components/crm/CrmInbox.tsx
CHANGED
|
@@ -77,7 +77,33 @@ export default function CrmInbox({
|
|
| 77 |
{msg.direction === 'INBOUND' ? 'REΓU' : 'ENVOYΓ'}
|
| 78 |
</span>
|
| 79 |
</div>
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
</div>
|
| 82 |
</div>
|
| 83 |
<div className="text-right">
|
|
|
|
| 77 |
{msg.direction === 'INBOUND' ? 'REΓU' : 'ENVOYΓ'}
|
| 78 |
</span>
|
| 79 |
</div>
|
| 80 |
+
|
| 81 |
+
{/* Media Proxy (Audio or Image) */}
|
| 82 |
+
{msg.mediaType === 'audio' && (
|
| 83 |
+
<div className="mb-2 mt-2 w-64">
|
| 84 |
+
<audio
|
| 85 |
+
controls
|
| 86 |
+
src={`${import.meta.env.VITE_API_URL}/v1/organizations/${msg.organizationId}/messages/${msg.id}/media`}
|
| 87 |
+
className="w-full h-8"
|
| 88 |
+
controlsList="nodownload"
|
| 89 |
+
/>
|
| 90 |
+
</div>
|
| 91 |
+
)}
|
| 92 |
+
|
| 93 |
+
{msg.mediaType === 'image' && (
|
| 94 |
+
<div className="mb-3 mt-2 relative rounded-2xl overflow-hidden border border-slate-200 bg-slate-50 w-full max-w-sm">
|
| 95 |
+
<img
|
| 96 |
+
src={`${import.meta.env.VITE_API_URL}/v1/organizations/${msg.organizationId}/messages/${msg.id}/media`}
|
| 97 |
+
alt="MΓ©dia WhatsApp"
|
| 98 |
+
className="w-full h-auto max-h-80 object-cover hover:scale-105 transition-transform duration-500"
|
| 99 |
+
loading="lazy"
|
| 100 |
+
/>
|
| 101 |
+
</div>
|
| 102 |
+
)}
|
| 103 |
+
|
| 104 |
+
<p className={`text-sm text-slate-500 font-medium ${msg.mediaType ? '' : 'line-clamp-1'}`}>
|
| 105 |
+
{msg.content}
|
| 106 |
+
</p>
|
| 107 |
</div>
|
| 108 |
</div>
|
| 109 |
<div className="text-right">
|
apps/admin/src/pages/CrmConversationalDashboard.tsx
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
-
import { useState, useEffect } from 'react';
|
| 2 |
import { useAuth } from '@/lib/auth';
|
| 3 |
import { useTenant } from '@/lib/tenant';
|
| 4 |
import { Megaphone, Bot, FileSignature, BarChart3 } from 'lucide-react';
|
|
|
|
| 5 |
import CrmStatsHeader from '@/components/crm/CrmStatsHeader';
|
| 6 |
import CrmAIAssistant from '@/components/crm/CrmAIAssistant';
|
| 7 |
import CrmInbox from '@/components/crm/CrmInbox';
|
|
@@ -10,6 +11,7 @@ interface Message {
|
|
| 10 |
id: string;
|
| 11 |
role: 'user' | 'assistant';
|
| 12 |
content: string;
|
|
|
|
| 13 |
timestamp: Date;
|
| 14 |
}
|
| 15 |
|
|
@@ -70,6 +72,9 @@ export default function CrmConversationalDashboard() {
|
|
| 70 |
const [isGenerating, setIsGenerating] = useState(false);
|
| 71 |
const [isDragging, setIsDragging] = useState(false);
|
| 72 |
const [uploadedFile, setUploadedFile] = useState<{ name: string, listId: string, listName: string } | null>(null);
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
const fetchInbox = async () => {
|
| 75 |
if (!token || !selectedOrgId) return;
|
|
@@ -97,38 +102,53 @@ export default function CrmConversationalDashboard() {
|
|
| 97 |
if (!token || !selectedOrgId) return;
|
| 98 |
|
| 99 |
setIsUploading(true);
|
| 100 |
-
const
|
| 101 |
-
formData.append('file', file);
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
|
| 112 |
-
|
| 113 |
-
const
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
}
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
} finally {
|
| 130 |
-
setIsUploading(false);
|
| 131 |
-
}
|
| 132 |
};
|
| 133 |
|
| 134 |
const handleSendMessage = async (e?: React.FormEvent) => {
|
|
@@ -165,6 +185,7 @@ export default function CrmConversationalDashboard() {
|
|
| 165 |
id: Date.now().toString(),
|
| 166 |
role: 'assistant',
|
| 167 |
content: data.message,
|
|
|
|
| 168 |
timestamp: new Date()
|
| 169 |
};
|
| 170 |
setMessages(prev => [...prev, aiMsg]);
|
|
@@ -253,6 +274,68 @@ export default function CrmConversationalDashboard() {
|
|
| 253 |
}
|
| 254 |
};
|
| 255 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
return (
|
| 257 |
<div className="flex flex-col h-[calc(100vh-64px)] bg-slate-50">
|
| 258 |
<CrmStatsHeader
|
|
@@ -286,6 +369,9 @@ export default function CrmConversationalDashboard() {
|
|
| 286 |
}}
|
| 287 |
onSendMessage={handleSendMessage}
|
| 288 |
onValidateAndSend={handleValidateAndSend}
|
|
|
|
|
|
|
|
|
|
| 289 |
suggestions={SUGGESTED_ACTIONS}
|
| 290 |
/>
|
| 291 |
) : (
|
|
|
|
| 1 |
+
import { useState, useEffect, useRef } from 'react';
|
| 2 |
import { useAuth } from '@/lib/auth';
|
| 3 |
import { useTenant } from '@/lib/tenant';
|
| 4 |
import { Megaphone, Bot, FileSignature, BarChart3 } from 'lucide-react';
|
| 5 |
+
import { read, utils } from 'xlsx';
|
| 6 |
import CrmStatsHeader from '@/components/crm/CrmStatsHeader';
|
| 7 |
import CrmAIAssistant from '@/components/crm/CrmAIAssistant';
|
| 8 |
import CrmInbox from '@/components/crm/CrmInbox';
|
|
|
|
| 11 |
id: string;
|
| 12 |
role: 'user' | 'assistant';
|
| 13 |
content: string;
|
| 14 |
+
type?: 'campaign' | 'support';
|
| 15 |
timestamp: Date;
|
| 16 |
}
|
| 17 |
|
|
|
|
| 72 |
const [isGenerating, setIsGenerating] = useState(false);
|
| 73 |
const [isDragging, setIsDragging] = useState(false);
|
| 74 |
const [uploadedFile, setUploadedFile] = useState<{ name: string, listId: string, listName: string } | null>(null);
|
| 75 |
+
const [isRecording, setIsRecording] = useState(false);
|
| 76 |
+
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
|
| 77 |
+
const audioChunksRef = useRef<Blob[]>([]);
|
| 78 |
|
| 79 |
const fetchInbox = async () => {
|
| 80 |
if (!token || !selectedOrgId) return;
|
|
|
|
| 102 |
if (!token || !selectedOrgId) return;
|
| 103 |
|
| 104 |
setIsUploading(true);
|
| 105 |
+
const reader = new FileReader();
|
|
|
|
| 106 |
|
| 107 |
+
reader.onload = async (event) => {
|
| 108 |
+
try {
|
| 109 |
+
// 1. Parse Excel/CSV on the client side
|
| 110 |
+
const data = new Uint8Array(event.target?.result as ArrayBuffer);
|
| 111 |
+
const workbook = read(data, { type: 'array' });
|
| 112 |
+
const firstSheetName = workbook.SheetNames[0];
|
| 113 |
+
const worksheet = workbook.Sheets[firstSheetName];
|
| 114 |
+
const jsonData = utils.sheet_to_json(worksheet);
|
| 115 |
|
| 116 |
+
// 2. Send JSON to the new bulk endpoint
|
| 117 |
+
const res = await fetch(`${import.meta.env.VITE_API_URL}/v1/organizations/${selectedOrgId}/contacts/bulk`, {
|
| 118 |
+
method: 'POST',
|
| 119 |
+
headers: {
|
| 120 |
+
'Content-Type': 'application/json',
|
| 121 |
+
'Authorization': `Bearer ${token}`
|
| 122 |
+
},
|
| 123 |
+
body: JSON.stringify({
|
| 124 |
+
contacts: jsonData,
|
| 125 |
+
listName: file.name.split('.')[0]
|
| 126 |
+
})
|
| 127 |
+
});
|
| 128 |
+
|
| 129 |
+
if (res.ok) {
|
| 130 |
+
const data = await res.json();
|
| 131 |
+
setUploadedFile({ name: file.name, listId: data.listId, listName: data.listName });
|
| 132 |
+
|
| 133 |
+
const aiMsg: Message = {
|
| 134 |
+
id: Date.now().toString(),
|
| 135 |
+
role: 'assistant',
|
| 136 |
+
content: `Votre liste de diffusion "${data.listName}" a bien été créée avec ${data.results.created} contacts ! Que souhaitez-vous envoyer à ces contacts ?`,
|
| 137 |
+
timestamp: new Date()
|
| 138 |
+
};
|
| 139 |
+
setMessages(prev => [...prev, aiMsg]);
|
| 140 |
+
} else {
|
| 141 |
+
alert("Erreur lors de l'importation des contacts.");
|
| 142 |
+
}
|
| 143 |
+
} catch (err) {
|
| 144 |
+
console.error("Parsing/Upload failed:", err);
|
| 145 |
+
alert("Une erreur technique est survenue.");
|
| 146 |
+
} finally {
|
| 147 |
+
setIsUploading(false);
|
| 148 |
}
|
| 149 |
+
};
|
| 150 |
+
|
| 151 |
+
reader.readAsArrayBuffer(file);
|
|
|
|
|
|
|
|
|
|
| 152 |
};
|
| 153 |
|
| 154 |
const handleSendMessage = async (e?: React.FormEvent) => {
|
|
|
|
| 185 |
id: Date.now().toString(),
|
| 186 |
role: 'assistant',
|
| 187 |
content: data.message,
|
| 188 |
+
type: data.type,
|
| 189 |
timestamp: new Date()
|
| 190 |
};
|
| 191 |
setMessages(prev => [...prev, aiMsg]);
|
|
|
|
| 274 |
}
|
| 275 |
};
|
| 276 |
|
| 277 |
+
const startRecording = async () => {
|
| 278 |
+
try {
|
| 279 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 280 |
+
const mediaRecorder = new MediaRecorder(stream);
|
| 281 |
+
mediaRecorderRef.current = mediaRecorder;
|
| 282 |
+
audioChunksRef.current = [];
|
| 283 |
+
|
| 284 |
+
mediaRecorder.ondataavailable = (event) => {
|
| 285 |
+
if (event.data.size > 0) {
|
| 286 |
+
audioChunksRef.current.push(event.data);
|
| 287 |
+
}
|
| 288 |
+
};
|
| 289 |
+
|
| 290 |
+
mediaRecorder.onstop = async () => {
|
| 291 |
+
const audioBlob = new Blob(audioChunksRef.current, { type: 'audio/webm' });
|
| 292 |
+
await sendAudioToTranscription(audioBlob);
|
| 293 |
+
stream.getTracks().forEach(track => track.stop());
|
| 294 |
+
};
|
| 295 |
+
|
| 296 |
+
mediaRecorder.start();
|
| 297 |
+
setIsRecording(true);
|
| 298 |
+
} catch (err) {
|
| 299 |
+
console.error("Failed to start recording:", err);
|
| 300 |
+
alert("Impossible d'accΓ©der au micro.");
|
| 301 |
+
}
|
| 302 |
+
};
|
| 303 |
+
|
| 304 |
+
const stopRecording = () => {
|
| 305 |
+
if (mediaRecorderRef.current && isRecording) {
|
| 306 |
+
mediaRecorderRef.current.stop();
|
| 307 |
+
setIsRecording(false);
|
| 308 |
+
}
|
| 309 |
+
};
|
| 310 |
+
|
| 311 |
+
const sendAudioToTranscription = async (blob: Blob) => {
|
| 312 |
+
if (!token || !selectedOrgId) return;
|
| 313 |
+
|
| 314 |
+
const formData = new FormData();
|
| 315 |
+
formData.append('file', blob, 'recording.webm');
|
| 316 |
+
|
| 317 |
+
try {
|
| 318 |
+
const res = await fetch(`${import.meta.env.VITE_API_URL}/v1/organizations/${selectedOrgId}/campaigns/transcribe`, {
|
| 319 |
+
method: 'POST',
|
| 320 |
+
headers: {
|
| 321 |
+
'Authorization': `Bearer ${token}`
|
| 322 |
+
},
|
| 323 |
+
body: formData
|
| 324 |
+
});
|
| 325 |
+
|
| 326 |
+
if (res.ok) {
|
| 327 |
+
const data = await res.json();
|
| 328 |
+
if (data.text) {
|
| 329 |
+
setInput(data.text);
|
| 330 |
+
}
|
| 331 |
+
} else {
|
| 332 |
+
console.error("Transcription failed");
|
| 333 |
+
}
|
| 334 |
+
} catch (err) {
|
| 335 |
+
console.error("Error sending audio:", err);
|
| 336 |
+
}
|
| 337 |
+
};
|
| 338 |
+
|
| 339 |
return (
|
| 340 |
<div className="flex flex-col h-[calc(100vh-64px)] bg-slate-50">
|
| 341 |
<CrmStatsHeader
|
|
|
|
| 369 |
}}
|
| 370 |
onSendMessage={handleSendMessage}
|
| 371 |
onValidateAndSend={handleValidateAndSend}
|
| 372 |
+
isRecording={isRecording}
|
| 373 |
+
onStartRecording={startRecording}
|
| 374 |
+
onStopRecording={stopRecording}
|
| 375 |
suggestions={SUGGESTED_ACTIONS}
|
| 376 |
/>
|
| 377 |
) : (
|
apps/api/src/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import 'dotenv/config';
|
| 2 |
import fastify, { FastifyInstance } from 'fastify';
|
| 3 |
import cors from '@fastify/cors';
|
|
|
|
|
|
|
| 4 |
import { logger } from './logger';
|
| 5 |
import { prisma } from './services/prisma';
|
| 6 |
import { runWithTenant } from '@repo/database';
|
|
@@ -27,7 +29,7 @@ const server: FastifyInstance = fastify({
|
|
| 27 |
});
|
| 28 |
|
| 29 |
// Attach prisma to server instance for global access in routes
|
| 30 |
-
server.decorate('prisma', prisma);
|
| 31 |
|
| 32 |
// ββ Middleware & Plugins ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 33 |
server.register(cors as any, {
|
|
@@ -42,11 +44,11 @@ server.register(cors as any, {
|
|
| 42 |
credentials: true
|
| 43 |
});
|
| 44 |
|
| 45 |
-
server.register(
|
| 46 |
limits: { fileSize: 10 * 1024 * 1024 } // 10MB
|
| 47 |
});
|
| 48 |
|
| 49 |
-
server.register(
|
| 50 |
secret: process.env.JWT_SECRET ?? (() => { throw new Error('[STARTUP] JWT_SECRET environment variable is required'); })()
|
| 51 |
});
|
| 52 |
|
|
@@ -78,7 +80,7 @@ const registerRoutes = async () => {
|
|
| 78 |
}
|
| 79 |
|
| 80 |
// Multi-Tenant Enforcement
|
| 81 |
-
const user = request.user;
|
| 82 |
const requestedOrgId = request.headers['x-organization-id'] as string;
|
| 83 |
|
| 84 |
if (user && user.role !== 'SUPER_ADMIN') {
|
|
@@ -92,7 +94,7 @@ const registerRoutes = async () => {
|
|
| 92 |
}
|
| 93 |
|
| 94 |
// Centralized property for routes to use
|
| 95 |
-
request.organizationId = request.headers['x-organization-id'] as string;
|
| 96 |
});
|
| 97 |
|
| 98 |
scope.addHook('preHandler', (request, _reply, done) => {
|
|
|
|
| 1 |
import 'dotenv/config';
|
| 2 |
import fastify, { FastifyInstance } from 'fastify';
|
| 3 |
import cors from '@fastify/cors';
|
| 4 |
+
import multipart from '@fastify/multipart';
|
| 5 |
+
import jwt from '@fastify/jwt';
|
| 6 |
import { logger } from './logger';
|
| 7 |
import { prisma } from './services/prisma';
|
| 8 |
import { runWithTenant } from '@repo/database';
|
|
|
|
| 29 |
});
|
| 30 |
|
| 31 |
// Attach prisma to server instance for global access in routes
|
| 32 |
+
server.decorate('prisma', prisma as any);
|
| 33 |
|
| 34 |
// ββ Middleware & Plugins ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 35 |
server.register(cors as any, {
|
|
|
|
| 44 |
credentials: true
|
| 45 |
});
|
| 46 |
|
| 47 |
+
server.register(multipart, {
|
| 48 |
limits: { fileSize: 10 * 1024 * 1024 } // 10MB
|
| 49 |
});
|
| 50 |
|
| 51 |
+
server.register(jwt, {
|
| 52 |
secret: process.env.JWT_SECRET ?? (() => { throw new Error('[STARTUP] JWT_SECRET environment variable is required'); })()
|
| 53 |
});
|
| 54 |
|
|
|
|
| 80 |
}
|
| 81 |
|
| 82 |
// Multi-Tenant Enforcement
|
| 83 |
+
const user = request.user as any;
|
| 84 |
const requestedOrgId = request.headers['x-organization-id'] as string;
|
| 85 |
|
| 86 |
if (user && user.role !== 'SUPER_ADMIN') {
|
|
|
|
| 94 |
}
|
| 95 |
|
| 96 |
// Centralized property for routes to use
|
| 97 |
+
(request as any).organizationId = request.headers['x-organization-id'] as string;
|
| 98 |
});
|
| 99 |
|
| 100 |
scope.addHook('preHandler', (request, _reply, done) => {
|
apps/api/src/routes/admin.ts
CHANGED
|
@@ -3,6 +3,11 @@ import { prisma } from '../services/prisma';
|
|
| 3 |
import { whatsappQueue } from '../services/queue';
|
| 4 |
import { z } from 'zod';
|
| 5 |
import { calculateWER, formatError } from '../utils/metrics';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
// βββ Zod Schemas βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 8 |
const TrackSchema = z.object({
|
|
@@ -171,10 +176,11 @@ export async function adminRoutes(fastify: FastifyInstance) {
|
|
| 171 |
|
| 172 |
const currentDay = enrollment ? Math.floor(enrollment.currentDay) : 0;
|
| 173 |
|
|
|
|
| 174 |
await prisma.businessProfile.upsert({
|
| 175 |
where: { userId },
|
| 176 |
-
update: { lastUpdatedFromDay: currentDay },
|
| 177 |
-
create: { userId, lastUpdatedFromDay: currentDay }
|
| 178 |
});
|
| 179 |
|
| 180 |
// 3. Dispatch Background Job (Audio Delivery + Next Day Increment)
|
|
@@ -244,7 +250,8 @@ export async function adminRoutes(fastify: FastifyInstance) {
|
|
| 244 |
fastify.post('/tracks', async (req, reply) => {
|
| 245 |
const body = TrackSchema.safeParse(req.body);
|
| 246 |
if (!body.success) return reply.code(400).send({ error: body.error.flatten() });
|
| 247 |
-
const
|
|
|
|
| 248 |
return reply.code(201).send(track);
|
| 249 |
});
|
| 250 |
|
|
@@ -273,8 +280,6 @@ export async function adminRoutes(fastify: FastifyInstance) {
|
|
| 273 |
|
| 274 |
// ββ STT Quality Calibration Endpoint βββββββββββββββββββββββββββββββββββββββ
|
| 275 |
fastify.get('/stats/confidence-distribution', async (_req, reply) => {
|
| 276 |
-
const fs = require('fs');
|
| 277 |
-
const path = require('path');
|
| 278 |
const statsPath = path.join(__dirname, '../../data/calibration_stats.json');
|
| 279 |
|
| 280 |
try {
|
|
@@ -305,10 +310,12 @@ export async function adminRoutes(fastify: FastifyInstance) {
|
|
| 305 |
fastify.post<{ Params: { trackId: string } }>('/tracks/:trackId/days', async (req, reply) => {
|
| 306 |
const body = TrackDaySchema.safeParse(req.body);
|
| 307 |
if (!body.success) return reply.code(400).send({ error: body.error.flatten() });
|
|
|
|
| 308 |
const day = await prisma.trackDay.create({
|
| 309 |
data: {
|
| 310 |
...body.data,
|
| 311 |
trackId: req.params.trackId,
|
|
|
|
| 312 |
audioUrl: body.data.audioUrl || null,
|
| 313 |
buttonsJson: body.data.buttonsJson ? body.data.buttonsJson : undefined
|
| 314 |
}
|
|
@@ -373,7 +380,6 @@ export async function adminRoutes(fastify: FastifyInstance) {
|
|
| 373 |
if (!body.success) return reply.code(400).send({ error: body.error.flatten() });
|
| 374 |
|
| 375 |
|
| 376 |
-
const { normalizeWolof } = require('../scripts/normalizeWolof');
|
| 377 |
const normResult = normalizeWolof(body.data.transcription);
|
| 378 |
|
| 379 |
const rawWER = calculateWER(body.data.manualCorrection, body.data.transcription);
|
|
@@ -408,7 +414,6 @@ export async function adminRoutes(fastify: FastifyInstance) {
|
|
| 408 |
|
| 409 |
// Suggest new dictionary rules from TrainingData
|
| 410 |
fastify.get('/training/suggestions', async (_req, reply) => {
|
| 411 |
-
const diff = require('diff');
|
| 412 |
const trainingData = await prisma.trainingData.findMany({
|
| 413 |
where: { status: 'REVIEWED' }
|
| 414 |
});
|
|
@@ -475,7 +480,6 @@ export async function adminRoutes(fastify: FastifyInstance) {
|
|
| 475 |
|
| 476 |
const { normalizationService } = await import('../services/normalization');
|
| 477 |
const customRules = await normalizationService.getRules('WOLOF');
|
| 478 |
-
const { normalizeWolof } = require('../scripts/normalizeWolof');
|
| 479 |
|
| 480 |
let totalRawWER = 0;
|
| 481 |
let totalNormalizedWER = 0;
|
|
|
|
| 3 |
import { whatsappQueue } from '../services/queue';
|
| 4 |
import { z } from 'zod';
|
| 5 |
import { calculateWER, formatError } from '../utils/metrics';
|
| 6 |
+
import { getOrganizationId } from '@repo/database';
|
| 7 |
+
import fs from 'fs';
|
| 8 |
+
import path from 'path';
|
| 9 |
+
import * as diff from 'diff';
|
| 10 |
+
import { normalizeWolof } from '../scripts/normalizeWolof';
|
| 11 |
|
| 12 |
// βββ Zod Schemas βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
const TrackSchema = z.object({
|
|
|
|
| 176 |
|
| 177 |
const currentDay = enrollment ? Math.floor(enrollment.currentDay) : 0;
|
| 178 |
|
| 179 |
+
const organizationId = getOrganizationId() || 'default-org-id';
|
| 180 |
await prisma.businessProfile.upsert({
|
| 181 |
where: { userId },
|
| 182 |
+
update: { lastUpdatedFromDay: currentDay, organizationId },
|
| 183 |
+
create: { userId, lastUpdatedFromDay: currentDay, organizationId }
|
| 184 |
});
|
| 185 |
|
| 186 |
// 3. Dispatch Background Job (Audio Delivery + Next Day Increment)
|
|
|
|
| 250 |
fastify.post('/tracks', async (req, reply) => {
|
| 251 |
const body = TrackSchema.safeParse(req.body);
|
| 252 |
if (!body.success) return reply.code(400).send({ error: body.error.flatten() });
|
| 253 |
+
const organizationId = getOrganizationId() || 'default-org-id';
|
| 254 |
+
const track = await prisma.track.create({ data: { ...body.data, organizationId } });
|
| 255 |
return reply.code(201).send(track);
|
| 256 |
});
|
| 257 |
|
|
|
|
| 280 |
|
| 281 |
// ββ STT Quality Calibration Endpoint βββββββββββββββββββββββββββββββββββββββ
|
| 282 |
fastify.get('/stats/confidence-distribution', async (_req, reply) => {
|
|
|
|
|
|
|
| 283 |
const statsPath = path.join(__dirname, '../../data/calibration_stats.json');
|
| 284 |
|
| 285 |
try {
|
|
|
|
| 310 |
fastify.post<{ Params: { trackId: string } }>('/tracks/:trackId/days', async (req, reply) => {
|
| 311 |
const body = TrackDaySchema.safeParse(req.body);
|
| 312 |
if (!body.success) return reply.code(400).send({ error: body.error.flatten() });
|
| 313 |
+
const organizationId = getOrganizationId() || 'default-org-id';
|
| 314 |
const day = await prisma.trackDay.create({
|
| 315 |
data: {
|
| 316 |
...body.data,
|
| 317 |
trackId: req.params.trackId,
|
| 318 |
+
organizationId,
|
| 319 |
audioUrl: body.data.audioUrl || null,
|
| 320 |
buttonsJson: body.data.buttonsJson ? body.data.buttonsJson : undefined
|
| 321 |
}
|
|
|
|
| 380 |
if (!body.success) return reply.code(400).send({ error: body.error.flatten() });
|
| 381 |
|
| 382 |
|
|
|
|
| 383 |
const normResult = normalizeWolof(body.data.transcription);
|
| 384 |
|
| 385 |
const rawWER = calculateWER(body.data.manualCorrection, body.data.transcription);
|
|
|
|
| 414 |
|
| 415 |
// Suggest new dictionary rules from TrainingData
|
| 416 |
fastify.get('/training/suggestions', async (_req, reply) => {
|
|
|
|
| 417 |
const trainingData = await prisma.trainingData.findMany({
|
| 418 |
where: { status: 'REVIEWED' }
|
| 419 |
});
|
|
|
|
| 480 |
|
| 481 |
const { normalizationService } = await import('../services/normalization');
|
| 482 |
const customRules = await normalizationService.getRules('WOLOF');
|
|
|
|
| 483 |
|
| 484 |
let totalRawWER = 0;
|
| 485 |
let totalNormalizedWER = 0;
|
apps/api/src/routes/ai.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { PptxDeckRenderer } from '../services/renderers/pptx-renderer';
|
|
| 6 |
import { uploadFile } from '../services/storage';
|
| 7 |
import { convertToMp3IfNeeded } from '../services/ai/ffmpeg';
|
| 8 |
import { z } from 'zod';
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
export async function aiRoutes(fastify: FastifyInstance) {
|
|
@@ -323,7 +324,6 @@ export async function aiRoutes(fastify: FastifyInstance) {
|
|
| 323 |
const { contact, objective, language } = bodySchema.parse(request.body);
|
| 324 |
|
| 325 |
const organizationId = request.headers['x-organization-id'] as string;
|
| 326 |
-
const { prisma } = fastify;
|
| 327 |
|
| 328 |
const org = await prisma.organization.findUnique({
|
| 329 |
where: { id: organizationId },
|
|
@@ -353,7 +353,6 @@ export async function aiRoutes(fastify: FastifyInstance) {
|
|
| 353 |
});
|
| 354 |
const { query } = bodySchema.parse(request.body);
|
| 355 |
const organizationId = request.headers['x-organization-id'] as string;
|
| 356 |
-
const prisma = (fastify as any).prisma;
|
| 357 |
|
| 358 |
// Step 1: Detect intent using a lightweight prompt
|
| 359 |
const systemPrompt = `You are a CRM Command Interpreter. Your job is to classify the user's intent into one of these:
|
|
@@ -466,7 +465,6 @@ export async function aiRoutes(fastify: FastifyInstance) {
|
|
| 466 |
|
| 467 |
const { messages } = bodySchema.parse(request.body);
|
| 468 |
const organizationId = request.headers['x-organization-id'] as string;
|
| 469 |
-
const prisma = (fastify as any).prisma;
|
| 470 |
|
| 471 |
// 1. Fetch Org Credentials
|
| 472 |
const org = await prisma.organization.findUnique({
|
|
|
|
| 6 |
import { uploadFile } from '../services/storage';
|
| 7 |
import { convertToMp3IfNeeded } from '../services/ai/ffmpeg';
|
| 8 |
import { z } from 'zod';
|
| 9 |
+
import { prisma } from '../services/prisma';
|
| 10 |
|
| 11 |
|
| 12 |
export async function aiRoutes(fastify: FastifyInstance) {
|
|
|
|
| 324 |
const { contact, objective, language } = bodySchema.parse(request.body);
|
| 325 |
|
| 326 |
const organizationId = request.headers['x-organization-id'] as string;
|
|
|
|
| 327 |
|
| 328 |
const org = await prisma.organization.findUnique({
|
| 329 |
where: { id: organizationId },
|
|
|
|
| 353 |
});
|
| 354 |
const { query } = bodySchema.parse(request.body);
|
| 355 |
const organizationId = request.headers['x-organization-id'] as string;
|
|
|
|
| 356 |
|
| 357 |
// Step 1: Detect intent using a lightweight prompt
|
| 358 |
const systemPrompt = `You are a CRM Command Interpreter. Your job is to classify the user's intent into one of these:
|
|
|
|
| 465 |
|
| 466 |
const { messages } = bodySchema.parse(request.body);
|
| 467 |
const organizationId = request.headers['x-organization-id'] as string;
|
|
|
|
| 468 |
|
| 469 |
// 1. Fetch Org Credentials
|
| 470 |
const org = await prisma.organization.findUnique({
|
apps/api/src/routes/auth.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { FastifyInstance } from 'fastify';
|
|
| 2 |
|
| 3 |
import { AuthService } from '../services/auth';
|
| 4 |
import { logger } from '../logger';
|
|
|
|
| 5 |
|
| 6 |
export async function authRoutes(fastify: FastifyInstance) {
|
| 7 |
|
|
@@ -61,7 +62,7 @@ export async function authRoutes(fastify: FastifyInstance) {
|
|
| 61 |
fastify.get('/me', async (request, reply) => {
|
| 62 |
const { id } = request.user as any;
|
| 63 |
|
| 64 |
-
const user = await
|
| 65 |
where: { id },
|
| 66 |
include: { organization: true }
|
| 67 |
});
|
|
|
|
| 2 |
|
| 3 |
import { AuthService } from '../services/auth';
|
| 4 |
import { logger } from '../logger';
|
| 5 |
+
import { prisma } from '../services/prisma';
|
| 6 |
|
| 7 |
export async function authRoutes(fastify: FastifyInstance) {
|
| 8 |
|
|
|
|
| 62 |
fastify.get('/me', async (request, reply) => {
|
| 63 |
const { id } = request.user as any;
|
| 64 |
|
| 65 |
+
const user = await prisma.user.findUnique({
|
| 66 |
where: { id },
|
| 67 |
include: { organization: true }
|
| 68 |
});
|
apps/api/src/routes/campaigns.ts
CHANGED
|
@@ -11,11 +11,12 @@ export default async function campaignRoutes(fastify: FastifyInstance) {
|
|
| 11 |
}
|
| 12 |
|
| 13 |
try {
|
| 14 |
-
const { text, aiSource } = await aiService.generateBroadcastMessage(prompt);
|
| 15 |
|
| 16 |
return {
|
| 17 |
ok: true,
|
| 18 |
message: text,
|
|
|
|
| 19 |
aiSource,
|
| 20 |
listId // Echoing back the listId for frontend use
|
| 21 |
};
|
|
@@ -48,4 +49,25 @@ export default async function campaignRoutes(fastify: FastifyInstance) {
|
|
| 48 |
return reply.code(500).send({ error: 'Failed to enqueue campaign' });
|
| 49 |
}
|
| 50 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
}
|
|
|
|
| 11 |
}
|
| 12 |
|
| 13 |
try {
|
| 14 |
+
const { text, type, aiSource } = await aiService.generateBroadcastMessage(prompt);
|
| 15 |
|
| 16 |
return {
|
| 17 |
ok: true,
|
| 18 |
message: text,
|
| 19 |
+
type,
|
| 20 |
aiSource,
|
| 21 |
listId // Echoing back the listId for frontend use
|
| 22 |
};
|
|
|
|
| 49 |
return reply.code(500).send({ error: 'Failed to enqueue campaign' });
|
| 50 |
}
|
| 51 |
});
|
| 52 |
+
|
| 53 |
+
// Transcribe Audio for Campaign Command
|
| 54 |
+
fastify.post('/:id/campaigns/transcribe', async (req, reply) => {
|
| 55 |
+
const data = await req.file();
|
| 56 |
+
if (!data) {
|
| 57 |
+
return reply.code(400).send({ error: 'Audio file is required' });
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
try {
|
| 61 |
+
const buffer = await data.toBuffer();
|
| 62 |
+
const { text } = await aiService.transcribeAudio(buffer, data.filename);
|
| 63 |
+
|
| 64 |
+
return {
|
| 65 |
+
ok: true,
|
| 66 |
+
text
|
| 67 |
+
};
|
| 68 |
+
} catch (err) {
|
| 69 |
+
fastify.log.error(err);
|
| 70 |
+
return reply.code(500).send({ error: 'Transcription failed' });
|
| 71 |
+
}
|
| 72 |
+
});
|
| 73 |
}
|
apps/api/src/routes/organizations.ts
CHANGED
|
@@ -437,4 +437,125 @@ export async function organizationRoutes(fastify: FastifyInstance) {
|
|
| 437 |
return reply.code(500).send({ error: 'Failed to process reply' });
|
| 438 |
}
|
| 439 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
}
|
|
|
|
| 437 |
return reply.code(500).send({ error: 'Failed to process reply' });
|
| 438 |
}
|
| 439 |
});
|
| 440 |
+
|
| 441 |
+
// 13. CRM: Media Proxy for WhatsApp (Audio & Images)
|
| 442 |
+
fastify.get('/:id/messages/:messageId/media', async (req, reply) => {
|
| 443 |
+
const { id: organizationId, messageId } = req.params as { id: string; messageId: string };
|
| 444 |
+
|
| 445 |
+
try {
|
| 446 |
+
// 1. Verify message and organization ownership
|
| 447 |
+
const message = await prisma.message.findUnique({
|
| 448 |
+
where: { id: messageId, organizationId }
|
| 449 |
+
});
|
| 450 |
+
|
| 451 |
+
if (!message || !message.mediaId) {
|
| 452 |
+
return reply.code(404).send({ error: 'Media message not found' });
|
| 453 |
+
}
|
| 454 |
+
|
| 455 |
+
// 2. Get decrypted secrets for the organization
|
| 456 |
+
const { getTenantSecrets } = await import('../services/organization');
|
| 457 |
+
const secrets = await getTenantSecrets(organizationId);
|
| 458 |
+
|
| 459 |
+
if (!secrets || !secrets.systemUserToken) {
|
| 460 |
+
return reply.code(401).send({ error: 'WhatsApp configuration missing' });
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
// 3. Download media from Meta
|
| 464 |
+
const { downloadMedia } = await import('../services/whatsapp-utils');
|
| 465 |
+
const { buffer, mimeType } = await downloadMedia(message.mediaId, secrets.systemUserToken);
|
| 466 |
+
|
| 467 |
+
// 4. Stream back to client
|
| 468 |
+
reply.type(mimeType).send(buffer);
|
| 469 |
+
} catch (err: any) {
|
| 470 |
+
logger.error({ err, messageId }, '[CRM-MEDIA-PROXY] Failed to proxy media');
|
| 471 |
+
return reply.code(500).send({ error: 'Failed to retrieve media' });
|
| 472 |
+
}
|
| 473 |
+
});
|
| 474 |
+
|
| 475 |
+
// 14. CRM: Bulk Contact Import from JSON (Parsed by Frontend)
|
| 476 |
+
fastify.post('/:id/contacts/bulk', async (req, reply) => {
|
| 477 |
+
const { id: organizationId } = req.params as { id: string };
|
| 478 |
+
const { contacts, listName } = req.body as { contacts: any[], listName?: string };
|
| 479 |
+
|
| 480 |
+
if (!contacts || !Array.isArray(contacts)) {
|
| 481 |
+
return reply.code(400).send({ error: 'Invalid contacts data' });
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
const date = new Date().toLocaleDateString('fr-FR');
|
| 485 |
+
const finalListName = listName || `Import du ${date}`;
|
| 486 |
+
|
| 487 |
+
logger.info(`[CRM-BULK-IMPORT] Importing ${contacts.length} contacts for Org: ${organizationId}`);
|
| 488 |
+
|
| 489 |
+
try {
|
| 490 |
+
// 1. Create the Broadcast List first
|
| 491 |
+
const broadcastList = await prisma.broadcastList.create({
|
| 492 |
+
data: {
|
| 493 |
+
name: finalListName,
|
| 494 |
+
organizationId
|
| 495 |
+
}
|
| 496 |
+
});
|
| 497 |
+
|
| 498 |
+
const results = { created: 0, updated: 0, errors: 0 };
|
| 499 |
+
|
| 500 |
+
// We use a sequential loop for stability or chunking for performance
|
| 501 |
+
// Here we use a transaction for the whole batch for safety
|
| 502 |
+
for (const row of contacts) {
|
| 503 |
+
try {
|
| 504 |
+
// Heuristic for phone and name (reuse from legacy import)
|
| 505 |
+
const phoneKey = Object.keys(row).find(k =>
|
| 506 |
+
k.toLowerCase().includes('phone') ||
|
| 507 |
+
k.toLowerCase().includes('tΓ©lΓ©phone') ||
|
| 508 |
+
k.toLowerCase().includes('tel') ||
|
| 509 |
+
k.toLowerCase().includes('num')
|
| 510 |
+
);
|
| 511 |
+
const nameKey = Object.keys(row).find(k =>
|
| 512 |
+
k.toLowerCase().includes('name') ||
|
| 513 |
+
k.toLowerCase().includes('nom')
|
| 514 |
+
);
|
| 515 |
+
|
| 516 |
+
let phoneNumber = phoneKey ? String(row[phoneKey]).replace(/\s+/g, '').replace(/^\+/, '').replace(/\D/g, '') : null;
|
| 517 |
+
const name = nameKey ? String(row[nameKey]).trim() : null;
|
| 518 |
+
|
| 519 |
+
if (!phoneNumber || phoneNumber.length < 7) {
|
| 520 |
+
results.errors++;
|
| 521 |
+
continue;
|
| 522 |
+
}
|
| 523 |
+
|
| 524 |
+
if (phoneNumber.length === 9) phoneNumber = `221${phoneNumber}`;
|
| 525 |
+
|
| 526 |
+
const attributes: any = { ...row };
|
| 527 |
+
if (phoneKey) delete attributes[phoneKey];
|
| 528 |
+
if (nameKey) delete attributes[nameKey];
|
| 529 |
+
|
| 530 |
+
// Upsert contact and connect to list
|
| 531 |
+
await prisma.contact.upsert({
|
| 532 |
+
where: {
|
| 533 |
+
phoneNumber_organizationId: { phoneNumber, organizationId }
|
| 534 |
+
},
|
| 535 |
+
update: {
|
| 536 |
+
name,
|
| 537 |
+
attributes,
|
| 538 |
+
broadcastLists: { connect: { id: broadcastList.id } }
|
| 539 |
+
},
|
| 540 |
+
create: {
|
| 541 |
+
phoneNumber,
|
| 542 |
+
name,
|
| 543 |
+
attributes,
|
| 544 |
+
organizationId,
|
| 545 |
+
broadcastLists: { connect: { id: broadcastList.id } }
|
| 546 |
+
}
|
| 547 |
+
});
|
| 548 |
+
|
| 549 |
+
results.created++;
|
| 550 |
+
} catch (err) {
|
| 551 |
+
results.errors++;
|
| 552 |
+
}
|
| 553 |
+
}
|
| 554 |
+
|
| 555 |
+
return { ok: true, listId: broadcastList.id, listName: finalListName, results };
|
| 556 |
+
} catch (err) {
|
| 557 |
+
logger.error({ err }, '[CRM-BULK-IMPORT] Master failed');
|
| 558 |
+
return reply.code(500).send({ error: 'Failed to process bulk import' });
|
| 559 |
+
}
|
| 560 |
+
});
|
| 561 |
}
|
apps/api/src/routes/payments.ts
CHANGED
|
@@ -134,19 +134,22 @@ export async function stripeWebhookRoute(fastify: FastifyInstance) {
|
|
| 134 |
const orgId = session.metadata?.organizationId; // If it's an org sub, it has this
|
| 135 |
|
| 136 |
if (userId && trackId) {
|
| 137 |
-
//
|
|
|
|
|
|
|
| 138 |
try {
|
| 139 |
await prisma.$transaction(async (tx) => {
|
| 140 |
await tx.payment.upsert({
|
| 141 |
where: { stripeSessionId: session.id },
|
| 142 |
-
update: {
|
| 143 |
create: {
|
| 144 |
userId,
|
| 145 |
trackId,
|
| 146 |
amount: session.amount_total,
|
| 147 |
status: 'COMPLETED',
|
| 148 |
stripeSessionId: session.id,
|
| 149 |
-
currency: session.currency || 'XOF'
|
|
|
|
| 150 |
}
|
| 151 |
});
|
| 152 |
|
|
@@ -160,7 +163,8 @@ export async function stripeWebhookRoute(fastify: FastifyInstance) {
|
|
| 160 |
userId,
|
| 161 |
trackId,
|
| 162 |
status: 'ACTIVE',
|
| 163 |
-
currentDay: 1
|
|
|
|
| 164 |
}
|
| 165 |
});
|
| 166 |
}
|
|
|
|
| 134 |
const orgId = session.metadata?.organizationId; // If it's an org sub, it has this
|
| 135 |
|
| 136 |
if (userId && trackId) {
|
| 137 |
+
// Determine organization context (mandatory for hardened schema)
|
| 138 |
+
const targetOrgId = orgId || session.metadata?.targetOrganizationId || 'default-org-id';
|
| 139 |
+
|
| 140 |
try {
|
| 141 |
await prisma.$transaction(async (tx) => {
|
| 142 |
await tx.payment.upsert({
|
| 143 |
where: { stripeSessionId: session.id },
|
| 144 |
+
update: { organizationId: targetOrgId },
|
| 145 |
create: {
|
| 146 |
userId,
|
| 147 |
trackId,
|
| 148 |
amount: session.amount_total,
|
| 149 |
status: 'COMPLETED',
|
| 150 |
stripeSessionId: session.id,
|
| 151 |
+
currency: session.currency || 'XOF',
|
| 152 |
+
organizationId: targetOrgId
|
| 153 |
}
|
| 154 |
});
|
| 155 |
|
|
|
|
| 163 |
userId,
|
| 164 |
trackId,
|
| 165 |
status: 'ACTIVE',
|
| 166 |
+
currentDay: 1,
|
| 167 |
+
organizationId: targetOrgId
|
| 168 |
}
|
| 169 |
});
|
| 170 |
}
|
apps/api/src/routes/whatsapp.ts
CHANGED
|
@@ -95,7 +95,8 @@ export async function whatsappRoutes(fastify: FastifyInstance) {
|
|
| 95 |
await prisma.analyticsLog.create({
|
| 96 |
data: {
|
| 97 |
campaignHistoryId: history.id,
|
| 98 |
-
eventType: 'READ'
|
|
|
|
| 99 |
}
|
| 100 |
});
|
| 101 |
}
|
|
|
|
| 95 |
await prisma.analyticsLog.create({
|
| 96 |
data: {
|
| 97 |
campaignHistoryId: history.id,
|
| 98 |
+
eventType: 'READ',
|
| 99 |
+
organizationId: history.organizationId
|
| 100 |
}
|
| 101 |
});
|
| 102 |
}
|
apps/api/src/scripts/migrate-json-to-sql.ts
CHANGED
|
@@ -46,43 +46,11 @@ async function migrate() {
|
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
|
|
|
| 49 |
// 2. Migrate Team Members (BusinessProfile.teamMembers -> TeamMember)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
}
|
| 54 |
-
});
|
| 55 |
-
|
| 56 |
-
logger.info(`Found ${profilesWithTeam.length} BusinessProfile records with teamMembers JSON.`);
|
| 57 |
-
|
| 58 |
-
for (const profile of profilesWithTeam) {
|
| 59 |
-
const team = profile.teamMembers as any;
|
| 60 |
-
if (Array.isArray(team)) {
|
| 61 |
-
for (const member of team) {
|
| 62 |
-
if (!member || typeof member !== 'object') continue;
|
| 63 |
-
|
| 64 |
-
const name = member.name || member.fullName || 'Unknown';
|
| 65 |
-
const existing = await (prisma as any).teamMember.findFirst({
|
| 66 |
-
where: {
|
| 67 |
-
businessProfileId: profile.id,
|
| 68 |
-
name: name
|
| 69 |
-
}
|
| 70 |
-
});
|
| 71 |
-
|
| 72 |
-
if (!existing) {
|
| 73 |
-
await (prisma as any).teamMember.create({
|
| 74 |
-
data: {
|
| 75 |
-
businessProfileId: profile.id,
|
| 76 |
-
name: name,
|
| 77 |
-
role: member.role || member.position,
|
| 78 |
-
bio: member.bio || member.description
|
| 79 |
-
}
|
| 80 |
-
});
|
| 81 |
-
logger.info(`Migrated team member "${name}" for profile ${profile.id}`);
|
| 82 |
-
}
|
| 83 |
-
}
|
| 84 |
-
}
|
| 85 |
-
}
|
| 86 |
|
| 87 |
logger.info('β
Neon data migration completed successfully!');
|
| 88 |
}
|
|
|
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
| 49 |
+
/*
|
| 50 |
// 2. Migrate Team Members (BusinessProfile.teamMembers -> TeamMember)
|
| 51 |
+
// DEPRECATED: Field removed from schema
|
| 52 |
+
...
|
| 53 |
+
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
logger.info('β
Neon data migration completed successfully!');
|
| 56 |
}
|
apps/api/src/scripts/test-e2e-journey.ts
CHANGED
|
@@ -72,11 +72,13 @@ async function runTests() {
|
|
| 72 |
const track = await prisma.track.findFirst({ where: { title: { contains: 'Comprendre son business' } } });
|
| 73 |
if (!track) throw new Error("Could not find T1 Track in DB.");
|
| 74 |
|
|
|
|
| 75 |
const user = await prisma.user.create({
|
| 76 |
data: {
|
| 77 |
phone: TEST_PHONE,
|
| 78 |
language: 'WOLOF',
|
| 79 |
activity: 'Couture',
|
|
|
|
| 80 |
}
|
| 81 |
});
|
| 82 |
|
|
@@ -85,7 +87,8 @@ async function runTests() {
|
|
| 85 |
userId: user.id,
|
| 86 |
trackId: track.id,
|
| 87 |
currentDay: 1,
|
| 88 |
-
status: 'ACTIVE'
|
|
|
|
| 89 |
}
|
| 90 |
});
|
| 91 |
|
|
@@ -120,7 +123,7 @@ async function runTests() {
|
|
| 120 |
await prisma.userProgress.upsert({
|
| 121 |
where: { userId_trackId: { userId: user.id, trackId: track.id } },
|
| 122 |
update: { exerciseStatus: 'PENDING_REVIEW' as any },
|
| 123 |
-
create: { userId: user.id, trackId: track.id, exerciseStatus: 'PENDING_REVIEW' as any }
|
| 124 |
});
|
| 125 |
|
| 126 |
const preReview = await prisma.userProgress.findUnique({ where: { userId_trackId: { userId: user.id, trackId: track.id } } });
|
|
|
|
| 72 |
const track = await prisma.track.findFirst({ where: { title: { contains: 'Comprendre son business' } } });
|
| 73 |
if (!track) throw new Error("Could not find T1 Track in DB.");
|
| 74 |
|
| 75 |
+
const organizationId = 'default-org-id';
|
| 76 |
const user = await prisma.user.create({
|
| 77 |
data: {
|
| 78 |
phone: TEST_PHONE,
|
| 79 |
language: 'WOLOF',
|
| 80 |
activity: 'Couture',
|
| 81 |
+
organizationId
|
| 82 |
}
|
| 83 |
});
|
| 84 |
|
|
|
|
| 87 |
userId: user.id,
|
| 88 |
trackId: track.id,
|
| 89 |
currentDay: 1,
|
| 90 |
+
status: 'ACTIVE',
|
| 91 |
+
organizationId: 'default-org-id'
|
| 92 |
}
|
| 93 |
});
|
| 94 |
|
|
|
|
| 123 |
await prisma.userProgress.upsert({
|
| 124 |
where: { userId_trackId: { userId: user.id, trackId: track.id } },
|
| 125 |
update: { exerciseStatus: 'PENDING_REVIEW' as any },
|
| 126 |
+
create: { userId: user.id, trackId: track.id, exerciseStatus: 'PENDING_REVIEW' as any, organizationId: 'default-org-id' }
|
| 127 |
});
|
| 128 |
|
| 129 |
const preReview = await prisma.userProgress.findUnique({ where: { userId_trackId: { userId: user.id, trackId: track.id } } });
|
apps/api/src/services/ai/index.ts
CHANGED
|
@@ -284,9 +284,24 @@ export class AIService {
|
|
| 284 |
return { ...data, aiSource: source };
|
| 285 |
}
|
| 286 |
|
| 287 |
-
async generateBroadcastMessage(userPrompt: string): Promise<{ text: string, aiSource: string }> {
|
| 288 |
-
const systemPrompt =
|
| 289 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
}
|
| 291 |
|
| 292 |
async extractBusinessProfile(userInput: string, dayNumber: number, userLanguage: string = 'FR'): Promise<any> {
|
|
|
|
| 284 |
return { ...data, aiSource: source };
|
| 285 |
}
|
| 286 |
|
| 287 |
+
async generateBroadcastMessage(userPrompt: string): Promise<{ text: string, type: 'campaign' | 'support', aiSource: string }> {
|
| 288 |
+
const systemPrompt = `Tu es l'Assistant CRM intelligent de la plateforme XAMLΓ. Tu dois analyser la demande de l'utilisateur et choisir entre deux comportements :
|
| 289 |
+
Cas 1 (CrΓ©ation de campagne) : Si l'utilisateur demande explicitement d'Γ©crire une offre, une promotion ou un message Γ envoyer Γ ses clients, rΓ©dige un message WhatsApp percutant avec des emojis.
|
| 290 |
+
Cas 2 (Support et Configuration) : Si l'utilisateur pose une question, demande comment configurer un message d'absence, comment crΓ©er un template Meta, ou demande une analyse, agis comme un expert technique. Explique la procΓ©dure Γ©tape par Γ©tape de maniΓ¨re claire. Dans ce cas prΓ©cis, NE rΓ©dige PAS de message destinΓ© Γ Γͺtre envoyΓ© aux clients. Ne fais pas de publicitΓ© pour tes services.
|
| 291 |
+
|
| 292 |
+
IMPORTANT: Tu dois impΓ©rativement rΓ©pondre au format JSON suivant :
|
| 293 |
+
{
|
| 294 |
+
"type": "campaign" | "support",
|
| 295 |
+
"content": "Le texte de ta rΓ©ponse ici"
|
| 296 |
+
}`;
|
| 297 |
+
|
| 298 |
+
const schema = z.object({
|
| 299 |
+
type: z.enum(['campaign', 'support']),
|
| 300 |
+
content: z.string()
|
| 301 |
+
});
|
| 302 |
+
|
| 303 |
+
const { data, source } = await this.callWithFailover(systemPrompt + "\n\nInstruction utilisateur: " + userPrompt, schema, 0.7);
|
| 304 |
+
return { text: data.content, type: data.type, aiSource: source };
|
| 305 |
}
|
| 306 |
|
| 307 |
async extractBusinessProfile(userInput: string, dayNumber: number, userLanguage: string = 'FR'): Promise<any> {
|
apps/api/src/services/whatsapp-utils.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
export function normalizeCommand(text: string): string {
|
| 2 |
return text
|
| 3 |
.trim()
|
|
@@ -48,3 +50,36 @@ export function isFuzzyMatch(text: string, target: string, threshold = 0.8): boo
|
|
| 48 |
const similarity = 1 - distance / maxLength;
|
| 49 |
return similarity >= threshold;
|
| 50 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import axios from 'axios';
|
| 2 |
+
|
| 3 |
export function normalizeCommand(text: string): string {
|
| 4 |
return text
|
| 5 |
.trim()
|
|
|
|
| 50 |
const similarity = 1 - distance / maxLength;
|
| 51 |
return similarity >= threshold;
|
| 52 |
}
|
| 53 |
+
|
| 54 |
+
/**
|
| 55 |
+
* Download a WhatsApp media file from the Graph API.
|
| 56 |
+
* @param mediaId - The media ID from the WhatsApp webhook payload
|
| 57 |
+
* @param accessToken - WHATSAPP_ACCESS_TOKEN
|
| 58 |
+
* @returns { buffer, mimeType, fileSize }
|
| 59 |
+
*/
|
| 60 |
+
export async function downloadMedia(
|
| 61 |
+
mediaId: string,
|
| 62 |
+
accessToken: string
|
| 63 |
+
): Promise<{ buffer: Buffer; mimeType: string; fileSize: number }> {
|
| 64 |
+
// Step 1: Get the media URL
|
| 65 |
+
const metaRes = await axios.get(
|
| 66 |
+
`https://graph.facebook.com/v18.0/${mediaId}`,
|
| 67 |
+
{ headers: { Authorization: `Bearer ${accessToken}` } }
|
| 68 |
+
);
|
| 69 |
+
const { url, mime_type, file_size } = metaRes.data;
|
| 70 |
+
|
| 71 |
+
if (!url) throw new Error(`[downloadMedia] No URL returned for media ${mediaId}`);
|
| 72 |
+
|
| 73 |
+
// Step 2: Download the binary content
|
| 74 |
+
const mediaRes = await axios.get(url, {
|
| 75 |
+
headers: { Authorization: `Bearer ${accessToken}` },
|
| 76 |
+
responseType: 'arraybuffer',
|
| 77 |
+
timeout: 30_000
|
| 78 |
+
});
|
| 79 |
+
|
| 80 |
+
return {
|
| 81 |
+
buffer: Buffer.from(mediaRes.data),
|
| 82 |
+
mimeType: mime_type || 'audio/ogg',
|
| 83 |
+
fileSize: file_size || mediaRes.data.byteLength
|
| 84 |
+
};
|
| 85 |
+
}
|
apps/api/src/types/fastify.d.ts
CHANGED
|
@@ -1,25 +1,19 @@
|
|
| 1 |
-
import { FastifyInstance, FastifyRequest } from 'fastify';
|
| 2 |
import { PrismaClient } from '@repo/database';
|
| 3 |
-
import '@fastify/multipart';
|
| 4 |
|
| 5 |
declare module 'fastify' {
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
interface FastifyContextConfig {
|
| 23 |
-
requireAuth?: boolean;
|
| 24 |
-
}
|
| 25 |
}
|
|
|
|
|
|
|
| 1 |
import { PrismaClient } from '@repo/database';
|
|
|
|
| 2 |
|
| 3 |
declare module 'fastify' {
|
| 4 |
+
interface FastifyInstance {
|
| 5 |
+
prisma: PrismaClient;
|
| 6 |
+
}
|
| 7 |
+
interface FastifyRequest {
|
| 8 |
+
user: {
|
| 9 |
+
id: string;
|
| 10 |
+
role: string;
|
| 11 |
+
organizationId: string;
|
| 12 |
+
};
|
| 13 |
+
organizationId?: string;
|
| 14 |
+
rawBody?: Buffer;
|
| 15 |
+
}
|
| 16 |
+
interface FastifyContextConfig {
|
| 17 |
+
requireAuth?: boolean;
|
| 18 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
}
|
apps/whatsapp-worker/src/handlers/InboundHandler.ts
CHANGED
|
@@ -2,13 +2,67 @@ import { Job } from 'bullmq';
|
|
| 2 |
import { JobHandler, JobData } from './types';
|
| 3 |
import { logger } from '../logger';
|
| 4 |
import { WhatsAppLogic } from '../services/whatsapp-logic';
|
|
|
|
|
|
|
| 5 |
|
| 6 |
export class InboundHandler implements JobHandler {
|
| 7 |
async handle(job: Job<JobData>): Promise<void> {
|
| 8 |
-
|
| 9 |
|
| 10 |
-
if (!phone
|
| 11 |
-
logger.error(`[INBOUND_HANDLER] Missing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
return;
|
| 13 |
}
|
| 14 |
|
|
@@ -19,7 +73,9 @@ export class InboundHandler implements JobHandler {
|
|
| 19 |
text,
|
| 20 |
audioUrl,
|
| 21 |
imageUrl,
|
| 22 |
-
organizationId
|
|
|
|
|
|
|
| 23 |
);
|
| 24 |
}
|
| 25 |
}
|
|
|
|
| 2 |
import { JobHandler, JobData } from './types';
|
| 3 |
import { logger } from '../logger';
|
| 4 |
import { WhatsAppLogic } from '../services/whatsapp-logic';
|
| 5 |
+
import { aiService } from '../services/ai';
|
| 6 |
+
import { prisma } from '../services/prisma';
|
| 7 |
|
| 8 |
export class InboundHandler implements JobHandler {
|
| 9 |
async handle(job: Job<JobData>): Promise<void> {
|
| 10 |
+
let { phone, text, audioUrl, imageUrl, organizationId } = job.data;
|
| 11 |
|
| 12 |
+
if (!phone) {
|
| 13 |
+
logger.error(`[INBOUND_HANDLER] Missing phone number`);
|
| 14 |
+
return;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
organizationId = organizationId || 'default-org-id';
|
| 18 |
+
|
| 19 |
+
// ποΈ Handle Inbound Audio Transcription
|
| 20 |
+
if (audioUrl && (!text || text.trim() === '')) {
|
| 21 |
+
const mediaId = audioUrl; // In our bridge, audioUrl often carries the mediaId
|
| 22 |
+
logger.info(`[INBOUND_HANDLER] Detected audio inbound (${mediaId}). Starting transcription...`);
|
| 23 |
+
|
| 24 |
+
try {
|
| 25 |
+
// ... (transcription logic same)
|
| 26 |
+
let accessToken = process.env.WHATSAPP_ACCESS_TOKEN;
|
| 27 |
+
if (!accessToken) {
|
| 28 |
+
const org = await prisma.organization.findUnique({
|
| 29 |
+
where: { id: organizationId },
|
| 30 |
+
select: { systemUserToken: true }
|
| 31 |
+
});
|
| 32 |
+
accessToken = org?.systemUserToken || undefined;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
if (accessToken) {
|
| 36 |
+
const mediaRes = await fetch(`https://graph.facebook.com/v19.0/${mediaId}`, {
|
| 37 |
+
headers: { Authorization: `Bearer ${accessToken}` }
|
| 38 |
+
});
|
| 39 |
+
const mediaData = await mediaRes.json() as { url?: string };
|
| 40 |
+
|
| 41 |
+
if (mediaData.url) {
|
| 42 |
+
const audioRes = await fetch(mediaData.url, {
|
| 43 |
+
headers: { Authorization: `Bearer ${accessToken}` }
|
| 44 |
+
});
|
| 45 |
+
const audioBuffer = await audioRes.arrayBuffer();
|
| 46 |
+
const transcribedText = await aiService.transcribeAudio(Buffer.from(audioBuffer), `msg_${mediaId}.ogg`);
|
| 47 |
+
|
| 48 |
+
if (transcribedText) {
|
| 49 |
+
text = `π€ Transcription : ${transcribedText}`;
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
} catch (err) {
|
| 54 |
+
logger.error({ err, mediaId }, '[INBOUND_HANDLER] Transcription flow failed');
|
| 55 |
+
text = text || "π€ [Message vocal reΓ§u]";
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
// πΌοΈ Handle Inbound Images (Fallback content if caption is missing)
|
| 60 |
+
if (imageUrl && (!text || text.trim() === '')) {
|
| 61 |
+
text = "[Image reΓ§ue]";
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
if (text === undefined) {
|
| 65 |
+
logger.error(`[INBOUND_HANDLER] Missing text/content for ${phone}`);
|
| 66 |
return;
|
| 67 |
}
|
| 68 |
|
|
|
|
| 73 |
text,
|
| 74 |
audioUrl,
|
| 75 |
imageUrl,
|
| 76 |
+
organizationId,
|
| 77 |
+
audioUrl ? 'audio' : imageUrl ? 'image' : undefined,
|
| 78 |
+
audioUrl || imageUrl || undefined
|
| 79 |
);
|
| 80 |
}
|
| 81 |
}
|
apps/whatsapp-worker/src/handlers/MediaHandler.ts
CHANGED
|
@@ -123,7 +123,14 @@ export class MediaHandler implements JobHandler {
|
|
| 123 |
await prisma.userProgress.upsert({
|
| 124 |
where: { userId_trackId: { userId: user.id, trackId: activeEnrollment.trackId } },
|
| 125 |
update: { exerciseStatus: 'PENDING_REVIEW' as any, adminTranscription: transcribedText, confidenceScore: confidence },
|
| 126 |
-
create: {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
});
|
| 128 |
await sendTextMessage(phone, "ποΈ Nyangi jaxas sa kΓ ddu. Xamle dina la tontu ci kanam !", tenantConfig);
|
| 129 |
return;
|
|
@@ -132,11 +139,11 @@ export class MediaHandler implements JobHandler {
|
|
| 132 |
}
|
| 133 |
|
| 134 |
if (transcribedText) {
|
| 135 |
-
await WhatsAppLogic.handleIncomingMessage(phone, transcribedText, audioUrl, undefined, organizationId);
|
| 136 |
}
|
| 137 |
}
|
| 138 |
} else if (mimeType?.startsWith('image/')) {
|
| 139 |
-
await WhatsAppLogic.handleIncomingMessage(phone, job.data.caption || 'Image reΓ§ue', undefined, audioUrl, organizationId);
|
| 140 |
}
|
| 141 |
} catch (err) {
|
| 142 |
logger.error(`[MEDIA_HANDLER] download-media failed:`, err);
|
|
|
|
| 123 |
await prisma.userProgress.upsert({
|
| 124 |
where: { userId_trackId: { userId: user.id, trackId: activeEnrollment.trackId } },
|
| 125 |
update: { exerciseStatus: 'PENDING_REVIEW' as any, adminTranscription: transcribedText, confidenceScore: confidence },
|
| 126 |
+
create: {
|
| 127 |
+
userId: user.id,
|
| 128 |
+
trackId: activeEnrollment.trackId,
|
| 129 |
+
organizationId: organizationId as string,
|
| 130 |
+
exerciseStatus: 'PENDING_REVIEW' as any,
|
| 131 |
+
adminTranscription: transcribedText,
|
| 132 |
+
confidenceScore: confidence
|
| 133 |
+
}
|
| 134 |
});
|
| 135 |
await sendTextMessage(phone, "ποΈ Nyangi jaxas sa kΓ ddu. Xamle dina la tontu ci kanam !", tenantConfig);
|
| 136 |
return;
|
|
|
|
| 139 |
}
|
| 140 |
|
| 141 |
if (transcribedText) {
|
| 142 |
+
await WhatsAppLogic.handleIncomingMessage(phone, transcribedText, audioUrl, undefined, organizationId, 'audio', mediaId);
|
| 143 |
}
|
| 144 |
}
|
| 145 |
} else if (mimeType?.startsWith('image/')) {
|
| 146 |
+
await WhatsAppLogic.handleIncomingMessage(phone, job.data.caption || 'Image reΓ§ue', undefined, audioUrl, organizationId, 'image', mediaId);
|
| 147 |
}
|
| 148 |
} catch (err) {
|
| 149 |
logger.error(`[MEDIA_HANDLER] download-media failed:`, err);
|
apps/whatsapp-worker/src/index.ts
CHANGED
|
@@ -138,18 +138,35 @@ server.post('/v1/internal/whatsapp/inbound', async (req: FastifyRequest, reply:
|
|
| 138 |
backoff: { type: 'exponential', delay: 1000 }
|
| 139 |
});
|
| 140 |
} else if (msg.mediaId) {
|
| 141 |
-
// Handle media (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
await whatsappQueue.add('download-media', {
|
| 143 |
mediaId: msg.mediaId,
|
| 144 |
-
mimeType:
|
| 145 |
phone: msg.phone,
|
| 146 |
organizationId,
|
| 147 |
caption: msg.caption
|
| 148 |
}, { priority: 1, attempts: 3, backoff: { type: 'exponential', delay: 2000 } });
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
await whatsappQueue.add('send-message-direct', {
|
| 151 |
phone: msg.phone,
|
| 152 |
-
text:
|
| 153 |
organizationId
|
| 154 |
});
|
| 155 |
}
|
|
|
|
| 138 |
backoff: { type: 'exponential', delay: 1000 }
|
| 139 |
});
|
| 140 |
} else if (msg.mediaId) {
|
| 141 |
+
// ποΈ Handle media (Images and Audio)
|
| 142 |
+
const isImage = msg.mediaType === 'image';
|
| 143 |
+
|
| 144 |
+
logger.info(`[BRIDGE] Enqueuing inbound media (${msg.mediaType}) from ${msg.phone}`);
|
| 145 |
+
|
| 146 |
+
// 1. Traditional Media Processing (for training/pedagogy)
|
| 147 |
await whatsappQueue.add('download-media', {
|
| 148 |
mediaId: msg.mediaId,
|
| 149 |
+
mimeType: isImage ? 'image/jpeg' : 'audio/ogg',
|
| 150 |
phone: msg.phone,
|
| 151 |
organizationId,
|
| 152 |
caption: msg.caption
|
| 153 |
}, { priority: 1, attempts: 3, backoff: { type: 'exponential', delay: 2000 } });
|
| 154 |
|
| 155 |
+
// 2. New CRM Inbox flow
|
| 156 |
+
await whatsappQueue.add('handle-inbound', {
|
| 157 |
+
phone: msg.phone,
|
| 158 |
+
text: msg.caption || '', // Use caption as text if available
|
| 159 |
+
audioUrl: !isImage ? msg.mediaId : undefined,
|
| 160 |
+
imageUrl: isImage ? msg.mediaId : undefined,
|
| 161 |
+
organizationId
|
| 162 |
+
}, {
|
| 163 |
+
attempts: 3,
|
| 164 |
+
backoff: { type: 'exponential', delay: 1000 }
|
| 165 |
+
});
|
| 166 |
+
|
| 167 |
await whatsappQueue.add('send-message-direct', {
|
| 168 |
phone: msg.phone,
|
| 169 |
+
text: isImage ? "β³ J'analyse ton image..." : "β³ J'analyse ton audio...",
|
| 170 |
organizationId
|
| 171 |
});
|
| 172 |
}
|
apps/whatsapp-worker/src/services/ai.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { logger } from '../logger';
|
| 2 |
+
import { getApiUrl, getAdminApiKey } from '../config';
|
| 3 |
+
|
| 4 |
+
export class AIService {
|
| 5 |
+
static async transcribeAudio(audioBuffer: Buffer, filename: string = 'audio.ogg'): Promise<string> {
|
| 6 |
+
try {
|
| 7 |
+
const AI_API_BASE_URL = getApiUrl();
|
| 8 |
+
const apiKey = getAdminApiKey();
|
| 9 |
+
|
| 10 |
+
// Create a FormData-like structure or just send base64 if the API supports it
|
| 11 |
+
// Based on MediaHandler.ts, the API expects { audioBase64, filename, language }
|
| 12 |
+
const res = await fetch(`${AI_API_BASE_URL.replace(/\/$/, "")}/v1/ai/transcribe`, {
|
| 13 |
+
method: 'POST',
|
| 14 |
+
headers: {
|
| 15 |
+
'Content-Type': 'application/json',
|
| 16 |
+
'Authorization': `Bearer ${apiKey}`
|
| 17 |
+
},
|
| 18 |
+
body: JSON.stringify({
|
| 19 |
+
audioBase64: audioBuffer.toString('base64'),
|
| 20 |
+
filename: filename
|
| 21 |
+
})
|
| 22 |
+
});
|
| 23 |
+
|
| 24 |
+
if (res.ok) {
|
| 25 |
+
const data = await res.json() as { text: string };
|
| 26 |
+
return data.text || '';
|
| 27 |
+
} else {
|
| 28 |
+
const error = await res.text();
|
| 29 |
+
logger.error(`[AI_SERVICE] Transcription API error: ${error}`);
|
| 30 |
+
}
|
| 31 |
+
} catch (err) {
|
| 32 |
+
logger.error(`[AI_SERVICE] Transcription failed: ${err}`);
|
| 33 |
+
}
|
| 34 |
+
return '';
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
export const aiService = AIService;
|
apps/whatsapp-worker/src/services/whatsapp-logic.ts
CHANGED
|
@@ -40,7 +40,15 @@ export class WhatsAppLogic {
|
|
| 40 |
.toUpperCase();
|
| 41 |
}
|
| 42 |
|
| 43 |
-
static async handleIncomingMessage(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
const traceId = audioUrl ? `[STT-FLOW-${phone.slice(-4)}]` : imageUrl ? `[IMG-FLOW-${phone.slice(-4)}]` : `[TXT-FLOW-${phone.slice(-4)}]`;
|
| 45 |
const normalizedText = this.normalizeCommand(text);
|
| 46 |
logger.info(`${traceId} Orchestrating Inbound: ${normalizedText}`);
|
|
@@ -54,6 +62,8 @@ export class WhatsAppLogic {
|
|
| 54 |
data: {
|
| 55 |
content: text,
|
| 56 |
mediaUrl: audioUrl || imageUrl,
|
|
|
|
|
|
|
| 57 |
direction: 'INBOUND',
|
| 58 |
userId: user?.id,
|
| 59 |
contactId: contact?.id,
|
|
|
|
| 40 |
.toUpperCase();
|
| 41 |
}
|
| 42 |
|
| 43 |
+
static async handleIncomingMessage(
|
| 44 |
+
phone: string,
|
| 45 |
+
text: string,
|
| 46 |
+
audioUrl?: string,
|
| 47 |
+
imageUrl?: string,
|
| 48 |
+
organizationId: string = 'default-org-id',
|
| 49 |
+
mediaType?: string,
|
| 50 |
+
mediaId?: string
|
| 51 |
+
) {
|
| 52 |
const traceId = audioUrl ? `[STT-FLOW-${phone.slice(-4)}]` : imageUrl ? `[IMG-FLOW-${phone.slice(-4)}]` : `[TXT-FLOW-${phone.slice(-4)}]`;
|
| 53 |
const normalizedText = this.normalizeCommand(text);
|
| 54 |
logger.info(`${traceId} Orchestrating Inbound: ${normalizedText}`);
|
|
|
|
| 62 |
data: {
|
| 63 |
content: text,
|
| 64 |
mediaUrl: audioUrl || imageUrl,
|
| 65 |
+
mediaType: mediaType || (audioUrl ? 'audio' : imageUrl ? 'image' : undefined),
|
| 66 |
+
mediaId: mediaId || (audioUrl && audioUrl.length > 20 ? audioUrl : undefined), // Fallback if audioUrl is actually the ID
|
| 67 |
direction: 'INBOUND',
|
| 68 |
userId: user?.id,
|
| 69 |
contactId: contact?.id,
|
packages/database/prisma/schema.prisma
CHANGED
|
@@ -43,8 +43,9 @@ model Organization {
|
|
| 43 |
userBadges UserBadge[]
|
| 44 |
progress UserProgress[]
|
| 45 |
phoneNumbers WhatsAppPhoneNumber[]
|
| 46 |
-
|
| 47 |
campaigns CampaignHistory[]
|
|
|
|
| 48 |
broadcastLists BroadcastList[]
|
| 49 |
}
|
| 50 |
|
|
@@ -99,12 +100,15 @@ model CampaignHistory {
|
|
| 99 |
|
| 100 |
model AnalyticsLog {
|
| 101 |
id String @id @default(uuid())
|
|
|
|
| 102 |
campaignHistoryId String
|
| 103 |
eventType String // CLICK, READ, RESPONSE
|
| 104 |
metadata Json?
|
| 105 |
occurredAt DateTime @default(now())
|
|
|
|
| 106 |
campaign CampaignHistory @relation(fields: [campaignHistoryId], references: [id], onDelete: Cascade)
|
| 107 |
|
|
|
|
| 108 |
@@index([campaignHistoryId])
|
| 109 |
@@index([eventType])
|
| 110 |
}
|
|
@@ -147,7 +151,7 @@ model User {
|
|
| 147 |
currentStreak Int @default(0)
|
| 148 |
longestStreak Int @default(0)
|
| 149 |
lastActivityAt DateTime?
|
| 150 |
-
organizationId String
|
| 151 |
businessProfile BusinessProfile?
|
| 152 |
enrollments Enrollment[]
|
| 153 |
messages Message[]
|
|
@@ -196,8 +200,7 @@ model BusinessProfile {
|
|
| 196 |
lastUpdatedFromDay Int @default(0)
|
| 197 |
createdAt DateTime @default(now())
|
| 198 |
updatedAt DateTime @updatedAt
|
| 199 |
-
|
| 200 |
-
organizationId String @default("default-org-id")
|
| 201 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 202 |
user User @relation(fields: [userId], references: [id])
|
| 203 |
teamMembersList TeamMember[]
|
|
@@ -211,7 +214,7 @@ model TeamMember {
|
|
| 211 |
name String?
|
| 212 |
role String?
|
| 213 |
bio String?
|
| 214 |
-
organizationId String
|
| 215 |
businessProfile BusinessProfile @relation(fields: [businessProfileId], references: [id], onDelete: Cascade)
|
| 216 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 217 |
|
|
@@ -229,7 +232,7 @@ model Track {
|
|
| 229 |
stripePriceId String?
|
| 230 |
createdAt DateTime @default(now())
|
| 231 |
updatedAt DateTime @updatedAt
|
| 232 |
-
organizationId String
|
| 233 |
enrollments Enrollment[]
|
| 234 |
payments Payment[]
|
| 235 |
organization Organization @relation(fields: [organizationId], references: [id])
|
|
@@ -258,7 +261,7 @@ model TrackDay {
|
|
| 258 |
unlockCondition String?
|
| 259 |
createdAt DateTime @default(now())
|
| 260 |
updatedAt DateTime @updatedAt
|
| 261 |
-
organizationId String
|
| 262 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 263 |
track Track @relation(fields: [trackId], references: [id])
|
| 264 |
|
|
@@ -283,7 +286,7 @@ model UserProgress {
|
|
| 283 |
updatedAt DateTime @updatedAt
|
| 284 |
iterationCount Int @default(0)
|
| 285 |
aiSource String?
|
| 286 |
-
organizationId String
|
| 287 |
userBadges UserBadge[]
|
| 288 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 289 |
track Track @relation(fields: [trackId], references: [id])
|
|
@@ -303,7 +306,7 @@ model Enrollment {
|
|
| 303 |
startedAt DateTime @default(now())
|
| 304 |
completedAt DateTime?
|
| 305 |
lastActivityAt DateTime @default(now())
|
| 306 |
-
organizationId String
|
| 307 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 308 |
track Track @relation(fields: [trackId], references: [id])
|
| 309 |
user User @relation(fields: [userId], references: [id])
|
|
@@ -322,7 +325,7 @@ model Response {
|
|
| 322 |
mediaUrl String?
|
| 323 |
createdAt DateTime @default(now())
|
| 324 |
aiSource String?
|
| 325 |
-
organizationId String
|
| 326 |
enrollment Enrollment @relation(fields: [enrollmentId], references: [id], onDelete: Cascade)
|
| 327 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 328 |
user User @relation(fields: [userId], references: [id])
|
|
@@ -338,10 +341,12 @@ model Message {
|
|
| 338 |
channel String @default("WHATSAPP")
|
| 339 |
content String?
|
| 340 |
mediaUrl String?
|
|
|
|
|
|
|
| 341 |
payload Json?
|
| 342 |
createdAt DateTime @default(now())
|
| 343 |
status MessageStatus @default(SENT)
|
| 344 |
-
organizationId String
|
| 345 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 346 |
user User? @relation(fields: [userId], references: [id])
|
| 347 |
contact Contact? @relation(fields: [contactId], references: [id])
|
|
@@ -362,7 +367,7 @@ model Payment {
|
|
| 362 |
stripeSessionId String? @unique
|
| 363 |
createdAt DateTime @default(now())
|
| 364 |
updatedAt DateTime @updatedAt
|
| 365 |
-
organizationId String
|
| 366 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 367 |
track Track @relation(fields: [trackId], references: [id])
|
| 368 |
user User @relation(fields: [userId], references: [id])
|
|
@@ -398,7 +403,7 @@ model UserBadge {
|
|
| 398 |
userProgressId String
|
| 399 |
name String
|
| 400 |
earnedAt DateTime @default(now())
|
| 401 |
-
organizationId String
|
| 402 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 403 |
userProgress UserProgress @relation(fields: [userProgressId], references: [id], onDelete: Cascade)
|
| 404 |
|
|
|
|
| 43 |
userBadges UserBadge[]
|
| 44 |
progress UserProgress[]
|
| 45 |
phoneNumbers WhatsAppPhoneNumber[]
|
| 46 |
+
analytics AnalyticsLog[]
|
| 47 |
campaigns CampaignHistory[]
|
| 48 |
+
contacts Contact[]
|
| 49 |
broadcastLists BroadcastList[]
|
| 50 |
}
|
| 51 |
|
|
|
|
| 100 |
|
| 101 |
model AnalyticsLog {
|
| 102 |
id String @id @default(uuid())
|
| 103 |
+
organizationId String
|
| 104 |
campaignHistoryId String
|
| 105 |
eventType String // CLICK, READ, RESPONSE
|
| 106 |
metadata Json?
|
| 107 |
occurredAt DateTime @default(now())
|
| 108 |
+
organization Organization @relation(fields: [organizationId], references: [id])
|
| 109 |
campaign CampaignHistory @relation(fields: [campaignHistoryId], references: [id], onDelete: Cascade)
|
| 110 |
|
| 111 |
+
@@index([organizationId])
|
| 112 |
@@index([campaignHistoryId])
|
| 113 |
@@index([eventType])
|
| 114 |
}
|
|
|
|
| 151 |
currentStreak Int @default(0)
|
| 152 |
longestStreak Int @default(0)
|
| 153 |
lastActivityAt DateTime?
|
| 154 |
+
organizationId String
|
| 155 |
businessProfile BusinessProfile?
|
| 156 |
enrollments Enrollment[]
|
| 157 |
messages Message[]
|
|
|
|
| 200 |
lastUpdatedFromDay Int @default(0)
|
| 201 |
createdAt DateTime @default(now())
|
| 202 |
updatedAt DateTime @updatedAt
|
| 203 |
+
organizationId String
|
|
|
|
| 204 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 205 |
user User @relation(fields: [userId], references: [id])
|
| 206 |
teamMembersList TeamMember[]
|
|
|
|
| 214 |
name String?
|
| 215 |
role String?
|
| 216 |
bio String?
|
| 217 |
+
organizationId String
|
| 218 |
businessProfile BusinessProfile @relation(fields: [businessProfileId], references: [id], onDelete: Cascade)
|
| 219 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 220 |
|
|
|
|
| 232 |
stripePriceId String?
|
| 233 |
createdAt DateTime @default(now())
|
| 234 |
updatedAt DateTime @updatedAt
|
| 235 |
+
organizationId String
|
| 236 |
enrollments Enrollment[]
|
| 237 |
payments Payment[]
|
| 238 |
organization Organization @relation(fields: [organizationId], references: [id])
|
|
|
|
| 261 |
unlockCondition String?
|
| 262 |
createdAt DateTime @default(now())
|
| 263 |
updatedAt DateTime @updatedAt
|
| 264 |
+
organizationId String
|
| 265 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 266 |
track Track @relation(fields: [trackId], references: [id])
|
| 267 |
|
|
|
|
| 286 |
updatedAt DateTime @updatedAt
|
| 287 |
iterationCount Int @default(0)
|
| 288 |
aiSource String?
|
| 289 |
+
organizationId String
|
| 290 |
userBadges UserBadge[]
|
| 291 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 292 |
track Track @relation(fields: [trackId], references: [id])
|
|
|
|
| 306 |
startedAt DateTime @default(now())
|
| 307 |
completedAt DateTime?
|
| 308 |
lastActivityAt DateTime @default(now())
|
| 309 |
+
organizationId String
|
| 310 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 311 |
track Track @relation(fields: [trackId], references: [id])
|
| 312 |
user User @relation(fields: [userId], references: [id])
|
|
|
|
| 325 |
mediaUrl String?
|
| 326 |
createdAt DateTime @default(now())
|
| 327 |
aiSource String?
|
| 328 |
+
organizationId String
|
| 329 |
enrollment Enrollment @relation(fields: [enrollmentId], references: [id], onDelete: Cascade)
|
| 330 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 331 |
user User @relation(fields: [userId], references: [id])
|
|
|
|
| 341 |
channel String @default("WHATSAPP")
|
| 342 |
content String?
|
| 343 |
mediaUrl String?
|
| 344 |
+
mediaType String?
|
| 345 |
+
mediaId String?
|
| 346 |
payload Json?
|
| 347 |
createdAt DateTime @default(now())
|
| 348 |
status MessageStatus @default(SENT)
|
| 349 |
+
organizationId String
|
| 350 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 351 |
user User? @relation(fields: [userId], references: [id])
|
| 352 |
contact Contact? @relation(fields: [contactId], references: [id])
|
|
|
|
| 367 |
stripeSessionId String? @unique
|
| 368 |
createdAt DateTime @default(now())
|
| 369 |
updatedAt DateTime @updatedAt
|
| 370 |
+
organizationId String
|
| 371 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 372 |
track Track @relation(fields: [trackId], references: [id])
|
| 373 |
user User @relation(fields: [userId], references: [id])
|
|
|
|
| 403 |
userProgressId String
|
| 404 |
name String
|
| 405 |
earnedAt DateTime @default(now())
|
| 406 |
+
organizationId String
|
| 407 |
organization Organization @relation(fields: [organizationId], references: [id])
|
| 408 |
userProgress UserProgress @relation(fields: [userProgressId], references: [id], onDelete: Cascade)
|
| 409 |
|
packages/database/seed.ts
CHANGED
|
@@ -10,10 +10,10 @@ const prisma = new PrismaClient();
|
|
| 10 |
async function main() {
|
| 11 |
console.log('π± Seeding database...');
|
| 12 |
|
| 13 |
-
|
| 14 |
const track = await prisma.track.upsert({
|
| 15 |
where: { id: 'seed-module-1-ecomoto' },
|
| 16 |
-
update: {},
|
| 17 |
create: {
|
| 18 |
id: 'seed-module-1-ecomoto',
|
| 19 |
title: 'Lancer son Business β Module 1',
|
|
@@ -21,6 +21,7 @@ async function main() {
|
|
| 21 |
duration: 3,
|
| 22 |
language: 'FR',
|
| 23 |
isPremium: false,
|
|
|
|
| 24 |
}
|
| 25 |
});
|
| 26 |
|
|
@@ -96,10 +97,10 @@ EXEMPLE :
|
|
| 96 |
where: { trackId: track.id, dayNumber: day.dayNumber }
|
| 97 |
});
|
| 98 |
if (existing) {
|
| 99 |
-
await prisma.trackDay.update({ where: { id: existing.id }, data: day });
|
| 100 |
console.log(`β
Updated Day ${day.dayNumber}: ${day.title}`);
|
| 101 |
} else {
|
| 102 |
-
await prisma.trackDay.create({ data: { ...day, trackId: track.id } });
|
| 103 |
console.log(`β
Created Day ${day.dayNumber}: ${day.title}`);
|
| 104 |
}
|
| 105 |
}
|
|
|
|
| 10 |
async function main() {
|
| 11 |
console.log('π± Seeding database...');
|
| 12 |
|
| 13 |
+
const DEFAULT_ORG_ID = 'default-org-id';
|
| 14 |
const track = await prisma.track.upsert({
|
| 15 |
where: { id: 'seed-module-1-ecomoto' },
|
| 16 |
+
update: { organizationId: DEFAULT_ORG_ID },
|
| 17 |
create: {
|
| 18 |
id: 'seed-module-1-ecomoto',
|
| 19 |
title: 'Lancer son Business β Module 1',
|
|
|
|
| 21 |
duration: 3,
|
| 22 |
language: 'FR',
|
| 23 |
isPremium: false,
|
| 24 |
+
organizationId: DEFAULT_ORG_ID
|
| 25 |
}
|
| 26 |
});
|
| 27 |
|
|
|
|
| 97 |
where: { trackId: track.id, dayNumber: day.dayNumber }
|
| 98 |
});
|
| 99 |
if (existing) {
|
| 100 |
+
await prisma.trackDay.update({ where: { id: existing.id }, data: { ...day, organizationId: DEFAULT_ORG_ID } });
|
| 101 |
console.log(`β
Updated Day ${day.dayNumber}: ${day.title}`);
|
| 102 |
} else {
|
| 103 |
+
await prisma.trackDay.create({ data: { ...day, trackId: track.id, organizationId: DEFAULT_ORG_ID } });
|
| 104 |
console.log(`β
Created Day ${day.dayNumber}: ${day.title}`);
|
| 105 |
}
|
| 106 |
}
|
pnpm-lock.yaml
CHANGED
|
@@ -60,6 +60,9 @@ importers:
|
|
| 60 |
recharts:
|
| 61 |
specifier: ^3.8.1
|
| 62 |
version: 3.8.1(@types/react@18.3.28)(react-dom@18.3.1(react@18.3.1))(react-is@18.3.1)(react@18.3.1)(redux@5.0.1)
|
|
|
|
|
|
|
|
|
|
| 63 |
devDependencies:
|
| 64 |
'@repo/tsconfig':
|
| 65 |
specifier: workspace:*
|
|
|
|
| 60 |
recharts:
|
| 61 |
specifier: ^3.8.1
|
| 62 |
version: 3.8.1(@types/react@18.3.28)(react-dom@18.3.1(react@18.3.1))(react-is@18.3.1)(react@18.3.1)(redux@5.0.1)
|
| 63 |
+
xlsx:
|
| 64 |
+
specifier: ^0.18.5
|
| 65 |
+
version: 0.18.5
|
| 66 |
devDependencies:
|
| 67 |
'@repo/tsconfig':
|
| 68 |
specifier: workspace:*
|