---
url: /reference/functions/events/system.md
---

# system

本文档介绍如何在应用中订阅系统事件。当用户在 PingCode 产品中执行操作时，系统会生成 PingCode 产品事件，如创建工作项，应用可以配置订阅并处理这些事件。

## 配置

`manifest.yml` 文件配置示例：

```yaml
event:
  triggers:
    - key: system_trigger
      type: system
      events:
        - pce:ship:idea:created
        - pce:ship:idea:updated
      handler: 
        function: ship-idea-handler
      filter:
        ignoreSelf: true
```

## 处理函数

当事件触发时，会被你定义的处理函数接收到。

### 示例

```javascript
import type { SystemEventHandler } from "@pc-nexus/event";

export const handler: SystemEventHandler = async (context, event) => {
    console.log(JSON.stringify(context));
    console.log(JSON.stringify(event));
};

```

### 参数

处理函数接收 `context` 和 `event` 两个参数。

#### context

`context` 为 `NexusAppContext` 类型，包含应用运行时的上下文信息，详情请参考 [app](/reference/functions/core/app) ，获取触发的事件信息可以从 `context` 中获取 如下：

#### event

`event` 是订阅事件所传递的数据，其类型定义如下：

```typescript
export interface EventHandlerEvent {
    event_type: string;
    self_generated: boolean;
    payload: {
        data: PrincipalInfo;
        changelog?: {
            origin?: originValue;
            target?: targetValue;
            property?: PropertyInfo;
        };
        source?: SystemEventSource;
    };
}
```

`payload` 中包含：

### 数据示例

以事件 `pce:pjm:workitem:created` 为例，当事件触发时完整的 `event` 参数如下：

```javascript
{
    "event_type": "pce:pjm:workitem:created",
    "self_generated": false,  
    "payload": {
        "data": {
            "id": "6a06d7954cd222a3dad91d45",
            "url": "https://open.pingcode.com/v1/project/work_items/6a06d7954cd222a3dad91d45",
            "project": {
                "id": "68c24404b2ae15e123c9e60c",
                "url": "https://open.pingcode.com/v1/project/projects/68c24404b2ae15e123c9e60c",
                "name": "test-hybrid",
                "type": "hybrid",
                "identifier": "H20020",
                "is_archived": 0,
                "is_deleted": 0
            },
            "identifier": "H20020-59",
            "title": "TEST",
            "type": "story",
            "start_at": null,
            "end_at": null,
            "parent_id": null,
            "short_id": "JoP34lQM",
            "html_url": "https://xxx.pingcode.com/pjm/workitems/JoP34lQM",
            "parent": null,
            "assignee": null,
            "state": {
                "id": "62d8f23c461c1ccf22123314",
                "url": "https://open.pingcode.com/v1/project/work_item_states/62d8f23c461c1ccf22123314",
                "name": "打开",
                "type": "pending",
                "color": "#56ABFB"
            },
            "priority": null,
            "board": {
                "id": "68c24404b2ae15e123c9e63b",
                "url": "https://open.pingcode.com/v1/project/projects/68c24404b2ae15e123c9e60c/boards/68c24404b2ae15e123c9e63b",
                "name": "默认看板",
                "work_item_types": [
                    "62d8f240461c1ccf221235d7",
                    "646b58929f3ece3cfda66c8c",
                    "636ca7d543350c333e49c3d0",
                    "62d8f23c461c1ccf22123309",
                    "636ca7f643350c333e49c3e1"
                ]
            },
            "entry": {
                "id": "68c24404b2ae15e123c9e635",
                "url": "https://open.pingcode.com/v1/project/projects/68c24404b2ae15e123c9e60c/boards/68c24404b2ae15e123c9e63b/entries/68c24404b2ae15e123c9e635",
                "name": "需求池"
            },
            "swimlane": {
                "id": "68c24404b2ae15e123c9e63a",
                "url": "https://open.pingcode.com/v1/project/projects/68c24404b2ae15e123c9e60c/boards/68c24404b2ae15e123c9e63b/swimlanes/68c24404b2ae15e123c9e63a",
                "name": "默认泳道"
            },
            "version": null,
            "versions": null,
            "sprint": null,
            "phase": null,
            "story_points": null,
            "estimated_workload": null,
            "remaining_workload": null,
            "description": null,
            "completed_at": null,
            "properties": {
                "risk": null,
                "backlog_type": null,
                "backlog_from": null,
                "schedule_mode": null,
                "dangechengyuan": null,
                "duogechengyuan": null,
                "workload": {
                    "reported_total": 0,
                    "remaining": 0,
                    "estimated": {
                        "duration": 0,
                        "estimated_at": 1778833301,
                        "estimated_by": null
                    }
                },
                "operation_time": 1778833301,
                "entry_status": 1,
                "entry_position": 16384
            },
            "tags": [],
            "participants": [
                {
                    "id": "c1ecd63e5af2461e810d402767e78fc8",
                    "url": "https://open.pingcode.com/v1/participants/c1ecd63e5af2461e810d402767e78fc8?principal_type=work_item&principal_id=6a06d7954cd222a3dad91d45",
                    "type": "user",
                    "user": {
                        "id": "c1ecd63e5af2461e810d402767e78fc8",
                        "url": "https://open.pingcode.com/v1/directory/users/c1ecd63e5af2461e810d402767e78fc8",
                        "name": "xxx",
                        "display_name": "xxx",
                        "avatar": null
                    }
                }
            ],
            "created_at": 1778833301,
            "created_by": {
                "id": "c1ecd63e5af2461e810d402767e78fc8",
                "url": "https://open.pingcode.com/v1/directory/users/c1ecd63e5af2461e810d402767e78fc8",
                "name": "xxx",
                "display_name": "xxx",
                "avatar": null
            },
            "updated_at": 1778833301,
            "updated_by": {
                "id": "c1ecd63e5af2461e810d402767e78fc8",
                "url": "https://open.pingcode.com/v1/directory/users/c1ecd63e5af2461e810d402767e78fc8",
                "name": "xxx",
                "display_name": "xxx",
                "avatar": null
            },
            "is_archived": 0,
            "is_deleted": 0
        }
    }
}
```

## 事件

目前 Nexus 平台支持的系统事件如下：

## 作用域范围

订阅事件需要在应用 `manifest.yaml` 中申请相应的作用域范围，不同事件需要不同的作用域。如果权限不足，应用将无法接收到事件或获取完整数据。

```yaml
event:
  triggers:
    - key: system_trigger
      type: system
      events:
        - pce:ship:idea:created
        - pce:ship:idea:updated
      handler: 
        function: ship-idea-handler
permissions:
  scopes:
    - pcp:read:ship:idea
```

## 事件循环

在 `manifest.yml` 的触发器配置中，建议设置 `filter.ignoreSelf: true` 。这可以确保应用自身通过 APIs 触发的操作所产生的事件不会被自己再次订阅到，从而避免无限循环。

```yaml
event:
  triggers:
    - key: system_trigger
      type: system
      events:
        - pce:ship:idea:created
        - pce:ship:idea:updated
      handler: 
        function: ship-idea-handler
      filter:
        ignoreSelf: true
```
