🧱 通知ブロック
通知ブロックは、@nodeblocks/backend-sdk の blocks からエクスポートされる再利用可能なフィルターおよび MongoDB 操作です。neverthrow の結果を返し、HTTP レスポンス自体は生成しません。通知ルートが選択されたエラーをステータスにマッピングします。
一覧
| エクスポート | 種類 | 入力 | 結果 / 動作 / エラー |
|---|---|---|---|
NotificationBlockError | エラークラス | メッセージ、任意のデータ | 基底 BlockError のサブクラス。 |
NotificationNotFoundBlockError | エラークラス | メッセージ、任意のデータ | 通知未検出のブロックエラー。 |
NotificationDbError | エラークラス | メッセージ、任意のデータ | 通知データベースのブロックエラー。 |
buildNotificationRead | ビルダー | なし | ok({ isRead: true })。 |
buildGetNotificationFilter | ビルダー | 通知 ID、任意のアイデンティティ ID | ok({ id, receiverId? })。 |
buildFindNotificationsFilter | ビルダー | アイデンティティ ID | ok({ receiverId })。 |
buildNotificationsReadFilter | ビルダー | アイデンティティ ID、タイムスタンプ | ok({ createdAt: { $lte }, isRead: false, receiverId })。 |
updateNotification | 非同期更新 | コレクション、ID、更新内容 | ok(true)、未検出、または DB エラー。 |
getNotification | 非同期取得 | コレクション、フィルター、任意のオプション | 一致する文書、未検出、または DB エラー。 |
findNotifications | 非同期取得 | コレクション、フィルター、任意のオプション | 一致する文書配列、または DB エラー。 |
createNotification | 非同期作成 | コレクション、未読の受信者ペイロード | 作成済み ID、または DB エラー。 |
createNotificationBatch | 非同期一括作成 | コレクション、未読の受信者ペイロード群 | 作成済み ID 群、または DB エラー。 |
updateNotificationBatch | 非同期一括更新 | コレクション、更新内容、フィルター | ok(true)、未検出、または DB エラー。 |
詳細
NotificationBlockError
実装
BlockError を拡張する基底クラスで、独自の動作は追加しません。通知固有の未検出エラーまたはデータベースエラーを返し得る操作の、宣言上のエラースーパータイプです。
NotificationNotFoundBlockError
実装
NotificationBlockError を拡張します。updateNotificationToReadRoute はこれを 404 にマッピングします。一括ルートも存在しないアンカーを 404 にマッピングしますが、その後の未読レコードなしの結果は意図的に成功へ変換します。
NotificationDbError
実装
NotificationBlockError を拡張します。通知ルートはこのエラーを 500 にマッピングします。ブロックはこれをスローせず、ソースで定義されたメッセージとともに返します。
buildNotificationRead
実装
シグネチャは buildNotificationRead(): Result<{ isRead: true }, never> です。常に ok({ isRead: true }) を返します。updateNotificationToReadRoute と updateNotificationToReadBatchRoute で使用します。
buildGetNotificationFilter
実装
シグネチャは buildGetNotificationFilter(notificationId, identityId?): Result<{ id: string; receiverId?: string }, never> です。notificationId を文字列化し、真値の identityId がある場合にのみ文字列化した receiverId を追加します。updateNotificationToReadRoute と updateNotificationToReadBatchRoute の一括アンカー取得で使用します。
buildFindNotificationsFilter
実装
シグネチャは buildFindNotificationsFilter(identityId): Result<{ receiverId: string }, never> です。ID を文字列化し、既読状態、ソート、ページネーションのフィールドは返しません。findNotificationsRoute で使用します。
buildNotificationsReadFilter
実装
シグネチャは buildNotificationsReadFilter(identityId, timestamp): Result<Record<string, unknown>, never> です。両方の値を文字列化し、{ createdAt: { $lte: timestamp }, isRead: false, receiverId } を作成します。アンカーの createdAt を検証した後、updateNotificationToReadBatchRoute で使用します。
updateNotification
実装
updateOne({ id: String(notificationId) }, { $set: updateBaseEntity({ ...notification }) }) を呼び出します。matchedCount と modifiedCount の両方が 0 でない場合のみ ok(true) を返します。一致が 0 件なら NotificationNotFoundBlockError('Notification not found.')、変更が 0 件なら NotificationDbError('Failed to update notification.')、スローされたエラーなら NotificationDbError('Unknown error updating notification.') を返します。updateNotificationToReadRoute で使用します。
getNotification
実装
findOne(filter, options) を呼び出します。文書があれば ok(document)、なければ NotificationNotFoundBlockError('Notification not found.')、データベースエラーがスローされた場合は NotificationDbError('Unknown error getting notification.') を返します。updateNotificationToReadRoute と updateNotificationToReadBatchRoute で使用します。
findNotifications
実装
{ filter, options }、NotificationDbError、'Unknown error finding notifications.' を渡して findResources に委譲します。Mongo の配列を変更せずに返し、自身では正規化もページネーションも行いません。findNotificationsRoute で使用します。
createNotification
実装
receiverId: string と isRead: false が必須のペイロードを受け取り、createBaseEntity を適用して 1 件の文書を挿入し、生成された id を返します。insertedId がない場合は 'Failed to create notification.'、スローされたエラーの場合は 'Unknown error creating notification.' を、いずれも NotificationDbError として返します。カスタム合成でのみ使用します。
createNotificationBatch
実装
すべての未読受信者ペイロードに createBaseEntity を適用し、insertMany を呼び出して生成された ID 群を返します。MongoBulkWriteError は数値の書き込みエラーインデックスがあれば最初のものを返し、なければ汎用の一括書き込みメッセージを返します。ほかの失敗では 'Unknown error creating notifications.' を返します。カスタム合成でのみ使用します。
updateNotificationBatch
実装
updateMany(filter, { $set: updateBaseEntity(notification) }) を呼び出します。一致し変更されたレコードがある場合のみ ok(true) を返します。一致が 0 件なら NotificationNotFoundBlockError('Notifications not found.')、変更が 0 件なら NotificationDbError('Failed to update notifications.')、スローされたエラーなら NotificationDbError('Unknown error updating notifications.') を返します。updateNotificationToReadBatchRoute で使用され、このルートは未検出結果を ok(false) に変換します。