Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions backend/src/api/webhooks/discourse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PlatformType } from '@crowd/types'
import IntegrationRepository from '../../database/repositories/integrationRepository'
import TenantRepository from '../../database/repositories/tenantRepository'
import SequelizeRepository from '../../database/repositories/sequelizeRepository'
import IncomingWebhookRepository from '../../database/repositories/incomingWebhookRepository'
import { WebhookType } from '../../types/webhooks'
Expand All @@ -15,13 +14,23 @@ export default async (req, res) => {
const event = req.headers['x-discourse-event']
const data = req.body

const options = await SequelizeRepository.getDefaultIRepositoryOptions()
const tenant = await TenantRepository.findById(req.params.tenantId, options)
const optionsWithTenant = await SequelizeRepository.getDefaultIRepositoryOptions(null, tenant)
const integration = (await IntegrationRepository.findByPlatform(
PlatformType.DISCOURSE,
optionsWithTenant,
)) as any
let integration

try {
integration = await IntegrationRepository.findActiveIntegrationByPlatform(
PlatformType.DISCOURSE,
req.params.tenantId,
)
} catch (error) {
req.log.error({ error }, 'Internal error when verifying Discourse webhook')
await req.responseHandler.success(
req,
res,
'Internal error when verifying Discourse webhook',
200,
)
return
}

if (integration) {
try {
Expand Down Expand Up @@ -83,7 +92,10 @@ export default async (req, res) => {

await req.responseHandler.success(req, res, {}, 204)
} else {
req.log.error({ tenant }, 'No integration found for incoming Discourse Webhook!')
req.log.error(
{ tenantId: req?.params?.tenantId },
'No integration found for incoming Discourse Webhook!',
)
await req.responseHandler.success(req, res, {}, 200)
}
}
19 changes: 18 additions & 1 deletion backend/src/database/repositories/integrationRepository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import lodash from 'lodash'
import Sequelize, { QueryTypes } from 'sequelize'
import { IntegrationRunState } from '@crowd/types'
import { IntegrationRunState, PlatformType } from '@crowd/types'
import SequelizeRepository from './sequelizeRepository'
import AuditLogRepository from './auditLogRepository'
import SequelizeFilterUtils from '../utils/sequelizeFilterUtils'
Expand Down Expand Up @@ -228,6 +228,23 @@ class IntegrationRepository {
return this._populateRelations(record)
}

static async findActiveIntegrationByPlatform(platform: PlatformType, tenantId: string) {
const options = await SequelizeRepository.getDefaultIRepositoryOptions()

const record = await options.database.integration.findOne({
where: {
platform,
tenantId,
},
})

if (!record) {
throw new Error404()
}

return this._populateRelations(record)
}

/**
* Find all active integrations for a platform
* @param platform The platform we want to find all active integrations for
Expand Down