Skip to content

Commit

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

add billing functionality to subscription service

BREAKING CHANGE:
yes

gh-34

## Description

Please include a summary of the change and which issue is fixed. Please
also include relevant motivation and context. List any dependencies that
are required for this change.

Fixes # (issue)

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Intermediate change (work in progress)

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration

- [ ] Test A
- [ ] Test B

## Checklist:

- [ ] Performed a self-review of my own code
- [ ] npm test passes on your machine
- [ ] New tests added or existing tests modified to cover all changes
- [ ] Code conforms with the style guide
- [ ] API Documentation in code was updated
- [ ] Any dependent changes have been merged and published in downstream
modules
  • Loading branch information
Tyagi-Sunny authored Dec 13, 2024
1 parent 944a5c3 commit a1ea1e3
Show file tree
Hide file tree
Showing 43 changed files with 3,150 additions and 1,223 deletions.
2,733 changes: 1,546 additions & 1,187 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 VARCHAR(255),
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",
"loopback4-billing": "^1.0.0",
"loopback-connector-postgresql": "^7.1.1",
"loopback4-authentication": "^12.0.2",
"loopback4-authorization": "^7.0.2",
Expand Down
83 changes: 56 additions & 27 deletions services/subscription-service/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,83 @@
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

import {inject, Binding} from '@loopback/context';
import {
Binding,
Component,
ControllerClass,
CoreBindings,
inject,
ProviderMap,
ServiceOrProviderClass,
ControllerClass,
} from '@loopback/core';
import {Class, Model, Repository} from '@loopback/repository';
import {Class, Repository, Model} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {
BearerVerifierBindings,
BearerVerifierComponent,
BearerVerifierConfig,
BearerVerifierType,
CoreComponent,
SECURITY_SCHEME_SPEC,
ServiceSequence,
BearerVerifierBindings,
BearerVerifierType,
BearerVerifierConfig,
BearerVerifierComponent,
} from '@sourceloop/core';
import {
FeatureToggleBindings,
FeatureToggleServiceComponent,
} from '@sourceloop/feature-toggle-service';
import {BillingComponent} from 'loopback4-billing';
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,
CurrencyController,
PlanController,
PlanFeaturesController,
PlanSizesController,
PlanSubscriptionController,
ResourceController,
ServiceController,
SubscriptionController,
PlanSubscriptionController,
PlanSizesController,
PlanFeaturesController,
} from './controllers';
import {
SubscriptionServiceBindings,
SYSTEM_USER,
WEBHOOK_VERIFIER,
} from './keys';
import {
BillingCycle,
Currency,
Plan,
PlanSizes,
Resource,
BillingCustomer,
Invoice,
Service,
Subscription,
PlanSizes,
} from './models';
import {
FeatureToggleBindings,
FeatureToggleServiceComponent,
} from '@sourceloop/feature-toggle-service';
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 +97,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 @@ -109,17 +125,26 @@ export class SubscriptionServiceComponent implements Component {
ServiceRepository,
SubscriptionRepository,
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 +158,10 @@ export class SubscriptionServiceComponent implements Component {
PlanSubscriptionController,
PlanSizesController,
PlanFeaturesController,
BillingCustomerController,
BillingInvoiceController,
BillingPaymentSourceController,
WebhookController,
];
}

Expand Down
Loading

0 comments on commit a1ea1e3

Please sign in to comment.