File size: 17,526 Bytes
cc442ef
 
 
 
 
 
 
 
 
3bf9adc
2ab1980
 
348fb8a
a966957
 
 
2ab1980
 
 
 
 
 
 
 
 
55a1607
e286845
 
2ab1980
 
f8274a4
 
2ab1980
8280d7d
4a0c3ba
 
8280d7d
 
 
 
0fd3320
 
2ab1980
8280d7d
0fd3320
2ab1980
 
 
 
 
 
 
 
 
 
 
 
30d60ea
b43e552
30d60ea
263f6e0
3992613
 
 
 
 
 
 
99f6bca
3992613
 
 
263f6e0
 
 
e01e314
0f2f80a
3992613
 
 
6248bf4
 
263f6e0
 
 
 
 
 
 
 
 
 
 
b43e552
 
 
 
 
 
 
 
2a3e2a1
b43e552
 
 
 
 
 
 
 
 
 
 
 
 
 
30d60ea
b43e552
2a3e2a1
b43e552
 
30d60ea
b43e552
 
30d60ea
b43e552
b168be2
b43e552
 
 
 
6248bf4
2ab1980
6248bf4
2ab1980
 
6248bf4
2ab1980
 
6248bf4
 
3bf9adc
 
 
2ab1980
3bf9adc
 
 
 
2ab1980
3bf9adc
 
cc442ef
d9186b1
2ab1980
d9186b1
 
 
 
 
 
 
2ab1980
 
d9186b1
 
 
99f6bca
 
65aabf4
d9186b1
 
 
 
2ab1980
d9186b1
b43e552
e289c5c
2ab1980
 
e289c5c
2ab1980
 
65aabf4
 
b43e552
 
 
 
 
 
 
 
 
 
 
 
 
 
65aabf4
3bf9adc
 
d9186b1
 
 
 
 
 
 
 
 
 
 
 
3bf9adc
 
de6a95b
30d60ea
e289c5c
2ab1980
 
e289c5c
 
de6a95b
 
 
 
 
 
 
 
61fd7f5
 
2e888dc
e289c5c
 
 
cc442ef
 
 
3bf9adc
 
 
 
 
 
 
 
 
30d60ea
3bf9adc
 
2ab1980
3bf9adc
 
e289c5c
 
cc442ef
 
 
5746532
 
97f541f
5746532
 
f0f0df4
19d10a4
 
5746532
 
 
 
 
ca816a7
d9186b1
5746532
 
 
30d60ea
e289c5c
2ab1980
e289c5c
 
cfbb685
5746532
cc442ef
5746532
d9186b1
 
 
 
 
 
de6a95b
d9186b1
 
41fe9a9
 
 
d9186b1
 
 
 
30d60ea
2ab1980
3bf9adc
d9186b1
 
5746532
 
e289c5c
d74c1b3
cc442ef
 
 
d9186b1
 
 
 
97f541f
d9186b1
 
 
99f6bca
 
3bf9adc
d9186b1
 
 
e289c5c
97f541f
e289c5c
b168be2
cc442ef
 
 
3bf9adc
 
 
 
 
 
 
 
30d60ea
3bf9adc
 
 
e289c5c
 
9fe2cc7
 
cc442ef
 
 
3bf9adc
e01e314
 
3bf9adc
 
 
 
c5fde49
30d60ea
3bf9adc
 
4339e77
30d60ea
3bf9adc
e01e314
 
538f814
 
e01e314
b168be2
e289c5c
cc442ef
 
d9186b1
 
 
 
 
 
 
7b0c22b
d9186b1
 
30d60ea
3bf9adc
d9186b1
 
e289c5c
 
d9186b1
 
 
 
 
 
 
 
 
 
 
 
 
 
cfbb685
 
 
 
 
 
 
 
 
 
 
2ab1980
 
 
 
 
30d60ea
2ab1980
 
 
 
 
 
cc442ef
 
 
2ab1980
 
 
cc442ef
 
6248bf4
 
 
 
3992613
 
 
6248bf4
 
55a1607
 
 
 
 
cc442ef
 
 
 
 
 
 
 
0f2f80a
 
 
cc442ef
 
 
 
 
 
 
 
 
 
