Skip to content

Commit 9445234

Browse files
authored
Merge pull request #73 from lnbits/destroy_guard
feat: only allow "destroy" of instances with subscriptions after cancellation
2 parents 4c87762 + cd29fb4 commit 9445234

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

src/components/tables/InstancesTable.vue

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,19 @@
166166
class="q-ml-sm"
167167
flat
168168
dense
169-
:disable="props.row.enabled"
169+
:disable="
170+
props.row.enabled || props.row.hasActiveFiatSubscription
171+
"
170172
>
171173
<q-tooltip class="bg-indigo" :offset="[10, 10]">
172174
<span v-if="props.row.enabled">
173175
Destroy: you can only destroy a disabled instance. Please
174176
disable it first.
175177
</span>
178+
<span v-else-if="props.row.hasActiveFiatSubscription">
179+
Please end the subscription from /subscriptions for this
180+
instance first
181+
</span>
176182
<span v-else>
177183
Destroy: destroying will delete your instance and every bit of
178184
data.</span
@@ -2301,13 +2307,22 @@ export default defineComponent({
23012307
)
23022308
},
23032309
destroyInstance: function (id) {
2304-
if (this.data.find(i => i.id === id)?.enabled) {
2310+
const instance = this.data.find(i => i.id === id)
2311+
2312+
if (instance?.enabled) {
23052313
return this.q.notify({
23062314
message:
23072315
'You can only destroy a disabled instance. Please disable it first.',
23082316
color: 'warning'
23092317
})
23102318
}
2319+
if (instance?.hasActiveFiatSubscription) {
2320+
return this.q.notify({
2321+
message:
2322+
'Please end the subscription from /subscriptions for this instance first',
2323+
color: 'warning'
2324+
})
2325+
}
23112326
this.confirm(
23122327
`Destroy ${id}`,
23132328
'Are you sure you want to destroy?' +
@@ -2470,12 +2485,37 @@ export default defineComponent({
24702485
return false
24712486
}
24722487
},
2488+
getActiveFiatSubscriptionInstanceIds(subscriptions = []) {
2489+
return new Set(
2490+
subscriptions
2491+
.filter(subscription => {
2492+
if (!subscription) {
2493+
return false
2494+
}
2495+
2496+
if (+subscription.ended_at) {
2497+
return false
2498+
}
2499+
2500+
return true
2501+
})
2502+
.map(subscription => `${subscription.instance_id}`)
2503+
)
2504+
},
24732505
refreshState: async function () {
24742506
try {
2475-
const {data} = await saas.getInstances()
2507+
const [{data: instancesData}, {data: subscriptionsData}] =
2508+
await Promise.all([saas.getInstances(), saas.getUserSubscriptions()])
24762509
24772510
await this.serverStatus()
2478-
const tableData = (data || []).map(i => saas.mapInstance(i))
2511+
const activeFiatSubscriptionInstanceIds =
2512+
this.getActiveFiatSubscriptionInstanceIds(subscriptionsData || [])
2513+
const tableData = (instancesData || []).map(i => ({
2514+
...saas.mapInstance(i),
2515+
hasActiveFiatSubscription: activeFiatSubscriptionInstanceIds.has(
2516+
`${i.id}`
2517+
)
2518+
}))
24792519
24802520
this.activityStats[0].value = tableData.length
24812521
this.activityStats[1].value = tableData.filter(

0 commit comments

Comments
 (0)