mnemo_cards/mnemo_cards_backend/lib/database/tables/subscriptions.dart

70 lines
2.6 KiB
Dart
Raw Normal View History

2025-12-13 13:27:05 +00:00
import 'package:drift/drift.dart';
2025-12-13 23:35:14 +00:00
import 'package:drift_postgres/drift_postgres.dart';
2025-12-13 20:55:50 +00:00
import '../converters.dart' show JsonMapConverter, JsonListConverter;
2025-12-13 13:27:05 +00:00
import 'users.dart';
/// Таблица SubscriptionPlans - планы подписки
class SubscriptionPlans extends Table {
2025-12-13 23:35:14 +00:00
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
TextColumn get id => text()
.withDefault(const CustomExpression('gen_random_uuid()::text'))();
2025-12-13 13:27:05 +00:00
// UI информация (JSON)
2025-12-13 20:55:50 +00:00
TextColumn get ui => text().nullable().map(const JsonMapConverter())();
2025-12-13 13:27:05 +00:00
// Цена и валюта
TextColumn get price => text()();
TextColumn get currency => text()();
IntColumn get durationDays => integer()();
// Функции подписки (JSON array)
TextColumn get features => text()
.withDefault(const Constant('[]'))
.map(const JsonListConverter())();
// Платежная система
TextColumn get paymentId => text().nullable()();
TextColumn get paymentSystem => text()(); // enum as string
// Audit
2025-12-13 23:35:14 +00:00
Column<PgDateTime> get createdAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
Column<PgDateTime> get updatedAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
2025-12-13 22:00:36 +00:00
BoolColumn get isDeleted => boolean()
2025-12-14 21:55:42 +00:00
.customConstraint('NOT NULL DEFAULT FALSE')();
2025-12-14 21:45:52 +00:00
// PostgreSQL использует нативный BOOLEAN
2025-12-13 20:55:50 +00:00
@override
Set<Column> get primaryKey => {id};
2025-12-13 13:27:05 +00:00
}
/// Таблица UserSubscriptions - подписки пользователей
class UserSubscriptions extends Table {
2025-12-13 23:35:14 +00:00
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
TextColumn get id => text()
.withDefault(const CustomExpression('gen_random_uuid()::text'))();
TextColumn get userId => text()
.unique()
.references(Users, #id, onDelete: KeyAction.cascade)();
2025-12-13 13:27:05 +00:00
2025-12-13 23:35:14 +00:00
Column<PgDateTime> get start => customType(PgTypes.timestampWithTimezone)();
Column<PgDateTime> get finish => customType(PgTypes.timestampWithTimezone)();
2025-12-13 13:27:05 +00:00
// Функции подписки (JSON array)
TextColumn get features => text()
.withDefault(const Constant('[]'))
.map(const JsonListConverter())();
// Audit
2025-12-13 23:35:14 +00:00
Column<PgDateTime> get createdAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
Column<PgDateTime> get updatedAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
2025-12-13 20:55:50 +00:00
@override
Set<Column> get primaryKey => {id};
2025-12-13 13:27:05 +00:00
}
// Конвертеры импортированы из converters.dart