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 generateUuid, JsonListConverter, StringListConverter;
|
2025-12-13 13:27:05 +00:00
|
|
|
|
import 'users.dart';
|
|
|
|
|
|
|
|
|
|
|
|
/// Таблица PromoCodesCampaigns - кампании промокодов
|
|
|
|
|
|
class PromoCodesCampaigns 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
|
|
|
|
|
|
|
|
|
|
TextColumn get template => text()();
|
|
|
|
|
|
TextColumn get name => text().nullable()();
|
|
|
|
|
|
|
|
|
|
|
|
// Продукты (JSON array)
|
|
|
|
|
|
TextColumn get products => text()
|
|
|
|
|
|
.withDefault(const Constant('[]'))
|
|
|
|
|
|
.map(const JsonListConverter())();
|
|
|
|
|
|
|
|
|
|
|
|
IntColumn get activationsPerCode => integer()();
|
|
|
|
|
|
IntColumn get activationsPerUser => integer()();
|
|
|
|
|
|
IntColumn get generationSize => integer()();
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
// Статус (enum as string)
|
|
|
|
|
|
TextColumn get status => text()(); // created, preparing, ready, active, disabled
|
|
|
|
|
|
|
|
|
|
|
|
// Теги (JSON array)
|
|
|
|
|
|
TextColumn get tags => text()
|
|
|
|
|
|
.withDefault(const Constant('[]'))
|
|
|
|
|
|
.map(const StringListConverter())();
|
|
|
|
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
.withDefault(const Constant(false))
|
2025-12-13 23:35:14 +00:00
|
|
|
|
.customConstraint('')(); // PostgreSQL использует нативный BOOLEAN
|
2025-12-13 20:55:50 +00:00
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
Set<Column> get primaryKey => {id};
|
2025-12-13 13:27:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Таблица PromoCodes - промокоды
|
|
|
|
|
|
class PromoCodes 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 campaignId => text()
|
|
|
|
|
|
.references(PromoCodesCampaigns, #id, onDelete: KeyAction.cascade)();
|
2025-12-13 13:27:05 +00:00
|
|
|
|
|
|
|
|
|
|
TextColumn get code => text().unique()();
|
|
|
|
|
|
IntColumn get activations => integer().withDefault(const Constant(0))();
|
|
|
|
|
|
|
|
|
|
|
|
// Индивидуальный промокод (связан с пользователем)
|
2025-12-13 23:35:14 +00:00
|
|
|
|
TextColumn get userId => text()
|
|
|
|
|
|
.nullable()
|
|
|
|
|
|
.references(Users, #id, onDelete: KeyAction.cascade)();
|
2025-12-13 13:27:05 +00:00
|
|
|
|
|
|
|
|
|
|
// 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
|