mnemo_cards/mnemo_cards_backend/lib/database/tables/discounts.dart
Dmitry f81e405d4f
Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (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
bool
2025-12-14 01:00:36 +03:00

69 lines
2.6 KiB
Dart

import 'package:drift/drift.dart';
import '../converters.dart' show generateUuid, StringListConverter, JsonListConverter;
import '../postgres_constants.dart';
import 'users.dart';
/// Таблица DiscountCampaigns - кампании скидок
class DiscountCampaigns extends Table {
TextColumn get id => text().withDefault(const Constant('gen_random_uuid()'))();
TextColumn get name => text().nullable()();
DateTimeColumn get start => dateTime()();
DateTimeColumn get finish => dateTime()();
// Статус (enum as string)
TextColumn get status => text()(); // created, active, expired, disabled
// Теги (JSON array)
TextColumn get tags => text()
.withDefault(const Constant('[]'))
.map(const StringListConverter())();
// Audit
DateTimeColumn get createdAt => dateTime().withDefault(currentTimestamp)();
DateTimeColumn get updatedAt => dateTime().withDefault(currentTimestamp)();
BoolColumn get isDeleted => boolean()
.withDefault(const Constant(false))
.customConstraint('')(); // PostgreSQL использует нативный BOOLEAN, не нужен CHECK
@override
Set<Column> get primaryKey => {id};
}
/// Таблица Discounts - скидки
class Discounts extends Table {
TextColumn get id => text().withDefault(const Constant('gen_random_uuid()'))();
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
DateTimeColumn get createdAt => dateTime().withDefault(currentTimestamp)();
DateTimeColumn get updatedAt => dateTime().withDefault(currentTimestamp)();
BoolColumn get isDeleted => boolean()
.withDefault(const Constant(false))
.customConstraint('')(); // PostgreSQL использует нативный BOOLEAN, не нужен CHECK
@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)();
DateTimeColumn get grantedAt => dateTime().withDefault(currentTimestamp)();
@override
Set<Column> get primaryKey => {discountId, userId};
}
// Конвертеры импортированы из converters.dart