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
2 changes: 1 addition & 1 deletion .helm
Submodule .helm updated from 3deb51 to 3f3dd3
3 changes: 3 additions & 0 deletions apps/condo/domains/common/constants/webhooks.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const WEBHOOK_EVENT_PAYMENT_STATUS_UPDATED = 'payment.status.updated'
const WEBHOOK_EVENT_SUBSCRIPTION_ACTIVATED = 'subscription.activated'

const WEBHOOK_EVENTS = [
WEBHOOK_EVENT_PAYMENT_STATUS_UPDATED,
WEBHOOK_EVENT_SUBSCRIPTION_ACTIVATED,
]

module.exports = {
WEBHOOK_EVENT_PAYMENT_STATUS_UPDATED,
WEBHOOK_EVENT_SUBSCRIPTION_ACTIVATED,
WEBHOOK_EVENTS,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
* Generated by `createservice subscription.ActivateSubscriptionContextService --type mutations`
*/

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

const {
PAYMENT_DONE_STATUS,
} = require('@condo/domains/acquiring/constants/payment')
const { freezePaymentInfo } = require('@condo/domains/acquiring/utils/billingFridge')
const { WEBHOOK_EVENT_SUBSCRIPTION_ACTIVATED } = require('@condo/domains/common/constants/webhooks')
const { INVOICE_STATUS_PAID } = require('@condo/domains/marketplace/constants')
const access = require('@condo/domains/subscription/access/ActivateSubscriptionContextService')
const { SUBSCRIPTION_CONTEXT_STATUS } = require('@condo/domains/subscription/constants')
Expand Down Expand Up @@ -86,7 +89,7 @@
summary: 'Activates a subscription context with status CREATED. Checks that invoice is paid, retrieves payment method from multiPayment, and updates context status to DONE.',
errors: ERRORS,
},
resolver: async (parent, args, context) => {

Check failure on line 92 in apps/condo/domains/subscription/schema/ActivateSubscriptionContextService.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=open-condo-software_condo&issues=AZ6R8zRv-XsmwqxOWeTJ&open=AZ6R8zRv-XsmwqxOWeTJ&pullRequest=7699
const { data } = args
const { dv, sender, subscriptionContext: subscriptionContextInput } = data

Expand Down Expand Up @@ -170,6 +173,48 @@
frozenPaymentInfo,
})

const webhookUrl = conf['SUBSCRIPTION_ACTIVATED_WEBHOOK_URL']
const webhookSecret = conf['SUBSCRIPTION_ACTIVATED_WEBHOOK_SECRET']
if (webhookUrl && webhookSecret) {
try {
const { keystone: internalContext } = getSchemaCtx('WebhookPayload')
const payerOrg = await getById('Organization', invoice.payerOrganization)
if (!payerOrg) {
throw new Error(`Organization not found: ${invoice.payerOrganization}`)
}
const createdByUser = invoice.createdBy
? await getById('User', invoice.createdBy)
: null
await queueWebhookPayload(internalContext, {
url: webhookUrl,
secret: webhookSecret,
eventType: WEBHOOK_EVENT_SUBSCRIPTION_ACTIVATED,
modelName: 'SubscriptionContext',
itemId: subscriptionContext.id,
payload: {
invoiceId: invoice.id,
paidAt: invoice.paidAt,
toPay: invoice.toPay,
planName: invoice.rows?.[0]?.name,
organization: {
id: payerOrg.id,
name: payerOrg.name,
tin: payerOrg.tin,
},
...(createdByUser && {
user: {
id: createdByUser.id,
name: createdByUser.name,
},
}),
},
sender: { dv: 1, fingerprint: 'activateSubscriptionContext' },
})
} catch (err) {
logger.error({ msg: 'Failed to queue subscription webhook', err, data: { subscriptionContextId: subscriptionContext.id } })
}
}

const updatedSubscriptionContext = await getById('SubscriptionContext', subscriptionContext.id)
logger.info({ msg: 'Subscription context activated successfully', data: { subscriptionContextId: subscriptionContext.id, status: updatedSubscriptionContext.status, bindingId: updatedSubscriptionContext.bindingId } })

Expand Down
2 changes: 1 addition & 1 deletion apps/condo/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -91911,7 +91911,7 @@ type WebhookPayload {
"""
secret: String

""" 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).
""" 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).
"""
eventType: String

Expand Down
2 changes: 1 addition & 1 deletion apps/condo/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120089,7 +120089,7 @@ export type WebhookPayload = {
deletedAt?: Maybe<Scalars['String']['output']>;
/** Data structure Version */
dv?: Maybe<Scalars['Int']['output']>;
/** 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). */
/** 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). */
eventType?: Maybe<Scalars['String']['output']>;
/** Timestamp after which no more send attempts will be made */
expiresAt?: Maybe<Scalars['String']['output']>;
Expand Down
Loading