4339e77
 
 
 
 
 
 
cc442ef
 
 
 
 
b5f6b08
 
 
 
 
 
5746532
c5fde49
 
 
 
 
 
 
 
 
 
 
 
 
5746532
 
 
 
 
 
 
 
4967196
41fe9a9
5746532
d9186b1
83c2a9a
 
 
 
 
 
 
cfbb685
8280d7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fd3320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfbb685
 
 
0fd3320
 
 
cfbb685
 
 
 
 
d74c1b3
cfbb685
2a3e2a1
 
 
 
 
 
 
 
 
 
 
 
 
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Organization {
  id                 String                @id @default(uuid())
  name               String
  wabaId                   String?
  metaBusinessId           String?
  systemUserToken          String?
  systemUserTokenIssuedAt  DateTime?
  createdAt          DateTime              @default(now())
  updatedAt          DateTime              @updatedAt
  slug               String?               @unique // For subdomains like polytech.xamle.studio
  brandingData       Json?
  customPrompt       String?
  personalityConfig  Json?
  flowConfig         Json?
  knowledgeBaseUrl   String?
  mode               OrganizationMode      @default(EDTECH)
  useCase            OrganizationUseCase   @default(EDUCATION)
  isCrmActive        Boolean               @default(false)
  isEdTechActive     Boolean               @default(false)
  webhookSecret      String?
  webhookUrl         String?
  openAiApiKey       String?
  googleAiApiKey     String?
  subscriptionStatus String?               @default("ACTIVE")
  subscriptionPlan   SubscriptionPlan      @default(STARTER)
  stripeCustomerId   String?               @unique
  stripeSubscriptionId String?             @unique
  aiCreditsLimit     Int                   @default(500)
  aiCreditsUsed      Int                   @default(0)
  whatsappMessagesSent Int                 @default(0)
  billingPeriodStart DateTime              @default(now())
  walletBalance      Int                   @default(0)
  isHardStopped      Boolean               @default(false)
  businessProfiles   BusinessProfile[]
  usageEvents        UsageEvent[]
  walletTransactions WalletTransaction[]
  enrollments        Enrollment[]
  kbEntries          KnowledgeBaseEntry[]
  messages           Message[]
  payments           Payment[]
  responses          Response[]
  teamMembers        TeamMember[]
  tracks             Track[]
  trackDays          TrackDay[]
  users              User[]
  userBadges         UserBadge[]
  progress           UserProgress[]
  phoneNumbers       WhatsAppPhoneNumber[]
  analytics          AnalyticsLog[]
  campaigns          CampaignHistory[]
  contacts           Contact[]
  broadcastLists     BroadcastList[]
}

model Contact {
  id             String       @id @default(uuid())
  phoneNumber    String
  name           String?
  attributes     Json?        // For dynamic Excel columns
  tags           String[]     @default([])
  organizationId String
  createdAt      DateTime     @default(now())
  updatedAt      DateTime     @updatedAt
  organization      Organization @relation(fields: [organizationId], references: [id])
  campaigns         CampaignHistory[]
  broadcastLists    BroadcastList[] @relation("ContactBroadcastLists")
  messages          Message[]
  language          Language     @default(FR)

  @@unique([phoneNumber, organizationId])
  @@index([organizationId])
}

model BroadcastList {
  id             String       @id @default(uuid())
  name           String
  organizationId String
  organization   Organization @relation(fields: [organizationId], references: [id])
  contacts       Contact[]    @relation("ContactBroadcastLists")
  createdAt      DateTime     @default(now())

  @@index([organizationId])
}

// ... existing models ...

model CampaignHistory {
  id                String         @id @default(uuid())
  organizationId    String
  contactId         String
  whatsappMessageId String?        @unique // ID returned by Meta
  content           String
  status            CampaignStatus @default(SENT)
  error             String?
  sentAt            DateTime       @default(now())
  updatedAt         DateTime       @updatedAt
  organization      Organization   @relation(fields: [organizationId], references: [id])
  contact           Contact        @relation(fields: [contactId], references: [id], onDelete: Cascade)
  analytics         AnalyticsLog[]

  @@index([organizationId])
  @@index([contactId])
  @@index([whatsappMessageId])
}

