-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathInstanceFormHelper.js
More file actions
119 lines (112 loc) · 6.21 KB
/
InstanceFormHelper.js
File metadata and controls
119 lines (112 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { getTeamProperty } from '../../../TeamProperties.js'
import { useAccountSettingsStore } from '@/stores/account-settings.js'
import { useContextStore } from '@/stores/context.js'
export function useInstanceFormHelper () {
const teamRuntimeLimitReached = () => {
const team = useContextStore().team
let teamTypeRuntimeLimit = getTeamProperty(team, 'runtimes.limit')
const currentRuntimeCount = (team?.deviceCount ?? 0) + (team?.instanceCount ?? 0)
if (team?.billing?.trial && !team?.billing?.active && getTeamProperty(team, 'trial.runtimesLimit')) {
teamTypeRuntimeLimit = getTeamProperty(team, 'trial.runtimesLimit')
}
return (teamTypeRuntimeLimit > 0 && currentRuntimeCount >= teamTypeRuntimeLimit)
}
const decorateInstanceTypes = (instanceTypes) => {
const team = useContextStore().team
const features = useAccountSettingsStore().features
// Do a first pass of the instance types to disable any not allowed for this team
instanceTypes = instanceTypes.map(instanceType => {
// Need to combine the projectType billing info with any overrides
// from the current teamType
const existingInstanceCount = team?.instanceCountByType?.[instanceType.id] || 0
if (teamRuntimeLimitReached()) {
// The overall limit has been reached
instanceType.disabled = true
} else {
// Get individual properties to ensure we pickup any team overrides
if (!getTeamProperty(team, `instances.${instanceType.id}.active`)) {
// This instanceType is disabled for this teamType
instanceType.disabled = true
} else if (getTeamProperty(team, `instances.${instanceType.id}.creatable`) === false) {
// Type is active (it can exist), but not creatable (not allowed to create more) for this team type.
// This can happen follow a change of TeamType where different instance types are available.
// This check treats undefined as true for backwards compatibility
instanceType.disabled = true
} else {
const limit = getTeamProperty(team, `instances.${instanceType.id}.limit`)
if (limit !== null && limit <= existingInstanceCount) {
// This team has reached the limit of this instance type
instanceType.disabled = true
}
}
}
return instanceType
})
if (features.billing) {
// With billing enabled, do a second pass through the instance types
// to populate their billing info
instanceTypes = instanceTypes.map(instanceType => {
// Need to combine the projectType billing info with any overrides
// from the current teamType
let existingInstanceCount = team?.instanceCountByType?.[instanceType.id] || 0
if (getTeamProperty(team, 'devices.combinedFreeType') === instanceType.id) {
// Need to include device count as they use a combined free allocation
existingInstanceCount += team?.deviceCount ?? 0
}
instanceType.price = ''
instanceType.priceInterval = ''
instanceType.currency = ''
instanceType.cost = 0
if (!instanceType.disabled && !team?.billing?.unmanaged) {
let billingDescription
const teamTypeFreeCount = getTeamProperty(team, `instances.${instanceType.id}.free`)
// Get the right description based on the billing interval
let descriptionKey = 'description'
if (team?.billing?.interval === 'year') {
descriptionKey = 'yrDescription'
}
const teamTypeDescription = getTeamProperty(team, `instances.${instanceType.id}.${descriptionKey}`)
if (teamTypeDescription) {
// TeamType provides metadata to use - do not fall back to instanceType
if (existingInstanceCount >= (teamTypeFreeCount || 0)) {
billingDescription = teamTypeDescription
} else {
// This team is still within its free allowance so clear
// the billingDescription
}
} else {
billingDescription = instanceType.properties?.billingDescription
}
if (billingDescription) {
[instanceType.price, instanceType.priceInterval] = billingDescription.split('/')
instanceType.currency = instanceType.price.replace(/[\d.]+/, '')
instanceType.cost = (Number(instanceType.price.replace(/[^\d.]+/, '')) || 0) * 100
} else {
instanceType.price = ''
instanceType.priceInterval = ''
instanceType.currency = ''
instanceType.cost = 0
}
if (team?.billing?.trial) {
if (getTeamProperty(team, 'trial.instanceType')) {
const isTrialProjectType = instanceType.id === getTeamProperty(team, 'trial.instanceType')
if (!team?.billing?.active) {
// No active billing - only allow the trial instance type
instanceType.disabled = !isTrialProjectType
}
if (isTrialProjectType && team?.billing?.trialProjectAllowed) {
instanceType.price = 'Free Trial'
// instanceType.priceInterval = instanceType.properties?.billingDescription
}
}
}
}
return instanceType
})
}
return instanceTypes
}
return {
decorateInstanceTypes
}
}