( collection: CreateCollectionLike, mapping: SpecFieldMapping, roleFrontmatter: UnknownRecord, body?: string, now?: Date )
| 36 | }; |
| 37 | |
| 38 | export async function createTaskWithCompat( |
| 39 | collection: CreateCollectionLike, |
| 40 | mapping: SpecFieldMapping, |
| 41 | roleFrontmatter: UnknownRecord, |
| 42 | body?: string, |
| 43 | now?: Date |
| 44 | ): Promise<CreateResultLike> { |
| 45 | const taskType = getTaskTypeDef(collection); |
| 46 | const denormalized = denormalizeFrontmatter(roleFrontmatter, mapping); |
| 47 | const effectiveNow = now instanceof Date && !Number.isNaN(now.getTime()) ? now : new Date(); |
| 48 | |
| 49 | applyFieldDefaults(denormalized, taskType); |
| 50 | applyTimestampDefaults(denormalized, mapping, taskType, effectiveNow); |
| 51 | applyMatchDefaults(denormalized, taskType); |
| 52 | |
| 53 | const input: CreateInputLike = { |
| 54 | type: "task", |
| 55 | frontmatter: denormalized, |
| 56 | body, |
| 57 | }; |
| 58 | |
| 59 | const firstAttempt = await collection.create(input); |
| 60 | if (!firstAttempt.error || firstAttempt.error.code !== "path_required") { |
| 61 | return firstAttempt; |
| 62 | } |
| 63 | |
| 64 | const pathResolution = derivePathFromType(taskType, denormalized, mapping, effectiveNow); |
| 65 | if (!pathResolution.path) { |
| 66 | if (pathResolution.missingKeys && pathResolution.missingKeys.length > 0) { |
| 67 | const missing = pathResolution.missingKeys.join(", "); |
| 68 | return { |
| 69 | ...firstAttempt, |
| 70 | warnings: [ |
| 71 | `Cannot resolve path_pattern "${pathResolution.template}": missing template values for ${missing}.`, |
| 72 | ], |
| 73 | }; |
| 74 | } |
| 75 | return firstAttempt; |
| 76 | } |
| 77 | |
| 78 | return await collection.create({ |
| 79 | ...input, |
| 80 | path: pathResolution.path, |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | function getTaskTypeDef(collection: CreateCollectionLike): TaskTypeDefLike | undefined { |
| 85 | const maybeCollection = collection as unknown as { typeDefs?: Map<string, TaskTypeDefLike> }; |
nothing calls this directly
no test coverage detected