Skip to content

Commit 5f98219

Browse files
feat(condo): DOMA-13327 webhook after subscription activation (#7699)
* feat(condo): DOMA-13327 added webhook after subscription activation * feat(condo): DOMA-13327 fix env names * feat(condo): DOMA-13327 fixes after review * feat(condo): DOMA-13327 make types feat(condo): DOMA-13327 added tests feat(condo): DOMA-13327 added tests feat(condo): DOMA-13327 added tests
1 parent 03c2121 commit 5f98219

5 files changed

Lines changed: 52 additions & 4 deletions

File tree

.helm

Submodule .helm updated from 3deb512 to 3f3dd3b
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const WEBHOOK_EVENT_PAYMENT_STATUS_UPDATED = 'payment.status.updated'
2+
const WEBHOOK_EVENT_SUBSCRIPTION_ACTIVATED = 'subscription.activated'
23

34
const WEBHOOK_EVENTS = [
45
WEBHOOK_EVENT_PAYMENT_STATUS_UPDATED,
6+
WEBHOOK_EVENT_SUBSCRIPTION_ACTIVATED,
57
]
68

79
module.exports = {
810
WEBHOOK_EVENT_PAYMENT_STATUS_UPDATED,
11+
WEBHOOK_EVENT_SUBSCRIPTION_ACTIVATED,
912
WEBHOOK_EVENTS,
1013
}

apps/condo/domains/subscription/schema/ActivateSubscriptionContextService.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
* Generated by `createservice subscription.ActivateSubscriptionContextService --type mutations`
33
*/
44

5+
const conf = require('@open-condo/config')
56
const { GQLError, GQLErrorCode: { BAD_USER_INPUT } } = require('@open-condo/keystone/errors')
67
const { getLogger } = require('@open-condo/keystone/logging')
7-
const { GQLCustomSchema, find, getById, itemsQuery } = require('@open-condo/keystone/schema')
8+
const { GQLCustomSchema, find, getById, getSchemaCtx, itemsQuery } = require('@open-condo/keystone/schema')
9+
const { queueWebhookPayload } = require('@open-condo/webhooks/utils/queueWebhookPayload')
810

911
const {
1012
PAYMENT_DONE_STATUS,
1113
} = require('@condo/domains/acquiring/constants/payment')
1214
const { freezePaymentInfo } = require('@condo/domains/acquiring/utils/billingFridge')
15+
const { WEBHOOK_EVENT_SUBSCRIPTION_ACTIVATED } = require('@condo/domains/common/constants/webhooks')
1316
const { INVOICE_STATUS_PAID } = require('@condo/domains/marketplace/constants')
1417
const access = require('@condo/domains/subscription/access/ActivateSubscriptionContextService')
1518
const { SUBSCRIPTION_CONTEXT_STATUS } = require('@condo/domains/subscription/constants')
@@ -170,6 +173,48 @@ const ActivateSubscriptionContextService = new GQLCustomSchema('ActivateSubscrip
170173
frozenPaymentInfo,
171174
})
172175

176+
const webhookUrl = conf['SUBSCRIPTION_ACTIVATED_WEBHOOK_URL']
177+
const webhookSecret = conf['SUBSCRIPTION_ACTIVATED_WEBHOOK_SECRET']
178+
if (webhookUrl && webhookSecret) {
179+
try {
180+
const { keystone: internalContext } = getSchemaCtx('WebhookPayload')
181+
const payerOrg = await getById('Organization', invoice.payerOrganization)
182+
if (!payerOrg) {
183+
throw new Error(`Organization not found: ${invoice.payerOrganization}`)
184+
}
185+
const createdByUser = invoice.createdBy
186+
? await getById('User', invoice.createdBy)
187+
: null
188+
await queueWebhookPayload(internalContext, {
189+
url: webhookUrl,
190+
secret: webhookSecret,
191+
eventType: WEBHOOK_EVENT_SUBSCRIPTION_ACTIVATED,
192+
modelName: 'SubscriptionContext',
193+
itemId: subscriptionContext.id,
194+
payload: {
195+
invoiceId: invoice.id,
196+
paidAt: invoice.paidAt,
197+
toPay: invoice.toPay,
198+
planName: invoice.rows?.[0]?.name,
199+
organization: {
200+
id: payerOrg.id,
201+
name: payerOrg.name,
202+
tin: payerOrg.tin,
203+
},
204+
...(createdByUser && {
205+
user: {
206+
id: createdByUser.id,
207+
name: createdByUser.name,
208+
},
209+
}),
210+
},
211+
sender: { dv: 1, fingerprint: 'activateSubscriptionContext' },
212+
})
213+
} catch (err) {
214+
logger.error({ msg: 'Failed to queue subscription webhook', err, data: { subscriptionContextId: subscriptionContext.id } })
215+
}
216+
}
217+
173218
const updatedSubscriptionContext = await getById('SubscriptionContext', subscriptionContext.id)
174219
logger.info({ msg: 'Subscription context activated successfully', data: { subscriptionContextId: subscriptionContext.id, status: updatedSubscriptionContext.status, bindingId: updatedSubscriptionContext.bindingId } })
175220

apps/condo/schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91911,7 +91911,7 @@ type WebhookPayload {
9191191911
"""
9191291912
secret: String
9191391913

91914-
""" Type of event that triggered this webhook. Custom events: payment.status.updated. Auto-generated events from models with webHooked() plugin: ModelName.created, ModelName.updated, ModelName.deleted (e.g., Payment.created, Ticket.updated).
91914+
""" Type of event that triggered this webhook. Custom events: payment.status.updated, subscription.activated. Auto-generated events from models with webHooked() plugin: ModelName.created, ModelName.updated, ModelName.deleted (e.g., Payment.created, Ticket.updated).
9191591915
"""
9191691916
eventType: String
9191791917

apps/condo/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120089,7 +120089,7 @@ export type WebhookPayload = {
120089120089
deletedAt?: Maybe<Scalars['String']['output']>;
120090120090
/** Data structure Version */
120091120091
dv?: Maybe<Scalars['Int']['output']>;
120092-
/** Type of event that triggered this webhook. Custom events: payment.status.updated. Auto-generated events from models with webHooked() plugin: ModelName.created, ModelName.updated, ModelName.deleted (e.g., Payment.created, Ticket.updated). */
120092+
/** Type of event that triggered this webhook. Custom events: payment.status.updated, subscription.activated. Auto-generated events from models with webHooked() plugin: ModelName.created, ModelName.updated, ModelName.deleted (e.g., Payment.created, Ticket.updated). */
120093120093
eventType?: Maybe<Scalars['String']['output']>;
120094120094
/** Timestamp after which no more send attempts will be made */
120095120095
expiresAt?: Maybe<Scalars['String']['output']>;

0 commit comments

Comments
 (0)