CognxSafeTrack commited on
Commit ·
1403ab3
1
Parent(s): 9dfe8d2
fix(comp5): add 429 catch on generateFeedback, use proper imports for whatsappQueue/scheduleTrackDay + Wolof translations
Browse files
apps/api/src/services/whatsapp.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import { prisma } from './prisma';
|
| 2 |
-
import { scheduleMessage, enrollUser } from './queue';
|
|
|
|
| 3 |
|
| 4 |
export class WhatsAppService {
|
| 5 |
static async handleIncomingMessage(phone: string, text: string) {
|
|
@@ -102,15 +103,37 @@ export class WhatsAppService {
|
|
| 102 |
});
|
| 103 |
|
| 104 |
if (trackDay) {
|
| 105 |
-
await scheduleMessage(user.id,
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
const feedback = await aiService.generateFeedback(
|
| 109 |
-
text,
|
| 110 |
-
trackDay.exercisePrompt || '',
|
| 111 |
-
trackDay.lessonText || ''
|
| 112 |
);
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
await scheduleMessage(user.id, feedback);
|
| 115 |
|
| 116 |
await prisma.userProgress.update({
|
|
@@ -126,19 +149,19 @@ export class WhatsAppService {
|
|
| 126 |
data: { lastActivityAt: new Date() }
|
| 127 |
});
|
| 128 |
|
| 129 |
-
//
|
| 130 |
const totalDays = pendingProgress.track.duration;
|
| 131 |
if (pendingProgress.currentDay >= totalDays) {
|
| 132 |
-
await scheduleMessage(user.id,
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
trackId: pendingProgress.trackId,
|
| 138 |
-
dayNumber: pendingProgress.currentDay + 1
|
| 139 |
-
});
|
| 140 |
} else {
|
| 141 |
-
await scheduleMessage(user.id,
|
|
|
|
|
|
|
|
|
|
| 142 |
}
|
| 143 |
}
|
| 144 |
return;
|
|
|
|
| 1 |
import { prisma } from './prisma';
|
| 2 |
+
import { scheduleMessage, enrollUser, whatsappQueue, scheduleTrackDay } from './queue';
|
| 3 |
+
import { QuotaExceededError } from './ai/openai-provider';
|
| 4 |
|
| 5 |
export class WhatsAppService {
|
| 6 |
static async handleIncomingMessage(phone: string, text: string) {
|
|
|
|
| 103 |
});
|
| 104 |
|
| 105 |
if (trackDay) {
|
| 106 |
+
await scheduleMessage(user.id, user.language === 'WOLOF'
|
| 107 |
+
? "⏳ Defar ak sa tontu..."
|
| 108 |
+
: "⏳ Analyse de votre réponse..."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
);
|
| 110 |
|
| 111 |
+
let feedback: string;
|
| 112 |
+
try {
|
| 113 |
+
const { aiService } = await import('./ai');
|
| 114 |
+
feedback = await aiService.generateFeedback(
|
| 115 |
+
text,
|
| 116 |
+
trackDay.exercisePrompt || '',
|
| 117 |
+
trackDay.lessonText || ''
|
| 118 |
+
);
|
| 119 |
+
} catch (err: any) {
|
| 120 |
+
if (err instanceof QuotaExceededError) {
|
| 121 |
+
// OpenAI 429 — send fallback and requeue the same exercise response
|
| 122 |
+
console.warn('[FEEDBACK] OpenAI quota exceeded — fallback + requeue 120s');
|
| 123 |
+
await scheduleMessage(user.id, user.language === 'WOLOF'
|
| 124 |
+
? "Bañ ma jëfandikoo léegi. Dinanu la tontu ci 2 minute."
|
| 125 |
+
: "Je reviens vers toi dans 2 minutes pour analyser ta réponse."
|
| 126 |
+
);
|
| 127 |
+
// Requeue the same incoming message after delay
|
| 128 |
+
await whatsappQueue.add('send-message', {
|
| 129 |
+
userId: user.id,
|
| 130 |
+
text: '↩️ Analyse retardée — réenvoie ta réponse dans 2 min si tu ne reçois rien.'
|
| 131 |
+
}, { delay: 125_000 });
|
| 132 |
+
return;
|
| 133 |
+
}
|
| 134 |
+
throw err;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
await scheduleMessage(user.id, feedback);
|
| 138 |
|
| 139 |
await prisma.userProgress.update({
|
|
|
|
| 149 |
data: { lastActivityAt: new Date() }
|
| 150 |
});
|
| 151 |
|
| 152 |
+
// Advance to next day or mark complete
|
| 153 |
const totalDays = pendingProgress.track.duration;
|
| 154 |
if (pendingProgress.currentDay >= totalDays) {
|
| 155 |
+
await scheduleMessage(user.id, user.language === 'WOLOF'
|
| 156 |
+
? "🎉 Baraka Allahu fik ! Jeex nga module bi. Dokumaan yi dinañu leen yónnee ci kanam !"
|
| 157 |
+
: "🎉 Félicitations ! Vous avez terminé ce module. Vos documents intelligents arrivent bientôt !"
|
| 158 |
+
);
|
| 159 |
+
await scheduleTrackDay(user.id, pendingProgress.trackId, pendingProgress.currentDay + 1);
|
|
|
|
|
|
|
|
|
|
| 160 |
} else {
|
| 161 |
+
await scheduleMessage(user.id, user.language === 'WOLOF'
|
| 162 |
+
? "Baax na ! Yónnee *SUITE* ngir dem ci kanam."
|
| 163 |
+
: "Bravo ! Envoyez *SUITE* pour passer à la leçon suivante."
|
| 164 |
+
);
|
| 165 |
}
|
| 166 |
}
|
| 167 |
return;
|