Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
1.0 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import {mapStripeChargeToRow} from '@app/api/billing/mappers/StripeToBillingMapper';
import {upsertMirrorRow} from '@app/api/billing/repositories/BillingRepoHelpers';
import {fetchOne} from '@app/api/database/CassandraQueryExecution';
import type {BillingChargeRow} from '@app/api/database/types/BillingTypes';
import {BillingCharges} from '@app/api/Tables';
import type Stripe from 'stripe';

const FETCH_BY_ID = BillingCharges.selectCql({
	where: BillingCharges.where.eq('provider_id'),
	limit: 1,
});

export class BillingChargeRepository {
	async findById(providerId: string): Promise<BillingChargeRow | null> {
		return fetchOne<BillingChargeRow>(FETCH_BY_ID, {provider_id: providerId});
	}

	async upsertFromStripe(c: Stripe.Charge): Promise<{
		changed: boolean;
		row: BillingChargeRow;
	}> {
		const mapped = mapStripeChargeToRow(c);
		const existing = await this.findById(mapped.primary.provider_id);
		return upsertMirrorRow(BillingCharges, existing, mapped.primary);
	}
}