A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(api): add operator pass tables and withdrawal rules
Hampus4e6b6fd444ef
diff --git a/fluxer_api/src/api/operator_pass/OperatorPassPurchaseRules.ts b/fluxer_api/src/api/operator_pass/OperatorPassPurchaseRules.tsnew file mode 100644index 000000000..f59f6c42c--- /dev/null+++ b/fluxer_api/src/api/operator_pass/OperatorPassPurchaseRules.ts@@ -0,0 +1,118 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {getWithdrawalPeriodEndsAt} from '@app/api/billing/WithdrawalPeriod';+import type {OperatorPassPurchaseRow, OperatorPassUnitRow} from '@app/api/database/types/OperatorPassTypes';+import type {+ OperatorPassCurrency,+ OperatorPassPurchaseResponse,+ OperatorPassPurchaseStatus,+} from '@fluxer/schema/src/domains/operator_pass/OperatorPassSchemas';++const WITHDRAWABLE_STATUSES: ReadonlyArray<OperatorPassPurchaseStatus> = ['completed', 'disputed'];+const UNDELIVERED_WITHDRAWABLE_STATUSES: ReadonlyArray<OperatorPassPurchaseStatus> = [+ 'pending',+ ...WITHDRAWABLE_STATUSES,+];+const DAY_MS = 24 * 60 * 60 * 1000;++const COUNTRY_DEADLINE_UTC_OFFSET_MS = -5 * 60 * 60 * 1000;++export function getOperatorPassWithdrawalEndsOn(endsAt: Date): string {+ return new Date(endsAt.getTime() + COUNTRY_DEADLINE_UTC_OFFSET_MS).toISOString().slice(0, 10);+}++export function getOperatorPassCountryWithdrawalDeadline(endsAt: Date): Date {+ const [year, month, day] = getOperatorPassWithdrawalEndsOn(endsAt).split('-').map(Number) as [number, number, number];+ return new Date(Date.UTC(year, month - 1, day + 1) - COUNTRY_DEADLINE_UTC_OFFSET_MS);+}++export function getOperatorPassWithdrawalDeadline(purchase: OperatorPassPurchaseRow): Date | null {+ if (!purchase.withdrawal_ends_at) return null;+ return purchase.withdrawal_days != null+ ? getOperatorPassCountryWithdrawalDeadline(purchase.withdrawal_ends_at)+ : purchase.withdrawal_ends_at;+}++export function isOperatorPassWithinWithdrawalDeadline(purchase: OperatorPassPurchaseRow, receivedAt: Date): boolean {+ const deadline = getOperatorPassWithdrawalDeadline(purchase);+ return deadline != null && receivedAt.getTime() < deadline.getTime();+}++export function getOperatorPassCountryWithdrawalEndsAt(purchasedAt: Date, days: number | null): Date {+ return days === null ? getWithdrawalPeriodEndsAt(purchasedAt) : new Date(purchasedAt.getTime() + days * DAY_MS);+}++export function isOperatorPassPurchaseWithdrawable(+ purchase: OperatorPassPurchaseRow,+ units: ReadonlyArray<OperatorPassUnitRow>,+ receivedAt: Date,+): boolean {+ return (+ UNDELIVERED_WITHDRAWABLE_STATUSES.includes(purchase.status) &&+ purchase.purchased_at != null &&+ purchase.payment_intent_id != null &&+ purchase.perks_granted_at == null &&+ units.length === 0 &&+ isOperatorPassWithinWithdrawalDeadline(purchase, receivedAt)+ );+}++export function listOperatorPassCountryWithdrawableUnits(+ purchase: OperatorPassPurchaseRow,+ units: ReadonlyArray<OperatorPassUnitRow>,+ receivedAt: Date,+): Array<OperatorPassUnitRow> {+ return units.filter((unit) => isOperatorPassUnitWithdrawable(purchase, unit, receivedAt));+}++export function isOperatorPassUnitWithdrawable(+ purchase: OperatorPassPurchaseRow,+ unit: OperatorPassUnitRow,+ receivedAt: Date,+): boolean {+ return (+ WITHDRAWABLE_STATUSES.includes(purchase.status) &&+ unit.status === 'held' &&+ unit.source === 'purchase' &&+ unit.holder_id === purchase.purchaser_id &&+ purchase.withdrawal_days != null &&+ isOperatorPassWithinWithdrawalDeadline(purchase, receivedAt)+ );+}++export function getOperatorPassWithdrawalRightEndsAt(purchase: OperatorPassPurchaseRow): Date | null {+ if (purchase.withdrawal_days != null) {+ return getOperatorPassWithdrawalDeadline(purchase);+ }+ return purchase.perks_granted_at ?? purchase.withdrawal_ends_at ?? null;+}++export function mapOperatorPassPurchaseToResponse(+ purchase: OperatorPassPurchaseRow,+ units: ReadonlyArray<OperatorPassUnitRow>,+ now: Date,+): OperatorPassPurchaseResponse {+ const withdrawable = isOperatorPassPurchaseWithdrawable(purchase, units, now);+ return {+ id: purchase.checkout_session_id,+ status: purchase.status,+ quantity: Math.max(1, purchase.quantity ?? 1),+ amount_minor: purchase.amount_minor,+ currency: purchase.currency as OperatorPassCurrency,+ purchased_at: purchase.purchased_at?.toISOString() ?? null,+ delivered_at: purchase.perks_granted_at?.toISOString() ?? null,+ waiver_consent_at: purchase.waiver_consent_at?.toISOString() ?? null,+ withdrawal_country: purchase.withdrawal_country ?? null,+ withdrawal_ends_at:+ withdrawable || purchase.withdrawal_days != null+ ? (getOperatorPassWithdrawalDeadline(purchase)?.toISOString() ?? null)+ : null,+ withdrawal_ends_on:+ purchase.withdrawal_days != null && purchase.withdrawal_ends_at+ ? getOperatorPassWithdrawalEndsOn(purchase.withdrawal_ends_at)+ : null,+ withdrawn_at: purchase.withdrawn_at?.toISOString() ?? null,+ has_donation: purchase.subscription_id != null,+ can_withdraw: withdrawable,+ };+}diff --git a/fluxer_api/src/api/operator_pass/OperatorPassSecrets.ts b/fluxer_api/src/api/operator_pass/OperatorPassSecrets.tsnew file mode 100644index 000000000..c55f56044--- /dev/null+++ b/fluxer_api/src/api/operator_pass/OperatorPassSecrets.ts@@ -0,0 +1,12 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {createHmac} from 'node:crypto';+import {Config} from '@app/api/Config';++export const OPERATOR_PASS_QUOTE_SECRET_LABEL = 'fluxer-operator-pass-quote-v1';+export const OPERATOR_PASS_UNSUBSCRIBE_SECRET_LABEL = 'fluxer-operator-pass-unsubscribe-v1';+export const OPERATOR_PASS_FOLLOWUP_SECRET_LABEL = 'fluxer-operator-pass-waitlist-followup-v1';++export function deriveOperatorPassSecret(label: string): string {+ return createHmac('sha256', Config.auth.sudoModeSecret).update(label).digest('hex');+}diff --git a/fluxer_api/src/api/operator_pass/OperatorPassTables.ts b/fluxer_api/src/api/operator_pass/OperatorPassTables.tsnew file mode 100644index 000000000..ff6b4e6e8--- /dev/null+++ b/fluxer_api/src/api/operator_pass/OperatorPassTables.ts@@ -0,0 +1,238 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {defineTable} from '@app/api/database/CassandraTableDsl';+import {+ OPERATOR_PASS_COLUMNS,+ OPERATOR_PASS_OFFER_BY_USER_COLUMNS,+ OPERATOR_PASS_OFFER_COLUMNS,+ OPERATOR_PASS_PURCHASE_BY_PAYMENT_INTENT_COLUMNS,+ OPERATOR_PASS_PURCHASE_BY_PURCHASER_COLUMNS,+ OPERATOR_PASS_PURCHASE_COLUMNS,+ OPERATOR_PASS_RELEASE_COLUMNS,+ OPERATOR_PASS_REMOVAL_COLUMNS,+ OPERATOR_PASS_REMOVAL_HISTORY_COLUMNS,+ OPERATOR_PASS_UNIT_BY_HOLDER_COLUMNS,+ OPERATOR_PASS_UNIT_BY_PURCHASE_COLUMNS,+ OPERATOR_PASS_UNIT_COLUMNS,+ OPERATOR_PASS_UNIT_EVENT_COLUMNS,+ OPERATOR_PASS_WAITLIST_BY_ENTRY_ID_COLUMNS,+ OPERATOR_PASS_WAITLIST_COLUMNS,+ OPERATOR_PASS_WALL_AUDIT_LOG_COLUMNS,+ OPERATOR_PASS_WALL_AVATAR_REF_COLUMNS,+ OPERATOR_PASS_WALL_DECISION_COLUMNS,+ OPERATOR_PASS_WALL_ENTRY_COLUMNS,+ OPERATOR_PASS_WALL_EVENT_COLUMNS,+ OPERATOR_PASS_WALL_PENDING_COLUMNS,+ OPERATOR_PASS_WALL_REVISION_COLUMNS,+ OPERATOR_PASS_WALL_STATE_COLUMNS,+ OPERATOR_PASS_WALL_TOMBSTONE_COLUMNS,+ type OperatorPassOfferByUserRow,+ type OperatorPassOfferRow,+ type OperatorPassPurchaseByPaymentIntentRow,+ type OperatorPassPurchaseByPurchaserRow,+ type OperatorPassPurchaseRow,+ type OperatorPassReleaseRow,+ type OperatorPassRemovalHistoryRow,+ type OperatorPassRemovalRow,+ type OperatorPassRow,+ type OperatorPassUnitByHolderRow,+ type OperatorPassUnitByPurchaseRow,+ type OperatorPassUnitEventRow,+ type OperatorPassUnitRow,+ type OperatorPassWaitlistByEntryIdRow,+ type OperatorPassWaitlistRow,+ type OperatorPassWallAuditLogRow,+ type OperatorPassWallAvatarRefRow,+ type OperatorPassWallDecisionRow,+ type OperatorPassWallEntryRow,+ type OperatorPassWallEventRow,+ type OperatorPassWallPendingRow,+ type OperatorPassWallRevisionRow,+ type OperatorPassWallStateRow,+ type OperatorPassWallTombstoneRow,+} from '@app/api/database/types/OperatorPassTypes';++export const OperatorPassWaitlist = defineTable<OperatorPassWaitlistRow, 'email'>({+ name: 'operator_pass_waitlist',+ columns: OPERATOR_PASS_WAITLIST_COLUMNS,+ primaryKey: ['email'],+});++export const OperatorPassWaitlistByEntryId = defineTable<OperatorPassWaitlistByEntryIdRow, 'entry_id'>({+ name: 'operator_pass_waitlist_by_entry_id',+ columns: OPERATOR_PASS_WAITLIST_BY_ENTRY_ID_COLUMNS,+ primaryKey: ['entry_id'],+});++export const OperatorPasses = defineTable<OperatorPassRow, 'user_id'>({+ name: 'operator_passes',+ columns: OPERATOR_PASS_COLUMNS,+ primaryKey: ['user_id'],+});++export const OperatorPassPurchases = defineTable<OperatorPassPurchaseRow, 'checkout_session_id'>({+ name: 'operator_pass_purchases',+ columns: OPERATOR_PASS_PURCHASE_COLUMNS,+ primaryKey: ['checkout_session_id'],+});++export const OperatorPassPurchasesByPaymentIntent = defineTable<+ OperatorPassPurchaseByPaymentIntentRow,+ 'payment_intent_id'+>({+ name: 'operator_pass_purchases_by_payment_intent',+ columns: OPERATOR_PASS_PURCHASE_BY_PAYMENT_INTENT_COLUMNS,+ primaryKey: ['payment_intent_id'],+});++export const OperatorPassPurchasesByPurchaser = defineTable<+ OperatorPassPurchaseByPurchaserRow,+ 'purchaser_id' | 'checkout_session_id',+ 'purchaser_id'+>({+ name: 'operator_pass_purchases_by_purchaser',+ columns: OPERATOR_PASS_PURCHASE_BY_PURCHASER_COLUMNS,+ primaryKey: ['purchaser_id', 'checkout_session_id'],+ partitionKey: ['purchaser_id'],+});++export const OperatorPassRemovals = defineTable<OperatorPassRemovalRow, 'user_id'>({+ name: 'operator_pass_removals',+ columns: OPERATOR_PASS_REMOVAL_COLUMNS,+ primaryKey: ['user_id'],+});++export const OperatorPassRemovalHistory = defineTable<+ OperatorPassRemovalHistoryRow,+ 'user_id' | 'removed_at',+ 'user_id'+>({+ name: 'operator_pass_removal_history',+ columns: OPERATOR_PASS_REMOVAL_HISTORY_COLUMNS,+ primaryKey: ['user_id', 'removed_at'],+ partitionKey: ['user_id'],+});++export const OperatorPassWallEntries = defineTable<OperatorPassWallEntryRow, 'user_id'>({+ name: 'operator_pass_wall_entries',+ columns: OPERATOR_PASS_WALL_ENTRY_COLUMNS,+ primaryKey: ['user_id'],+});++export const OperatorPassWallEvents = defineTable<+ OperatorPassWallEventRow,+ 'user_id' | 'occurred_at' | 'event_id',+ 'user_id'+>({+ name: 'operator_pass_wall_events',+ columns: OPERATOR_PASS_WALL_EVENT_COLUMNS,+ primaryKey: ['user_id', 'occurred_at', 'event_id'],+ partitionKey: ['user_id'],+});++export const OperatorPassReleases = defineTable<OperatorPassReleaseRow, 'year'>({+ name: 'operator_pass_releases',+ columns: OPERATOR_PASS_RELEASE_COLUMNS,+ primaryKey: ['year'],+});++export const OperatorPassUnits = defineTable<OperatorPassUnitRow, 'pass_id'>({+ name: 'operator_pass_units',+ columns: OPERATOR_PASS_UNIT_COLUMNS,+ primaryKey: ['pass_id'],+});++export const OperatorPassUnitsByHolder = defineTable<OperatorPassUnitByHolderRow, 'holder_id' | 'pass_id', 'holder_id'>(+ {+ name: 'operator_pass_units_by_holder',+ columns: OPERATOR_PASS_UNIT_BY_HOLDER_COLUMNS,+ primaryKey: ['holder_id', 'pass_id'],+ partitionKey: ['holder_id'],+ },+);++export const OperatorPassUnitsByPurchase = defineTable<+ OperatorPassUnitByPurchaseRow,+ 'purchase_id' | 'pass_id',+ 'purchase_id'+>({+ name: 'operator_pass_units_by_purchase',+ columns: OPERATOR_PASS_UNIT_BY_PURCHASE_COLUMNS,+ primaryKey: ['purchase_id', 'pass_id'],+ partitionKey: ['purchase_id'],+});++export const OperatorPassOffers = defineTable<OperatorPassOfferRow, 'offer_id'>({+ name: 'operator_pass_offers',+ columns: OPERATOR_PASS_OFFER_COLUMNS,+ primaryKey: ['offer_id'],+});++export const OperatorPassOffersByUser = defineTable<OperatorPassOfferByUserRow, 'user_id' | 'offer_id', 'user_id'>({+ name: 'operator_pass_offers_by_user',+ columns: OPERATOR_PASS_OFFER_BY_USER_COLUMNS,+ primaryKey: ['user_id', 'offer_id'],+ partitionKey: ['user_id'],+});++export const OperatorPassUnitEvents = defineTable<+ OperatorPassUnitEventRow,+ 'pass_id' | 'occurred_at' | 'event_id',+ 'pass_id'+>({+ name: 'operator_pass_unit_events',+ columns: OPERATOR_PASS_UNIT_EVENT_COLUMNS,+ primaryKey: ['pass_id', 'occurred_at', 'event_id'],+ partitionKey: ['pass_id'],+});++export const OperatorPassWallRevisions = defineTable<OperatorPassWallRevisionRow, 'user_id' | 'revision_id', 'user_id'>(+ {+ name: 'operator_pass_wall_revisions',+ columns: OPERATOR_PASS_WALL_REVISION_COLUMNS,+ primaryKey: ['user_id', 'revision_id'],+ partitionKey: ['user_id'],+ },+);++export const OperatorPassWallDecisions = defineTable<OperatorPassWallDecisionRow, 'user_id' | 'decision_id', 'user_id'>(+ {+ name: 'operator_pass_wall_decisions',+ columns: OPERATOR_PASS_WALL_DECISION_COLUMNS,+ primaryKey: ['user_id', 'decision_id'],+ partitionKey: ['user_id'],+ },+);++export const OperatorPassWallAvatarRefs = defineTable<OperatorPassWallAvatarRefRow, 'avatar' | 'user_id', 'avatar'>({+ name: 'operator_pass_wall_avatar_refs',+ columns: OPERATOR_PASS_WALL_AVATAR_REF_COLUMNS,+ primaryKey: ['avatar', 'user_id'],+ partitionKey: ['avatar'],+});++export const OperatorPassWallPending = defineTable<OperatorPassWallPendingRow, 'bucket' | 'user_id', 'bucket'>({+ name: 'operator_pass_wall_pending',+ columns: OPERATOR_PASS_WALL_PENDING_COLUMNS,+ primaryKey: ['bucket', 'user_id'],+ partitionKey: ['bucket'],+});++export const OperatorPassWallAuditLogs = defineTable<OperatorPassWallAuditLogRow, 'user_id' | 'log_id', 'user_id'>({+ name: 'operator_pass_wall_audit_logs',+ columns: OPERATOR_PASS_WALL_AUDIT_LOG_COLUMNS,+ primaryKey: ['user_id', 'log_id'],+ partitionKey: ['user_id'],+});++export const OperatorPassWallState = defineTable<OperatorPassWallStateRow, 'id'>({+ name: 'operator_pass_wall_state',+ columns: OPERATOR_PASS_WALL_STATE_COLUMNS,+ primaryKey: ['id'],+});++export const OperatorPassWallTombstones = defineTable<OperatorPassWallTombstoneRow, 'token_'>({+ name: 'operator_pass_wall_tombstones',+ columns: OPERATOR_PASS_WALL_TOMBSTONE_COLUMNS,+ primaryKey: ['token_'],+});diff --git a/fluxer_api/src/api/operator_pass/OperatorPassWallConstants.ts b/fluxer_api/src/api/operator_pass/OperatorPassWallConstants.tsnew file mode 100644index 000000000..1b6d27fdb--- /dev/null+++ b/fluxer_api/src/api/operator_pass/OperatorPassWallConstants.ts@@ -0,0 +1,9 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {MS_PER_HOUR} from '@fluxer/date_utils/src/DateConstants';++export const OPERATOR_PASS_WALL_FALLBACK_NAME = 'Operator';++export const OPERATOR_PASS_WALL_REVIEW_TARGET_MS = 72 * MS_PER_HOUR;++export const OPERATOR_PASS_WALL_UPDATE_ATTEMPTS = 5;