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 3a7aa05 commit 9bd232b
Show file tree
Hide file tree
Showing 36 changed files with 1,661 additions and 302 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
53 changes: 41 additions & 12 deletions services/subscription-service/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,12 @@ import {
SECURITY_SCHEME_SPEC,
ServiceSequence,
} from '@sourceloop/core';
import {BillingComponent} from 'local-billing';
import {AuthenticationComponent} from 'loopback4-authentication';
import {
AuthorizationBindings,
AuthorizationComponent,
} from 'loopback4-authorization';
import {SubscriptionServiceBindings} from './keys';
import {ISubscriptionServiceConfig} from './types';
import {
BillingCycleRepository,
CurrencyRepository,
PlanItemRepository,
PlanRepository,
ResourceRepository,
ServiceRepository,
SubscriptionRepository,
} from './repositories';
import {
BillinCycleController,
CurrencyController,
Expand All @@ -50,11 +40,22 @@ import {
ServiceController,
SubscriptionController,
} from './controllers';
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';
import {WebhookVerifierProvider} from './interceptors';
import {
SubscriptionServiceBindings,
SYSTEM_USER,
WEBHOOK_VERIFIER,
} from './keys';
import {
BillingCycle,
Currency,
PlanItem,
Invoice,
Plan,
PlanItem,
Resource,
Service,
Subscription,
Expand All @@ -63,6 +64,20 @@ import {
FeatureToggleBindings,
FeatureToggleServiceComponent,
} from '@sourceloop/feature-toggle-service';
import {BillingCustomer} from './models/billing-customer.model';
import {
BillingCycleRepository,
CurrencyRepository,
InvoiceRepository,
PlanItemRepository,
PlanRepository,
ResourceRepository,
ServiceRepository,
SubscriptionRepository,
} from './repositories';
import {BillingCustomerRepository} from './repositories/billing-customer.repository';
import {ISubscriptionServiceConfig} from './types';
import {SystemUserProvider} from './providers';

export class SubscriptionServiceComponent implements Component {
constructor(
Expand All @@ -81,6 +96,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 Down Expand Up @@ -108,6 +124,8 @@ export class SubscriptionServiceComponent implements Component {
ResourceRepository,
ServiceRepository,
SubscriptionRepository,
BillingCustomerRepository,
InvoiceRepository,
];

this.models = [
Expand All @@ -116,9 +134,16 @@ export class SubscriptionServiceComponent implements Component {
PlanItem,
Plan,
Resource,
BillingCustomer,
Invoice,
Service,
Subscription,
];
this.bindings = [
Binding.bind(WEBHOOK_VERIFIER).toProvider(WebhookVerifierProvider),

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

this.controllers = [
BillinCycleController,
Expand All @@ -131,6 +156,10 @@ export class SubscriptionServiceComponent implements Component {
ServiceController,
SubscriptionController,
PlanSubscriptionController,
BillingCustomerController,
BillingInvoiceController,
BillingPaymentSourceController,
WebhookController,
];
}

Expand Down
Loading

0 comments on commit 9bd232b

Please sign in to comment.