-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
335 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { registerAs } from '@nestjs/config'; | ||
|
||
export default registerAs('s3', () => ({ | ||
accessKey: process.env.AWS_ACCESS_KEY || '', | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || '', | ||
region: process.env.AWS_REGION || 'us-east-2', | ||
bucketName: process.env.AWS_S3_BUCKET_NAME || '', | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
gerge branch 'dev' into feat/s3-resume-upload | ||
# Please enter a commit message to explain why this merge is necessary, | ||
# especially if it merges an updated upstream into a topic branch. | ||
# | ||
# Lines starting with '#' will be ignored, and an empty message aborts | ||
# the commit. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,43 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { BillingPlanService } from './billing-plan.service'; | ||
import { BillingPlanController } from './billing-plan.controller'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { BillingPlan } from './entities/billing-plan.entity'; | ||
import { User } from '@modules/user/entities/user.entity'; | ||
import { Organisation } from '@modules/organisations/entities/organisations.entity'; | ||
import { OrganisationUserRole } from '@modules/role/entities/organisation-user-role.entity'; | ||
import { Role } from '@modules/role/entities/role.entity'; | ||
import { BillingPlanController } from './billing-plan.controller'; | ||
import { BillingPlanService } from './billing-plan.service'; | ||
import { User } from '../user/entities/user.entity'; | ||
import { Organisation } from '../organisations/entities/organisations.entity'; | ||
import { OrganisationUserRole } from '../role/entities/organisation-user-role.entity'; | ||
import { Role } from '../role/entities/role.entity'; | ||
import { MailerModule } from '@nestjs-modules/mailer'; | ||
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter'; | ||
import { ScheduleModule } from '@nestjs/schedule'; | ||
import { SubscriptionScheduler } from './subscription-scheduler'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([BillingPlan, User, Organisation, OrganisationUserRole, Role])], | ||
imports: [ | ||
TypeOrmModule.forFeature([BillingPlan, User, Organisation, OrganisationUserRole, Role]), | ||
MailerModule.forRoot({ | ||
transport: { | ||
host: 'smtp.example.com', | ||
port: 587, | ||
auth: { | ||
user: '[email protected]', | ||
pass: 'password', | ||
}, | ||
}, | ||
defaults: { | ||
from: '"No Reply" <[email protected]>', | ||
}, | ||
template: { | ||
dir: __dirname + '/../../email/hng-templates', | ||
adapter: new HandlebarsAdapter(), | ||
options: { | ||
strict: true, | ||
}, | ||
}, | ||
}), | ||
ScheduleModule.forRoot(), | ||
], | ||
controllers: [BillingPlanController], | ||
providers: [BillingPlanService], | ||
providers: [BillingPlanService, SubscriptionScheduler], | ||
}) | ||
export class BillingPlanModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Cron } from '@nestjs/schedule'; | ||
import { BillingPlanService } from './billing-plan.service'; | ||
|
||
@Injectable() | ||
export class SubscriptionScheduler { | ||
constructor(private readonly billingPlanService: BillingPlanService) {} | ||
|
||
@Cron('0 0 * * *') | ||
async handleCron() { | ||
console.log('Running daily subscription renewal reminder job'); | ||
const subscriptions = await this.billingPlanService.getAllSubscriptions(); | ||
for (const subscription of subscriptions) { | ||
await this.billingPlanService.sendRenewalReminder(subscription.id); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import 'module-alias/register'; | ||
import 'reflect-metadata'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { BillingPlanService } from '../billing-plan.service'; | ||
import { Repository } from 'typeorm'; | ||
|
@@ -9,6 +7,8 @@ import { NotFoundException, BadRequestException, HttpStatus } from '@nestjs/comm | |
import { CustomHttpException } from '@shared/helpers/custom-http-filter'; | ||
import * as SYS_MSG from '@shared/constants/SystemMessages'; | ||
import { BillingPlanMapper } from '../mapper/billing-plan.mapper'; | ||
import { SubscriptionScheduler } from '../subscription-scheduler'; | ||
import { MailerService } from '@nestjs-modules/mailer'; | ||
|
||
describe('BillingPlanService', () => { | ||
let service: BillingPlanService; | ||
|
@@ -22,6 +22,12 @@ describe('BillingPlanService', () => { | |
provide: getRepositoryToken(BillingPlan), | ||
useClass: Repository, | ||
}, | ||
{ | ||
provide: MailerService, | ||
useValue: { | ||
sendMail: jest.fn().mockResolvedValue(undefined), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
|
@@ -48,6 +54,8 @@ describe('BillingPlanService', () => { | |
is_active: true, | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
expirationDate: new Date(), | ||
email: '[email protected]', | ||
}; | ||
|
||
jest.spyOn(repository, 'findOne').mockResolvedValue(billingPlan as BillingPlan); | ||
|
@@ -70,6 +78,8 @@ describe('BillingPlanService', () => { | |
is_active: true, | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
expirationDate: new Date(), | ||
email: '[email protected]', | ||
}, | ||
{ | ||
id: '2', | ||
|
@@ -80,16 +90,20 @@ describe('BillingPlanService', () => { | |
is_active: true, | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
expirationDate: new Date(), | ||
email: '[email protected]', | ||
}, | ||
{ | ||
id: '1', | ||
id: '3', | ||
name: 'Premium', | ||
description: 'premium plan', | ||
amount: 120, | ||
frequency: 'monthly', | ||
is_active: true, | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
expirationDate: new Date(), | ||
email: '[email protected]', | ||
}, | ||
]; | ||
|
||
|
@@ -121,6 +135,8 @@ describe('BillingPlanService', () => { | |
is_active: true, | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
expirationDate: new Date(), | ||
email: '[email protected]', | ||
}; | ||
|
||
jest.spyOn(repository, 'findOneBy').mockResolvedValue(billingPlan as BillingPlan); | ||
|
@@ -144,3 +160,38 @@ describe('BillingPlanService', () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe('SubscriptionScheduler', () => { | ||
let scheduler: SubscriptionScheduler; | ||
let billingPlanService: BillingPlanService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
SubscriptionScheduler, | ||
{ | ||
provide: BillingPlanService, | ||
useValue: { | ||
getAllSubscriptions: jest.fn().mockResolvedValue([ | ||
{ id: 1, expirationDate: new Date(), email: '[email protected]' }, | ||
{ id: 2, expirationDate: new Date(), email: '[email protected]' }, | ||
]), | ||
sendRenewalReminder: jest.fn().mockResolvedValue(undefined), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
scheduler = module.get<SubscriptionScheduler>(SubscriptionScheduler); | ||
billingPlanService = module.get<BillingPlanService>(BillingPlanService); | ||
}); | ||
|
||
it('should send renewal reminders to all subscriptions', async () => { | ||
await scheduler.handleCron(); | ||
|
||
expect(billingPlanService.getAllSubscriptions).toHaveBeenCalled(); | ||
expect(billingPlanService.sendRenewalReminder).toHaveBeenCalledTimes(2); | ||
expect(billingPlanService.sendRenewalReminder).toHaveBeenCalledWith(1); | ||
expect(billingPlanService.sendRenewalReminder).toHaveBeenCalledWith(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<html> | ||
<head> | ||
<title>Subscription Renewal Reminder</title> | ||
</head> | ||
<body> | ||
<p>Dear {{name}},</p> | ||
<p>This is a friendly reminder that your subscription is set to expire on {{expirationDate}}.</p> | ||
<p>Please renew your subscription to continue enjoying our services.</p> | ||
<p>Thank you!</p> | ||
</body> | ||
</html> |
Oops, something went wrong.