Files

81 lines
2.4 KiB
TypeScript

// The handlers of the module's procedures: each resolves its ports from the request's container and
// calls one use case. No rule lives here.
import type { Container } from "@conusai-kit/core"
import { implement } from "@conusai-kit/core"
import { DATA_SOURCE } from "@conusai-kit/data"
import { filesDeps } from "@conusai-kit/files"
import {
attachmentAdd,
attachmentRemove,
noteCreate,
noteDelete,
noteGet,
noteList,
noteUpdate,
} from "../contract/index.js"
import {
addAttachment,
createNote,
deleteNote,
getNote,
listNotes,
removeAttachment,
updateNote,
} from "../core/notes.js"
import { flagsOf, type NotesDeps } from "../core/ports.js"
/** The ports a handler needs: this module's data source, the files module's, and the flags. */
export function notesDeps(container: Container): NotesDeps {
return { data: container.resolve(DATA_SOURCE), files: filesDeps(container), flags: flagsOf(container) }
}
export const noteListImpl = implement(noteList, ({ input, ctx }) =>
listNotes(notesDeps(ctx.container), ctx.scope, {
...(input.cursor === undefined ? {} : { cursor: input.cursor }),
...(input.limit === undefined ? {} : { limit: input.limit }),
}),
)
export const noteGetImpl = implement(noteGet, ({ input, ctx }) =>
getNote(notesDeps(ctx.container), ctx.scope, input.noteId),
)
export const noteCreateImpl = implement(noteCreate, ({ input, ctx }) =>
createNote(notesDeps(ctx.container), ctx.scope, { title: input.title, body: input.body }),
)
export const noteUpdateImpl = implement(noteUpdate, ({ input, ctx }) =>
updateNote(notesDeps(ctx.container), ctx.scope, {
noteId: input.noteId,
title: input.title,
body: input.body,
expectedVersion: input.expectedVersion,
}),
)
export const noteDeleteImpl = implement(noteDelete, async ({ input, ctx }) => {
await deleteNote(notesDeps(ctx.container), ctx.scope, input.noteId)
})
export const attachmentAddImpl = implement(attachmentAdd, ({ input, ctx }) =>
addAttachment(notesDeps(ctx.container), ctx.scope, { noteId: input.noteId, fileId: input.fileId }),
)
export const attachmentRemoveImpl = implement(attachmentRemove, async ({ input, ctx }) => {
await removeAttachment(notesDeps(ctx.container), ctx.scope, {
noteId: input.noteId,
attachmentId: input.attachmentId,
})
})
export const NOTE_PROCEDURES = [
noteListImpl,
noteGetImpl,
noteCreateImpl,
noteUpdateImpl,
noteDeleteImpl,
attachmentAddImpl,
attachmentRemoveImpl,
]