model AnalyticsLog {
  id                String          @id @default(uuid())
  organizationId    String
  campaignHistoryId String
  eventType         AnalyticsEventType
  metadata          Json?
  occurredAt        DateTime        @default(now())
  organization      Organization    @relation(fields: [organizationId], references: [id])
  campaign          CampaignHistory @relation(fields: [campaignHistoryId], references: [id], onDelete: Cascade)

  @@index([organizationId])
  @@index([campaignHistoryId])
  @@index([eventType])
}

// ... existing enums ...

model KnowledgeBaseEntry {
  id             String                 @id @default(uuid())
  organizationId String
  content        String
  embedding      Unsupported("vector")?
  metadata       Json?
  createdAt      DateTime               @default(now())
  organization   Organization           @relation(fields: [organizationId], references: [id])

  @@index([organizationId])
}

model WhatsAppPhoneNumber {
  id             String       @id
  displayPhone   String
  organizationId String
  createdAt      DateTime     @default(now())
  updatedAt      DateTime     @updatedAt
  organization   Organization @relation(fields: [organizationId], references: [id])
}

model User {
  id              String           @id @default(uuid())
  phone           String?          // Made optional for Email-based admins
  name            String?
  role            Role             @default(STUDENT)
  language        Language         @default(FR)
  city            String?
  activity        String?
  createdAt       DateTime         @default(now())
  updatedAt       DateTime         @updatedAt
  email           String?          
  passwordHash    String?
  currentStreak   Int              @default(0)
  longestStreak   Int              @default(0)
  lastActivityAt  DateTime?
  deletedAt       DateTime?
  organizationId  String
  businessProfile BusinessProfile?
  enrollments     Enrollment[]
  messages        Message[]
  payments        Payment[]
  responses       Response[]
  organization    Organization     @relation(fields: [organizationId], references: [id])
  progress        UserProgress[]
  pushSubscriptions PushSubscription[]

  @@unique([phone, organizationId])
  @@unique([email, organizationId])
  @@index([organizationId])
  @@index([phone])
  @@index([email])
}

model PushSubscription {
  id             String   @id @default(uuid())
  userId         String
  organizationId String
  endpoint       String   @unique
  p256dh         String
  auth           String
  createdAt      DateTime @default(now())
  user           User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@index([organizationId])
  @@index([userId])
}

model BusinessProfile {
  id                   String       @id @default(uuid())
  userId               String       @unique
  activityLabel        String?
  activityPhrase       String?
  activityType         String?
  locationCity         String?
  mainCustomer         String?
  mainProblem          String?
  offerSimple          String?
  promise              String?
  marketData           Json?
  competitorList       Json?
  financialProjections Json?
  fundingAsk           String?
  lastUpdatedFromDay   Int          @default(0)
  createdAt            DateTime     @default(now())
  updatedAt            DateTime     @updatedAt
  organizationId       String       
  organization         Organization @relation(fields: [organizationId], references: [id])
  user                 User         @relation(fields: [userId], references: [id])
  teamMembersList      TeamMember[]

  @@index([organizationId])
}

model TeamMember {
  id                String          @id @default(uuid())
  businessProfileId String
  name              String?
  role              String?
  bio               String?
  photoUrl          String?
  organizationId    String
  businessProfile   BusinessProfile @relation(fields: [businessProfileId], references: [id], onDelete: Cascade)
  organization      Organization    @relation(fields: [organizationId], references: [id])

  @@index([organizationId])
}

model Track {
  id             String         @id @default(uuid())
  title          String
  description    String?
  duration       Int
  language       Language       @default(FR)
  isPremium      Boolean        @default(false)
  priceAmount    Int?
  createdAt      DateTime       @default(now())
  updatedAt      DateTime       @updatedAt
  organizationId String         
  enrollments    Enrollment[]
  payments       Payment[]
  organization   Organization   @relation(fields: [organizationId], references: [id])
  days           TrackDay[]
  progress       UserProgress[]

  @@index([organizationId])
}

