Skip to content

Commit 1a804a5

Browse files
committed
shopinbit refactor wip
1 parent 0042ca9 commit 1a804a5

47 files changed

Lines changed: 2909 additions & 4148 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/db/drift/shared_db/shared_database.dart

Lines changed: 176 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import 'package:drift/drift.dart';
22
import 'package:drift_flutter/drift_flutter.dart';
33
import 'package:path/path.dart' as path;
44

5-
import '../../../models/shopinbit/shopinbit_order_model.dart'
6-
show ShopInBitCategory, ShopInBitOrderStatus;
5+
import "../../../models/shopinbit/shopinbit_enums.dart";
6+
import "../../../services/shopinbit/src/models/message.dart";
77
import '../../../utilities/stack_file_system.dart';
88
import 'tables/cakepay_orders.dart';
99
import 'tables/shopin_bit_settings.dart';
@@ -27,8 +27,8 @@ abstract final class SharedDrift {
2727
}
2828

2929
@DriftDatabase(
30-
tables: [CakepayOrders, ShopinBitSettings, ShopInBitTickets],
31-
daos: [ShopinBitSettingsDao],
30+
tables: [CakepayOrders, ShopInBitSettings, ShopInBitTickets],
31+
daos: [ShopInBitSettingsDao, ShopInBitTicketsDao],
3232
)
3333
final class SharedDatabase extends _$SharedDatabase {
3434
SharedDatabase._([QueryExecutor? executor])
@@ -41,7 +41,7 @@ final class SharedDatabase extends _$SharedDatabase {
4141
MigrationStrategy get migration => MigrationStrategy(
4242
onUpgrade: (m, from, to) async {
4343
if (from == 1 && to == 2) {
44-
await m.createTable(shopinBitSettings);
44+
await m.createTable(shopInBitSettings);
4545
await m.createTable(shopInBitTickets);
4646
}
4747
},
@@ -61,35 +61,183 @@ final class SharedDatabase extends _$SharedDatabase {
6161
}
6262
}
6363

64-
@DriftAccessor(tables: [ShopinBitSettings])
65-
class ShopinBitSettingsDao extends DatabaseAccessor<SharedDatabase>
66-
with _$ShopinBitSettingsDaoMixin {
67-
ShopinBitSettingsDao(super.db);
64+
@DriftAccessor(tables: [ShopInBitTickets])
65+
class ShopInBitTicketsDao extends DatabaseAccessor<SharedDatabase>
66+
with _$ShopInBitTicketsDaoMixin {
67+
ShopInBitTicketsDao(super.db);
6868

69-
Future<ShopinBitSetting> getSettings() async {
70-
final ShopinBitSetting? row = await (select(
71-
shopinBitSettings,
72-
)..where((t) => t.id.equals(0))).getSingleOrNull();
73-
if (row != null) return row;
69+
// -- Reads --
7470

75-
return into(
76-
shopinBitSettings,
77-
).insertReturning(ShopinBitSettingsCompanion.insert(id: const Value(0)));
71+
Future<ShopInBitTicket?> getByApiId(int apiTicketId) {
72+
return (select(
73+
shopInBitTickets,
74+
)..where((t) => t.apiTicketId.equals(apiTicketId))).getSingleOrNull();
7875
}
7976

80-
Future<void> setGuidelinesAccepted(bool accepted) =>
81-
_update(ShopinBitSettingsCompanion(guidelinesAccepted: Value(accepted)));
77+
Stream<ShopInBitTicket?> watchByApiId(int apiTicketId) {
78+
return (select(
79+
shopInBitTickets,
80+
)..where((t) => t.apiTicketId.equals(apiTicketId))).watchSingleOrNull();
81+
}
82+
83+
Future<List<ShopInBitTicket>> getByCustomerKey(String customerKey) {
84+
return (select(shopInBitTickets)
85+
..where((t) => t.customerKey.equals(customerKey))
86+
..orderBy([(t) => OrderingTerm.desc(t.createdAt)]))
87+
.get();
88+
}
89+
90+
/// All tickets for the active customer key, newest first.
91+
Stream<List<ShopInBitTicket>> watchByCustomerKey(String customerKey) {
92+
return (select(shopInBitTickets)
93+
..where((t) => t.customerKey.equals(customerKey))
94+
..orderBy([(t) => OrderingTerm.desc(t.createdAt)]))
95+
.watch();
96+
}
97+
98+
// -- Writes --
99+
100+
/// Insert a brand-new ticket. Caller must supply every required field;
101+
/// pass nullable fields through the companion's `Value(...)` wrappers.
102+
Future<void> insertTicket(ShopInBitTicketsCompanion companion) async {
103+
await into(shopInBitTickets).insert(companion);
104+
}
105+
106+
/// Patch an existing ticket. Use `Value.absent()` (the companion default)
107+
/// for fields you don't want to touch. Returns true if a row was updated.
108+
Future<bool> updateTicket(
109+
int apiTicketId,
110+
ShopInBitTicketsCompanion patch,
111+
) async {
112+
final int rows = await (update(
113+
shopInBitTickets,
114+
)..where((t) => t.apiTicketId.equals(apiTicketId))).write(patch);
115+
return rows > 0;
116+
}
117+
118+
Future<int> deleteByApiId(int apiTicketId) {
119+
return (delete(
120+
shopInBitTickets,
121+
)..where((t) => t.apiTicketId.equals(apiTicketId))).go();
122+
}
123+
124+
Future<int> deleteByCustomerKey(String customerKey) {
125+
return (delete(
126+
shopInBitTickets,
127+
)..where((t) => t.customerKey.equals(customerKey))).go();
128+
}
129+
}
130+
131+
@DriftAccessor(tables: [ShopInBitSettings])
132+
class ShopInBitSettingsDao extends DatabaseAccessor<SharedDatabase>
133+
with _$ShopInBitSettingsDaoMixin {
134+
ShopInBitSettingsDao(super.db);
135+
136+
// -- "Current" (= most-recently-used) row --
137+
138+
/// Returns the settings row for the most-recently-used customer key,
139+
/// or null if the user has never generated/recovered one.
140+
Future<ShopInBitSetting?> getCurrentSettings() {
141+
return (select(shopInBitSettings)
142+
..orderBy([(t) => OrderingTerm.desc(t.lastUsedAt)])
143+
..limit(1))
144+
.getSingleOrNull();
145+
}
146+
147+
Stream<ShopInBitSetting?> watchCurrentSettings() {
148+
return (select(shopInBitSettings)
149+
..orderBy([(t) => OrderingTerm.desc(t.lastUsedAt)])
150+
..limit(1))
151+
.watchSingleOrNull();
152+
}
153+
154+
// -- Specific row by customer key --
155+
156+
Future<ShopInBitSetting?> getByKey(String customerKey) {
157+
return (select(
158+
shopInBitSettings,
159+
)..where((t) => t.customerKey.equals(customerKey))).getSingleOrNull();
160+
}
161+
162+
Stream<ShopInBitSetting?> watchByKey(String customerKey) {
163+
return (select(
164+
shopInBitSettings,
165+
)..where((t) => t.customerKey.equals(customerKey))).watchSingleOrNull();
166+
}
82167

83-
Future<void> setSetupComplete(bool complete) =>
84-
_update(ShopinBitSettingsCompanion(setupComplete: Value(complete)));
168+
Stream<List<ShopInBitSetting>> watchAll() {
169+
return (select(
170+
shopInBitSettings,
171+
)..orderBy([(t) => OrderingTerm.desc(t.lastUsedAt)])).watch();
172+
}
85173

86-
Future<void> setDisplayName(String name) =>
87-
_update(ShopinBitSettingsCompanion(displayName: Value(name)));
174+
// -- Writes --
88175

89-
Future<void> _update(ShopinBitSettingsCompanion changes) async {
90-
await getSettings(); // ensure row exists
91-
await (update(
92-
shopinBitSettings,
93-
)..where((t) => t.id.equals(0))).write(changes);
176+
/// Insert if missing, otherwise bump [lastUsedAt]. Returns the row.
177+
Future<ShopInBitSetting> upsert(String customerKey) {
178+
final DateTime now = DateTime.now();
179+
return into(shopInBitSettings).insertReturning(
180+
ShopInBitSettingsCompanion.insert(
181+
customerKey: customerKey,
182+
createdAt: Value(now),
183+
lastUsedAt: Value(now),
184+
),
185+
onConflict: DoUpdate(
186+
(_) => ShopInBitSettingsCompanion(lastUsedAt: Value(now)),
187+
target: [shopInBitSettings.customerKey],
188+
),
189+
);
94190
}
191+
192+
Future<int> touch(String customerKey) => _write(
193+
customerKey,
194+
ShopInBitSettingsCompanion(lastUsedAt: Value(DateTime.now())),
195+
);
196+
197+
Future<int> setPrivacyAccepted(String customerKey, bool value) => _write(
198+
customerKey,
199+
ShopInBitSettingsCompanion(privacyAccepted: Value(value)),
200+
);
201+
202+
Future<int> setGuidelinesAccepted(
203+
String customerKey,
204+
ShopInBitCategory category,
205+
bool value,
206+
) {
207+
final ShopInBitSettingsCompanion patch = switch (category) {
208+
.concierge => ShopInBitSettingsCompanion(
209+
conciergeGuidelinesAccepted: Value(value),
210+
),
211+
.travel => ShopInBitSettingsCompanion(
212+
travelGuidelinesAccepted: Value(value),
213+
),
214+
.car => ShopInBitSettingsCompanion(carGuidelinesAccepted: Value(value)),
215+
};
216+
return _write(customerKey, patch);
217+
}
218+
219+
Future<int> setSetupComplete(String customerKey, bool value) => _write(
220+
customerKey,
221+
ShopInBitSettingsCompanion(setupComplete: Value(value)),
222+
);
223+
224+
Future<int> deleteByKey(String customerKey) {
225+
return (delete(
226+
shopInBitSettings,
227+
)..where((t) => t.customerKey.equals(customerKey))).go();
228+
}
229+
230+
Future<int> _write(String customerKey, ShopInBitSettingsCompanion changes) {
231+
return (update(
232+
shopInBitSettings,
233+
)..where((t) => t.customerKey.equals(customerKey))).write(changes);
234+
}
235+
}
236+
237+
extension ShopInBitSettingGuidelines on ShopInBitSetting {
238+
bool guidelinesAcceptedFor(ShopInBitCategory category) => switch (category) {
239+
.concierge => conciergeGuidelinesAccepted,
240+
.travel => travelGuidelinesAccepted,
241+
.car => carGuidelinesAccepted,
242+
};
95243
}

0 commit comments

Comments
 (0)