chore(webhooks): refactor last functions for multi region
This commit is contained in:
Родитель
11e7249191
Коммит
440ac2fa49
|
@ -2,7 +2,7 @@
|
|||
|
||||
const knex = require('@/db/knex')
|
||||
|
||||
const { dispatchStreamEvent } = require('../../webhooks/services/webhooks-new')
|
||||
const { dispatchStreamEvent } = require('../../webhooks/services/webhooks')
|
||||
const { getStream } = require('@/modules/core/repositories/streams')
|
||||
const {
|
||||
createWebhookEventFactory
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { Webhook } from '@/modules/webhooks/domain/types'
|
||||
import { WebhookEvent } from '@/test/graphql/generated/graphql'
|
||||
import { Webhook, WebhookEvent } from '@/modules/webhooks/domain/types'
|
||||
|
||||
export type CreateWebhook = (
|
||||
webhook: Pick<
|
||||
|
@ -37,3 +36,17 @@ export type GetStreamWebhooks = ({
|
|||
export type CreateWebhookEvent = (
|
||||
event: Pick<WebhookEvent, 'id' | 'payload' | 'webhookId'>
|
||||
) => Promise<string>
|
||||
|
||||
export type GetLastWebhookEvents = ({
|
||||
webhookId,
|
||||
limit
|
||||
}: {
|
||||
webhookId: string
|
||||
limit?: number
|
||||
}) => Promise<WebhookEvent[]>
|
||||
|
||||
export type GetWebhookEventsCount = ({
|
||||
webhookId
|
||||
}: {
|
||||
webhookId: string
|
||||
}) => Promise<number>
|
||||
|
|
|
@ -9,3 +9,13 @@ export type Webhook = {
|
|||
createdAt: Date
|
||||
updatedAt: Date
|
||||
}
|
||||
|
||||
export type WebhookEvent = {
|
||||
id: string
|
||||
webhookId: string
|
||||
status: number
|
||||
statusInfo: string
|
||||
retryCount: number
|
||||
lastUpdate: Date
|
||||
payload: string
|
||||
}
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
const {
|
||||
getLastWebhookEvents,
|
||||
getWebhookEventsCount
|
||||
} = require('../../services/webhooks')
|
||||
|
||||
module.exports = {
|
||||
Webhook: {
|
||||
async history(parent, args) {
|
||||
const items = await getLastWebhookEvents({
|
||||
webhookId: parent.id,
|
||||
limit: args.limit
|
||||
})
|
||||
const totalCount = await getWebhookEventsCount({ webhookId: parent.id })
|
||||
|
||||
return { items, totalCount }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,14 +4,16 @@ import {
|
|||
createWebhook,
|
||||
deleteWebhook,
|
||||
updateWebhook
|
||||
} from '@/modules/webhooks/services/webhooks-new'
|
||||
} from '@/modules/webhooks/services/webhooks'
|
||||
import { Roles } from '@speckle/shared'
|
||||
import {
|
||||
countWebhooksByStreamIdFactory,
|
||||
createWebhookFactory,
|
||||
deleteWebhookFactory,
|
||||
getLastWebhookEventsFactory,
|
||||
getStreamWebhooksFactory,
|
||||
getWebhookByIdFactory,
|
||||
getWebhookEventsCountFactory,
|
||||
updateWebhookFactory
|
||||
} from '@/modules/webhooks/repositories/webhooks'
|
||||
import { db } from '@/db/knex'
|
||||
|
@ -43,7 +45,18 @@ const streamWebhooksResolver = async (
|
|||
export = {
|
||||
Webhook: {
|
||||
projectId: (parent) => parent.streamId,
|
||||
hasSecret: (parent) => !!parent.secret?.length
|
||||
hasSecret: (parent) => !!parent.secret?.length,
|
||||
history: async (parent, args) => {
|
||||
const items = await getLastWebhookEventsFactory({ db })({
|
||||
webhookId: parent.id,
|
||||
limit: args.limit
|
||||
})
|
||||
const totalCount = await getWebhookEventsCountFactory({ db })({
|
||||
webhookId: parent.id
|
||||
})
|
||||
|
||||
return { items, totalCount }
|
||||
}
|
||||
},
|
||||
Stream: {
|
||||
webhooks: streamWebhooksResolver
|
|
@ -1,12 +1,14 @@
|
|||
import { Knex } from 'knex'
|
||||
import { Webhook } from '@/modules/webhooks/domain/types'
|
||||
import { Webhook, WebhookEvent } from '@/modules/webhooks/domain/types'
|
||||
import {
|
||||
CountWebhooksByStreamId,
|
||||
CreateWebhook,
|
||||
CreateWebhookEvent,
|
||||
DeleteWebhook,
|
||||
GetLastWebhookEvents,
|
||||
GetStreamWebhooks,
|
||||
GetWebhookById,
|
||||
GetWebhookEventsCount,
|
||||
UpdateWebhook
|
||||
} from '@/modules/webhooks/domain/operations'
|
||||
|
||||
|
@ -14,7 +16,7 @@ type WebhookConfig = Omit<Webhook, 'triggers'> & { triggers: Record<string, true
|
|||
|
||||
const tables = (db: Knex) => ({
|
||||
webhooksConfigs: db<WebhookConfig>('webhooks_config'),
|
||||
webhooksEvents: db('webhooks_events')
|
||||
webhooksEvents: db<WebhookEvent>('webhooks_events')
|
||||
})
|
||||
|
||||
const toTriggersObj = (triggers: string[]): Record<string, true> =>
|
||||
|
@ -101,3 +103,25 @@ export const createWebhookEventFactory =
|
|||
async (event) => {
|
||||
return await tables(db).webhooksEvents.insert(event).returning('id')
|
||||
}
|
||||
|
||||
export const getLastWebhookEventsFactory =
|
||||
({ db }: { db: Knex }): GetLastWebhookEvents =>
|
||||
async ({ webhookId, limit }) => {
|
||||
if (!limit) {
|
||||
limit = 100
|
||||
}
|
||||
|
||||
return await tables(db)
|
||||
.webhooksEvents.select('*')
|
||||
.where({ webhookId })
|
||||
.orderBy('lastUpdate', 'desc')
|
||||
.limit(limit)
|
||||
}
|
||||
|
||||
export const getWebhookEventsCountFactory =
|
||||
({ db }: { db: Knex }): GetWebhookEventsCount =>
|
||||
async ({ webhookId }) => {
|
||||
const [res] = await tables(db).webhooksEvents.count().where({ webhookId })
|
||||
|
||||
return parseInt(res.count.toString())
|
||||
}
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
const knex = require('@/db/knex')
|
||||
|
||||
const WebhooksEvents = () => knex('webhooks_events')
|
||||
|
||||
module.exports = {
|
||||
async getLastWebhookEvents({ webhookId, limit }) {
|
||||
if (!limit) {
|
||||
limit = 100
|
||||
}
|
||||
|
||||
return await WebhooksEvents()
|
||||
.select('*')
|
||||
.where({ webhookId })
|
||||
.orderBy('lastUpdate', 'desc')
|
||||
.limit(limit)
|
||||
},
|
||||
|
||||
async getWebhookEventsCount({ webhookId }) {
|
||||
const [res] = await WebhooksEvents().count().where({ webhookId })
|
||||
|
||||
return parseInt(res.count)
|
||||
}
|
||||
}
|
|
@ -9,7 +9,6 @@ const {
|
|||
} = require('@/test/hooks')
|
||||
const { noErrors } = require('@/test/helpers')
|
||||
const { createPersonalAccessToken } = require('../../core/services/tokens')
|
||||
const { getLastWebhookEvents } = require('../services/webhooks')
|
||||
const { createUser } = require('../../core/services/users')
|
||||
const { createStream, grantPermissionsStream } = require('../../core/services/streams')
|
||||
const { Scopes, Roles } = require('@speckle/shared')
|
||||
|
@ -20,7 +19,8 @@ const {
|
|||
updateWebhookFactory,
|
||||
deleteWebhookFactory,
|
||||
getStreamWebhooksFactory,
|
||||
createWebhookEventFactory
|
||||
createWebhookEventFactory,
|
||||
getLastWebhookEventsFactory
|
||||
} = require('@/modules/webhooks/repositories/webhooks')
|
||||
const { db } = require('@/db/knex')
|
||||
const {
|
||||
|
@ -28,7 +28,7 @@ const {
|
|||
updateWebhook: updateWebhookService,
|
||||
deleteWebhook,
|
||||
dispatchStreamEvent
|
||||
} = require('@/modules/webhooks/services/webhooks-new')
|
||||
} = require('@/modules/webhooks/services/webhooks')
|
||||
const { Users, Streams } = require('@/modules/core/dbSchema')
|
||||
const { getServerInfo } = require('@/modules/core/services/generic')
|
||||
const { getStream } = require('@/modules/core/repositories/streams')
|
||||
|
@ -207,7 +207,7 @@ describe('Webhooks @webhooks', () => {
|
|||
event: 'commit_create',
|
||||
eventPayload: { test: 'payload123' }
|
||||
})
|
||||
const lastEvents = await getLastWebhookEvents({ webhookId })
|
||||
const lastEvents = await getLastWebhookEventsFactory({ db })({ webhookId })
|
||||
expect(lastEvents).to.have.lengthOf(1)
|
||||
expect(JSON.parse(lastEvents[0].payload).test).to.equal('payload123')
|
||||
})
|
||||
|
|
Загрузка…
Ссылка в новой задаче