model TrackDay {
  id                String       @id @default(uuid())
  trackId           String
  dayNumber         Int
  title             String?
  audioUrl          String?
  imageUrl          String?
  videoUrl          String?
  videoCaption      String?
  lessonText        String?
  exerciseType      ExerciseType @default(TEXT)
  exercisePrompt    String?
  validationKeyword String?
  buttonsJson       Json?
  exerciseCriteria  Json?
  badges            Json?
  unlockCondition   String?
  createdAt         DateTime     @default(now())
  updatedAt         DateTime     @updatedAt
  organizationId    String       
  organization      Organization @relation(fields: [organizationId], references: [id])
  track             Track        @relation(fields: [trackId], references: [id])

  @@index([organizationId])
  @@index([trackId, organizationId])
}

model UserProgress {
  id                 String         @id @default(uuid())
  userId             String
  trackId            String
  score              Int            @default(0)
  lastInteraction    DateTime       @default(now())
  exerciseStatus     ExerciseStatus @default(PENDING)
  badges             Json?          @map("badges")
  behavioralScoring  Json?
  confidenceScore    Float?
  adminTranscription String?
  overrideAudioUrl   String?
  reviewedBy         String?
  createdAt          DateTime       @default(now())
  updatedAt          DateTime       @updatedAt
  iterationCount     Int            @default(0)
  aiSource           String?
  organizationId     String         
  userBadges         UserBadge[]
  organization       Organization   @relation(fields: [organizationId], references: [id])
  track              Track          @relation(fields: [trackId], references: [id])
  user               User           @relation(fields: [userId], references: [id])

  @@unique([userId, trackId])
  @@index([organizationId])
  @@index([organizationId, exerciseStatus])
}

model Enrollment {
  id             String           @id @default(uuid())
  userId         String
  trackId        String
  status         EnrollmentStatus @default(ACTIVE)
  currentDay     Int              @default(1)
  startedAt      DateTime         @default(now())
  completedAt    DateTime?
  lastActivityAt DateTime         @default(now())
  deletedAt      DateTime?
  organizationId String
  organization   Organization     @relation(fields: [organizationId], references: [id])
  track          Track            @relation(fields: [trackId], references: [id])
  user           User             @relation(fields: [userId], references: [id])
  responses      Response[]

  @@unique([userId, trackId])
  @@index([organizationId])
  @@index([userId])
}

model Response {
  id             String       @id @default(uuid())
  enrollmentId   String
  userId         String
  dayNumber      Int
  content        String?
  mediaUrl       String?
  createdAt      DateTime     @default(now())
  aiSource       String?
  organizationId String       
  enrollment     Enrollment   @relation(fields: [enrollmentId], references: [id], onDelete: Cascade)
  organization   Organization @relation(fields: [organizationId], references: [id])
  user           User         @relation(fields: [userId], references: [id])

  @@index([organizationId])
  @@index([userId])
  @@index([enrollmentId])
}

model Message {
  id             String       @id @default(uuid())
  userId         String?
  contactId      String?
  direction      Direction
  channel        String       @default("WHATSAPP")
  content        String?
  mediaUrl       String?
  mediaType      MediaType?
  mediaId        String?
  payload        Json?
  createdAt      DateTime     @default(now())
  status         MessageStatus @default(SENT)
  organizationId String       
  organization   Organization @relation(fields: [organizationId], references: [id])
  user           User?        @relation(fields: [userId], references: [id])
  contact        Contact?     @relation(fields: [contactId], references: [id])

  @@index([userId, createdAt])
  @@index([contactId, createdAt])
  @@index([organizationId, createdAt])
  @@index([organizationId])
}

model Payment {
  id              String        @id @default(uuid())
  userId          String
  trackId         String
  amount          Int
  currency        String        @default("XOF")
  status          PaymentStatus @default(PENDING)
  paymentSessionId String?      @unique
  createdAt       DateTime      @default(now())
  updatedAt       DateTime      @updatedAt
  organizationId  String        
  organization    Organization  @relation(fields: [organizationId], references: [id])
  track           Track         @relation(fields: [trackId], references: [id])
  user            User          @relation(fields: [userId], references: [id])

  @@index([organizationId])
}

model TrainingData {
  id               String         @id @default(uuid())
  audioUrl         String
  transcription    String
  manualCorrection String?
  rawWER           Float?
  normalizedWER    Float?
  status           TrainingStatus @default(PENDING)
  createdAt        DateTime       @default(now())
  updatedAt        DateTime       @updatedAt
}

