@plandesk/api 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/events.d.ts CHANGED
@@ -15,14 +15,18 @@ export type DocumentCreatedEvent = {
15
15
  export type CommentCreatedEvent = {
16
16
  type: 'comment_created';
17
17
  commentId: string;
18
- documentId: string;
19
18
  projectId: string;
19
+ target_type: string;
20
+ target_id: string;
21
+ documentId?: string;
20
22
  };
21
23
  export type CommentUpdatedEvent = {
22
24
  type: 'comment_updated';
23
25
  commentId: string;
24
- documentId: string;
25
26
  projectId: string;
27
+ target_type: string;
28
+ target_id: string;
29
+ documentId?: string;
26
30
  };
27
31
  export type FolderCreatedEvent = {
28
32
  type: 'folder_created';
@@ -1,40 +1,48 @@
1
1
  import { Hono } from 'hono';
2
- import { InvalidCommentError } from '../services/comments.js';
2
+ import { InvalidCommentError, } from '../services/comments.js';
3
3
  function parseIncludeResolved(value) {
4
4
  return value === 'true';
5
5
  }
6
- export function createCommentsRouter(commentService) {
7
- const router = new Hono();
8
- router.post('/documents/:id/comments', async (c) => {
9
- const body = await c.req.json();
10
- if (typeof body.body !== 'string') {
11
- return c.json({ error: 'invalid_argument' }, 400);
12
- }
13
- try {
14
- const comment = commentService.create(c.req.param('id'), {
15
- body: body.body,
16
- passage: body.passage,
17
- });
18
- if (!comment) {
19
- return c.json({ error: 'not_found' }, 404);
20
- }
21
- return c.json(comment, 201);
22
- }
23
- catch (error) {
24
- if (error instanceof InvalidCommentError) {
25
- return c.json({ error: 'invalid_argument' }, 400);
26
- }
27
- throw error;
28
- }
29
- });
30
- router.get('/documents/:id/comments', (c) => {
31
- const includeResolved = parseIncludeResolved(c.req.query('include_resolved'));
32
- const comments = commentService.listByDocument(c.req.param('id'), { includeResolved });
33
- if (!comments) {
6
+ async function handleCreateComment(c, commentService, target) {
7
+ const body = await c.req.json();
8
+ if (typeof body.body !== 'string') {
9
+ return c.json({ error: 'invalid_argument' }, 400);
10
+ }
11
+ try {
12
+ const comment = commentService.create(target, {
13
+ body: body.body,
14
+ passage: body.passage,
15
+ });
16
+ if (!comment) {
34
17
  return c.json({ error: 'not_found' }, 404);
35
18
  }
36
- return c.json(comments);
37
- });
19
+ return c.json(comment, 201);
20
+ }
21
+ catch (error) {
22
+ if (error instanceof InvalidCommentError) {
23
+ return c.json({ error: 'invalid_argument' }, 400);
24
+ }
25
+ throw error;
26
+ }
27
+ }
28
+ function handleListComments(c, commentService, target) {
29
+ const includeResolved = parseIncludeResolved(c.req.query('include_resolved'));
30
+ const comments = commentService.listByTarget(target, { includeResolved });
31
+ if (!comments) {
32
+ return c.json({ error: 'not_found' }, 404);
33
+ }
34
+ return c.json(comments);
35
+ }
36
+ export function createCommentsRouter(commentService) {
37
+ const router = new Hono();
38
+ router.post('/documents/:id/comments', (c) => handleCreateComment(c, commentService, { type: 'document', id: c.req.param('id') }));
39
+ router.get('/documents/:id/comments', (c) => handleListComments(c, commentService, { type: 'document', id: c.req.param('id') }));
40
+ router.post('/tasks/:id/comments', (c) => handleCreateComment(c, commentService, { type: 'task', id: c.req.param('id') }));
41
+ router.get('/tasks/:id/comments', (c) => handleListComments(c, commentService, { type: 'task', id: c.req.param('id') }));
42
+ router.post('/notes/:id/comments', (c) => handleCreateComment(c, commentService, { type: 'note', id: c.req.param('id') }));
43
+ router.get('/notes/:id/comments', (c) => handleListComments(c, commentService, { type: 'note', id: c.req.param('id') }));
44
+ router.post('/submissions/:id/comments', (c) => handleCreateComment(c, commentService, { type: 'submission', id: c.req.param('id') }));
45
+ router.get('/submissions/:id/comments', (c) => handleListComments(c, commentService, { type: 'submission', id: c.req.param('id') }));
38
46
  router.get('/projects/:id/comments', (c) => {
39
47
  const includeResolved = parseIncludeResolved(c.req.query('include_resolved'));
40
48
  const comments = commentService.listByProject(c.req.param('id'), { includeResolved });
@@ -1,4 +1,4 @@
1
- import type { AgentRun, AgentRunEvent, Document, DocumentComment, Edge, Folder, Goal, Note, Project, Tag, Task, TaskStatus } from '@plandesk/db';
1
+ import type { AgentRun, AgentRunEvent, Document, Comment, Edge, Folder, Goal, Note, Project, Tag, Task, TaskStatus } from '@plandesk/db';
2
2
  export type PaginationParams = {
3
3
  limit?: number;
4
4
  offset?: number;
@@ -91,13 +91,15 @@ export type SerializedNote = {
91
91
  export declare function serializeNote(note: Note): SerializedNote;
92
92
  export type SerializedComment = {
93
93
  id: string;
94
- document_id: string;
94
+ target_type: Comment['targetType'];
95
+ target_id: string;
96
+ document_id: string | null;
95
97
  passage: string | null;
96
98
  body: string;
97
99
  resolved: boolean;
98
100
  created_at: string;
99
101
  };
100
- export declare function serializeComment(comment: DocumentComment): SerializedComment;
102
+ export declare function serializeComment(comment: Comment): SerializedComment;
101
103
  export declare function buildDocumentTree(documents: Document[]): SerializedDocumentTree[];
102
104
  export type SerializedFolder = {
103
105
  id: string;
package/dist/serialize.js CHANGED
@@ -123,7 +123,9 @@ export function serializeNote(note) {
123
123
  export function serializeComment(comment) {
124
124
  return {
125
125
  id: comment.id,
126
- document_id: comment.documentId,
126
+ target_type: comment.targetType,
127
+ target_id: comment.targetId,
128
+ document_id: comment.targetType === 'document' ? comment.targetId : null,
127
129
  passage: comment.passage,
128
130
  body: comment.body,
129
131
  resolved: comment.resolved,
@@ -1,10 +1,14 @@
1
- import { type Db } from '@plandesk/db';
1
+ import { type CommentTargetType, type Db } from '@plandesk/db';
2
2
  import type { EventBus } from '../events.js';
3
3
  import { type SerializedComment } from '../serialize.js';
4
4
  export type CommentServiceDeps = {
5
5
  db: Db;
6
6
  eventBus: EventBus;
7
7
  };
8
+ export type CommentTarget = {
9
+ type: CommentTargetType;
10
+ id: string;
11
+ };
8
12
  export type CreateCommentInput = {
9
13
  body: string;
10
14
  passage?: string | null;
@@ -17,7 +21,11 @@ export declare class InvalidCommentError extends Error {
17
21
  constructor(message: string);
18
22
  }
19
23
  export declare function createCommentService(deps: CommentServiceDeps): {
20
- create(documentId: string, input: CreateCommentInput): SerializedComment | undefined;
24
+ create(target: CommentTarget, input: CreateCommentInput): SerializedComment | undefined;
25
+ listByTarget(target: CommentTarget, options?: {
26
+ includeResolved?: boolean;
27
+ }): SerializedComment[] | undefined;
28
+ resolveTargetProjectId(target: CommentTarget): string | undefined;
21
29
  listByDocument(documentId: string, options?: {
22
30
  includeResolved?: boolean;
23
31
  }): SerializedComment[] | undefined;
@@ -1,4 +1,4 @@
1
- import { createDocumentComment, deleteDocumentComment as dbDeleteDocumentComment, getDocument as dbGetDocument, getDocumentComment as dbGetDocumentComment, getProject, listCommentsByDocument as dbListCommentsByDocument, listCommentsByProject as dbListCommentsByProject, updateDocumentComment as dbUpdateDocumentComment, } from '@plandesk/db';
1
+ import { createComment, deleteComment as dbDeleteComment, getComment as dbGetComment, getDocument as dbGetDocument, getNote as dbGetNote, getProject, getSubmission, getTask, listCommentsByProject as dbListCommentsByProject, listCommentsByTarget as dbListCommentsByTarget, updateComment as dbUpdateComment, } from '@plandesk/db';
2
2
  import { serializeComment } from '../serialize.js';
3
3
  export class InvalidCommentError extends Error {
4
4
  constructor(message) {
@@ -11,34 +11,69 @@ function assertNonEmptyBody(body) {
11
11
  throw new InvalidCommentError('Comment body must not be empty');
12
12
  }
13
13
  }
14
+ function targetProjectId(db, target) {
15
+ switch (target.type) {
16
+ case 'document':
17
+ return dbGetDocument(db, target.id)?.projectId;
18
+ case 'task':
19
+ return getTask(db, target.id)?.projectId;
20
+ case 'note':
21
+ return dbGetNote(db, target.id)?.projectId;
22
+ case 'submission':
23
+ return getSubmission(db, target.id)?.projectId;
24
+ }
25
+ }
26
+ function emitCommentCreated(eventBus, commentId, projectId, target) {
27
+ eventBus.emit({
28
+ type: 'comment_created',
29
+ commentId,
30
+ projectId,
31
+ target_type: target.type,
32
+ target_id: target.id,
33
+ ...(target.type === 'document' ? { documentId: target.id } : {}),
34
+ });
35
+ }
36
+ function emitCommentUpdated(eventBus, commentId, projectId, target) {
37
+ eventBus.emit({
38
+ type: 'comment_updated',
39
+ commentId,
40
+ projectId,
41
+ target_type: target.type,
42
+ target_id: target.id,
43
+ ...(target.type === 'document' ? { documentId: target.id } : {}),
44
+ });
45
+ }
14
46
  export function createCommentService(deps) {
15
47
  const { db, eventBus } = deps;
16
48
  return {
17
- create(documentId, input) {
18
- const document = dbGetDocument(db, documentId);
19
- if (!document) {
49
+ create(target, input) {
50
+ const projectId = targetProjectId(db, target);
51
+ if (!projectId) {
20
52
  return undefined;
21
53
  }
22
54
  assertNonEmptyBody(input.body);
23
- const comment = createDocumentComment(db, {
24
- documentId,
55
+ const comment = createComment(db, {
56
+ projectId,
57
+ targetType: target.type,
58
+ targetId: target.id,
25
59
  body: input.body,
26
60
  passage: input.passage,
27
61
  });
28
- eventBus.emit({
29
- type: 'comment_created',
30
- commentId: comment.id,
31
- documentId,
32
- projectId: document.projectId,
33
- });
62
+ emitCommentCreated(eventBus, comment.id, projectId, target);
34
63
  return serializeComment(comment);
35
64
  },
36
- listByDocument(documentId, options) {
37
- const document = dbGetDocument(db, documentId);
38
- if (!document) {
65
+ listByTarget(target, options) {
66
+ const projectId = targetProjectId(db, target);
67
+ if (!projectId) {
39
68
  return undefined;
40
69
  }
41
- return dbListCommentsByDocument(db, documentId, options).map(serializeComment);
70
+ return dbListCommentsByTarget(db, target.type, target.id, options).map(serializeComment);
71
+ },
72
+ resolveTargetProjectId(target) {
73
+ return targetProjectId(db, target);
74
+ },
75
+ listByDocument(documentId, options) {
76
+ return this.listByTarget({ type: 'document', id: documentId }, options);
42
77
  },
43
78
  listByProject(projectId, options) {
44
79
  const project = getProject(db, projectId);
@@ -48,47 +83,49 @@ export function createCommentService(deps) {
48
83
  return dbListCommentsByProject(db, projectId, options).map(serializeComment);
49
84
  },
50
85
  update(id, input) {
51
- const existing = dbGetDocumentComment(db, id);
86
+ const existing = dbGetComment(db, id);
52
87
  if (!existing) {
53
88
  return undefined;
54
89
  }
55
90
  if (input.body !== undefined) {
56
91
  assertNonEmptyBody(input.body);
57
92
  }
58
- const comment = dbUpdateDocumentComment(db, id, input);
93
+ const comment = dbUpdateComment(db, id, input);
59
94
  if (!comment) {
60
95
  return undefined;
61
96
  }
62
- const document = dbGetDocument(db, comment.documentId);
63
- if (!document) {
97
+ const projectId = targetProjectId(db, {
98
+ type: comment.targetType,
99
+ id: comment.targetId,
100
+ });
101
+ if (!projectId) {
64
102
  return undefined;
65
103
  }
66
- eventBus.emit({
67
- type: 'comment_updated',
68
- commentId: id,
69
- documentId: comment.documentId,
70
- projectId: document.projectId,
104
+ emitCommentUpdated(eventBus, id, projectId, {
105
+ type: comment.targetType,
106
+ id: comment.targetId,
71
107
  });
72
108
  return serializeComment(comment);
73
109
  },
74
110
  delete(id) {
75
- const existing = dbGetDocumentComment(db, id);
111
+ const existing = dbGetComment(db, id);
76
112
  if (!existing) {
77
113
  return false;
78
114
  }
79
- const document = dbGetDocument(db, existing.documentId);
80
- if (!document) {
115
+ const projectId = targetProjectId(db, {
116
+ type: existing.targetType,
117
+ id: existing.targetId,
118
+ });
119
+ if (!projectId) {
81
120
  return false;
82
121
  }
83
- const deleted = dbDeleteDocumentComment(db, id);
122
+ const deleted = dbDeleteComment(db, id);
84
123
  if (!deleted) {
85
124
  return false;
86
125
  }
87
- eventBus.emit({
88
- type: 'comment_updated',
89
- commentId: id,
90
- documentId: existing.documentId,
91
- projectId: document.projectId,
126
+ emitCommentUpdated(eventBus, id, projectId, {
127
+ type: existing.targetType,
128
+ id: existing.targetId,
92
129
  });
93
130
  return true;
94
131
  },
@@ -1,4 +1,4 @@
1
- import { createDocument as dbCreateDocument, deleteCommentsByDocumentId, deleteDocument as dbDeleteDocument, detachDocumentChildren, getDocument as dbGetDocument, getDocumentByTask as dbGetDocumentByTask, getFolderByProjectAndId, getProject, getTask, listDocuments as dbListDocuments, listFolders as dbListFolders, updateDocument as dbUpdateDocument, } from '@plandesk/db';
1
+ import { createDocument as dbCreateDocument, deleteCommentsByTarget, deleteDocument as dbDeleteDocument, detachDocumentChildren, getDocument as dbGetDocument, getDocumentByTask as dbGetDocumentByTask, getFolderByProjectAndId, getProject, getTask, listDocuments as dbListDocuments, listFolders as dbListFolders, updateDocument as dbUpdateDocument, } from '@plandesk/db';
2
2
  import { buildDocumentTree, buildFolderTree, serializeDocument, } from '../serialize.js';
3
3
  export class InvalidDocumentError extends Error {
4
4
  constructor(message) {
@@ -128,7 +128,7 @@ export function createDocumentService(deps) {
128
128
  }
129
129
  db.transaction((tx) => {
130
130
  detachDocumentChildren(tx, id);
131
- deleteCommentsByDocumentId(tx, id);
131
+ deleteCommentsByTarget(tx, 'document', id);
132
132
  dbDeleteDocument(tx, id);
133
133
  });
134
134
  eventBus.emit({ type: 'canvas_updated', projectId: existing.projectId });
@@ -1,4 +1,4 @@
1
- import { createNote as dbCreateNote, deleteNote as dbDeleteNote, getNote as dbGetNote, getProject, listNotes as dbListNotes, updateNote as dbUpdateNote, } from '@plandesk/db';
1
+ import { createNote as dbCreateNote, deleteCommentsByTarget, deleteNote as dbDeleteNote, getNote as dbGetNote, getProject, listNotes as dbListNotes, updateNote as dbUpdateNote, } from '@plandesk/db';
2
2
  import { serializeNote } from '../serialize.js';
3
3
  export class InvalidNoteError extends Error {
4
4
  constructor(message) {
@@ -66,6 +66,7 @@ export function createNoteService(deps) {
66
66
  if (!deleted) {
67
67
  return false;
68
68
  }
69
+ deleteCommentsByTarget(db, 'note', id);
69
70
  eventBus.emit({ type: 'note_updated', noteId: id, projectId: existing.projectId });
70
71
  return true;
71
72
  },
@@ -1,4 +1,4 @@
1
- import { createTag, createTask, deleteEdgesByTaskId, deleteTask as dbDeleteTask, deleteTaskTagsByTaskId, getOrCreateDefaultGoal, getProject, listGoals, getTagByName, getTask, InvalidTaskStatusError, isTaskStatus, listEdges, listTagsByTaskForProject, listTagsForTask, listTasks, nullDocumentsLinkedTask, setTaskTags, taskIdsWithAnyTagName, updateTask, } from '@plandesk/db';
1
+ import { createTag, createTask, deleteCommentsByTarget, deleteEdgesByTaskId, deleteTask as dbDeleteTask, deleteTaskTagsByTaskId, getOrCreateDefaultGoal, getProject, listGoals, getTagByName, getTask, InvalidTaskStatusError, isTaskStatus, listEdges, listTagsByTaskForProject, listTagsForTask, listTasks, nullDocumentsLinkedTask, setTaskTags, taskIdsWithAnyTagName, updateTask, } from '@plandesk/db';
2
2
  import { serializeTask } from '../serialize.js';
3
3
  import { normalizeTagName } from './tags.js';
4
4
  export class InvalidGoalReferenceError extends Error {
@@ -132,6 +132,7 @@ export function createTaskService(deps) {
132
132
  }
133
133
  const projectId = task.projectId;
134
134
  db.transaction((tx) => {
135
+ deleteCommentsByTarget(tx, 'task', id);
135
136
  deleteEdgesByTaskId(tx, id);
136
137
  nullDocumentsLinkedTask(tx, id);
137
138
  deleteTaskTagsByTaskId(tx, id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plandesk/api",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -18,7 +18,7 @@
18
18
  "devDependencies": {
19
19
  "typescript": "^6.0.3",
20
20
  "vitest": "^3.2.6",
21
- "@plandesk/db": "0.7.0",
21
+ "@plandesk/db": "0.8.0",
22
22
  "@plandesk/sync-server": "0.5.0"
23
23
  },
24
24
  "dependencies": {