stateset-db 1.22.0

Database implementations for StateSet iCommerce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! PostgreSQL implementation of wishlist repository

use super::map_db_error;
use chrono::{DateTime, Utc};
use sqlx::FromRow;
use sqlx::postgres::PgPool;
use stateset_core::{
    AddWishlistItem, CommerceError, CreateWishlist, ProductId, Result, UpdateWishlist, Wishlist,
    WishlistFilter, WishlistId, WishlistItem, WishlistRepository,
};
use uuid::Uuid;

/// PostgreSQL wishlist repository
#[derive(Debug, Clone)]
pub struct PgWishlistRepository {
    pool: PgPool,
}

#[derive(FromRow)]
struct WishlistRow {
    id: Uuid,
    customer_id: Uuid,
    name: String,
    is_public: bool,
    created_at: DateTime<Utc>,
    updated_at: DateTime<Utc>,
}

#[derive(FromRow)]
struct WishlistItemRow {
    #[allow(dead_code)]
    id: Uuid,
    #[allow(dead_code)]
    wishlist_id: Uuid,
    product_id: Uuid,
    variant_id: Option<String>,
    priority: Option<i32>,
    quantity: i32,
    notes: Option<String>,
    added_at: DateTime<Utc>,
}

impl PgWishlistRepository {
    pub const fn new(pool: PgPool) -> Self {
        Self { pool }
    }

    fn row_to_wishlist(row: WishlistRow, items: Vec<WishlistItem>) -> Wishlist {
        Wishlist {
            id: WishlistId::from(row.id),
            customer_id: row.customer_id.into(),
            name: row.name,
            is_public: row.is_public,
            items,
            created_at: row.created_at,
            updated_at: row.updated_at,
        }
    }

    fn row_to_item(row: WishlistItemRow) -> WishlistItem {
        WishlistItem {
            product_id: ProductId::from(row.product_id),
            variant_id: row.variant_id,
            added_at: row.added_at,
            note: row.notes,
            quantity: row.quantity.max(0) as u32,
            priority: row.priority,
        }
    }

    async fn load_items_async(&self, wishlist_id: Uuid) -> Result<Vec<WishlistItem>> {
        let rows = sqlx::query_as::<_, WishlistItemRow>(
            "SELECT id, wishlist_id, product_id, variant_id, priority, quantity, notes, added_at
             FROM wishlist_items WHERE wishlist_id = $1 ORDER BY added_at",
        )
        .bind(wishlist_id)
        .fetch_all(&self.pool)
        .await
        .map_err(map_db_error)?;

        Ok(rows.into_iter().map(Self::row_to_item).collect())
    }

    async fn load_items_batch_async(
        &self,
        ids: &[Uuid],
    ) -> Result<std::collections::HashMap<Uuid, Vec<WishlistItem>>> {
        let mut map: std::collections::HashMap<Uuid, Vec<WishlistItem>> =
            std::collections::HashMap::with_capacity(ids.len());
        if ids.is_empty() {
            return Ok(map);
        }
        let rows = sqlx::query_as::<_, WishlistItemRow>(
            "SELECT id, wishlist_id, product_id, variant_id, priority, quantity, notes, added_at
             FROM wishlist_items WHERE wishlist_id = ANY($1) ORDER BY added_at",
        )
        .bind(ids.to_vec())
        .fetch_all(&self.pool)
        .await
        .map_err(map_db_error)?;
        for row in rows {
            let parent = row.wishlist_id;
            map.entry(parent).or_default().push(Self::row_to_item(row));
        }
        Ok(map)
    }

    // ---- async helpers ----

    async fn create_async(&self, input: CreateWishlist) -> Result<Wishlist> {
        let id = Uuid::new_v4();
        let now = Utc::now();

        sqlx::query(
            "INSERT INTO wishlists (id, customer_id, name, is_public, created_at, updated_at)
             VALUES ($1, $2, $3, $4, $5, $6)",
        )
        .bind(id)
        .bind(input.customer_id.into_uuid())
        .bind(&input.name)
        .bind(input.is_public)
        .bind(now)
        .bind(now)
        .execute(&self.pool)
        .await
        .map_err(map_db_error)?;

        self.get_async(id).await?.ok_or(CommerceError::NotFound)
    }

    async fn get_async(&self, id: Uuid) -> Result<Option<Wishlist>> {
        let row = sqlx::query_as::<_, WishlistRow>(
            "SELECT id, customer_id, name, is_public, created_at, updated_at
             FROM wishlists WHERE id = $1",
        )
        .bind(id)
        .fetch_optional(&self.pool)
        .await
        .map_err(map_db_error)?;

        match row {
            Some(r) => {
                let items = self.load_items_async(r.id).await?;
                Ok(Some(Self::row_to_wishlist(r, items)))
            }
            None => Ok(None),
        }
    }

