使用Camunda和Trello自动执行手动任务(中)

使用Camunda和Trello自动执行手动任务(中)

仍然缺少将控制器集成到 worker中的功能:

import { StorageController } from '../storage.controller'

import { TrelloController } from '../trello.controller'

import { ZeebeController } from '../zeebe.controller'

export class TrelloWorkerController {

  constructor(

    private zeebeController: ZeebeController,

    private store: StorageController,

  ) {}

  public createWorker(taskType: 'trelloAddCard') {

    this.zeebeController.getZeebeClient().createWorker({

      taskType,

      taskHandler: async (job: any, complete: any, worker: any) => {

        const idList = job.customHeaders.idlist

        const name = job.customHeaders.name

        const trelloController = new TrelloController(this.store)

        try {

          switch (taskType) {

            case 'trelloAddCard':

              const id: string = await trelloController.addCard(idList, name)

              complete.success({ id })

              break

            default:

              complete.failure(`Tasktype ${taskType} unknown`)

          }

        } catch (error) {

          complete.failure('Failed to send slack message')

        }

      },

    })

  }

}

从上一篇文章开始,您已经对基本的实现感到熟悉。应该强调的是,列表和名称不是静态的。该参数 idList 是应在其上创建新条目的Trello列表的ID。名称是新卡的标题。最后,将创建的卡的ID写回到流程上下文中。

设置一个Webhook

我们的目标是在董事会上发生变化时得到通知。如果Trello卡已移至 Done,则应通知我们的流程。为此,Trello提供了 Webhooks。我们要做的就是提供一个HTTP终结点,当某些情况发生变化时,Trello会调用该终结点。

为此,我们提供以下端点:

const express = require('express')

import { NextFunction, Request, Response } from 'express'

import { StorageController } from '../../controller/storage.controller'

import { ZeebeController } from '../../controller/zeebe.controller'

import { TrelloBoardType } from '../../types/TrelloBoard.type'

import { Error, ErrorType } from '../../utils/Error'

export class TrelloWebhookRouter {

  public router = express.Router({ mergeParams: true })

  constructor(store: StorageController) {

    this.router.post(

      '/',

      async (req: Request, res: Response, next: NextFunction) => {

        const payload: TrelloBoardType = req.body as TrelloBoardType

 

        try {

          if (

            payload &&

            payload.action &&

            payload.action.type === 'updateCard' &&

            payload.action.data.listAfter.name === 'Done'

          ) {

            const id = payload.action.data.card.id

            const zeebeController = new ZeebeController()

            await zeebeController.publishMessage(id, 'Card done')

          }

          res.send()

        } catch (error) {

          throw new Error(ErrorType.Internal)

        }

      },

    )

 

    this.router.get(

      '/',

      async (req: Request, res: Response, next: NextFunction) => {

        res.send()

      },

    )

  }

}

 

我们要回应的两个特殊功能:

我们检查卡是否已更改:

payload.action.type === 'updateCard' &&

并且我们检查Done 更改后卡片是否在 列表中:

payload.action.data.listAfter.name === "Done";

上面已显示已更改的卡的ID:

const id = payload.action.data.card.id;

我们CorrelationKey 在此过程中对消息事件使用此ID  ,以便正确的实例做出相应的反应。

最后,唯一缺少的是为Trello创建webhook,我们可以使用Trello API进行设置。为此,我们POST 使用以下查询参数向API发送 请求:

 

相关教程