model NormalizationRule {
  id          String   @id @default(uuid())
  original    String   @unique
  replacement String
  language    String   @default("WOLOF")
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt

  @@index([original])
}

model UserBadge {
  id             String       @id @default(uuid())
  userProgressId String
  name           String
  earnedAt       DateTime     @default(now())
  organizationId String       
  organization   Organization @relation(fields: [organizationId], references: [id])
  userProgress   UserProgress @relation(fields: [userProgressId], references: [id], onDelete: Cascade)

  @@index([organizationId])
}

enum Role {
  STUDENT
  ADMIN
  ORG_MEMBER
  ORG_ADMIN
  SUPER_ADMIN
}

enum OrganizationMode {
  EDTECH
  WEBHOOK
  AI_AGENT
  CRM_MARKETING
  PEDAGOGY
  CUSTOMER_SERVICE
}

enum OrganizationUseCase {
  EDUCATION
  CRM_WHATSAPP
}

enum EnrollmentStatus {
  ACTIVE
  COMPLETED
  DROPPED
}

enum Language {
  FR
  EN
  ES
  PT
  WOLOF
}

enum ContentType {
  TEXT
  AUDIO
  IMAGE
  VIDEO
}

enum MessageStatus {
  SENT
  DELIVERED
  READ
  RECEIVED
}

enum Direction {
  INBOUND
  OUTBOUND
}

enum PaymentStatus {
  PENDING
  COMPLETED
  FAILED
  REFUNDED
}

enum ResponseType {
  TEXT
  AUDIO
  VIDEO
}

enum MediaType {
  AUDIO
  IMAGE
  VIDEO
  DOCUMENT
}

enum ExerciseType {
  TEXT
  AUDIO
  BUTTON
}

enum ExerciseStatus {
  PENDING
  PENDING_REMEDIATION
  PENDING_REVIEW
  COMPLETED
  PENDING_DEEPDIVE
}

enum TrainingStatus {
  PENDING
  REVIEWED
  IGNORED
}

enum SubscriptionPlan {
  STARTER
  GROWTH
  SCALE
  ENTERPRISE
}

enum UsageEventType {
  AI_TEXT
  AI_AUDIO
  AI_IMAGE
  WHATSAPP_SENT
}

enum UsageProvider {
  GEMINI
  OPENAI
  META
}

enum UsageFeature {
  LESSON
  FEEDBACK
  DEEPDIVE
  TRANSCRIPTION
  IMAGE_ANALYSIS
  SPEECH
  CAMPAIGN
  KB_QUERY
  ONBOARDING
  OTHER
}

model UsageEvent {
  id             String         @id @default(uuid())
  organizationId String
  userId         String?
  type           UsageEventType
  provider       UsageProvider
  feature        UsageFeature
  tokensIn       Int            @default(0)
  tokensOut      Int            @default(0)
  durationMs     Int?
  costUsd        Float          @default(0)
  createdAt      DateTime       @default(now())
  organization   Organization   @relation(fields: [organizationId], references: [id])

  @@index([organizationId, createdAt])
  @@index([organizationId, type])
}

model WalletTransaction {
  id             String                @id @default(uuid())
  organizationId String
  amount         Int
  balanceAfter   Int
  type           WalletTransactionType
  description    String?
  actorId        String?
  byok           Boolean               @default(false)
  metadata       Json?
  createdAt      DateTime              @default(now())
  organization   Organization          @relation(fields: [organizationId], references: [id])

  @@index([organizationId, createdAt])
  @@index([type])
  @@index([organizationId, type])
}

enum WalletTransactionType {
  TOP_UP_MANUAL
  TOP_UP_PAYMENT
  DEBIT_AI
  DEBIT_WHATSAPP
  DEBIT_BROADCAST
  ADJUSTMENT
}

model AuditLog {
  id         String   @id @default(uuid())
  action     String
  actorId    String?
  resourceId String?
  details    Json?
  createdAt  DateTime @default(now())

  @@index([action])
  @@index([actorId])
  @@index([resourceId])
  @@index([createdAt])
}

enum CampaignStatus {
  SENT
  DELIVERED
  READ
  FAILED
}

enum AnalyticsEventType {
  CLICK
  READ
  RESPONSE
}