🛒 オーダーサービス
オーダーサービスは、オーダーエンティティをCRUD操作で管理するための完全な REST API を提供します。NodeBlocks の関数型合成アプローチで構築され、MongoDB とシームレスに統合します。
🚀 クイックスタート
import express from 'express';
import {middlewares, services, drivers} from '@nodeblocks/backend-sdk';
const {nodeBlocksErrorMiddleware} = middlewares;
const {orderService} = services;
const {withMongo} = drivers;
const connectToDatabase = withMongo('mongodb://localhost:27017/?authSource=admin', 'dev', 'user', 'password');
express()
.use(
orderService(
{
...(await connectToDatabase('orders')),
...(await connectToDatabase('identities')),
...(await connectToDatabase('organizations')), // 組織スコープのオーダー一覧に必要
},
{
authSecrets: {
authEncSecret: 'your-encryption-secret',
authSignSecret: 'your-signing-secret',
},
authMode: 'bearer', // または 'cookie'
identity: {
typeIds: {
admin: '100',
guest: '000',
regular: '001',
},
},
organization: {
roles: {
admin: 'admin',
member: 'member',
owner: 'owner',
},
},
},
)
)
.use(nodeBlocksErrorMiddleware())
.listen(8089, () => console.log('Server running'));
📋 エンドポイント概要
オーダー操作
| メソッド | パス | 説明 | 認証必須 |
|---|---|---|---|
POST | /orders | 新しいオーダーを作成 | ✅ 必須 |
GET | /orders/:orderId | IDでオーダーを取得 | ✅ 必須 |
GET | /orders | オーダーを一覧/フィルタ(管理者以外は自分の identityId を指定) | ✅ 必須 |
GET | /orders/organizations/:organizationId | 組織のオーダーを一覧取得 | ✅ 必須 |
PATCH | /orders/:orderId | オーダーを更新 | ✅ 必須 |
DELETE | /orders/:orderId | オーダーを削除 | ✅ 必須 |
🗄️ エンティティスキーマ
オーダーエンティティは、自動生成されるベースフィールドと、アイテム配列および価格を含む複雑なオーダー固有のデータで構成されます:
{
"items": [
{
"productId": "string (uuid)",
"quantity": "number",
"price": "number"
}
],
"total": "number",
"subtotal": "number",
"tax": "number",
"currency": "string",
"status": "string",
"identityId": "string (uuid)",
"organizationId": "string (uuid)",
"createdAt": "string (datetime)",
"id": "string",
"updatedAt": "string (datetime)"
}
フィールド詳細
| フィールド | 型 | 自動生成 | 必須 | 説明 |
|---|---|---|---|---|
items | array | ❌ | ✅ | 製品詳細を含むオーダーアイテムの配列 |
total | number | ❌ | ✅ | オーダー合計金額(最小0) |
subtotal | number | ❌ | ❌ | 税抜小計(最小0) |
tax | number | ❌ | ❌ | 税金額(最小0) |
currency | string | ❌ | ❌ | 通貨コード(例: "USD", "EUR") |
status | string | ❌ | ❌ | オーダーステータス(例: "pending", "completed") |
identityId | string (uuid) | ❌ | ✅ | オーダーを発注したアイデンティティ |
organizationId | string (uuid) | ❌ | ❌ | オーダーに関連付けられた組織 |
createdAt | datetime | ✅ | ✅ | 作成日時 |
id | string | ✅ | ✅ | 一意識別子(UUID) |
updatedAt | datetime | ✅ | ✅ | 最終更新日時 |
アイテム配列スキーマ
items 配列内の各アイテムには以下のものが含まれている必要があります:
| フィールド | 型 | 必須 | 検証 | 説明 |
|---|---|---|---|---|
productId | string (uuid) | ✅ | UUID形式 | 製品識別子 |
quantity | number | ✅ | 最小1 | 注文数量 |
price | number | ✅ | 最小0 | 単価 |
📝 注意: スキーマは
additionalProperties: falseを強制するため、定義されたフィールドのみが許可されます。自動生成フィールドはサービス側で設定され、作成/更新リクエストに含めないでください。
🔐 認証ヘッダー
すべてのエンドポイントで、次の認証情報を含めてください:
Authorization: Bearer <access_token> // bearer モード(既定)
x-nb-fingerprint: <device_fingerprint>
サービスが authMode: 'cookie' に設定されている場合、アクセストークンは Authorization ヘッダーではなく accessToken Cookie から読み取られます。
⚠️ 重要: 認可時にフィンガープリントを指定した場合、認証済みのすべてのリクエストで
x-nb-fingerprintヘッダーが必須です。欠如している場合は 401 Unauthorized が返ります。
🔧 APIエンドポイント
1. オーダー作成
アイテム配列と価格詳細を含む提供された情報で新しいオーダーを作成します。
リクエスト:
- メソッド:
POST - パス:
/orders - ヘッダー:
Content-Type: application/json- bearer モードでは
Authorization: Bearer <token>、cookie モードではaccessTokenCookie x-nb-fingerprint: <device-fingerprint>
- 認可: bearer モードではベアラートークン、
authMode: 'cookie'では Cookie ベース認証が必要
リクエストボディ:
| フィールド | 型 | 必須 | 説明 |
|---|---|---|---|
items | array | ✅ | オーダーアイテムの配列(アイテムスキーマ参照) |
total | number | ✅ | オーダー合計金額 |
identityId | string (uuid) | ✅ | オーダーを発注したアイデンティティ |
subtotal | number | ❌ | 税抜小計 |
tax | number | ❌ | 税金額 |
currency | string | ❌ | 通貨コード |
status | string | ❌ | オーダーステータス |
organizationId | string (uuid) | ❌ | オーダーに関連付けられた組織 |
レスポンスボディ: バリデーション:
- スキーマ検証: 自動強制(items、total、identityId必須)
- ルートバリデーション:
- 認証済みリクエストが必要(bearer モードではベアラートークン、
authMode: 'cookie'では Cookie ベース認証) - 管理者ロールまたは自己(オーダー所有者)必須
- 認証済みリクエストが必要(bearer モードではベアラートークン、
エラーレスポンス:
リクエストボディがない場合:
HTTP/1.1 400 Bad Request
Content-Type: application/json
{
"error": {
"message": "Request body is required"
}
}
挿入操作で挿入 ID が返されない場合:
HTTP/1.1 400 Bad Request
Content-Type: application/json
{
"error": {
"message": "Failed to create order"
}
}
作成中に予期しないサーバーエラーが発生した場合:
HTTP/1.1 500 Internal Server Error
Content-Type: application/json
{
"error": {
"message": "Unknown error creating order"
}
}
認可に関する注意:
オーダーを作成するとき、管理者以外の呼び出し元はリクエストボディの identityId をアクセストークンの identityId と一致させる必要があります。プラットフォームの管理者ユーザーは、任意の identityId のオーダーを作成できます(バリデーター: some(checkIdentityType(['admin']), isSelf(...)))。
リクエスト例:
curl -X POST http://localhost:8089/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <access_token>" \
-d '{
"identityId": "30000000-0000-4000-a000-000000000001",
"items": [
{
"productId": "10000000-0000-4000-a000-000000000001",
"quantity": 2,
"price": 50
},
{
"productId": "10000000-0000-4000-a000-000000000002",
"quantity": 1,
"price": 25
}
],
"subtotal": 125,
"tax": 12.5,
"total": 137.5,
"currency": "USD",
"status": "pending",
"organizationId": "20000000-0000-4000-a000-000000000001"
}'
成功レスポンス:
HTTP/1.1 200 OK
Content-Type: application/json
{
"currency": "USD",
"items": [
{
"productId": "10000000-0000-4000-a000-000000000001",
"quantity": 2,
"price": 50
},
{
"productId": "10000000-0000-4000-a000-000000000002",
"quantity": 1,
"price": 25
}
],
"organizationId": "20000000-0000-4000-a000-000000000001",
"identityId": "30000000-0000-4000-a000-000000000001",
"subtotal": 125,
"tax": 12.5,
"total": 137.5,
"status": "pending",
"createdAt": "2025-07-04T01:59:20.084Z",
"id": "10283907-d41a-4647-a235-18ec63f3369a",
"updatedAt": "2025-07-04T01:59:20.084Z"
}
エラーレスポンス:
リクエストボディに必須フィールドがない場合:
HTTP/1.1 400 Bad Request
Content-Type: application/json
{
"error": {
"message": "Validation Error",
"data": [
"request body must have required property 'identityId'",
"request body must have required property 'items'"
]
}
}
認証が失敗した場合:
HTTP/1.1 401 Unauthorized
Content-Type: application/json
{
"error": {
"message": "token could not be verified"
}
}
認可が失敗した場合:
HTTP/1.1 403 Forbidden
Content-Type: application/json
{
"error": {
"message": "User is not authorized to access this order"
}
}
2. IDでオーダー取得
一意のIDで特定のオーダーを取得します。
リクエスト:
- メソッド:
GET - パス:
/orders/:orderId - ヘッダー:
- bearer モードでは
Authorization: Bearer <token>、cookie モードではaccessTokenCookie x-nb-fingerprint: <device-fingerprint>
- bearer モードでは
- 認可: bearer モードではベアラートークン、
authMode: 'cookie'では Cookie ベース認証が必要
URL パラメータ:
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
orderId | string | ✅ | 一意のオーダー識別子 |
レスポンスボディ:
| フィールド | 型 | 説明 |
|---|---|---|
items | array | 製品詳細を含むオーダーアイテムの配列 |
items[].productId | string | 製品識別子 |
items[].quantity | number | 注文数量 |
items[].price | number | 単価 |
total | number | オーダー合計金額 |
subtotal | number | 税抜小計 |
tax | number | 税金額 |
currency | string | 通貨コード |
status | string | オーダーステータス |
identityId | string | オーダーを発注したアイデンティティ |
organizationId | string | オーダーに関連付けられた組織 |
createdAt | string | 作成日時 |
id | string | 一意のオーダー識別子 |
updatedAt | string | 最終更新日時 |
バリデーション:
- スキーマ検証: なし(GET リクエスト)
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロールまたはオーダー所有権必須
リクエスト例:
curl http://localhost:8089/orders/10283907-d41a-4647-a235-18ec63f3369a \
-H "Authorization: Bearer <access_token>"
成功レスポンス:
HTTP/1.1 200 OK
Content-Type: application/json
{
"currency": "USD",
"items": [
{
"productId": "10000000-0000-4000-a000-000000000001",
"quantity": 2,
"price": 50
},
{
"productId": "10000000-0000-4000-a000-000000000002",
"quantity": 1,
"price": 25
}
],
"organizationId": "20000000-0000-4000-a000-000000000001",
"identityId": "30000000-0000-4000-a000-000000000001",
"subtotal": 125,
"tax": 12.5,
"total": 137.5,
"status": "completed",
"createdAt": "2025-07-04T01:59:20.084Z",
"id": "10283907-d41a-4647-a235-18ec63f3369a",
"updatedAt": "2025-07-07T08:11:16.176Z"
}
エラーレスポンス:
オーダー ID がない場合:
HTTP/1.1 400 Bad Request
Content-Type: application/json
{"error":{"message":"Order ID is required"}}
取得中に予期しないサーバーエラーが発生した場合:
HTTP/1.1 500 Internal Server Error
Content-Type: application/json
{"error":{"message":"Failed to get order"}}
指定IDのオーダーが存在しない場合:
HTTP/1.1 404 Not Found
Content-Type: application/json
{
"error": {
"message": "Order not found"
}
}
3. オーダー一覧
フィルタやページングを指定してオーダーの一覧を取得します。
リクエスト:
- メソッド:
GET - パス:
/orders - ヘッダー:
Authorization: Bearer <token>x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
クエリパラメータ:
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
currency | string | ❌ | 通貨でフィルタ |
organizationId | string | ❌ | 組織でフィルタ |
status | string | ❌ | ステータスでフィルタ |
subtotal | number | ❌ | 小計金額でフィルタ |
tax | number | ❌ | 税金額でフィルタ |
total | number | ❌ | 合計金額でフィルタ |
identityId | string | ❌* | identityId でフィルタ(*管理者以外は必須。トークンの主体と一致する必要があります) |
page | number | ❌ | ページングのページ番号(1~1000) |
limit | number | ❌ | 1ページあたりの件数(1~50) |
レスポンスボディ: ページネーション情報を含むオーダー配列を返します。
レスポンス構造:
{
"data": [
{
"id": "string",
"items": [
{
"productId": "string",
"quantity": number,
"price": number
}
],
"total": number,
"subtotal": number,
"tax": number,
"currency": "string",
"status": "string",
"identityId": "string",
"organizationId": "string",
"createdAt": "string",
"updatedAt": "string"
}
],
"metadata": {
"pagination": {
"page": number,
"limit": number,
"total": number,
"totalPages": number,
"hasNext": boolean,
"hasPrev": boolean
}
}
}
| フィールド | 型 | 説明 |
|---|---|---|
items | array | 製品詳細を含むオーダーアイテムの配列 |
items[].productId | string | 製品識別子 |
items[].quantity | number | 注文数量 |
items[].price | number | 単価 |
total | number | オーダー合計金額 |
subtotal | number | 税抜小計 |
tax | number | 税金額 |
currency | string | 通貨コード |
status | string | オーダーステータス |
identityId | string | オーダーを発注したアイデンティティ |
organizationId | string | オーダーに関連付けられた組織 |
createdAt | string | 作成日時 |
id | string | 一意のオーダー識別子 |
updatedAt | string | 最終更新日時 |
バリデーション:
- スキーマ検証: ルートスキーマで定義されたパスおよびクエリパラメータに自動適用
- ルートバリデーション:
- 認証済みリクエストが必要(bearer モードではベアラートークン、
authMode: 'cookie'では Cookie ベース認証) - 管理者ロール、またはクエリの
identityIdによる本人確認(requestQuery.identityIdに対するisSelf)が必要
- 認証済みリクエストが必要(bearer モードではベアラートークン、
管理者以外の呼び出し元は
?identityId=<自分のidentity-id>を指定する必要があります。identityIdを指定しない一覧取得は管理者専用です。
リクエスト例:
管理者として全件取得:
curl http://localhost:8089/orders \
-H "Authorization: Bearer <access_token>"
ステータスでフィルタ:
curl "http://localhost:8089/orders?status=completed&identityId=30000000-0000-4000-a000-000000000001" \
-H "Authorization: Bearer <access_token>"
通貨でフィルタ:
curl "http://localhost:8089/orders?currency=USD" \
-H "Authorization: Bearer <access_token>"
自分のオーダーを取得:
curl "http://localhost:8089/orders?identityId=30000000-0000-4000-a000-000000000001" \
-H "Authorization: Bearer <access_token>"
成功レスポンス:
HTTP/1.1 200 OK
Content-Type: application/json
{
"data": [
{
"currency": "USD",
"items": [
{
"productId": "10000000-0000-4000-a000-000000000001",
"quantity": 2,
"price": 50
},
{
"productId": "10000000-0000-4000-a000-000000000002",
"quantity": 1,
"price": 25
}
],
"organizationId": "20000000-0000-4000-a000-000000000001",
"identityId": "30000000-0000-4000-a000-000000000001",
"subtotal": 125,
"tax": 12.5,
"total": 137.5,
"status": "completed",
"createdAt": "2025-07-04T01:59:20.084Z",
"id": "10283907-d41a-4647-a235-18ec63f3369a",
"updatedAt": "2025-07-07T08:11:16.176Z"
}
],
"metadata": {"pagination": {"page": 1, "limit": 10, "total": 1, "totalPages": 1, "hasNext": false, "hasPrev": false}}
}
ページネーションを指定したリクエスト例:
curl "http://localhost:8089/orders?page=1&limit=10" \
-H "Authorization: Bearer <access_token>"
4. オーダー更新
部分更新で既存のオーダーを更新します。
リクエスト:
- メソッド:
PATCH - パス:
/orders/:orderId - ヘッダー:
Content-Type: application/jsonAuthorization: Bearer <token>x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
URL パラメータ:
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
orderId | string | ✅ | 一意のオーダー識別子 |
リクエストボディ(全フィールド任意):
| フィールド | 型 | 必須 | 説明 |
|---|---|---|---|
items | array | ❌ | オーダーアイテムの配列 |
total | number | ❌ | オーダー合計金額 |
subtotal | number | ❌ | 税抜小計 |
tax | number | ❌ | 税金額 |
currency | string | ❌ | 通貨コード |
status | string | ❌ | オーダーステータス |
identityId | string (uuid) | ❌ | オーダーを発注したアイデンティティ |
organizationId | string (uuid) | ❌ | オーダーに関連付けられた組織 |
レスポンスボディ:
| フィールド | 型 | 説明 |
|---|---|---|
items | array | 製品詳細を含むオーダーアイテムの配列 |
items[].productId | string | 製品識別子 |
items[].quantity | number | 注文数量 |
items[].price | number | 単価 |
total | number | 更新後のオーダー合計金額 |
subtotal | number | 更新後の税抜小計 |
tax | number | 更新後の税金額 |
currency | string | 更新後の通貨コード |
status | string | 更新後のオーダーステータス |
identityId | string | オーダーを発注したアイデンティティ |
organizationId | string | 更新後のオーダーに関連付けられた組織 |
createdAt | string | 作成日時 |
id | string | 一意のオーダー識別子 |
updatedAt | string | 最終更新日時 |
バリデーション:
- スキーマ検証: 自動強制(部分更新、全フィールド任意)
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロールまたはオーダー所有権必須
リクエスト例:
curl -X PATCH http://localhost:8089/orders/10283907-d41a-4647-a235-18ec63f3369a \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <access_token>" \
-d '{"status": "completed"}'
成功レスポンス:
HTTP/1.1 200 OK
Content-Type: application/json
{
"currency": "USD",
"items": [
{
"productId": "10000000-0000-4000-a000-000000000001",
"quantity": 2,
"price": 50
},
{
"productId": "10000000-0000-4000-a000-000000000002",
"quantity": 1,
"price": 25
}
],
"organizationId": "20000000-0000-4000-a000-000000000001",
"identityId": "30000000-0000-4000-a000-000000000001",
"subtotal": 125,
"tax": 12.5,
"total": 137.5,
"status": "completed",
"createdAt": "2025-07-04T01:59:20.084Z",
"id": "10283907-d41a-4647-a235-18ec63f3369a",
"updatedAt": "2025-07-07T08:11:16.176Z"
}
エラーレスポンス:
オーダー ID がない場合:
HTTP/1.1 400 Bad Request
Content-Type: application/json
{"error":{"message":"Order ID is required"}}
リクエストボディがない場合:
HTTP/1.1 400 Bad Request
Content-Type: application/json
{"error":{"message":"Request body is required"}}
更新操作でデータが変更されなかった場合:
HTTP/1.1 400 Bad Request
Content-Type: application/json
{"error":{"message":"Failed to update order"}}
更新中に予期しないサーバーエラーが発生した場合:
HTTP/1.1 500 Internal Server Error
Content-Type: application/json
{"error":{"message":"Failed to update order"}}
指定IDのオーダーが存在しない場合:
HTTP/1.1 404 Not Found
Content-Type: application/json
{
"error": {
"message": "Order not found"
}
}
5. オーダー削除
システムからオーダーを完全に削除します。
リクエスト:
- メソッド:
DELETE - パス:
/orders/:orderId - ヘッダー:
Authorization: Bearer <token>x-nb-fingerprint: <device-fingerprint>
- 認可: ベアラートークン必須
URL パラメータ:
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
orderId | string | ✅ | 一意のオーダー識別子 |
レスポンスボディ:
| フィールド | 型 | 説明 |
|---|---|---|
| なし | - | 成功時はレスポンスボディなし |
バリデーション:
- ルートバリデーション:
- 認証済みリクエスト(ベアラー)必須
- 管理者ロールまたはオーダー所有権必須
リクエスト例:
curl -X DELETE http://localhost:8089/orders/10283907-d41a-4647-a235-18ec63f3369a \
-H "Authorization: Bearer <access_token>"
成功レスポンス:
HTTP/1.1 204 No Content
エラーレスポンス:
オーダー ID がない場合:
HTTP/1.1 400 Bad Request
Content-Type: application/json
{"error":{"message":"Order ID is required"}}
削除中に予期しないサーバーエラーが発生した場合:
HTTP/1.1 500 Internal Server Error
Content-Type: application/json
{"error":{"message":"Failed to delete order"}}
指定IDのオーダーが存在しない場合:
HTTP/1.1 404 Not Found
Content-Type: application/json
{
"error": {
"message": "Order not found"
}
}
6. 組織 ID によるオーダー一覧
組織に属するオーダーの一覧を取得します。呼び出し元は管理者、組織オーナー、または組織メンバーである必要があります。
リクエスト:
- メソッド:
GET - パス:
/orders/organizations/:organizationId - 認証: ベアラーモードではアクセストークン、
authMode: 'cookie'では Cookie ベースの認証が必要です。
| 組織スコープアクセスの要件 |
|---|
呼び出し元は対象組織で owner、admin、または member の役割を持つ必要があります。 |
URL パラメータ: organizationId(string、必須)— 組織の一意識別子。
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
organizationId | string | ✅ | オーダーを取得する組織の一意識別子 |
クエリパラメータ:
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
page | number | ❌ | ページ番号(1~1000) |
limit | number | ❌ | 1ページあたりの件数(1~50) |
レスポンス: ページネーション情報を含む、その組織のオーダー配列を返します。
レスポンス構造:
{
"data": [{
"id": "string",
"items": [{"productId": "string", "quantity": number, "price": number}],
"total": number,
"subtotal": number,
"tax": number,
"currency": "string",
"status": "string",
"identityId": "string",
"organizationId": "string",
"createdAt": "string",
"updatedAt": "string"
}],
"metadata": {"pagination": {"page": number, "limit": number, "total": number, "totalPages": number, "hasNext": boolean, "hasPrev": boolean}}
}
リクエスト例:
curl "http://localhost:8089/orders/organizations/org-123?page=1&limit=10" \
-H "Authorization: Bearer <token>" \
-H "x-nb-fingerprint: <device-fingerprint>"
成功レスポンス:
HTTP/1.1 200 OK
Content-Type: application/json
{
"data": [{
"id": "order-456",
"organizationId": "org-123",
"currency": "USD",
"status": "confirmed",
"items": [{"productId": "prod-789", "quantity": 2, "price": 29.99}],
"createdAt": "2024-01-01T00:00:00.000Z",
"updatedAt": "2024-01-01T00:00:00.000Z"
}],
"metadata": {"pagination": {"page": 1, "limit": 10, "total": 1, "totalPages": 1, "hasNext": false, "hasPrev": false}}
}
エラーレスポンス:
認証トークンを確認できない場合:
{"error":{"message":"token could not be verified"}}
組織 ID が無効な場合:
{"error":{"message":"Invalid organization ID"}}
アイデンティティが組織のメンバーではない場合:
{"error":{"message":"Identity is not a member of the organization"}}
アイデンティティに組織へのアクセス権がない場合:
{"error":{"message":"Identity is not authorized to access this organization"}}
organizations データストアが設定されていない場合:
{"error":{"message":"db.organizations is not set"}}
組織ロール設定がない場合:
{"error":{"message":"configuration.organization.roles is not set"}}
組織の取得に失敗した場合:
{"error":{"message":"Failed to fetch organization"}}
オーダーの検索に失敗した場合:
{"error":{"message":"Failed to find orders"}}
ユースケース:
- 組織管理者による所属組織の全オーダーの確認
- 組織別オーダーデータを使用した財務レポートおよび分析
- 組織内のオーダー履歴にアクセスするカスタマーサービスチーム
- 組織スコープのデータアクセスに関するコンプライアンスおよび監査要件
⚙️ 設定オプション
データストア
| コレクション | 必須 | 説明 |
|---|---|---|
orders | ✅ | オーダー文書 |
identities | ✅ | 認証および所有者検証に使用 |
organizations | ✅ | 組織スコープのオーダー一覧に使用 |
サービス設定
interface OrderServiceConfiguration {
authSecrets: {
authEncSecret: string; // JWT 暗号化シークレット
authSignSecret: string; // JWT 署名シークレット
};
identity?: {
typeIds?: {
admin: string; // 管理者ユーザー種別識別子
guest: string; // ゲストユーザー種別識別子
regular: string; // 一般ユーザー種別識別子
};
};
}
設定詳細
オーダーサービスの設定は、セキュリティとユーザー種別管理の論理グループに整理されています。
🔐 セキュリティ設定
authSecrets - JWT トークンのセキュリティシークレット
- 型:
{ authEncSecret: string; authSignSecret: string } - 説明: JWT の暗号化および署名に使用する秘密鍵(トークン検証に使用)
- 必須: 本番環境では必須
- 子プロパティ:
authEncSecret: JWT ペイロード暗号化の秘密鍵authSignSecret: JWT 署名検証の秘密鍵
👥 ユーザー種別設定
user.typeIds - ユーザー種別識別子の設定
- 型:
{ admin?: string; guest?: string; user?: string } - 説明: ロールベースアクセス制御のためのカスタムユーザー種別識別子
- デフォルト:
undefined(デフォルトの種別検証を使用) - 子プロパティ:
admin: 管理者ユーザー種別の識別子- 型:
string - 説明: 管理者ユーザーのカスタム識別子
- 利用例: 管理操作のロールベースアクセス制御
- 例:
"admin","administrator","superuser"
- 型:
guest: ゲストユーザー種別の識別子- 型:
string - 説明: ゲストユーザーのカスタム識別子
- 利用例: 未認証/一時ユーザーの限定的アクセス
- 例:
"guest","visitor","anonymous"
- 型:
user: 一般ユーザー種別の識別子- 型:
string - 説明: 一般ユーザーのカスタム識別子
- 利用例: 標準的なユーザー権限
- 例:
"user","member","customer"
- 型:
設定例
const orderConfig = {
authSecrets: {
authEncSecret: process.env.AUTH_ENC_SECRET || 'your-enc-secret',
authSignSecret: process.env.AUTH_SIGN_SECRET || 'your-sign-secret'
},
user: {
typeIds: {
admin: 'administrator',
guest: 'visitor',
user: 'member'
}
}
};
🚨 エラーハンドリング
オーダーサービスのエラーは、適切なHTTPステータスコードとJSON形式で返されます:
代表的なエラーコード
| ステータス | エラーメッセージ | 説明 |
|---|---|---|
| 400 | Validation Error | リクエストボディ形式が無効または必須フィールドがない |
| 400 | request body must have required property 'identityId' | リクエストボディに identityId フィールドがない |
| 400 | request body must have required property 'items' | リクエストボディに items 配列がない |
| 400 | request body must have required property 'total' | リクエストボディに total フィールドがない |
| 400 | request body must NOT have additional properties | リクエストにサポートされていないフィールドが含まれている |
| 400 | Failed to create order | データベース挿入操作が挿入されたIDを返せなかった |
| 400 | Failed to update order | 更新操作でデータが変更されない(変更なし) |
| 400 | Failed to delete order | データベース削除操作が失敗した |
| 401 | token could not be verified | 認可トークンがない/無効 |
| 401 | Authentication failed | 認証トークンがない/無効 |
| 401 | Token fails security check | トークンのセキュリティ検証が失敗した |
| 403 | User is not authorized to access this order | 必要な権限がない(管理者/所有者アクセス) |
| 403 | User is not authorized to access this resource | 必要な権限がない(管理者アクセス) |
| 404 | Order not found | 要求された操作の対象オーダーが存在しない |
| 500 | Failed to create order | 作成中のDB接続問題/予期せぬ失敗 |
| 500 | Failed to get order | 取得中のDB接続問題/予期せぬ失敗 |
| 500 | Failed to find orders | 一覧取得中のDB接続問題/フィルタ構文不正/予期せぬ失敗 |
| 500 | Failed to update order | 更新中のDB接続問題/予期せぬ失敗 |
| 500 | Failed to delete order | 削除中のDB接続問題/予期せぬ失敗 |
エラーレスポンス形式
{
"error": {
"message": "Error message description",
"data": ["Additional error details"]
}
}
検証エラーには追加の詳細が含まれます:
{
"error": {
"message": "Validation Error",
"data": [
"request body must have required property 'items'",
"request body must have required property 'total'"
]
}
}
🔗 関連ドキュメント
- 製品サービス - 製品管理操作
- アイデンティティサービス - ユーザー管理操作
- 組織サービス - 組織管理操作
- 認証サービス - 認証および認可
- エラーハンドリング - エラーパターンの理解
- スキーマコンポーネント - データ検証の概念
- カスタムサービステュートリアル - 独自サービスの構築