    async fn update_async(&self, id: Uuid, input: UpdateWishlist) -> Result<Wishlist> {
        let now = Utc::now();

        let mut query = String::from("UPDATE wishlists SET updated_at = $1");
        let mut param_idx = 2u32;
        let mut has_name = false;
        let mut has_is_public = false;

        if input.name.is_some() {
            query.push_str(&format!(", name = ${param_idx}"));
            param_idx += 1;
            has_name = true;
        }
        if input.is_public.is_some() {
            query.push_str(&format!(", is_public = ${param_idx}"));
            param_idx += 1;
            has_is_public = true;
        }

        query.push_str(&format!(" WHERE id = ${param_idx}"));

        let mut q = sqlx::query(&query).bind(now);

        if has_name {
            q = q.bind(input.name.expect("checked above"));
        }
        if has_is_public {
            q = q.bind(input.is_public.expect("checked above"));
        }

        q = q.bind(id);

        q.execute(&self.pool).await.map_err(map_db_error)?;

        self.get_async(id).await?.ok_or(CommerceError::NotFound)
    }

    async fn list_async(&self, filter: WishlistFilter) -> Result<Vec<Wishlist>> {
        let mut query = String::from(
            "SELECT id, customer_id, name, is_public, created_at, updated_at
             FROM wishlists WHERE 1=1",
        );
        let mut param_idx = 1u32;
        let mut binds: Vec<WishlistBindValue> = Vec::new();

        if let Some(customer_id) = filter.customer_id {
            query.push_str(&format!(" AND customer_id = ${param_idx}"));
            param_idx += 1;
            binds.push(WishlistBindValue::Uuid(customer_id.into_uuid()));
        }
        if let Some(is_public) = filter.is_public {
            query.push_str(&format!(" AND is_public = ${param_idx}"));
            param_idx += 1;
            binds.push(WishlistBindValue::Bool(is_public));
        }
        let _ = param_idx;

        query.push_str(" ORDER BY created_at DESC");

        query.push_str(&format!(" LIMIT {}", super::effective_limit(filter.limit)));
        if let Some(offset) = filter.offset {
            query.push_str(&format!(" OFFSET {offset}"));
        }

        let mut q = sqlx::query_as::<_, WishlistRow>(&query);
        for bind in &binds {
            q = match bind {
                WishlistBindValue::Uuid(v) => q.bind(*v),
                WishlistBindValue::Bool(v) => q.bind(*v),
            };
        }

        let rows = q.fetch_all(&self.pool).await.map_err(map_db_error)?;

        let ids: Vec<Uuid> = rows.iter().map(|r| r.id).collect();
        let mut items_by_id = self.load_items_batch_async(&ids).await?;
        let mut result = Vec::with_capacity(rows.len());
        for row in rows {
            let items = items_by_id.remove(&row.id).unwrap_or_default();
            result.push(Self::row_to_wishlist(row, items));
        }
        Ok(result)
    }

    async fn delete_async(&self, id: Uuid) -> Result<()> {
        // Items are cascade-deleted via foreign key, but we explicitly delete for safety
        sqlx::query("DELETE FROM wishlist_items WHERE wishlist_id = $1")
            .bind(id)
            .execute(&self.pool)
            .await
            .map_err(map_db_error)?;

        sqlx::query("DELETE FROM wishlists WHERE id = $1")
            .bind(id)
            .execute(&self.pool)
            .await
            .map_err(map_db_error)?;

        Ok(())
    }

    async fn add_item_async(
        &self,
        wishlist_id: Uuid,
        item: AddWishlistItem,
    ) -> Result<WishlistItem> {
        let item_id = Uuid::new_v4();
        let now = Utc::now();

        // Verify wishlist exists
        let exists: Option<Uuid> = sqlx::query_scalar("SELECT id FROM wishlists WHERE id = $1")
            .bind(wishlist_id)
            .fetch_optional(&self.pool)
            .await
            .map_err(map_db_error)?;

        if exists.is_none() {
            return Err(CommerceError::NotFound);
        }

        let mut tx = self.pool.begin().await.map_err(map_db_error)?;

        sqlx::query(
            "INSERT INTO wishlist_items (id, wishlist_id, product_id, variant_id, priority, quantity, notes, added_at)
             VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
        )
        .bind(item_id)
        .bind(wishlist_id)
        .bind(item.product_id.into_uuid())
        .bind(&item.variant_id)
        .bind(item.priority)
        .bind(i32::try_from(item.quantity.unwrap_or(1)).unwrap_or(1))
        .bind(&item.note)
        .bind(now)
        .execute(tx.as_mut())
        .await
        .map_err(map_db_error)?;

        // Update wishlist updated_at
        sqlx::query("UPDATE wishlists SET updated_at = $1 WHERE id = $2")
            .bind(now)
            .bind(wishlist_id)
            .execute(tx.as_mut())
            .await
            .map_err(map_db_error)?;

        tx.commit().await.map_err(map_db_error)?;

        Ok(WishlistItem {
            product_id: item.product_id,
            variant_id: item.variant_id,
            added_at: now,
            note: item.note,
            quantity: item.quantity.unwrap_or(1),
            priority: item.priority,
        })
    }

