* Create a Monday.com webhook subscription via their GraphQL API. * Monday.com webhooks are board-scoped and event-type-specific.
(ctx: SubscriptionContext)
| 75 | * Monday.com webhooks are board-scoped and event-type-specific. |
| 76 | */ |
| 77 | async createSubscription(ctx: SubscriptionContext): Promise<SubscriptionResult | undefined> { |
| 78 | const config = getProviderConfig(ctx.webhook) |
| 79 | const triggerId = config.triggerId as string | undefined |
| 80 | const boardId = config.boardId as string | undefined |
| 81 | |
| 82 | if (!triggerId) { |
| 83 | logger.warn(`[${ctx.requestId}] Missing triggerId for Monday webhook ${ctx.webhook.id}`) |
| 84 | throw new Error('Trigger type is required for Monday.com webhook creation.') |
| 85 | } |
| 86 | |
| 87 | if (!boardId) { |
| 88 | logger.warn(`[${ctx.requestId}] Missing boardId for Monday webhook ${ctx.webhook.id}`) |
| 89 | throw new Error( |
| 90 | 'Board ID is required. Please provide a valid Monday.com board ID in the trigger configuration.' |
| 91 | ) |
| 92 | } |
| 93 | |
| 94 | const boardIdValidation = validateMondayNumericId(boardId, 'boardId') |
| 95 | if (!boardIdValidation.isValid) { |
| 96 | throw new Error(boardIdValidation.error!) |
| 97 | } |
| 98 | |
| 99 | const { MONDAY_EVENT_TYPE_MAP } = await import('@/triggers/monday/utils') |
| 100 | const eventType = MONDAY_EVENT_TYPE_MAP[triggerId] |
| 101 | if (!eventType) { |
| 102 | logger.warn(`[${ctx.requestId}] Unknown Monday trigger ID: ${triggerId}`) |
| 103 | throw new Error(`Unknown Monday.com trigger type: ${triggerId}`) |
| 104 | } |
| 105 | |
| 106 | const accessToken = await resolveAccessToken(config, ctx.userId, ctx.requestId) |
| 107 | const notificationUrl = getNotificationUrl(ctx.webhook) |
| 108 | |
| 109 | try { |
| 110 | const response = await fetch(MONDAY_API_URL, { |
| 111 | method: 'POST', |
| 112 | headers: { |
| 113 | 'Content-Type': 'application/json', |
| 114 | 'API-Version': '2024-10', |
| 115 | Authorization: accessToken, |
| 116 | }, |
| 117 | body: JSON.stringify({ |
| 118 | query: `mutation { create_webhook(board_id: ${boardIdValidation.sanitized}, url: ${JSON.stringify(notificationUrl)}, event: ${eventType}) { id board_id } }`, |
| 119 | }), |
| 120 | }) |
| 121 | |
| 122 | if (!response.ok) { |
| 123 | throw new Error( |
| 124 | `Monday.com API returned HTTP ${response.status}. Please verify your account connection and try again.` |
| 125 | ) |
| 126 | } |
| 127 | |
| 128 | const data = await response.json() |
| 129 | const errors = data.errors as Array<{ message: string }> | undefined |
| 130 | |
| 131 | if (errors && errors.length > 0) { |
| 132 | const errorMsg = errors.map((e) => e.message).join(', ') |
| 133 | logger.error(`[${ctx.requestId}] Failed to create Monday webhook`, { |
| 134 | errors: errorMsg, |
nothing calls this directly
no test coverage detected