Skip to content

Conversation

@gauravks13
Copy link
Collaborator

extension billing library webhook support

@@ -0,0 +1,25 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this file.

}

const webhookHandler = () => new WebhookHandler(SubscriptionModel);
const webhookHandler = (model) => new WebhookHandler(model);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move constructor to setupBilling function itself. We don't want to create singleton instance on memory directly. Export class itself instead.

index.js Outdated

const models = require("./models")(config.db_connection, config.collection_name, config.orm_type);
const { getActivePlans, subscribePlan, getActiveSubscription, updateSubscriptionStatus } = require("./controllers/subscription.helper")(config, models);
const { webhookHandler } = require("./handlers/webhook-handler");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import webhook handler class here and create instance here only.

index.js Outdated
getActiveSubscription,
updateSubscriptionStatus
updateSubscriptionStatus,
webhookHandler: webhookHandler(models.subscriptionModel)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass all models here instead of subscription model alone. In future multiple db models update will be handled on webhook handlers. Change accordingly on each handler as well.

@@ -0,0 +1,32 @@
module.exports = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name fixture file to whichever model fixture is here. Payload is too generic to understanding which db collection fixture it is written here.

success = true;
message = "Subscription is cancelled by user";
}
return {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

webhook handler will not return anything. remove return from here.


payloadFixture.status = "declined";

const data = await this.fdk_billing_instance.webhookHandler.handleExtensionSubscriptionUpdate("extension/extension-subscription", payloadFixture, subscriptionFixture.company_id);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verification data should be fetched from database post webhook handler is executed. Since webhook handler will update db records. Fetch data from db instead of this. Update for the same on all test cases.


const data = await this.fdk_billing_instance.webhookHandler.handleExtensionSubscriptionUpdate("extension/extension-subscription", payloadFixture, subscriptionFixture.company_id);

expect(data.success).toBeFalse()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add status check on each test cases. even for success false check status is unchanged.

const sellerSubscription = await this.model.getSubscriptionByPlatformId(payload._id, companyId);
const existingSubscription = await this.model.getActiveSubscription(companyId);
if (!sellerSubscription) {
return {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw exception here if subscription is not found. there will be no return value on webhook handlers.

const { clearData } = require("../../helpers/setup_db");
const subscriptionFixture = require("../../fixtures/subscription");
const ObjectId = require("mongoose").Types.ObjectId;
const payloadFixture = require("../../fixtures/payload");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename fixture variable name here also

'use strict';
class WebhookHandler {
constructor(models) {
this.model = models.subscriptionModel;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assign models object itself here. and use this.models.subscriptionModel every place in this file.

if (existingSubscription) {
await this.model.cancelSubscription(existingSubscription.id);
}
success = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lose these variables if not using it anymore later.

index.js Outdated
const models = require("./models")(config.db_connection, config.collection_name, config.orm_type);
const { getActivePlans, subscribePlan, getActiveSubscription, updateSubscriptionStatus } = require("./controllers/subscription.helper")(config, models);
const { WebhookHandler } = require("./handlers/webhook-handler");
const webhookHandler = (models) => new WebhookHandler(models);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we need function here? why can't we just create new instance directly? Think properly before adding extra unnecessary steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants