Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: make Webhook event type an enum #3021

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ post {
body:json {
{
"id": "{{uuid}}",
"type": "asset.liquidity_low",
"type": "ASSET_LIQUIDITY_LOW",
"data": {
"id": "{{assetId}}",
"asset": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ post {
body:json {
{
"id": "{{uuid}}",
"type": "incoming_payment.completed",
"type": "INCOMING_PAYMENT_COMPLETED",
"data": {
"id": "0141ee62-1c0f-4a57-9231-9e515a7cdffb",
"walletAddressId": "{{gfranklinWalletAddressId}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ post {
body:json {
{
"id": "{{uuid}}",
"type": "incoming_payment.created",
"type": "INCOMING_PAYMENT_CREATED",
"data": {
"id": "0141ee62-1c0f-4a57-9231-9e515a7cdffb",
"walletAddressId": "{{gfranklinWalletAddressId}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ post {
body:json {
{
"id": "{{uuid}}",
"type": "incoming_payment.expired",
"type": "INCOMING_PAYMENT_EXPIRED",
"data": {
"id": "0141ee62-1c0f-4a57-9231-9e515a7cdffb",
"walletAddressId": "{{gfranklinWalletAddressId}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ post {
body:json {
{
"id": "{{uuid}}",
"type": "outgoing_payment.created",
"type": "OUTGOING_PAYMENT_CREATED",
"data": {
"id": "2de34961-9bb8-404e-866a-288307114c74",
"walletAddressId": "{{gfranklinWalletAddressId}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ post {
body:json {
{
"id": "{{uuid}}",
"type": "outgoing_payment.completed",
"type": "OUTGOING_PAYMENT_COMPLETED",
"data": {
"id": "2de34961-9bb8-404e-866a-288307114c74",
"walletAddressId": "{{gfranklinWalletAddressId}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ post {
body:json {
{
"id": "{{uuid}}",
"type": "outgoing_payment.failed",
"type": "OUTGOING_PAYMENT_FAILED",
"data": {
"id": "2de34961-9bb8-404e-866a-288307114c74",
"walletAddressId": "{{gfranklinWalletAddressId}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ post {
body:json {
{
"id": "{{uuid}}",
"type": "peer.liquidity_low",
"type": "PEER_LIQUIDITY_LOW",
"data": {
"id": "{{peerId}}",
"asset": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ post {
body:json {
{
"id": "{{uuid}}",
"type":"wallet_address.not_found",
"type":"WALLET_ADDRESS_NOT_FOUND",
"data": {
"walletAddressUrl": "https://cloud-nine-wallet-backend/accounts/ya"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ post {
body:json {
{
"id": "{{uuid}}",
"type": "wallet_address.web_monetization",
"type": "WALLET_ADDRESS_WEB_MONETIZATION",
"data": {
"walletAddress": {
"id": "{{gfranklinWalletAddressId}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export async function handleLowLiquidity(wh: Webhook) {
throw new Error('id not found')
}

if (wh.type == 'asset.liquidity_low') {
if (wh.type == WebhookEventType.ASSET_LIQUIDITY_LOW) {
await depositAssetLiquidity(id, 1000000, uuid())
} else {
await depositPeerLiquidity(id, '1000000', uuid())
Expand Down
28 changes: 26 additions & 2 deletions localenv/mock-account-servicing-entity/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex
.raw(
'ALTER TABLE "webhookEvents" DROP CONSTRAINT IF EXISTS "webhookevents_related_resource_constraint"'
)
.then(() => {
// Update webhook event type to the enum standard
return knex.table('webhookEvents').update({
type: knex.raw(`REPLACE(UPPER(type), '.', '_')`)
})
})
.then(() => {
// Update the constraint webhookevents_related_resource_constraint to match the enum change
return knex.table('webhookEvents', function (table) {
table.check(
` (CASE WHEN type != 'WALLET_ADDRESS_NOT_FOUND' THEN
(
("outgoingPaymentId" IS NOT NULL)::int +
("incomingPaymentId" IS NOT NULL)::int +
("walletAddressId" IS NOT NULL)::int +
("peerId" IS NOT NULL)::int +
("assetId" IS NOT NULL)::int
) = 1
ELSE
(
("outgoingPaymentId" IS NOT NULL)::int +
("incomingPaymentId" IS NOT NULL)::int +
("walletAddressId" IS NOT NULL)::int +
("peerId" IS NOT NULL)::int +
("assetId" IS NOT NULL)::int
) = 0
END)
`,
null,
'webhookevents_related_resource_constraint'
)
})
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex
.raw(
'ALTER TABLE "webhookEvents" DROP CONSTRAINT IF EXISTS "webhookevents_related_resource_constraint"'
)
.then(() => {
return knex.table('webhookEvents').update({
type: knex.raw(`LOWER(type)`)
})
})
.then(() => {
return knex.raw(`UPDATE "webhookEvents" SET type = (
CASE
WHEN type LIKE '%payment%' THEN REPLACE(type, 'payment_', 'payment.')
WHEN type LIKE 'wallet_address_%' THEN REPLACE(type, 'address_', 'address.')
WHEN type LIKE '%liquidity%' THEN REPLACE(type, '_liquidity', '.liquidity')
END
)`)
})
.then(() => {
return knex.schema.table('webhookEvents', function (table) {
table.check(
` (CASE WHEN type != 'wallet_address.not_found' THEN
(
("outgoingPaymentId" IS NOT NULL)::int +
("incomingPaymentId" IS NOT NULL)::int +
("walletAddressId" IS NOT NULL)::int +
("peerId" IS NOT NULL)::int +
("assetId" IS NOT NULL)::int
) = 1
ELSE
(
("outgoingPaymentId" IS NOT NULL)::int +
("incomingPaymentId" IS NOT NULL)::int +
("walletAddressId" IS NOT NULL)::int +
("peerId" IS NOT NULL)::int +
("assetId" IS NOT NULL)::int
) = 0
END)
`,
null,
'webhookevents_related_resource_constraint'
)
})
})
}
12 changes: 8 additions & 4 deletions packages/backend/src/asset/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { randomAsset } from '../tests/asset'
import { truncateTables } from '../tests/tableManager'
import { Asset, AssetEvent, AssetEventError, AssetEventType } from './model'
import { isAssetError } from './errors'
import { WebhookEventType } from '../webhook/model'

describe('Models', (): void => {
let deps: IocContract<AppServices>
Expand Down Expand Up @@ -57,11 +58,11 @@ describe('Models', (): void => {
const event = (
await AssetEvent.query(knex).where(
'type',
AssetEventType.LiquidityLow
WebhookEventType.AssetLiquidityLow
)
)[0]
expect(event).toMatchObject({
type: AssetEventType.LiquidityLow,
type: WebhookEventType.AssetLiquidityLow,
data: {
id: asset.id,
asset: {
Expand All @@ -78,7 +79,10 @@ describe('Models', (): void => {
test('does not create webhook event if balance > liquidityThreshold', async (): Promise<void> => {
await asset.onDebit({ balance: BigInt(110) })
await expect(
AssetEvent.query(knex).where('type', AssetEventType.LiquidityLow)
AssetEvent.query(knex).where(
'type',
WebhookEventType.AssetLiquidityLow
)
).resolves.toEqual([])
})
})
Expand All @@ -94,7 +98,7 @@ describe('Models', (): void => {
)('Asset Id is required', async ({ type, error }): Promise<void> => {
expect(
AssetEvent.query().insert({
type
type: type as AssetEventType
})
).rejects.toThrow(error)
})
Expand Down
12 changes: 7 additions & 5 deletions packages/backend/src/asset/model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryContext } from 'objection'
import { LiquidityAccount, OnDebitOptions } from '../accounting/service'
import { BaseModel } from '../shared/baseModel'
import { WebhookEvent } from '../webhook/model'
import { WebhookEvent, WebhookEventType } from '../webhook/model'

export class Asset extends BaseModel implements LiquidityAccount {
public static get tableName(): string {
Expand Down Expand Up @@ -33,7 +33,7 @@ export class Asset extends BaseModel implements LiquidityAccount {
if (balance <= this.liquidityThreshold) {
await AssetEvent.query().insert({
assetId: this.id,
type: AssetEventType.LiquidityLow,
type: WebhookEventType.AssetLiquidityLow,
data: {
id: this.id,
asset: {
Expand All @@ -51,9 +51,11 @@ export class Asset extends BaseModel implements LiquidityAccount {
}
}

export enum AssetEventType {
LiquidityLow = 'asset.liquidity_low'
}
export type AssetEventType = Extract<
WebhookEventType,
WebhookEventType.AssetLiquidityLow
>
export const AssetEventType = [WebhookEventType.AssetLiquidityLow]

export type AssetEventData = {
id: string
Expand Down
Loading
Loading