You've already forked AstralRinth
forked from didirus/AstralRinth
Credit subscriptions (#4575)
* Implement subscription crediting * chore: query cache, clippy, fmt * Improve code, improve query for next open charge * chore: query cache, clippy, fmt * Move server ID copy button up * Node + region crediting * Make it less ugly * chore: query cache, clippy, fmt * Bugfixes * Fix lint * Adjust migration * Adjust migration * Remove billing change * Move DEFAULT_CREDIT_EMAIL_MESSAGE to utils.ts * Lint * Merge * bump clickhouse, disable validation * tombi fmt * Update cargo lock
This commit is contained in:
committed by
GitHub
parent
79502a19d6
commit
eeed4e572d
@@ -6,7 +6,7 @@ export interface ServersFetchOptions {
|
||||
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'
|
||||
contentType?: string
|
||||
body?: Record<string, any>
|
||||
version?: number
|
||||
version?: number | 'internal'
|
||||
override?: {
|
||||
url?: string
|
||||
token?: string
|
||||
@@ -82,7 +82,9 @@ export async function useServersFetch<T>(
|
||||
? `https://${newOverrideUrl}/${path.replace(/^\//, '')}`
|
||||
: version === 0
|
||||
? `${base}/modrinth/v${version}/${path.replace(/^\//, '')}`
|
||||
: `${base}/v${version}/${path.replace(/^\//, '')}`
|
||||
: version === 'internal'
|
||||
? `${base}/_internal/${path.replace(/^\//, '')}`
|
||||
: `${base}/modrinth/v${version}/${path.replace(/^\//, '')}`
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'User-Agent': 'Modrinth/1.0 (https://modrinth.com)',
|
||||
|
||||
@@ -461,6 +461,12 @@
|
||||
link: '/admin/servers/notices',
|
||||
shown: isAdmin(auth.user),
|
||||
},
|
||||
{
|
||||
id: 'servers-nodes',
|
||||
color: 'primary',
|
||||
link: '/admin/servers/nodes',
|
||||
shown: isAdmin(auth.user),
|
||||
},
|
||||
]"
|
||||
>
|
||||
<ModrinthIcon aria-hidden="true" />
|
||||
@@ -480,6 +486,7 @@
|
||||
<template #servers-notices>
|
||||
<IssuesIcon aria-hidden="true" /> {{ formatMessage(messages.manageServerNotices) }}
|
||||
</template>
|
||||
<template #servers-nodes> <ServerIcon aria-hidden="true" /> Server Nodes </template>
|
||||
</OverflowMenu>
|
||||
</ButtonStyled>
|
||||
<ButtonStyled type="transparent">
|
||||
|
||||
@@ -97,6 +97,41 @@
|
||||
</div>
|
||||
</div>
|
||||
</NewModal>
|
||||
<NewModal ref="creditModal">
|
||||
<template #title>
|
||||
<span class="text-lg font-extrabold text-contrast">Credit subscription</span>
|
||||
</template>
|
||||
<div class="flex flex-col gap-3">
|
||||
<div class="flex flex-col gap-2">
|
||||
<label for="days" class="flex flex-col gap-1">
|
||||
<span class="text-lg font-semibold text-contrast">Days to credit</span>
|
||||
<span>Enter the number of days to add to the next due date.</span>
|
||||
</label>
|
||||
<input id="days" v-model.number="creditDays" type="number" min="1" autocomplete="off" />
|
||||
</div>
|
||||
<div class="flex flex-col gap-2">
|
||||
<label for="sendEmail" class="flex flex-col gap-1">
|
||||
<span class="text-lg font-semibold text-contrast">Send email to user</span>
|
||||
<span>Notify the user about the credited days.</span>
|
||||
</label>
|
||||
<Toggle id="sendEmail" v-model="creditSendEmail" />
|
||||
</div>
|
||||
<div class="flex gap-2">
|
||||
<ButtonStyled color="brand">
|
||||
<button :disabled="crediting" @click="applyCredit">
|
||||
<CheckIcon aria-hidden="true" />
|
||||
Apply credit
|
||||
</button>
|
||||
</ButtonStyled>
|
||||
<ButtonStyled>
|
||||
<button @click="creditModal.hide()">
|
||||
<XIcon aria-hidden="true" />
|
||||
Cancel
|
||||
</button>
|
||||
</ButtonStyled>
|
||||
</div>
|
||||
</div>
|
||||
</NewModal>
|
||||
<div class="page experimental-styles-within">
|
||||
<div
|
||||
class="mb-4 flex items-center justify-between border-0 border-b border-solid border-divider pb-4"
|
||||
@@ -140,6 +175,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="subscription.metadata?.id" class="flex flex-col items-end gap-2">
|
||||
<CopyCode :text="subscription.metadata.id" />
|
||||
<ButtonStyled
|
||||
v-if="
|
||||
subscription.metadata?.type === 'pyro' || subscription.metadata?.type === 'medal'
|
||||
@@ -153,7 +189,12 @@
|
||||
<ServerIcon /> Server panel <ExternalIcon class="h-4 w-4" />
|
||||
</nuxt-link>
|
||||
</ButtonStyled>
|
||||
<CopyCode :text="subscription.metadata.id" />
|
||||
<ButtonStyled>
|
||||
<button @click="showCreditModal(subscription)">
|
||||
<CurrencyIcon />
|
||||
Credit
|
||||
</button>
|
||||
</ButtonStyled>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex flex-col gap-2">
|
||||
@@ -292,6 +333,7 @@ import {
|
||||
useRelativeTime,
|
||||
} from '@modrinth/ui'
|
||||
import { formatCategory, formatPrice } from '@modrinth/utils'
|
||||
import { DEFAULT_CREDIT_EMAIL_MESSAGE } from '@modrinth/utils/utils.ts'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
import ModrinthServersIcon from '~/components/ui/servers/ModrinthServersIcon.vue'
|
||||
@@ -370,6 +412,11 @@ const modifying = ref(false)
|
||||
const modifyModal = ref()
|
||||
const cancel = ref(false)
|
||||
|
||||
const crediting = ref(false)
|
||||
const creditModal = ref()
|
||||
const creditDays = ref(7)
|
||||
const creditSendEmail = ref(true)
|
||||
|
||||
function showRefundModal(charge) {
|
||||
selectedCharge.value = charge
|
||||
refundType.value = 'full'
|
||||
@@ -385,6 +432,44 @@ function showModifyModal(charge, subscription) {
|
||||
modifyModal.value.show()
|
||||
}
|
||||
|
||||
function showCreditModal(subscription) {
|
||||
selectedSubscription.value = subscription
|
||||
creditDays.value = 1
|
||||
creditSendEmail.value = true
|
||||
creditModal.value.show()
|
||||
}
|
||||
|
||||
async function applyCredit() {
|
||||
crediting.value = true
|
||||
try {
|
||||
const daysParsed = Math.max(1, Math.floor(Number(creditDays.value) || 1))
|
||||
await useBaseFetch('billing/credit', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
subscription_ids: [selectedSubscription.value.id],
|
||||
days: daysParsed,
|
||||
send_email: creditSendEmail.value,
|
||||
message: DEFAULT_CREDIT_EMAIL_MESSAGE,
|
||||
}),
|
||||
internal: true,
|
||||
})
|
||||
addNotification({
|
||||
title: 'Credit applied',
|
||||
text: 'The subscription due date has been updated.',
|
||||
type: 'success',
|
||||
})
|
||||
await refreshCharges()
|
||||
creditModal.value.hide()
|
||||
} catch (err) {
|
||||
addNotification({
|
||||
title: 'Error applying credit',
|
||||
text: err.data?.description ?? String(err),
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
crediting.value = false
|
||||
}
|
||||
|
||||
async function refundCharge() {
|
||||
refunding.value = true
|
||||
try {
|
||||
|
||||
277
apps/frontend/src/pages/admin/servers/nodes.vue
Normal file
277
apps/frontend/src/pages/admin/servers/nodes.vue
Normal file
@@ -0,0 +1,277 @@
|
||||
<template>
|
||||
<div class="page experimental-styles-within">
|
||||
<div
|
||||
class="mb-6 flex items-end justify-between border-0 border-b border-solid border-divider pb-4"
|
||||
>
|
||||
<h1 class="m-0 text-2xl">Server nodes</h1>
|
||||
<ButtonStyled color="brand">
|
||||
<button @click="openBatchModal"><PlusIcon /> Batch credit</button>
|
||||
</ButtonStyled>
|
||||
</div>
|
||||
|
||||
<NewModal ref="batchModal">
|
||||
<template #title>
|
||||
<span class="text-lg font-extrabold text-contrast">Batch credit</span>
|
||||
</template>
|
||||
<div class="flex w-[720px] max-w-[90vw] flex-col gap-6">
|
||||
<div class="flex flex-col gap-2">
|
||||
<label class="flex flex-col gap-1">
|
||||
<span class="text-lg font-semibold text-contrast"> Type </span>
|
||||
<span>Select target to credit.</span>
|
||||
</label>
|
||||
<TeleportDropdownMenu
|
||||
v-model="mode"
|
||||
:options="modeOptions"
|
||||
:display-name="(x) => x.name"
|
||||
name="Type"
|
||||
class="max-w-[8rem]"
|
||||
/>
|
||||
</div>
|
||||
<div class="flex flex-col gap-2">
|
||||
<label for="days" class="flex flex-col gap-1">
|
||||
<span class="text-lg font-semibold text-contrast"> Days to credit </span>
|
||||
</label>
|
||||
<input
|
||||
id="days"
|
||||
v-model.number="days"
|
||||
class="w-32"
|
||||
type="number"
|
||||
min="1"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div v-if="mode.id === 'nodes'" class="flex flex-col gap-3">
|
||||
<div class="flex flex-col gap-2">
|
||||
<label for="node-input" class="flex flex-col gap-1">
|
||||
<span class="text-lg font-semibold text-contrast"> Node hostnames </span>
|
||||
</label>
|
||||
<div class="flex items-center gap-2">
|
||||
<input
|
||||
id="node-input"
|
||||
v-model="nodeInput"
|
||||
class="w-32"
|
||||
type="text"
|
||||
autocomplete="off"
|
||||
/>
|
||||
<ButtonStyled color="blue" color-fill="text">
|
||||
<button class="shrink-0" @click="addNode">
|
||||
<PlusIcon />
|
||||
Add
|
||||
</button>
|
||||
</ButtonStyled>
|
||||
</div>
|
||||
<div v-if="selectedNodes.length" class="mt-1 flex flex-wrap gap-2">
|
||||
<TagItem v-for="h in selectedNodes" :key="`node-${h}`" :action="() => removeNode(h)">
|
||||
<XIcon />
|
||||
{{ h }}
|
||||
</TagItem>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-else class="flex flex-col gap-3">
|
||||
<div class="flex flex-col gap-2">
|
||||
<label for="region-select" class="flex flex-col gap-1">
|
||||
<span class="text-lg font-semibold text-contrast"> Region </span>
|
||||
<span>This will credit all active servers in the region.</span>
|
||||
</label>
|
||||
<TeleportDropdownMenu
|
||||
id="region-select"
|
||||
v-model="selectedRegion"
|
||||
:options="regions"
|
||||
:display-name="(x) => x.display"
|
||||
name="Region"
|
||||
class="max-w-[24rem]"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="between flex items-center gap-4">
|
||||
<label for="send-email-nodes" class="flex flex-col gap-1">
|
||||
<span class="text-lg font-semibold text-contrast"> Send email </span>
|
||||
</label>
|
||||
<Toggle id="send-email-nodes" v-model="sendEmail" />
|
||||
</div>
|
||||
|
||||
<div v-if="sendEmail" class="flex flex-col gap-2">
|
||||
<label for="message-region" class="flex flex-col gap-1">
|
||||
<span class="text-lg font-semibold text-contrast"> Customize Email </span>
|
||||
<span>
|
||||
Unless a particularly bad or out of the ordinary event happened, keep this to the
|
||||
default
|
||||
</span>
|
||||
</label>
|
||||
<div class="text-muted rounded-lg border border-divider bg-button-bg p-4">
|
||||
<p>Hi {user.name},</p>
|
||||
<div class="textarea-wrapper">
|
||||
<textarea
|
||||
id="message-region"
|
||||
v-model="message"
|
||||
rows="3"
|
||||
class="w-full overflow-hidden"
|
||||
/>
|
||||
</div>
|
||||
<p>
|
||||
To make up for it, we've added {{ days }} day{{ pluralize(days) }} to your Modrinth
|
||||
Servers subscription.
|
||||
</p>
|
||||
<p>
|
||||
Your next charge was scheduled for {credit.previous_due} and will now be on
|
||||
{credit.next_due}.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex gap-2">
|
||||
<ButtonStyled color="brand">
|
||||
<button :disabled="applyDisabled" @click="apply">
|
||||
<CheckIcon aria-hidden="true" />
|
||||
Apply credits
|
||||
</button>
|
||||
</ButtonStyled>
|
||||
<ButtonStyled>
|
||||
<button @click="batchModal?.hide?.()">
|
||||
<XIcon aria-hidden="true" />
|
||||
Cancel
|
||||
</button>
|
||||
</ButtonStyled>
|
||||
</div>
|
||||
</div>
|
||||
</NewModal>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { CheckIcon, PlusIcon, XIcon } from '@modrinth/assets'
|
||||
import {
|
||||
ButtonStyled,
|
||||
injectNotificationManager,
|
||||
NewModal,
|
||||
TagItem,
|
||||
TeleportDropdownMenu,
|
||||
Toggle,
|
||||
} from '@modrinth/ui'
|
||||
import { DEFAULT_CREDIT_EMAIL_MESSAGE } from '@modrinth/utils/utils.ts'
|
||||
import { computed, ref } from 'vue'
|
||||
|
||||
import { useBaseFetch } from '#imports'
|
||||
import { useServersFetch } from '~/composables/servers/servers-fetch.ts'
|
||||
|
||||
const { addNotification } = injectNotificationManager()
|
||||
|
||||
const batchModal = ref<InstanceType<typeof NewModal>>()
|
||||
|
||||
const days = ref(1)
|
||||
const sendEmail = ref(true)
|
||||
const message = ref('')
|
||||
|
||||
const modeOptions = [
|
||||
{ id: 'nodes', name: 'Nodes' },
|
||||
{ id: 'region', name: 'Region' },
|
||||
]
|
||||
const mode = ref(modeOptions[0])
|
||||
|
||||
const nodeInput = ref('')
|
||||
const selectedNodes = ref<string[]>([])
|
||||
|
||||
type RegionOpt = { key: string; display: string }
|
||||
const regions = ref<RegionOpt[]>([])
|
||||
const selectedRegion = ref<RegionOpt | null>(null)
|
||||
const nodeHostnames = ref<string[]>([])
|
||||
|
||||
function openBatchModal() {
|
||||
void ensureOverview()
|
||||
|
||||
message.value = DEFAULT_CREDIT_EMAIL_MESSAGE
|
||||
batchModal.value?.show()
|
||||
}
|
||||
|
||||
function addNode() {
|
||||
const v = nodeInput.value.trim()
|
||||
if (!v) return
|
||||
if (!nodeHostnames.value.includes(v)) {
|
||||
addNotification({
|
||||
title: 'Unknown node',
|
||||
text: "This hostname doesn't exist",
|
||||
type: 'error',
|
||||
})
|
||||
return
|
||||
}
|
||||
if (!selectedNodes.value.includes(v)) selectedNodes.value.push(v)
|
||||
nodeInput.value = ''
|
||||
}
|
||||
|
||||
function removeNode(v: string) {
|
||||
selectedNodes.value = selectedNodes.value.filter((x) => x !== v)
|
||||
}
|
||||
|
||||
const applyDisabled = computed(() => {
|
||||
if (days.value < 1) return true
|
||||
if (mode.value.id === 'nodes') return selectedNodes.value.length === 0
|
||||
return !selectedRegion.value
|
||||
})
|
||||
|
||||
async function ensureOverview() {
|
||||
if (regions.value.length || nodeHostnames.value.length) return
|
||||
try {
|
||||
const data = await useServersFetch<any>('/nodes/overview', { version: 'internal' })
|
||||
regions.value = (data.regions || []).map((r: any) => ({
|
||||
key: r.key,
|
||||
display: `${r.display_name} (${r.key})`,
|
||||
}))
|
||||
nodeHostnames.value = data.node_hostnames || []
|
||||
if (!selectedRegion.value && regions.value.length) selectedRegion.value = regions.value[0]
|
||||
} catch (err) {
|
||||
addNotification({ title: 'Failed to load nodes overview', text: String(err), type: 'error' })
|
||||
}
|
||||
}
|
||||
|
||||
async function apply() {
|
||||
try {
|
||||
const body =
|
||||
mode.value.id === 'nodes'
|
||||
? {
|
||||
nodes: selectedNodes.value.slice(),
|
||||
days: Math.max(1, Math.floor(days.value)),
|
||||
send_email: sendEmail.value,
|
||||
message: message.value?.trim() || DEFAULT_CREDIT_EMAIL_MESSAGE,
|
||||
}
|
||||
: {
|
||||
region: selectedRegion.value!.key,
|
||||
days: Math.max(1, Math.floor(days.value)),
|
||||
send_email: sendEmail.value,
|
||||
message: message.value?.trim() || DEFAULT_CREDIT_EMAIL_MESSAGE,
|
||||
}
|
||||
await useBaseFetch('billing/credit', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
internal: true,
|
||||
})
|
||||
addNotification({ title: 'Credits applied', type: 'success' })
|
||||
batchModal.value?.hide()
|
||||
selectedNodes.value = []
|
||||
nodeInput.value = ''
|
||||
message.value = ''
|
||||
} catch (err: any) {
|
||||
addNotification({
|
||||
title: 'Error applying credits',
|
||||
text: err?.data?.description ?? String(err),
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function pluralize(n: number): string {
|
||||
return n === 1 ? '' : 's'
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.page {
|
||||
padding: 1rem;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
max-width: 56rem;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,25 @@
|
||||
<script setup lang="ts">
|
||||
import { Heading, Text } from '@vue-email/components'
|
||||
|
||||
import StyledEmail from '../shared/StyledEmail.vue'
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<StyledEmail title="We’ve added time to your server">
|
||||
<Heading as="h1" class="mb-2 text-2xl font-bold">We’ve added time to your server</Heading>
|
||||
|
||||
<Text class="text-muted text-base">Hi {user.name},</Text>
|
||||
<Text class="text-muted text-base">{credit.header_message}</Text>
|
||||
|
||||
<Text class="text-muted text-base">
|
||||
To make up for it, we've added {credit.days_formatted} to your {credit.subscription.type}
|
||||
subscription.
|
||||
</Text>
|
||||
<Text class="text-muted text-base">
|
||||
Your next charge was scheduled for {credit.previous_due} and will now be on {credit.next_due}.
|
||||
</Text>
|
||||
|
||||
<Text class="text-muted text-base">Thank you for supporting us,</Text>
|
||||
<Text class="text-muted text-base">The Modrinth Team</Text>
|
||||
</StyledEmail>
|
||||
</template>
|
||||
@@ -18,6 +18,7 @@ export default {
|
||||
|
||||
// Subscriptions
|
||||
'subscription-tax-change': () => import('./account/SubscriptionTaxChange.vue'),
|
||||
'subscription-credited': () => import('./account/SubscriptionCredited.vue'),
|
||||
|
||||
// Moderation
|
||||
'report-submitted': () => import('./moderation/ReportSubmitted.vue'),
|
||||
|
||||
19
apps/labrinth/.sqlx/query-3d05de766a0987028d465a3305938164e4d79734c17c07111f8923f2faa517bf.json
generated
Normal file
19
apps/labrinth/.sqlx/query-3d05de766a0987028d465a3305938164e4d79734c17c07111f8923f2faa517bf.json
generated
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO users_subscriptions_credits\n (subscription_id, user_id, creditor_id, days, previous_due, next_due)\n SELECT * FROM UNNEST($1::bigint[], $2::bigint[], $3::bigint[], $4::int[], $5::timestamptz[], $6::timestamptz[])\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int8Array",
|
||||
"Int8Array",
|
||||
"Int8Array",
|
||||
"Int4Array",
|
||||
"TimestamptzArray",
|
||||
"TimestamptzArray"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "3d05de766a0987028d465a3305938164e4d79734c17c07111f8923f2faa517bf"
|
||||
}
|
||||
27
apps/labrinth/.sqlx/query-68968755bd5eacce9009d1d873d08ef679e6638e57e4711c2c215f32e691d856.json
generated
Normal file
27
apps/labrinth/.sqlx/query-68968755bd5eacce9009d1d873d08ef679e6638e57e4711c2c215f32e691d856.json
generated
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO users_subscriptions_credits\n (subscription_id, user_id, creditor_id, days, previous_due, next_due)\n VALUES ($1, $2, $3, $4, $5, $6)\n RETURNING id\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Int4"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int8",
|
||||
"Int8",
|
||||
"Int8",
|
||||
"Int4",
|
||||
"Timestamptz",
|
||||
"Timestamptz"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "68968755bd5eacce9009d1d873d08ef679e6638e57e4711c2c215f32e691d856"
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n SELECT\n charges.id, charges.user_id, charges.price_id, charges.amount, charges.currency_code, charges.status, charges.due, charges.last_attempt,\n charges.charge_type, charges.subscription_id, charges.tax_amount, charges.tax_platform_id,\n -- Workaround for https://github.com/launchbadge/sqlx/issues/3336\n charges.subscription_interval AS \"subscription_interval?\",\n charges.payment_platform,\n charges.payment_platform_id AS \"payment_platform_id?\",\n charges.parent_charge_id AS \"parent_charge_id?\",\n charges.net AS \"net?\",\n\t\t\t\tcharges.tax_last_updated AS \"tax_last_updated?\",\n\t\t\t\tcharges.tax_drift_loss AS \"tax_drift_loss?\",\n charges.tax_transaction_version AS \"tax_transaction_version?\",\n charges.tax_platform_accounting_time AS \"tax_platform_accounting_time?\"\n FROM charges\n WHERE subscription_id = $1 AND (status = 'open' OR status = 'expiring' OR status = 'cancelled' OR status = 'failed')",
|
||||
"query": "\n SELECT\n charges.id, charges.user_id, charges.price_id, charges.amount, charges.currency_code, charges.status, charges.due, charges.last_attempt,\n charges.charge_type, charges.subscription_id, charges.tax_amount, charges.tax_platform_id,\n -- Workaround for https://github.com/launchbadge/sqlx/issues/3336\n charges.subscription_interval AS \"subscription_interval?\",\n charges.payment_platform,\n charges.payment_platform_id AS \"payment_platform_id?\",\n charges.parent_charge_id AS \"parent_charge_id?\",\n charges.net AS \"net?\",\n\t\t\t\tcharges.tax_last_updated AS \"tax_last_updated?\",\n\t\t\t\tcharges.tax_drift_loss AS \"tax_drift_loss?\",\n charges.tax_transaction_version AS \"tax_transaction_version?\",\n charges.tax_platform_accounting_time AS \"tax_platform_accounting_time?\"\n FROM charges\n WHERE\n\t\t\t subscription_id = $1\n\t\t\t AND (status = 'open' OR status = 'expiring' OR status = 'cancelled' OR status = 'failed')\n\t\t\tORDER BY due ASC LIMIT 1",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -138,5 +138,5 @@
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "ead967d7a8e268a583eb44900a5ef7c45548b4cf3d8cb6545aad801f9fcc5a56"
|
||||
"hash": "91866517bf34fb8bf31a7a49832b18fca60c293ad349eaec07b573d22a28301c"
|
||||
}
|
||||
58
apps/labrinth/.sqlx/query-aafa63e08f9d556dcd55c4687f0323aa502ce9feb3f439a614bb7759dcb03b23.json
generated
Normal file
58
apps/labrinth/.sqlx/query-aafa63e08f9d556dcd55c4687f0323aa502ce9feb3f439a614bb7759dcb03b23.json
generated
Normal file
@@ -0,0 +1,58 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n SELECT us.id, us.user_id, us.price_id, us.interval, us.created, us.status, us.metadata\n FROM users_subscriptions us\n WHERE us.metadata->>'type' = 'pyro' AND us.metadata->>'id' = ANY($1::text[])\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Int8"
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "user_id",
|
||||
"type_info": "Int8"
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "price_id",
|
||||
"type_info": "Int8"
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"name": "interval",
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"name": "created",
|
||||
"type_info": "Timestamptz"
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"name": "status",
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"ordinal": 6,
|
||||
"name": "metadata",
|
||||
"type_info": "Jsonb"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"TextArray"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "aafa63e08f9d556dcd55c4687f0323aa502ce9feb3f439a614bb7759dcb03b23"
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
CREATE TABLE users_subscriptions_credits (
|
||||
id SERIAL PRIMARY KEY,
|
||||
subscription_id BIGINT NOT NULL REFERENCES users_subscriptions (id),
|
||||
user_id BIGINT NOT NULL REFERENCES users (id),
|
||||
creditor_id BIGINT NOT NULL REFERENCES users (id),
|
||||
days INTEGER NOT NULL,
|
||||
previous_due TIMESTAMPTZ NOT NULL,
|
||||
next_due TIMESTAMPTZ NOT NULL,
|
||||
created TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
INSERT INTO notifications_types
|
||||
(name, delivery_priority, expose_in_user_preferences, expose_in_site_notifications)
|
||||
VALUES ('subscription_credited', 1, FALSE, FALSE);
|
||||
|
||||
INSERT INTO users_notifications_preferences (user_id, channel, notification_type, enabled)
|
||||
VALUES (NULL, 'email', 'subscription_credited', TRUE);
|
||||
|
||||
INSERT INTO notifications_templates
|
||||
(channel, notification_type, subject_line, body_fetch_url, plaintext_fallback)
|
||||
VALUES
|
||||
(
|
||||
'email',
|
||||
'subscription_credited',
|
||||
'We’ve added time to your server',
|
||||
'https://modrinth.com/_internal/templates/email/subscription-credited',
|
||||
CONCAT(
|
||||
'Hi {user.name},',
|
||||
CHR(10),
|
||||
CHR(10),
|
||||
'{credit.header_message}',
|
||||
CHR(10),
|
||||
CHR(10),
|
||||
'To make up for it, we''ve added {credit.days_formatted} to your {credit.subscription.type} subscription.',
|
||||
CHR(10),
|
||||
CHR(10),
|
||||
'Your next charge was scheduled for {credit.previous_due} and will now be on {credit.next_due}.',
|
||||
CHR(10),
|
||||
CHR(10),
|
||||
'Thank you for supporting us,',
|
||||
CHR(10),
|
||||
'The Modrinth Team'
|
||||
)
|
||||
);
|
||||
@@ -233,7 +233,10 @@ impl DBCharge {
|
||||
) -> Result<Option<DBCharge>, DatabaseError> {
|
||||
let user_subscription_id = user_subscription_id.0;
|
||||
let res = select_charges_with_predicate!(
|
||||
"WHERE subscription_id = $1 AND (status = 'open' OR status = 'expiring' OR status = 'cancelled' OR status = 'failed')",
|
||||
"WHERE
|
||||
subscription_id = $1
|
||||
AND (status = 'open' OR status = 'expiring' OR status = 'cancelled' OR status = 'failed')
|
||||
ORDER BY due ASC LIMIT 1",
|
||||
user_subscription_id
|
||||
)
|
||||
.fetch_optional(exec)
|
||||
|
||||
@@ -35,6 +35,7 @@ pub mod user_subscription_item;
|
||||
pub mod users_compliance;
|
||||
pub mod users_notifications_preferences_item;
|
||||
pub mod users_redeemals;
|
||||
pub mod users_subscriptions_credits;
|
||||
pub mod version_item;
|
||||
|
||||
pub use affiliate_code_item::DBAffiliateCode;
|
||||
|
||||
@@ -160,6 +160,32 @@ impl DBUserSubscription {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_many_by_server_ids(
|
||||
server_ids: &[String],
|
||||
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<Vec<DBUserSubscription>, DatabaseError> {
|
||||
if server_ids.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let results = sqlx::query_as!(
|
||||
UserSubscriptionQueryResult,
|
||||
r#"
|
||||
SELECT us.id, us.user_id, us.price_id, us.interval, us.created, us.status, us.metadata
|
||||
FROM users_subscriptions us
|
||||
WHERE us.metadata->>'type' = 'pyro' AND us.metadata->>'id' = ANY($1::text[])
|
||||
"#,
|
||||
server_ids
|
||||
)
|
||||
.fetch_all(exec)
|
||||
.await?;
|
||||
|
||||
Ok(results
|
||||
.into_iter()
|
||||
.map(|r| r.try_into())
|
||||
.collect::<Result<Vec<_>, serde_json::Error>>()?)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SubscriptionWithCharge {
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
use crate::database::models::{DBUserId, DBUserSubscriptionId};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::query_scalar;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DBUserSubscriptionCredit {
|
||||
pub id: i32,
|
||||
pub subscription_id: DBUserSubscriptionId,
|
||||
pub user_id: DBUserId,
|
||||
pub creditor_id: DBUserId,
|
||||
pub days: i32,
|
||||
pub previous_due: DateTime<Utc>,
|
||||
pub next_due: DateTime<Utc>,
|
||||
pub created: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl DBUserSubscriptionCredit {
|
||||
/// Inserts this audit entry and sets its id.
|
||||
pub async fn insert<'a, E>(&mut self, exec: E) -> sqlx::Result<()>
|
||||
where
|
||||
E: sqlx::PgExecutor<'a>,
|
||||
{
|
||||
let id = query_scalar!(
|
||||
r#"
|
||||
INSERT INTO users_subscriptions_credits
|
||||
(subscription_id, user_id, creditor_id, days, previous_due, next_due)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id
|
||||
"#,
|
||||
self.subscription_id.0,
|
||||
self.user_id.0,
|
||||
self.creditor_id.0,
|
||||
self.days,
|
||||
self.previous_due,
|
||||
self.next_due,
|
||||
)
|
||||
.fetch_one(exec)
|
||||
.await?;
|
||||
|
||||
self.id = id;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn insert_many(
|
||||
exec: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
subscription_ids: &[DBUserSubscriptionId],
|
||||
user_ids: &[DBUserId],
|
||||
creditor_ids: &[DBUserId],
|
||||
days: &[i32],
|
||||
previous_dues: &[DateTime<Utc>],
|
||||
next_dues: &[DateTime<Utc>],
|
||||
) -> sqlx::Result<()> {
|
||||
debug_assert_eq!(subscription_ids.len(), user_ids.len());
|
||||
debug_assert_eq!(user_ids.len(), creditor_ids.len());
|
||||
debug_assert_eq!(creditor_ids.len(), days.len());
|
||||
debug_assert_eq!(days.len(), previous_dues.len());
|
||||
debug_assert_eq!(previous_dues.len(), next_dues.len());
|
||||
|
||||
let subs: Vec<i64> = subscription_ids.iter().map(|x| x.0).collect();
|
||||
let users: Vec<i64> = user_ids.iter().map(|x| x.0).collect();
|
||||
let creditors: Vec<i64> = creditor_ids.iter().map(|x| x.0).collect();
|
||||
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO users_subscriptions_credits
|
||||
(subscription_id, user_id, creditor_id, days, previous_due, next_due)
|
||||
SELECT * FROM UNNEST($1::bigint[], $2::bigint[], $3::bigint[], $4::int[], $5::timestamptz[], $6::timestamptz[])
|
||||
"#,
|
||||
&subs[..],
|
||||
&users[..],
|
||||
&creditors[..],
|
||||
&days[..],
|
||||
&previous_dues[..],
|
||||
&next_dues[..],
|
||||
)
|
||||
.execute(&mut **exec)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ use crate::database::ReadOnlyPgPool;
|
||||
use crate::queue::billing::{index_billing, index_subscriptions};
|
||||
use crate::queue::moderation::AutomatedModerationQueue;
|
||||
use crate::util::anrok;
|
||||
use crate::util::archon::ArchonClient;
|
||||
use crate::util::env::{parse_strings_from_var, parse_var};
|
||||
use crate::util::ratelimit::{AsyncRateLimiter, GCRAParameters};
|
||||
use sync::friends::handle_pubsub;
|
||||
@@ -64,6 +65,7 @@ pub struct LabrinthConfig {
|
||||
pub stripe_client: stripe::Client,
|
||||
pub anrok_client: anrok::Client,
|
||||
pub email_queue: web::Data<EmailQueue>,
|
||||
pub archon_client: web::Data<ArchonClient>,
|
||||
pub gotenberg_client: GotenbergClient,
|
||||
}
|
||||
|
||||
@@ -283,6 +285,10 @@ pub fn app_setup(
|
||||
stripe_client,
|
||||
anrok_client,
|
||||
gotenberg_client,
|
||||
archon_client: web::Data::new(
|
||||
ArchonClient::from_env()
|
||||
.expect("ARCHON_URL and PYRO_API_KEY must be set"),
|
||||
),
|
||||
email_queue: web::Data::new(email_queue),
|
||||
}
|
||||
}
|
||||
@@ -315,9 +321,9 @@ pub fn app_config(
|
||||
.app_data(web::Data::new(labrinth_config.ip_salt.clone()))
|
||||
.app_data(web::Data::new(labrinth_config.analytics_queue.clone()))
|
||||
.app_data(web::Data::new(labrinth_config.clickhouse.clone()))
|
||||
.app_data(labrinth_config.maxmind.clone())
|
||||
.app_data(labrinth_config.active_sockets.clone())
|
||||
.app_data(labrinth_config.automated_moderation_queue.clone())
|
||||
.app_data(labrinth_config.archon_client.clone())
|
||||
.app_data(web::Data::new(labrinth_config.stripe_client.clone()))
|
||||
.app_data(web::Data::new(labrinth_config.anrok_client.clone()))
|
||||
.app_data(labrinth_config.rate_limiter.clone())
|
||||
@@ -478,7 +484,6 @@ pub fn check_env_vars() -> bool {
|
||||
failed |= check_var::<String>("CLICKHOUSE_PASSWORD");
|
||||
failed |= check_var::<String>("CLICKHOUSE_DATABASE");
|
||||
|
||||
failed |= check_var::<String>("MAXMIND_ACCOUNT_ID");
|
||||
failed |= check_var::<String>("MAXMIND_LICENSE_KEY");
|
||||
|
||||
failed |= check_var::<String>("FLAME_ANVIL_URL");
|
||||
|
||||
@@ -109,6 +109,13 @@ pub enum LegacyNotificationBody {
|
||||
amount: String,
|
||||
service: String,
|
||||
},
|
||||
SubscriptionCredited {
|
||||
subscription_id: UserSubscriptionId,
|
||||
days: i32,
|
||||
previous_due: DateTime<Utc>,
|
||||
next_due: DateTime<Utc>,
|
||||
header_message: Option<String>,
|
||||
},
|
||||
PatCreated {
|
||||
token_name: String,
|
||||
},
|
||||
@@ -219,6 +226,9 @@ impl LegacyNotification {
|
||||
NotificationBody::TaxNotification { .. } => {
|
||||
Some("tax_notification".to_string())
|
||||
}
|
||||
NotificationBody::SubscriptionCredited { .. } => {
|
||||
Some("subscription_credited".to_string())
|
||||
}
|
||||
NotificationBody::PayoutAvailable { .. } => {
|
||||
Some("payout_available".to_string())
|
||||
}
|
||||
@@ -396,6 +406,19 @@ impl LegacyNotification {
|
||||
NotificationBody::PaymentFailed { amount, service } => {
|
||||
LegacyNotificationBody::PaymentFailed { amount, service }
|
||||
}
|
||||
NotificationBody::SubscriptionCredited {
|
||||
subscription_id,
|
||||
days,
|
||||
previous_due,
|
||||
next_due,
|
||||
header_message,
|
||||
} => LegacyNotificationBody::SubscriptionCredited {
|
||||
subscription_id,
|
||||
days,
|
||||
previous_due,
|
||||
next_due,
|
||||
header_message,
|
||||
},
|
||||
NotificationBody::Unknown => LegacyNotificationBody::Unknown,
|
||||
};
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ pub enum NotificationType {
|
||||
PasswordChanged,
|
||||
PasswordRemoved,
|
||||
EmailChanged,
|
||||
SubscriptionCredited,
|
||||
PaymentFailed,
|
||||
TaxNotification,
|
||||
PatCreated,
|
||||
@@ -78,6 +79,7 @@ impl NotificationType {
|
||||
NotificationType::PasswordChanged => "password_changed",
|
||||
NotificationType::PasswordRemoved => "password_removed",
|
||||
NotificationType::EmailChanged => "email_changed",
|
||||
NotificationType::SubscriptionCredited => "subscription_credited",
|
||||
NotificationType::PaymentFailed => "payment_failed",
|
||||
NotificationType::TaxNotification => "tax_notification",
|
||||
NotificationType::PatCreated => "pat_created",
|
||||
@@ -114,6 +116,7 @@ impl NotificationType {
|
||||
"password_changed" => NotificationType::PasswordChanged,
|
||||
"password_removed" => NotificationType::PasswordRemoved,
|
||||
"email_changed" => NotificationType::EmailChanged,
|
||||
"subscription_credited" => NotificationType::SubscriptionCredited,
|
||||
"payment_failed" => NotificationType::PaymentFailed,
|
||||
"tax_notification" => NotificationType::TaxNotification,
|
||||
"payout_available" => NotificationType::PayoutAvailable,
|
||||
@@ -220,6 +223,13 @@ pub enum NotificationBody {
|
||||
new_email: String,
|
||||
to_email: String,
|
||||
},
|
||||
SubscriptionCredited {
|
||||
subscription_id: UserSubscriptionId,
|
||||
days: i32,
|
||||
previous_due: DateTime<Utc>,
|
||||
next_due: DateTime<Utc>,
|
||||
header_message: Option<String>,
|
||||
},
|
||||
PaymentFailed {
|
||||
amount: String,
|
||||
service: String,
|
||||
@@ -312,6 +322,9 @@ impl NotificationBody {
|
||||
NotificationBody::EmailChanged { .. } => {
|
||||
NotificationType::EmailChanged
|
||||
}
|
||||
NotificationBody::SubscriptionCredited { .. } => {
|
||||
NotificationType::SubscriptionCredited
|
||||
}
|
||||
NotificationBody::PaymentFailed { .. } => {
|
||||
NotificationType::PaymentFailed
|
||||
}
|
||||
@@ -554,6 +567,12 @@ impl From<DBNotification> for Notification {
|
||||
"#".to_string(),
|
||||
vec![],
|
||||
),
|
||||
NotificationBody::SubscriptionCredited { .. } => (
|
||||
"Subscription credited".to_string(),
|
||||
"Your subscription has been credited with additional service time.".to_string(),
|
||||
"#".to_string(),
|
||||
vec![],
|
||||
),
|
||||
NotificationBody::PayoutAvailable { .. } => (
|
||||
"Payout available".to_string(),
|
||||
"A payout is available!".to_string(),
|
||||
|
||||
@@ -42,6 +42,12 @@ const TAXNOTIFICATION_BILLING_INTERVAL: &str =
|
||||
const TAXNOTIFICATION_DUE: &str = "taxnotification.due";
|
||||
const TAXNOTIFICATION_SERVICE: &str = "taxnotification.service";
|
||||
|
||||
const CREDIT_DAYS: &str = "credit.days_formatted";
|
||||
const CREDIT_PREVIOUS_DUE: &str = "credit.previous_due";
|
||||
const CREDIT_NEXT_DUE: &str = "credit.next_due";
|
||||
const CREDIT_HEADER_MESSAGE: &str = "credit.header_message";
|
||||
const CREDIT_SUBSCRIPTION_TYPE: &str = "credit.subscription.type";
|
||||
|
||||
const PAYMENTFAILED_AMOUNT: &str = "paymentfailed.amount";
|
||||
const PAYMENTFAILED_SERVICE: &str = "paymentfailed.service";
|
||||
|
||||
@@ -676,6 +682,47 @@ async fn collect_template_variables(
|
||||
Ok(EmailTemplate::Static(map))
|
||||
}
|
||||
|
||||
NotificationBody::SubscriptionCredited {
|
||||
subscription_id,
|
||||
days,
|
||||
previous_due,
|
||||
next_due,
|
||||
header_message,
|
||||
} => {
|
||||
map.insert(
|
||||
CREDIT_DAYS,
|
||||
format!("{days} day{}", if *days == 1 { "" } else { "s" }),
|
||||
);
|
||||
map.insert(CREDIT_PREVIOUS_DUE, date_human_readable(*previous_due));
|
||||
map.insert(CREDIT_NEXT_DUE, date_human_readable(*next_due));
|
||||
map.insert(SUBSCRIPTION_ID, to_base62(subscription_id.0));
|
||||
|
||||
// Only insert header message if provided; frontend sets default fallback
|
||||
if let Some(h) = header_message.clone() {
|
||||
map.insert(CREDIT_HEADER_MESSAGE, h);
|
||||
}
|
||||
|
||||
// Derive subscription type label for templates
|
||||
// Resolve product metadata via price_id join
|
||||
if let Some(info) = crate::database::models::user_subscription_item::DBUserSubscription::get(
|
||||
(*subscription_id).into(),
|
||||
&mut **exec,
|
||||
)
|
||||
.await
|
||||
.ok()
|
||||
.flatten()
|
||||
&& let Ok(Some(pinfo)) = crate::database::models::products_tax_identifier_item::product_info_by_product_price_id(info.price_id, &mut **exec).await {
|
||||
let label = match pinfo.product_metadata {
|
||||
crate::models::billing::ProductMetadata::Pyro { .. } => "server".to_string(),
|
||||
crate::models::billing::ProductMetadata::Medal { .. } => "server".to_string(),
|
||||
crate::models::billing::ProductMetadata::Midas => "Modrinth+".to_string(),
|
||||
};
|
||||
map.insert(CREDIT_SUBSCRIPTION_TYPE, label);
|
||||
}
|
||||
|
||||
Ok(EmailTemplate::Static(map))
|
||||
}
|
||||
|
||||
NotificationBody::Custom {
|
||||
title,
|
||||
body_md,
|
||||
|
||||
@@ -3,6 +3,7 @@ use crate::auth::get_user_from_headers;
|
||||
use crate::database::models::charge_item::DBCharge;
|
||||
use crate::database::models::notification_item::NotificationBuilder;
|
||||
use crate::database::models::products_tax_identifier_item::product_info_by_product_price_id;
|
||||
use crate::database::models::users_subscriptions_credits::DBUserSubscriptionCredit;
|
||||
use crate::database::models::{
|
||||
charge_item, generate_charge_id, product_item, user_subscription_item,
|
||||
};
|
||||
@@ -48,6 +49,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
|
||||
.service(edit_payment_method)
|
||||
.service(remove_payment_method)
|
||||
.service(charges)
|
||||
.service(credit)
|
||||
.service(active_servers)
|
||||
.service(initiate_payment)
|
||||
.service(stripe_webhook)
|
||||
@@ -2188,3 +2190,238 @@ pub async fn stripe_webhook(
|
||||
}
|
||||
|
||||
pub mod payments;
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn apply_credit_many_in_txn(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
redis: &RedisPool,
|
||||
current_user_id: crate::database::models::ids::DBUserId,
|
||||
subscription_ids: Vec<crate::models::ids::UserSubscriptionId>,
|
||||
days: i32,
|
||||
send_email: bool,
|
||||
message: String,
|
||||
) -> Result<(), ApiError> {
|
||||
use crate::database::models::ids::DBUserSubscriptionId;
|
||||
|
||||
let mut credit_sub_ids: Vec<DBUserSubscriptionId> =
|
||||
Vec::with_capacity(subscription_ids.len());
|
||||
let mut credit_user_ids: Vec<crate::database::models::ids::DBUserId> =
|
||||
Vec::with_capacity(subscription_ids.len());
|
||||
let mut credit_creditor_ids: Vec<crate::database::models::ids::DBUserId> =
|
||||
Vec::with_capacity(subscription_ids.len());
|
||||
let mut credit_days: Vec<i32> = Vec::with_capacity(subscription_ids.len());
|
||||
let mut credit_prev_dues: Vec<chrono::DateTime<chrono::Utc>> =
|
||||
Vec::with_capacity(subscription_ids.len());
|
||||
let mut credit_next_dues: Vec<chrono::DateTime<chrono::Utc>> =
|
||||
Vec::with_capacity(subscription_ids.len());
|
||||
|
||||
let subs_ids: Vec<DBUserSubscriptionId> = subscription_ids
|
||||
.iter()
|
||||
.map(|id| DBUserSubscriptionId(id.0 as i64))
|
||||
.collect();
|
||||
let subs = user_subscription_item::DBUserSubscription::get_many(
|
||||
&subs_ids,
|
||||
&mut **transaction,
|
||||
)
|
||||
.await?;
|
||||
|
||||
for subscription in subs {
|
||||
let mut open_charge = charge_item::DBCharge::get_open_subscription(
|
||||
subscription.id,
|
||||
&mut **transaction,
|
||||
)
|
||||
.await?
|
||||
.ok_or_else(|| {
|
||||
ApiError::InvalidInput(format!(
|
||||
"Could not find open charge for subscription {}",
|
||||
to_base62(subscription.id.0 as u64)
|
||||
))
|
||||
})?;
|
||||
|
||||
let previous_due = open_charge.due;
|
||||
open_charge.due = previous_due + Duration::days(days as i64);
|
||||
let next_due = open_charge.due;
|
||||
open_charge.upsert(&mut *transaction).await?;
|
||||
|
||||
credit_sub_ids.push(subscription.id);
|
||||
credit_user_ids.push(subscription.user_id);
|
||||
credit_creditor_ids.push(current_user_id);
|
||||
credit_days.push(days);
|
||||
credit_prev_dues.push(previous_due);
|
||||
credit_next_dues.push(next_due);
|
||||
|
||||
if send_email {
|
||||
NotificationBuilder {
|
||||
body: NotificationBody::SubscriptionCredited {
|
||||
subscription_id: subscription.id.into(),
|
||||
days,
|
||||
previous_due,
|
||||
next_due,
|
||||
header_message: Some(message.clone()),
|
||||
},
|
||||
}
|
||||
.insert(subscription.user_id, &mut *transaction, redis)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
DBUserSubscriptionCredit::insert_many(
|
||||
&mut *transaction,
|
||||
&credit_sub_ids,
|
||||
&credit_user_ids,
|
||||
&credit_creditor_ids,
|
||||
&credit_days,
|
||||
&credit_prev_dues,
|
||||
&credit_next_dues,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(eyre::eyre!(e)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreditRequest {
|
||||
#[serde(flatten)]
|
||||
pub target: CreditTarget,
|
||||
pub days: i32,
|
||||
pub send_email: bool,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum CreditTarget {
|
||||
Subscriptions {
|
||||
subscription_ids: Vec<crate::models::ids::UserSubscriptionId>,
|
||||
},
|
||||
Nodes {
|
||||
nodes: Vec<String>,
|
||||
},
|
||||
Region {
|
||||
region: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[post("credit")]
|
||||
pub async fn credit(
|
||||
req: HttpRequest,
|
||||
pool: web::Data<PgPool>,
|
||||
redis: web::Data<RedisPool>,
|
||||
session_queue: web::Data<AuthQueue>,
|
||||
archon_client: web::Data<crate::util::archon::ArchonClient>,
|
||||
body: web::Json<CreditRequest>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let user = get_user_from_headers(
|
||||
&req,
|
||||
&**pool,
|
||||
&redis,
|
||||
&session_queue,
|
||||
Scopes::SESSION_ACCESS,
|
||||
)
|
||||
.await?
|
||||
.1;
|
||||
|
||||
if !user.role.is_admin() {
|
||||
return Err(ApiError::CustomAuthentication(
|
||||
"You do not have permission to credit subscriptions!".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let CreditRequest {
|
||||
target,
|
||||
days,
|
||||
send_email,
|
||||
message,
|
||||
} = body.into_inner();
|
||||
|
||||
if days <= 0 {
|
||||
return Err(ApiError::InvalidInput(
|
||||
"Days must be greater than zero".to_string(),
|
||||
));
|
||||
}
|
||||
let mut transaction = pool.begin().await?;
|
||||
|
||||
match target {
|
||||
CreditTarget::Subscriptions { subscription_ids } => {
|
||||
if subscription_ids.is_empty() {
|
||||
return Err(ApiError::InvalidInput(
|
||||
"You must specify at least one subscription id".to_string(),
|
||||
));
|
||||
}
|
||||
apply_credit_many_in_txn(
|
||||
&mut transaction,
|
||||
&redis,
|
||||
crate::database::models::ids::DBUserId(user.id.0 as i64),
|
||||
subscription_ids,
|
||||
days,
|
||||
send_email,
|
||||
message,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
CreditTarget::Nodes { nodes } => {
|
||||
if nodes.is_empty() {
|
||||
return Err(ApiError::InvalidInput(
|
||||
"You must specify at least one node hostname".to_string(),
|
||||
));
|
||||
}
|
||||
let mut server_ids: Vec<String> = Vec::new();
|
||||
for hostname in nodes {
|
||||
let ids =
|
||||
archon_client.get_servers_by_hostname(&hostname).await?;
|
||||
server_ids.extend(ids);
|
||||
}
|
||||
server_ids.sort();
|
||||
server_ids.dedup();
|
||||
let subs = user_subscription_item::DBUserSubscription::get_many_by_server_ids(
|
||||
&server_ids,
|
||||
&mut *transaction,
|
||||
)
|
||||
.await?;
|
||||
if subs.is_empty() {
|
||||
return Err(ApiError::InvalidInput(
|
||||
"No subscriptions found for provided nodes".to_string(),
|
||||
));
|
||||
}
|
||||
apply_credit_many_in_txn(
|
||||
&mut transaction,
|
||||
&redis,
|
||||
crate::database::models::ids::DBUserId(user.id.0 as i64),
|
||||
subs.into_iter().map(|s| s.id.into()).collect(),
|
||||
days,
|
||||
send_email,
|
||||
message,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
CreditTarget::Region { region } => {
|
||||
let parsed_active =
|
||||
archon_client.get_active_servers_by_region(®ion).await?;
|
||||
let subs = user_subscription_item::DBUserSubscription::get_many_by_server_ids(
|
||||
&parsed_active,
|
||||
&mut *transaction,
|
||||
)
|
||||
.await?;
|
||||
if subs.is_empty() {
|
||||
return Err(ApiError::InvalidInput(
|
||||
"No subscriptions found for provided region".to_string(),
|
||||
));
|
||||
}
|
||||
apply_credit_many_in_txn(
|
||||
&mut transaction,
|
||||
&redis,
|
||||
crate::database::models::ids::DBUserId(user.id.0 as i64),
|
||||
subs.into_iter().map(|s| s.id.into()).collect(),
|
||||
days,
|
||||
send_email,
|
||||
message,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
transaction.commit().await?;
|
||||
|
||||
Ok(HttpResponse::NoContent().finish())
|
||||
}
|
||||
|
||||
@@ -72,4 +72,58 @@ impl ArchonClient {
|
||||
|
||||
Ok(response.json::<CreateServerResponse>().await?.uuid)
|
||||
}
|
||||
|
||||
pub async fn get_servers_by_hostname(
|
||||
&self,
|
||||
hostname: &str,
|
||||
) -> Result<Vec<String>, reqwest::Error> {
|
||||
#[derive(Deserialize)]
|
||||
struct NodeByHostnameResponse {
|
||||
servers: Vec<NodeServerEntry>,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
struct NodeServerEntry {
|
||||
id: String,
|
||||
#[allow(dead_code)]
|
||||
available: Option<bool>,
|
||||
}
|
||||
|
||||
let res = self
|
||||
.client
|
||||
.get(format!(
|
||||
"{}/_internal/nodes/by-hostname/{}",
|
||||
self.base_url, hostname
|
||||
))
|
||||
.header(X_MASTER_KEY, &self.pyro_api_key)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
let parsed: NodeByHostnameResponse = res.json().await?;
|
||||
Ok(parsed.servers.into_iter().map(|s| s.id).collect())
|
||||
}
|
||||
|
||||
pub async fn get_active_servers_by_region(
|
||||
&self,
|
||||
region: &str,
|
||||
) -> Result<Vec<String>, reqwest::Error> {
|
||||
#[derive(Deserialize)]
|
||||
struct RegionResponse {
|
||||
active_servers: Vec<String>,
|
||||
}
|
||||
|
||||
let res = self
|
||||
.client
|
||||
.get(format!(
|
||||
"{}/_internal/nodes/regions/{}",
|
||||
self.base_url, region
|
||||
))
|
||||
.header(X_MASTER_KEY, &self.pyro_api_key)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
let parsed: RegionResponse = res.json().await?;
|
||||
Ok(parsed.active_servers)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -373,3 +373,5 @@ export function arrayBufferToBase64(buffer: Uint8Array | ArrayBuffer): string {
|
||||
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer)
|
||||
return btoa(String.fromCharCode(...bytes))
|
||||
}
|
||||
export const DEFAULT_CREDIT_EMAIL_MESSAGE =
|
||||
"We're really sorry about the recent issues with your server."
|
||||
|
||||
Reference in New Issue
Block a user