Skip to content

Commit f4fc54d

Browse files
fix(subscription-service): refactor the code (#80)
## Description Restored the keys to their previous state ## 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
1 parent 2da1ac2 commit f4fc54d

28 files changed

Lines changed: 173 additions & 180 deletions

services/subscription-service/src/component.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ import {
4545
PlanSizesController,
4646
PlanFeaturesController,
4747
} from './controllers';
48-
import {SubscriptionServiceBindings} from './keys';
48+
import {
49+
SubscriptionServiceBindings,
50+
SYSTEM_USER,
51+
WEBHOOK_VERIFIER,
52+
} from './keys';
4953
import {
5054
BillingCycle,
5155
Currency,
@@ -68,7 +72,7 @@ import {
6872
BillingCustomerRepository,
6973
InvoiceRepository,
7074
} from './repositories';
71-
import {SubscriptionServiceConfig} from './types';
75+
import {ISubscriptionServiceConfig} from './types';
7276
import {WebhookVerifierProvider} from './interceptors/webhook-verifier.interceptor';
7377
import {SystemUserProvider} from './providers';
7478
import {BillingCustomerController} from './controllers/billing-customer.controller';
@@ -81,7 +85,7 @@ export class SubscriptionServiceComponent implements Component {
8185
@inject(CoreBindings.APPLICATION_INSTANCE)
8286
private readonly application: RestApplication,
8387
@inject(SubscriptionServiceBindings.Config, {optional: true})
84-
private readonly subscriptionConfig?: SubscriptionServiceConfig,
88+
private readonly subscriptionConfig?: ISubscriptionServiceConfig,
8589
) {
8690
this.providers = {};
8791

@@ -136,13 +140,9 @@ export class SubscriptionServiceComponent implements Component {
136140
PlanSizes,
137141
];
138142
this.bindings = [
139-
Binding.bind(SubscriptionServiceBindings.WEBHOOK_VERIFIER).toProvider(
140-
WebhookVerifierProvider,
141-
),
143+
Binding.bind(WEBHOOK_VERIFIER).toProvider(WebhookVerifierProvider),
142144

143-
Binding.bind(SubscriptionServiceBindings.SYSTEM_USER).toProvider(
144-
SystemUserProvider,
145-
),
145+
Binding.bind(SYSTEM_USER).toProvider(SystemUserProvider),
146146
];
147147

148148
this.controllers = [

services/subscription-service/src/controllers/webhook.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {intercept} from '@loopback/core';
22
import {repository} from '@loopback/repository';
33
import {post, requestBody} from '@loopback/rest';
44
import {authorize} from 'loopback4-authorization';
5-
import {SubscriptionServiceBindings} from '../keys';
5+
import {WEBHOOK_VERIFIER} from '../keys';
66
import {InvoiceRepository} from '../repositories';
77
import {BillingCustomerRepository} from '../repositories/billing-customer.repository';
88
import {IContent, IPayload} from '../types';
@@ -18,7 +18,7 @@ export class WebhookController {
1818
@authorize({
1919
permissions: ['*'],
2020
})
21-
@intercept(SubscriptionServiceBindings.WEBHOOK_VERIFIER)
21+
@intercept(WEBHOOK_VERIFIER)
2222
@post('/webhooks/billing-payment')
2323
async handleWebhook(@requestBody() payload: IPayload): Promise<void> {
2424
const content = payload.content;

services/subscription-service/src/interceptors/webhook-verifier.interceptor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import {
99
import {HttpErrors, RequestContext} from '@loopback/rest';
1010
import {ILogger, LOGGER} from '@sourceloop/core';
1111
import {AuthenticationBindings, IAuthUser} from 'loopback4-authentication';
12-
import {SubscriptionServiceBindings} from '../keys';
12+
import {SYSTEM_USER} from '../keys';
1313

1414
export class WebhookVerifierProvider implements Provider<Interceptor> {
1515
constructor(
1616
@inject(LOGGER.LOGGER_INJECT)
1717
private readonly logger: ILogger,
1818
@inject.setter(AuthenticationBindings.CURRENT_USER)
1919
private readonly setCurrentUser: Setter<IAuthUser>,
20-
@inject(SubscriptionServiceBindings.SYSTEM_USER)
20+
@inject(SYSTEM_USER)
2121
private readonly systemUser: IAuthUser,
2222
) {}
2323

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
import {BindingKey, Interceptor} from '@loopback/core';
2-
import {SubscriptionServiceConfig} from './types';
2+
import {ISubscriptionServiceConfig} from './types';
33
import {BINDING_PREFIX} from '@sourceloop/core';
44
import {VerifyFunction} from 'loopback4-authentication';
55
import {AnyObject} from '@loopback/repository';
66
import {IAuthUser} from 'loopback4-authorization';
77

88
export namespace SubscriptionServiceBindings {
9-
export const WEBHOOK_VERIFIER = BindingKey.create<Interceptor>(
10-
'sf.webhook.verifier',
11-
);
12-
13-
/**
14-
* Binding key for the lead token verifier.
15-
*/
16-
export const LEAD_TOKEN_VERIFIER = BindingKey.create<
17-
VerifyFunction.BearerFn<AnyObject>
18-
>('sf.user.lead.verifier');
19-
20-
/**
21-
* Binding key for the system user.
22-
*/
23-
export const SYSTEM_USER = BindingKey.create<IAuthUser & AnyObject>(
24-
'sf.user.system',
25-
);
26-
export const Config = BindingKey.create<SubscriptionServiceConfig>(
9+
export const Config = BindingKey.create<ISubscriptionServiceConfig>(
2710
`${BINDING_PREFIX}.task.config`,
2811
);
2912
}
13+
14+
export const WEBHOOK_VERIFIER = BindingKey.create<Interceptor>(
15+
'sf.webhook.verifier',
16+
);
17+
18+
/**
19+
* Binding key for the lead token verifier.
20+
*/
21+
export const LEAD_TOKEN_VERIFIER = BindingKey.create<
22+
VerifyFunction.BearerFn<AnyObject>
23+
>('sf.user.lead.verifier');
24+
25+
/**
26+
* Binding key for the system user.
27+
*/
28+
export const SYSTEM_USER = BindingKey.create<IAuthUser & AnyObject>(
29+
'sf.user.system',
30+
);

services/subscription-service/src/sequelize-component.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ import {
4545
PlanSizesController,
4646
PlanFeaturesController,
4747
} from './controllers';
48-
import {SubscriptionServiceBindings} from './keys';
48+
import {
49+
SubscriptionServiceBindings,
50+
SYSTEM_USER,
51+
WEBHOOK_VERIFIER,
52+
} from './keys';
4953
import {
5054
BillingCycle,
5155
Currency,
@@ -68,7 +72,7 @@ import {
6872
BillingCustomerRepository,
6973
InvoiceRepository,
7074
} from './repositories/sequelize';
71-
import {SubscriptionServiceConfig} from './types';
75+
import {ISubscriptionServiceConfig} from './types';
7276
import {WebhookVerifierProvider} from './interceptors/webhook-verifier.interceptor';
7377
import {SystemUserProvider} from './providers';
7478
import {BillingCustomerController} from './controllers/billing-customer.controller';
@@ -81,7 +85,7 @@ export class SubscriptionSequelizeServiceComponent implements Component {
8185
@inject(CoreBindings.APPLICATION_INSTANCE)
8286
private readonly application: RestApplication,
8387
@inject(SubscriptionServiceBindings.Config, {optional: true})
84-
private readonly subscriptionConfig?: SubscriptionServiceConfig,
88+
private readonly subscriptionConfig?: ISubscriptionServiceConfig,
8589
) {
8690
this.providers = {};
8791

@@ -136,13 +140,9 @@ export class SubscriptionSequelizeServiceComponent implements Component {
136140
PlanSizes,
137141
];
138142
this.bindings = [
139-
Binding.bind(SubscriptionServiceBindings.WEBHOOK_VERIFIER).toProvider(
140-
WebhookVerifierProvider,
141-
),
143+
Binding.bind(WEBHOOK_VERIFIER).toProvider(WebhookVerifierProvider),
142144

143-
Binding.bind(SubscriptionServiceBindings.SYSTEM_USER).toProvider(
144-
SystemUserProvider,
145-
),
145+
Binding.bind(SYSTEM_USER).toProvider(SystemUserProvider),
146146
];
147147

148148
this.controllers = [

services/subscription-service/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export interface IPayload {
4141
export interface IContent {
4242
invoice: TInvoice;
4343
}
44-
export interface SubscriptionServiceConfig extends IServiceConfig {
44+
export interface ISubscriptionServiceConfig extends IServiceConfig {
4545
useCustomSequence: boolean;
4646
useSequelize?: boolean;
4747
}

services/tenant-management-service/src/__tests__/acceptance/test-helper.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from 'loopback4-authentication';
2020
import {RateLimitSecurityBindings} from 'loopback4-ratelimiter';
2121
import {
22+
EventConnectorBinding,
2223
TenantManagementServiceBindings,
2324
TenantManagementServiceComponent,
2425
WebhookTenantManagementServiceComponent,
@@ -135,9 +136,7 @@ function setupEventConnector(app: RestApplication) {
135136
}
136137
}
137138

138-
app
139-
.bind(TenantManagementServiceBindings.EventConnectorBinding)
140-
.toClass(EventConnector);
139+
app.bind(EventConnectorBinding).toClass(EventConnector);
141140
}
142141

143142
export interface AppWithClient {
@@ -175,7 +174,7 @@ export class TestTenantMgmtServiceApplication extends BootMixin(
175174
constructor(options: ApplicationConfig = {}) {
176175
super(options);
177176
this.static('/', path.join(__dirname, '../public'));
178-
this.bind(TenantManagementServiceBindings.config).to({
177+
this.bind(TenantManagementServiceBindings.Config).to({
179178
useCustomSequence: true,
180179
});
181180
this.component(TenantManagementServiceComponent);

services/tenant-management-service/src/__tests__/acceptance/webhook.controller.acceptance.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import {ILogger, LOGGER, STATUS_CODE} from '@sourceloop/core';
44
import {createHmac, randomBytes} from 'crypto';
55
import {TenantMgmtServiceApplication} from '../../application';
66
import {TenantStatus} from '../../enums';
7-
// import {PostWebhookHandlerServiceKey, WEBHOOK_CONFIG} from '../../keys';
8-
import {TenantManagementServiceBindings} from '../../keys';
7+
import {PostWebhookHandlerServiceKey, WEBHOOK_CONFIG} from '../../keys';
98
import {
109
ContactRepository,
1110
ResourceRepository,
@@ -31,7 +30,7 @@ describe('WebhookController', () => {
3130

3231
before('setupApplication', async () => {
3332
({app, client} = await setupApplication(notifStub));
34-
webhookConfig = app.getSync(TenantManagementServiceBindings.WEBHOOK_CONFIG);
33+
webhookConfig = app.getSync(WEBHOOK_CONFIG);
3534
const logger = app.getSync<ILogger>(LOGGER.LOGGER_INJECT);
3635
loggerSpy = sinon.spy(logger);
3736
app.bind(LOGGER.LOGGER_INJECT).to(logger).inScope(BindingScope.SINGLETON);
@@ -53,7 +52,7 @@ describe('WebhookController', () => {
5352
});
5453

5554
postWebhookHandlerServiceStub = sinon.stub();
56-
app.bind(TenantManagementServiceBindings.PostWebhookHandlerServiceKey).to({
55+
app.bind(PostWebhookHandlerServiceKey).to({
5756
postWebhookHandler: postWebhookHandlerServiceStub,
5857
});
5958
});

services/tenant-management-service/src/__tests__/unit/sequelize.application.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class TenantMgmtServiceApplication extends BootMixin(
1919
constructor(options: ApplicationConfig = {}) {
2020
super(options);
2121
this.static('/', path.join(__dirname, '../public'));
22-
this.bind(TenantManagementServiceBindings.config).to({
22+
this.bind(TenantManagementServiceBindings.Config).to({
2323
useCustomSequence: false,
2424
useSequelize: true,
2525
});

services/tenant-management-service/src/component.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ import {
4141
TenantController,
4242
} from './controllers';
4343
import {InvoiceController} from './controllers/invoice.controller';
44-
import {TenantManagementServiceBindings} from './keys';
44+
import {
45+
TenantManagementServiceBindings,
46+
LEAD_TOKEN_VERIFIER,
47+
SYSTEM_USER,
48+
EventConnectorBinding,
49+
} from './keys';
4550
import {
4651
Address,
4752
Contact,
@@ -79,14 +84,14 @@ import {
7984
OnboardingService,
8085
ProvisioningService,
8186
} from './services';
82-
import {TenantManagementServiceConfig} from './types';
87+
import {ITenantManagementServiceConfig} from './types';
8388

8489
export class TenantManagementServiceComponent implements Component {
8590
constructor(
8691
@inject(CoreBindings.APPLICATION_INSTANCE)
8792
private readonly application: RestApplication,
88-
@inject(TenantManagementServiceBindings.config, {optional: true})
89-
private readonly tenantMgmtConfig?: TenantManagementServiceConfig,
93+
@inject(TenantManagementServiceBindings.Config, {optional: true})
94+
private readonly tenantMgmtConfig?: ITenantManagementServiceConfig,
9095
) {
9196
this.providers = {};
9297

@@ -152,12 +157,8 @@ export class TenantManagementServiceComponent implements Component {
152157
];
153158

154159
this.bindings = [
155-
Binding.bind(
156-
TenantManagementServiceBindings.LEAD_TOKEN_VERIFIER,
157-
).toProvider(LeadTokenVerifierProvider),
158-
Binding.bind(TenantManagementServiceBindings.SYSTEM_USER).toProvider(
159-
SystemUserProvider,
160-
),
160+
Binding.bind(LEAD_TOKEN_VERIFIER).toProvider(LeadTokenVerifierProvider),
161+
Binding.bind(SYSTEM_USER).toProvider(SystemUserProvider),
161162
createServiceBinding(ProvisioningService),
162163
createServiceBinding(OnboardingService),
163164
createServiceBinding(LeadAuthenticator),
@@ -166,10 +167,7 @@ export class TenantManagementServiceComponent implements Component {
166167
createServiceBinding(InvoicePDFGenerator),
167168
];
168169

169-
this.addClassBindingIfNotPresent(
170-
TenantManagementServiceBindings.EventConnectorBinding.key,
171-
EventConnector,
172-
);
170+
this.addClassBindingIfNotPresent(EventConnectorBinding.key, EventConnector);
173171
}
174172

175173
providers?: ProviderMap = {};

0 commit comments

Comments
 (0)