Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Deploy Admin Panel / Deploy Admin Panel (push) Waiting to run
Deploy Admin Panel / Admin Panel Verification (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions
81 lines
3.1 KiB
Dart
81 lines
3.1 KiB
Dart
import 'package:drift/drift.dart';
|
|
import 'package:drift_postgres/drift_postgres.dart';
|
|
import '../converters.dart' show generateUuid, StringListConverter, JsonListConverter;
|
|
import 'users.dart';
|
|
|
|
/// Таблица DiscountCampaigns - кампании скидок
|
|
class DiscountCampaigns extends Table {
|
|
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
|
|
TextColumn get id => text()
|
|
.withDefault(const CustomExpression('gen_random_uuid()::text'))();
|
|
|
|
TextColumn get name => text().nullable()();
|
|
|
|
Column<PgDateTime> get start => customType(PgTypes.timestampWithTimezone)();
|
|
Column<PgDateTime> get finish => customType(PgTypes.timestampWithTimezone)();
|
|
|
|
// Статус (enum as string)
|
|
TextColumn get status => text()(); // created, active, expired, disabled
|
|
|
|
// Теги (JSON array)
|
|
TextColumn get tags => text()
|
|
.withDefault(const Constant('[]'))
|
|
.map(const StringListConverter())();
|
|
|
|
// Audit
|
|
Column<PgDateTime> get createdAt => customType(PgTypes.timestampWithTimezone)
|
|
.withDefault(now())();
|
|
Column<PgDateTime> get updatedAt => customType(PgTypes.timestampWithTimezone)
|
|
.withDefault(now())();
|
|
BoolColumn get isDeleted => boolean()
|
|
.withDefault(const Constant(false))
|
|
.customConstraint('')(); // PostgreSQL использует нативный BOOLEAN
|
|
|
|
@override
|
|
Set<Column> get primaryKey => {id};
|
|
}
|
|
|
|
/// Таблица Discounts - скидки
|
|
class Discounts extends Table {
|
|
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
|
|
TextColumn get id => text()
|
|
.withDefault(const CustomExpression('gen_random_uuid()::text'))();
|
|
TextColumn get campaignId => text()
|
|
.references(DiscountCampaigns, #id, onDelete: KeyAction.cascade)();
|
|
|
|
// Процент скидки (0-100)
|
|
RealColumn get discountPercent => real()();
|
|
|
|
// Продукты (JSON array)
|
|
TextColumn get products => text()
|
|
.withDefault(const Constant('[]'))
|
|
.map(const JsonListConverter())();
|
|
|
|
// Audit
|
|
Column<PgDateTime> get createdAt => customType(PgTypes.timestampWithTimezone)
|
|
.withDefault(now())();
|
|
Column<PgDateTime> get updatedAt => customType(PgTypes.timestampWithTimezone)
|
|
.withDefault(now())();
|
|
BoolColumn get isDeleted => boolean()
|
|
.withDefault(const Constant(false))
|
|
.customConstraint('')(); // PostgreSQL использует нативный BOOLEAN
|
|
|
|
@override
|
|
Set<Column> get primaryKey => {id};
|
|
}
|
|
|
|
/// Junction table для связи Discounts ↔ UserDatas (many-to-many)
|
|
class DiscountUserDatas extends Table {
|
|
TextColumn get discountId => text()
|
|
.references(Discounts, #id, onDelete: KeyAction.cascade)();
|
|
TextColumn get userId => text()
|
|
.references(Users, #id, onDelete: KeyAction.cascade)();
|
|
|
|
Column<PgDateTime> get grantedAt => customType(PgTypes.timestampWithTimezone)
|
|
.withDefault(now())();
|
|
|
|
@override
|
|
Set<Column> get primaryKey => {discountId, userId};
|
|
}
|
|
|
|
// Конвертеры импортированы из converters.dart
|