Files
speckle-server/packages/server/modules/webhooks/services/webhooks-new.ts
T
Alessandro Magionami 73e28d72c6 Alessandro/web 957 refactor webhooks module (#2664)
* chore(webhooks): fix test failing when running alone

* chore(webhooks): create domain types and repository

* chore(webhooks): create webhook refactor multiregion

* chore(webhooks): triggers field is required in graphql schema
2024-08-19 09:23:27 +02:00

45 lines
1.2 KiB
TypeScript

import {
CountWebhooksByStreamId,
CreateWebhook
} from '@/modules/webhooks/domain/operations'
import { Webhook } from '@/modules/webhooks/domain/types'
import { SetValuesNullable } from '@speckle/shared'
import crs from 'crypto-random-string'
const MAX_STREAM_WEBHOOKS = 100
export const createWebhook =
({
createWebhookConfig,
countWebhooksByStreamId
}: {
createWebhookConfig: CreateWebhook
countWebhooksByStreamId: CountWebhooksByStreamId
}) =>
async ({
streamId,
url,
description,
secret,
enabled,
triggers
}: Pick<Webhook, 'streamId' | 'enabled' | 'triggers'> &
Partial<SetValuesNullable<Pick<Webhook, 'url' | 'description' | 'secret'>>>) => {
const streamWebhookCount = await countWebhooksByStreamId({ streamId })
if (streamWebhookCount >= MAX_STREAM_WEBHOOKS) {
throw new Error(
`Maximum number of webhooks for a stream reached (${MAX_STREAM_WEBHOOKS})`
)
}
return await createWebhookConfig({
id: crs({ length: 10 }),
streamId,
url: url ?? undefined,
description: description ?? undefined,
secret: secret ?? undefined,
enabled,
triggers
})
}