{"record":{"id":"c18195b9d16d48e6","repo":"yikart/AiToEarn","slug":"draftgenerationmemorynotfound","errorCode":"DraftGenerationMemoryNotFound","errorMessage":"ResponseCode.DraftGenerationMemoryNotFound","messagePattern":"ResponseCode\\.DraftGenerationMemoryNotFound","errorType":"error_code","errorClass":"AppException","httpStatus":null,"severity":"error","filePath":"project/aitoearn-backend/apps/aitoearn-ai/src/core/draft-generation/draft-generation-memory.service.ts","lineNumber":53,"sourceCode":"  async listMemories(userId: string, contentType?: DraftGenerationMemoryContentType) {\n    return await this.draftGenerationMemoryRepository.listByUserId(userId, contentType)\n  }\n\n  async getPlannerMemoryContext(userId: string, contentType: DraftGenerationMemoryContentType): Promise<PlannerMemoryContext> {\n    const memory = await this.draftGenerationMemoryRepository.getByUserIdAndContentType(userId, contentType)\n    if (!memory) {\n      return { memoryItems: [] }\n    }\n\n    return {\n      memoryItems: (memory.items ?? []).map(item => item.text),\n    }\n  }\n\n  async deleteItem(userId: string, itemId: string) {\n    const memory = await this.draftGenerationMemoryRepository.deleteItemByUserIdAndItemId(userId, itemId)\n    if (!memory) {\n      throw new AppException(ResponseCode.DraftGenerationMemoryNotFound)\n    }\n    return memory\n  }\n\n  async regenerateMemory(userId: string, contentType: DraftGenerationMemoryContentType, plannerModel?: string) {\n    const { aiLogs, materials } = await this.listMemorySamples(userId, contentType)\n    const sampleCount = aiLogs.length + materials.length\n    const now = new Date()\n\n    if (sampleCount === 0) {\n      const emptyMemory = await this.draftGenerationMemoryRepository.updateByUserIdAndContentType(userId, contentType, {\n        $set: { userId, contentType, items: [], sampleCount: 0, lastGeneratedAt: now },\n      })\n      if (!emptyMemory) {\n        throw new AppException(ResponseCode.DraftGenerationMemoryNotFound)\n      }\n      return emptyMemory\n    }","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/yikart/AiToEarn/blob/d3aa8bea5b146a8675607cf0144d891aad3e9683/project/aitoearn-backend/apps/aitoearn-ai/src/core/draft-generation/draft-generation-memory.service.ts#L35-L71","documentation":"deleteItem() calls the repository to delete a draft-generation memory item by (userId, itemId) and throws AppException(ResponseCode.DraftGenerationMemoryNotFound) when no document was deleted — i.e. the item does not exist for that user.","triggerScenarios":"Calling deleteItem with an itemId that was already deleted, never existed, or belongs to a different userId; stale item id in the client after another device removed it.","commonSituations":"Double-click delete issuing two DELETE calls; client caches an old memory list; id copied from another user's session or environment.","solutions":["Fetch the current memory list first and delete only existing item ids","Handle DraftGenerationMemoryNotFound idempotently on the client (treat second delete as success)","Verify the itemId and userId pairing — ids are scoped per user"],"exampleFix":"// before\nawait memoryApi.deleteItem(itemId) // throws if already gone\n// after\ntry { await memoryApi.deleteItem(itemId) } catch (e) { if (e.code !== 'DraftGenerationMemoryNotFound') throw e }","handlingStrategy":"try-catch","validationCode":"const memory = await memoryRepo.getByUserIdAndItemId(userId, itemId); if (!memory) return; // already deleted, skip call","typeGuard":null,"tryCatchPattern":"try { await draftGenerationMemoryService.deleteItem(userId, itemId) } catch (e) { if (e instanceof AppException && e.code === ResponseCode.DraftGenerationMemoryNotFound) return; throw e }","preventionTips":["Treat delete-as-idempotent: refresh the list after each delete and ignore NotFound on retries","Disable delete buttons for items already removed in the UI","Scope item ids per user and never reuse ids across accounts"],"tags":["not-found","crud","draft-generation","memory"],"backgroundTag":"resource-not-found","analyzedSha":"d3aa8bea5b146a8675607cf0144d891aad3e9683","analyzedAt":"2026-08-31T14:19:24.185Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}