chore(webhooks): getStreamWebhooks function refactor
This commit is contained in:
@@ -28,3 +28,7 @@ export type UpdateWebhook = ({
|
||||
}) => Promise<string>
|
||||
|
||||
export type DeleteWebhook = ({ id }: Pick<Webhook, 'id'>) => Promise<number>
|
||||
|
||||
export type GetStreamWebhooks = ({
|
||||
streamId
|
||||
}: Pick<Webhook, 'streamId'>) => Promise<Webhook[]>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Resolvers } from '@/modules/core/graph/generated/graphql'
|
||||
import { Resolvers, Webhook } from '@/modules/core/graph/generated/graphql'
|
||||
import { authorizeResolver } from '@/modules/shared'
|
||||
import {
|
||||
createWebhook,
|
||||
@@ -10,13 +10,43 @@ import {
|
||||
countWebhooksByStreamIdFactory,
|
||||
createWebhookFactory,
|
||||
deleteWebhookFactory,
|
||||
getStreamWebhooksFactory,
|
||||
getWebhookByIdFactory,
|
||||
updateWebhookFactory
|
||||
} from '@/modules/webhooks/repositories/webhooks'
|
||||
import { db } from '@/db/knex'
|
||||
import { ForbiddenError } from '@/modules/shared/errors'
|
||||
import { TokenResourceIdentifier } from '@/modules/core/domain/tokens/types'
|
||||
|
||||
const streamWebhooksResolver = async (
|
||||
parent: { id: string },
|
||||
args: { id?: string },
|
||||
context: { resourceAccessRules?: TokenResourceIdentifier[] | null; userId: string }
|
||||
) => {
|
||||
await authorizeResolver(
|
||||
context.userId,
|
||||
parent.id,
|
||||
Roles.Stream.Owner,
|
||||
context.resourceAccessRules
|
||||
)
|
||||
|
||||
if (args.id) {
|
||||
const wh = (await getWebhookByIdFactory({ db })({ id: args.id })) as Webhook | null
|
||||
const items = wh ? [wh] : []
|
||||
return { items, totalCount: items.length }
|
||||
}
|
||||
|
||||
const items = await getStreamWebhooksFactory({ db })({ streamId: parent.id })
|
||||
return { items, totalCount: items.length }
|
||||
}
|
||||
|
||||
export = {
|
||||
Stream: {
|
||||
webhooks: streamWebhooksResolver
|
||||
},
|
||||
Project: {
|
||||
webhooks: streamWebhooksResolver
|
||||
},
|
||||
Mutation: {
|
||||
webhookCreate: async (_parent, args, context) => {
|
||||
await authorizeResolver(
|
||||
|
||||
@@ -1,40 +1,9 @@
|
||||
const { authorizeResolver } = require('@/modules/shared')
|
||||
const {
|
||||
getStreamWebhooks,
|
||||
getLastWebhookEvents,
|
||||
getWebhookEventsCount
|
||||
} = require('../../services/webhooks')
|
||||
const { Roles } = require('@speckle/shared')
|
||||
const { getWebhookByIdFactory } = require('../../repositories/webhooks')
|
||||
const { db } = require('@/db/knex')
|
||||
|
||||
const streamWebhooksResolver = async (parent, args, context) => {
|
||||
await authorizeResolver(
|
||||
context.userId,
|
||||
parent.id,
|
||||
Roles.Stream.Owner,
|
||||
context.resourceAccessRules
|
||||
)
|
||||
|
||||
if (args.id) {
|
||||
const wh = await getWebhookByIdFactory({ db })({ id: args.id })
|
||||
const items = wh ? [wh] : []
|
||||
return { items, totalCount: items.length }
|
||||
}
|
||||
|
||||
const items = await getStreamWebhooks({ streamId: parent.id })
|
||||
return { items, totalCount: items.length }
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Stream: {
|
||||
webhooks: streamWebhooksResolver
|
||||
},
|
||||
|
||||
Project: {
|
||||
webhooks: streamWebhooksResolver
|
||||
},
|
||||
|
||||
Webhook: {
|
||||
projectId: (parent) => parent.streamId,
|
||||
hasSecret: (parent) => !!parent.secret?.length,
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
CountWebhooksByStreamId,
|
||||
CreateWebhook,
|
||||
DeleteWebhook,
|
||||
GetStreamWebhooks,
|
||||
GetWebhookById,
|
||||
UpdateWebhook
|
||||
} from '@/modules/webhooks/domain/operations'
|
||||
@@ -79,3 +80,17 @@ export const deleteWebhookFactory =
|
||||
async ({ id }) => {
|
||||
return await tables(db).webhooksConfigs.where({ id }).del()
|
||||
}
|
||||
|
||||
export const getStreamWebhooksFactory =
|
||||
({ db }: { db: Knex }): GetStreamWebhooks =>
|
||||
async ({ streamId }) => {
|
||||
const webhooks = await tables(db)
|
||||
.webhooksConfigs.select('*')
|
||||
.where({ streamId })
|
||||
.orderBy('updatedAt', 'desc')
|
||||
|
||||
return webhooks.map((webhook) => ({
|
||||
...webhook,
|
||||
triggers: toTriggersArray(webhook.triggers)
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -4,26 +4,12 @@ const knex = require('@/db/knex')
|
||||
const { getStream } = require('@/modules/core/repositories/streams')
|
||||
const crs = require('crypto-random-string')
|
||||
|
||||
const WebhooksConfig = () => knex('webhooks_config')
|
||||
const WebhooksEvents = () => knex('webhooks_events')
|
||||
const Users = () => knex('users')
|
||||
|
||||
const { getServerInfo } = require('../../core/services/generic')
|
||||
|
||||
module.exports = {
|
||||
async getStreamWebhooks({ streamId }) {
|
||||
const webhooks = await WebhooksConfig()
|
||||
.select('*')
|
||||
.where({ streamId })
|
||||
.orderBy('updatedAt', 'desc')
|
||||
|
||||
for (const webhook of webhooks) {
|
||||
webhook.triggers = Object.keys(webhook.triggers)
|
||||
}
|
||||
|
||||
return webhooks
|
||||
},
|
||||
|
||||
async dispatchStreamEvent({ streamId, event, eventPayload }, { trx } = {}) {
|
||||
// Add server info
|
||||
eventPayload.server = await getServerInfo()
|
||||
|
||||
@@ -9,11 +9,7 @@ const {
|
||||
} = require('@/test/hooks')
|
||||
const { noErrors } = require('@/test/helpers')
|
||||
const { createPersonalAccessToken } = require('../../core/services/tokens')
|
||||
const {
|
||||
getStreamWebhooks,
|
||||
getLastWebhookEvents,
|
||||
dispatchStreamEvent
|
||||
} = require('../services/webhooks')
|
||||
const { getLastWebhookEvents, dispatchStreamEvent } = require('../services/webhooks')
|
||||
const { createUser } = require('../../core/services/users')
|
||||
const { createStream, grantPermissionsStream } = require('../../core/services/streams')
|
||||
const { Scopes, Roles } = require('@speckle/shared')
|
||||
@@ -22,7 +18,8 @@ const {
|
||||
countWebhooksByStreamIdFactory,
|
||||
getWebhookByIdFactory,
|
||||
updateWebhookFactory,
|
||||
deleteWebhookFactory
|
||||
deleteWebhookFactory,
|
||||
getStreamWebhooksFactory
|
||||
} = require('@/modules/webhooks/repositories/webhooks')
|
||||
const { db } = require('@/db/knex')
|
||||
const {
|
||||
@@ -35,6 +32,7 @@ const { Users, Streams } = require('@/modules/core/dbSchema')
|
||||
const updateWebhook = updateWebhookService({
|
||||
updateWebhookConfig: updateWebhookFactory({ db })
|
||||
})
|
||||
const getStreamWebhooks = getStreamWebhooksFactory({ db })
|
||||
|
||||
describe('Webhooks @webhooks', () => {
|
||||
const getWebhook = getWebhookByIdFactory({ db })
|
||||
|
||||
Reference in New Issue
Block a user