bichnhan2701 commited on
Commit
5a24a39
·
1 Parent(s): 87908ee

Fix bug duplicate create note again

Browse files
Files changed (1) hide show
  1. app/api/notes/notes_audio.py +32 -13
app/api/notes/notes_audio.py CHANGED
@@ -1,14 +1,14 @@
 
1
  from fastapi import APIRouter, BackgroundTasks, HTTPException
2
  from pydantic import BaseModel
3
  from typing import Dict, List, Optional
4
  from app.services.note_store import create_note
5
- from app.jobs.async_enrichment_job import enrich_note
6
  from app.models.enums import NoteType, NoteStatus
7
  from app.utils.time import now_ts
8
 
9
 
10
  router = APIRouter(prefix="/internal/notes", tags=["internal"])
11
-
12
 
13
  class CreateAudioNoteRequest(BaseModel):
14
  note_id: str
@@ -18,39 +18,58 @@ class CreateAudioNoteRequest(BaseModel):
18
 
19
 
20
  @router.post("/audio")
21
- async def create_audio_note(req: CreateAudioNoteRequest, bg: BackgroundTasks):
 
 
 
 
 
22
  now = now_ts()
23
- has_enrichment = bool(req.generate)
24
 
25
  allowed = {"normalize", "keywords", "summary", "mindmap"}
26
  if not set(req.generate).issubset(allowed):
27
- raise HTTPException(400, "Invalid generate task, generate list must not be empty")
28
-
 
 
 
 
 
 
 
 
 
29
  note = {
30
  "note_id": req.note_id,
31
  "type": NoteType.audio,
32
  "raw_text": req.raw_text,
33
  "metadata": req.metadata,
34
- "status": NoteStatus.processing if has_enrichment else NoteStatus.created,
 
35
  "created_at": now,
36
  "updated_at": now,
37
  }
38
 
39
- if has_enrichment:
 
40
  note.update({
41
  "normalized_text": None,
42
  "title": None,
43
- "keywords": None,
44
  "summary": None,
45
  "mindmap": None,
46
  })
47
 
48
- create_note(note)
 
 
 
 
49
 
50
- if has_enrichment:
51
- bg.add_task(enrich_note, req.note_id, req.generate)
52
 
53
  return {
54
  "note_id": req.note_id,
55
  "status": note["status"],
56
- }
 
1
+ import logging
2
  from fastapi import APIRouter, BackgroundTasks, HTTPException
3
  from pydantic import BaseModel
4
  from typing import Dict, List, Optional
5
  from app.services.note_store import create_note
 
6
  from app.models.enums import NoteType, NoteStatus
7
  from app.utils.time import now_ts
8
 
9
 
10
  router = APIRouter(prefix="/internal/notes", tags=["internal"])
11
+ logger = logging.getLogger(__name__)
12
 
13
  class CreateAudioNoteRequest(BaseModel):
14
  note_id: str
 
18
 
19
 
20
  @router.post("/audio")
21
+ async def create_audio_note(req: CreateAudioNoteRequest, bg: BackgroundTasks):
22
+ """
23
+ Idempotent create:
24
+ - Nếu note đã tồn tại → return luôn
25
+ - KHÔNG throw 500 vì duplicate
26
+ """
27
  now = now_ts()
 
28
 
29
  allowed = {"normalize", "keywords", "summary", "mindmap"}
30
  if not set(req.generate).issubset(allowed):
31
+ raise HTTPException(400, "Invalid generate task")
32
+
33
+ # 🔒 IDEMPOTENT CHECK
34
+ existing = get_note(req.note_id)
35
+ if existing:
36
+ logger.info("Note already exists, skip create note_id=%s", req.note_id)
37
+ return {
38
+ "note_id": req.note_id,
39
+ "status": existing.get("status"),
40
+ }
41
+
42
  note = {
43
  "note_id": req.note_id,
44
  "type": NoteType.audio,
45
  "raw_text": req.raw_text,
46
  "metadata": req.metadata,
47
+ # 🔥 PLACEHOLDER CREATED
48
+ "status": NoteStatus.created,
49
  "created_at": now,
50
  "updated_at": now,
51
  }
52
 
53
+ # Chuẩn bị field cho enrich (nhưng CHƯA chạy)
54
+ if req.generate:
55
  note.update({
56
  "normalized_text": None,
57
  "title": None,
58
+ "keywords": [],
59
  "summary": None,
60
  "mindmap": None,
61
  })
62
 
63
+ try:
64
+ create_note(note)
65
+ except ValueError:
66
+ # double-safe (race condition)
67
+ logger.warning("Race create_note, ignore note_id=%s", req.note_id)
68
 
69
+ # ❌ KHÔNG chạy enrich ở đây
70
+ # enrich CHỈ được trigger sau ASR xong (transcribed)
71
 
72
  return {
73
  "note_id": req.note_id,
74
  "status": note["status"],
75
+ }