    async fn remove_item_async(&self, wishlist_id: Uuid, product_id: Uuid) -> Result<()> {
        let now = Utc::now();

        sqlx::query("DELETE FROM wishlist_items WHERE wishlist_id = $1 AND product_id = $2")
            .bind(wishlist_id)
            .bind(product_id)
            .execute(&self.pool)
            .await
            .map_err(map_db_error)?;

        // Update wishlist updated_at
        sqlx::query("UPDATE wishlists SET updated_at = $1 WHERE id = $2")
            .bind(now)
            .bind(wishlist_id)
            .execute(&self.pool)
            .await
            .map_err(map_db_error)?;

        Ok(())
    }
}

/// Internal enum for heterogeneous bind parameters
enum WishlistBindValue {
    Uuid(Uuid),
    Bool(bool),
}

impl WishlistRepository for PgWishlistRepository {
    fn create(&self, input: CreateWishlist) -> Result<Wishlist> {
        super::block_on(self.create_async(input))
    }

    fn get(&self, id: WishlistId) -> Result<Option<Wishlist>> {
        super::block_on(self.get_async(id.into_uuid()))
    }

    fn update(&self, id: WishlistId, input: UpdateWishlist) -> Result<Wishlist> {
        super::block_on(self.update_async(id.into_uuid(), input))
    }

    fn list(&self, filter: WishlistFilter) -> Result<Vec<Wishlist>> {
        super::block_on(self.list_async(filter))
    }

    fn delete(&self, id: WishlistId) -> Result<()> {
        super::block_on(self.delete_async(id.into_uuid()))
    }

    fn add_item(&self, wishlist_id: WishlistId, item: AddWishlistItem) -> Result<WishlistItem> {
        super::block_on(self.add_item_async(wishlist_id.into_uuid(), item))
    }

    fn remove_item(&self, wishlist_id: WishlistId, product_id: ProductId) -> Result<()> {
        super::block_on(self.remove_item_async(wishlist_id.into_uuid(), product_id.into_uuid()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn row_to_wishlist_maps_fields_correctly() {
        let now = Utc::now();
        let id = Uuid::new_v4();
        let customer_id = Uuid::new_v4();

        let row = WishlistRow {
            id,
            customer_id,
            name: "Birthday Ideas".into(),
            is_public: true,
            created_at: now,
            updated_at: now,
        };

        let wishlist = PgWishlistRepository::row_to_wishlist(row, vec![]);
        assert_eq!(wishlist.name, "Birthday Ideas");
        assert!(wishlist.is_public);
        assert!(wishlist.items.is_empty());
        assert_eq!(wishlist.id.into_uuid(), id);
    }

    #[test]
    fn row_to_item_maps_fields_correctly() {
        let now = Utc::now();
        let product_id = Uuid::new_v4();

        let row = WishlistItemRow {
            id: Uuid::new_v4(),
            wishlist_id: Uuid::new_v4(),
            product_id,
            variant_id: Some("size-L".into()),
            priority: Some(1),
            quantity: 3,
            notes: Some("Red color".into()),
            added_at: now,
        };

        let item = PgWishlistRepository::row_to_item(row);
        assert_eq!(item.product_id.into_uuid(), product_id);
        assert_eq!(item.variant_id.as_deref(), Some("size-L"));
        assert_eq!(item.priority, Some(1));
        assert_eq!(item.note.as_deref(), Some("Red color"));
        assert_eq!(item.quantity, 3, "quantity must be read from the row, not hard-coded");
    }

    #[test]
    fn row_to_item_handles_nullable_fields() {
        let now = Utc::now();
        let row = WishlistItemRow {
            id: Uuid::new_v4(),
            wishlist_id: Uuid::new_v4(),
            product_id: Uuid::new_v4(),
            variant_id: None,
            priority: None,
            quantity: 1,
            notes: None,
            added_at: now,
        };

        let item = PgWishlistRepository::row_to_item(row);
        assert!(item.variant_id.is_none());
        assert!(item.priority.is_none());
        assert!(item.note.is_none());
        assert_eq!(item.quantity, 1);
    }
}