Skip to content

Commit

Permalink
feat(subscription-service): add billing functionality to subscription…
Browse files Browse the repository at this point in the history
… service

add billing functionality to subscription service

BREAKING CHANGE:
yes

gh-34
  • Loading branch information
Tyagi-Sunny committed Aug 22, 2024
1 parent 5e03dcb commit 0f32417
Show file tree
Hide file tree
Showing 36 changed files with 1,655 additions and 353 deletions.
495 changes: 211 additions & 284 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

let dbm;
let type;
let seed;
let fs = require('fs');
let path = require('path');
let Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function (options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function (db) {
let filePath = path.join(
__dirname,
'sqls',
'20240209122448-add-customer-table-up.sql',
);
return new Promise(function (resolve, reject) {
fs.readFile(filePath, {encoding: 'utf-8'}, function (err, data) {
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
}).then(function (data) {
return db.runSql(data);
});
};

exports.down = function (db) {
let filePath = path.join(
__dirname,
'sqls',
'20240209122448-add-customer-table-down.sql',
);
return new Promise(function (resolve, reject) {
fs.readFile(filePath, {encoding: 'utf-8'}, function (err, data) {
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
}).then(function (data) {
return db.runSql(data);
});
};

exports._meta = {
version: 1,
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ CREATE TABLE plans (
CONSTRAINT pk_plans_id PRIMARY KEY ( id )
);



CREATE TABLE subscriptions (
id uuid DEFAULT (md5(((random())::text || (clock_timestamp())::text)))::uuid NOT NULL ,
created_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
drop table main.billing_customer;
drop table main.invoice;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

CREATE TABLE main.billing_customer (
id uuid DEFAULT (md5(((random())::text || (clock_timestamp())::text)))::uuid NOT NULL,
tenant_id varchar(255) NOT NULL,
customer_id varchar(255) NOT NULL,
payment_source_id varchar(255),
created_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
modified_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
deleted boolean DEFAULT false NOT NULL,
deleted_on timestamptz,
deleted_by uuid,
created_by uuid NOT NULL,
modified_by uuid,
CONSTRAINT pk_billing_customer_id PRIMARY KEY (id),
CONSTRAINT uq_billing_customer_customer_id UNIQUE (customer_id)
);



CREATE TABLE main.invoice (
id UUID DEFAULT (md5(((random())::text || (clock_timestamp())::text)))::uuid NOT NULL,
invoice_id VARCHAR(255) NOT NULL,
invoice_status BOOLEAN,
billing_customer_id uuid NOT NULL,
-- subscription_id uuid,
created_on TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
modified_on TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
deleted BOOLEAN DEFAULT false NOT NULL,
deleted_on TIMESTAMPTZ,
deleted_by UUID,
created_by UUID NOT NULL,
modified_by UUID,
CONSTRAINT pk_invoice_id PRIMARY KEY (id),
CONSTRAINT fk_invoice_customer FOREIGN KEY (billing_customer_id) REFERENCES main.billing_customer(id)

-- CONSTRAINT fk_invoice_subscription FOREIGN KEY (subscription_id) REFERENCES main.subscriptions(id) -- Add this constraint
);

ALTER TABLE main.subscriptions
ADD COLUMN invoice_id uuid NOT NULL,
ADD CONSTRAINT fk_subscriptions_invoice FOREIGN KEY (invoice_id) REFERENCES main.invoice(id);
3 changes: 2 additions & 1 deletion services/subscription-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"@loopback/context": "^7.0.2",
"@loopback/core": "^6.0.2",
"@loopback/openapi-v3": "^10.0.2",
"@loopback/repository": "^7.0.2",
"@loopback/repository": "^7.0.4",
"@loopback/rest": "^14.0.2",
"@loopback/rest-explorer": "^7.0.2",
"@loopback/service-proxy": "^7.0.2",
Expand All @@ -84,6 +84,7 @@
"@types/jsonwebtoken": "^9.0.5",
"dotenv": "^16.0.3",
"dotenv-extended": "^2.9.0",
"local-billing": "file:local-billing-0.0.1.tgz",
"loopback-connector-postgresql": "^7.1.1",
"loopback4-authentication": "^12.0.2",
"loopback4-authorization": "^7.0.2",
Expand Down
98 changes: 35 additions & 63 deletions services/subscription-service/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,27 @@
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
import {
Binding,
Component,
ControllerClass,
CoreBindings,
inject,
ProviderMap,
ServiceOrProviderClass,
} from '@loopback/core';
import {Class, Model, Repository} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {
BearerVerifierBindings,
BearerVerifierComponent,
BearerVerifierConfig,
BearerVerifierType,
CoreComponent,
SECURITY_SCHEME_SPEC,
ServiceSequence,
} from '@sourceloop/core';
import {AuthenticationComponent} from 'loopback4-authentication';
import {
AuthorizationBindings,
AuthorizationComponent,
} from 'loopback4-authorization';
import {SubscriptionServiceBindings} from './keys';
import {ISubscriptionServiceConfig} from './types';
import {
BillingCycleRepository,
CurrencyRepository,
PlanRepository,
PlanSizesRepository,
ResourceRepository,
ServiceRepository,
SubscriptionRepository,
} from './repositories';
import {
BillinCycleController,
CurrencyController,
HomePageController,
PingController,
PlanController,
PlanFeaturesController,
PlanSizesController,
PlanSubscriptionController,
ResourceController,
ServiceController,
SubscriptionController,
} from './controllers';
import {
BillingCycle,
Currency,
Plan,
PlanSizes,
Resource,
Service,
Subscription,
} from './models';
import {
FeatureToggleBindings,
FeatureToggleServiceComponent,
} from '@sourceloop/feature-toggle-service';

import { inject, Binding } from "@loopback/context";
import { Component, CoreBindings, ProviderMap, ServiceOrProviderClass, ControllerClass } from "@loopback/core";
import { Class, Repository, Model } from "@loopback/repository";
import { RestApplication } from "@loopback/rest";
import { CoreComponent, SECURITY_SCHEME_SPEC, ServiceSequence, BearerVerifierBindings, BearerVerifierType, BearerVerifierConfig, BearerVerifierComponent } from "@sourceloop/core";
import { FeatureToggleBindings, FeatureToggleServiceComponent } from "@sourceloop/feature-toggle-service";
import { BillingComponent } from "local-billing";
import { AuthenticationComponent } from "loopback4-authentication";
import { AuthorizationBindings, AuthorizationComponent } from "loopback4-authorization";
import { BillinCycleController, HomePageController, PingController, CurrencyController, PlanController, ResourceController, ServiceController, SubscriptionController, PlanSubscriptionController, PlanSizesController, PlanFeaturesController } from "./controllers";
import { SubscriptionServiceBindings, SYSTEM_USER, WEBHOOK_VERIFIER } from "./keys";
import { BillingCycle, Currency, Plan, Resource, BillingCustomer, Invoice, Service, Subscription, PlanSizes } from "./models";
import { BillingCycleRepository, CurrencyRepository, PlanRepository, ResourceRepository, ServiceRepository, SubscriptionRepository, PlanSizesRepository, BillingCustomerRepository, InvoiceRepository } from "./repositories";
import { ISubscriptionServiceConfig } from "./types";
import { WebhookVerifierProvider } from "./interceptors/webhook-verifier.interceptor";
import { SystemUserProvider } from "./providers";
import { BillingCustomerController } from "./controllers/billing-customer.controller";
import { BillingInvoiceController } from "./controllers/billing-invoice.controller";
import { BillingPaymentSourceController } from "./controllers/billing-payment-source.controller";
import { WebhookController } from "./controllers/webhook.controller";

export class SubscriptionServiceComponent implements Component {
constructor(
Expand All @@ -82,6 +41,7 @@ export class SubscriptionServiceComponent implements Component {
.bind(FeatureToggleBindings.Config)
.to({bindControllers: true, useCustomSequence: true});
this.application.component(FeatureToggleServiceComponent);
this.application.component(BillingComponent);

this.application.api({
openapi: '3.0.0',
Expand All @@ -108,18 +68,26 @@ export class SubscriptionServiceComponent implements Component {
ResourceRepository,
ServiceRepository,
SubscriptionRepository,
PlanSizesRepository,
PlanSizesRepository,BillingCustomerRepository,
InvoiceRepository,
];

this.models = [
BillingCycle,
Currency,
Plan,
Resource,
BillingCustomer,
Invoice,
Service,
Subscription,
PlanSizes,
];
this.bindings = [
Binding.bind(WEBHOOK_VERIFIER).toProvider(WebhookVerifierProvider),

Binding.bind(SYSTEM_USER).toProvider(SystemUserProvider),
];

this.controllers = [
BillinCycleController,
Expand All @@ -133,6 +101,10 @@ export class SubscriptionServiceComponent implements Component {
PlanSubscriptionController,
PlanSizesController,
PlanFeaturesController,
BillingCustomerController,
BillingInvoiceController,
BillingPaymentSourceController,
WebhookController,
];
}

Expand Down
Loading

0 comments on commit 0f32417

Please sign in to comment.