Skip to content

Commit

Permalink
fix(server): add max renewal time in bundle; fix subscription price (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow authored Mar 16, 2023
1 parent acf1d67 commit 6139f74
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
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

0 comments on commit 6139f74

Please sign in to comment.