Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): add max renewal time in bundle; fix subscription price #908

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ model Bundle {
state String @default("Active") // Active, Inactive
limitCountPerUser Int // limit count of application per user could create
subscriptionOptions BundleSubscriptionOption[]
maxRenewalTime Int // in seconds

resource BundleResource

Expand Down
17 changes: 16 additions & 1 deletion server/src/initializer/initializer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class InitializerService {
displayName: 'Standard',
limitCountPerUser: 10,
priority: 0,
maxRenewalTime: 3600 * 24 * 365 * 10,
resource: {
limitCPU: 1 * CPU_UNIT,
limitMemory: 512,
Expand All @@ -105,11 +106,25 @@ export class InitializerService {
subscriptionOptions: [
{
name: 'monthly',
displayName: 'Monthly',
displayName: '1 Month',
duration: 31 * 24 * 3600,
price: 0,
specialPrice: 0,
},
{
name: 'half-yearly',
displayName: '6 Months',
duration: 6 * 31 * 24 * 3600,
price: 0,
specialPrice: 0,
},
{
name: 'yearly',
displayName: '12 Months',
duration: 12 * 31 * 24 * 3600,
price: 0,
specialPrice: 0,
},
],
region: {
connect: {
Expand Down
28 changes: 21 additions & 7 deletions server/src/subscription/subscription.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class SubscriptionController {
// check account balance
const account = await this.accountService.findOne(user.id)
const balance = account?.balance || 0
const priceAmount = option.specialPrice || option.price
const priceAmount = option.specialPrice
if (balance < priceAmount) {
return ResponseUtil.error(
`account balance is not enough, need ${priceAmount} but only ${account.balance}`,
Expand Down Expand Up @@ -170,14 +170,28 @@ export class SubscriptionController {
if (!option) {
return ResponseUtil.error(`duration not supported in bundle`)
}
const priceAmount = option.specialPrice || option.price
const priceAmount = option.specialPrice

// check max renewal time
const MAX_RENEWAL_AT = Date.now() + bundle.maxRenewalTime * 1000
const newExpiredAt = subscription.expiredAt.getTime() + duration * 1000
if (newExpiredAt > MAX_RENEWAL_AT) {
return ResponseUtil.error(
`max renewal time is ${MAX_RENEWAL_AT} for bundle ${bundle.name}`,
)
}

// check account balance
const account = await this.accountService.findOne(user.id)
const balance = account?.balance || 0
if (balance < priceAmount) {
return ResponseUtil.error(
`account balance is not enough, need ${priceAmount} but only ${account.balance}`,
)
}

// renew subscription
const res = await this.subscriptService.renew(
subscription,
duration,
priceAmount,
)
const res = await this.subscriptService.renew(subscription, option)
return ResponseUtil.ok(res)
}

Expand Down
12 changes: 4 additions & 8 deletions server/src/subscription/subscription.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class SubscriptionService {
data: {
subscriptionId: subscription.id,
duration: option.duration,
amount: option.price,
amount: option.specialPrice,
phase: SubscriptionRenewalPhase.Pending,
lockedAt: TASK_LOCK_INIT_TIME,
createdBy: userid,
Expand Down Expand Up @@ -106,17 +106,13 @@ export class SubscriptionService {
/**
* Renew a subscription by creating a subscription renewal
*/
async renew(
subscription: Subscription,
duration: number,
priceAmount: number,
) {
async renew(subscription: Subscription, option: BundleSubscriptionOption) {
// create subscription renewal
const res = await this.prisma.subscriptionRenewal.create({
data: {
subscriptionId: subscription.id,
duration: duration,
amount: priceAmount,
duration: option.duration,
amount: option.specialPrice,
phase: SubscriptionRenewalPhase.Pending,
lockedAt: TASK_LOCK_INIT_TIME,
createdBy: subscription.createdBy,
Expand Down