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
437
438
439
440
441
442
443
444
//! SQLite implementation of the channel repository

use super::{
    map_db_error, parse_datetime_row, parse_enum_row, parse_json_row, parse_uuid_opt_row,
    parse_uuid_row, with_immediate_transaction,
};
use chrono::Utc;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rusqlite::OptionalExtension;
use stateset_core::{
    Channel, ChannelFilter, ChannelId, ChannelProductMapping, ChannelProductSyncItem,
    ChannelRepository, ChannelStatus, ChannelType, CommerceError, CreateChannel, Result,
    UpdateChannel,
};

#[derive(Debug)]
pub struct SqliteChannelRepository {
    pool: Pool<SqliteConnectionManager>,
}

impl SqliteChannelRepository {
    #[must_use]
    pub const fn new(pool: Pool<SqliteConnectionManager>) -> Self {
        Self { pool }
    }

    fn conn(&self) -> Result<r2d2::PooledConnection<SqliteConnectionManager>> {
        self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))
    }

    fn row_to_channel(row: &rusqlite::Row<'_>) -> rusqlite::Result<Channel> {
        let tags_json: String = row.get("tags")?;
        let metadata_json: String = row.get("metadata")?;
        Ok(Channel {
            id: parse_uuid_row(&row.get::<_, String>("id")?, "channel", "id")?.into(),
            name: row.get("name")?,
            channel_type: parse_enum_row::<ChannelType>(
                &row.get::<_, String>("channel_type")?,
                "channel",
                "channel_type",
            )?,
            integration: row.get("integration")?,
            status: parse_enum_row::<ChannelStatus>(
                &row.get::<_, String>("status")?,
                "channel",
                "status",
            )?,
            api_locked: row.get::<_, i32>("api_locked")? != 0,
            default_warehouse_id: parse_uuid_opt_row(
                row.get::<_, Option<String>>("default_warehouse_id")?,
                "channel",
                "default_warehouse_id",
            )?
            .map(Into::into),
            tags: parse_json_row(&tags_json, "channel", "tags")?,
            metadata: parse_json_row(&metadata_json, "channel", "metadata")?,
            created_at: parse_datetime_row(
                &row.get::<_, String>("created_at")?,
                "channel",
                "created_at",
            )?,
            updated_at: parse_datetime_row(
                &row.get::<_, String>("updated_at")?,
                "channel",
                "updated_at",
            )?,
        })
    }

    fn row_to_mapping(row: &rusqlite::Row<'_>) -> rusqlite::Result<ChannelProductMapping> {
        Ok(ChannelProductMapping {
            channel_id: parse_uuid_row(
                &row.get::<_, String>("channel_id")?,
                "mapping",
                "channel_id",
            )?
            .into(),
            channel_sku: row.get("channel_sku")?,
            product_id: parse_uuid_row(
                &row.get::<_, String>("product_id")?,
                "mapping",
                "product_id",
            )?
            .into(),
            internal_sku: row.get("internal_sku")?,
            created_at: parse_datetime_row(
                &row.get::<_, String>("created_at")?,
                "mapping",
                "created_at",
            )?,
            updated_at: parse_datetime_row(
                &row.get::<_, String>("updated_at")?,
                "mapping",
                "updated_at",
            )?,
        })
    }

    fn json_err(e: serde_json::Error) -> rusqlite::Error {
        rusqlite::Error::ToSqlConversionFailure(Box::new(CommerceError::DatabaseError(
            e.to_string(),
        )))
    }
}

impl ChannelRepository for SqliteChannelRepository {
    fn create(&self, input: CreateChannel) -> Result<Channel> {
        let id = ChannelId::new();
        let id_str = id.to_string();
        let now_str = Utc::now().to_rfc3339();
        let tags_json = serde_json::to_string(&input.tags)
            .map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let metadata_json = serde_json::to_string(&input.metadata)
            .map_err(|e| CommerceError::DatabaseError(e.to_string()))?;

        with_immediate_transaction(&self.pool, |tx| {
            tx.execute(
                "INSERT INTO channels (id, name, channel_type, integration, status, api_locked, default_warehouse_id, tags, metadata, created_at, updated_at)
                 VALUES (?, ?, ?, ?, 'active', 0, ?, ?, ?, ?, ?)",
                rusqlite::params![
                    &id_str,
                    &input.name,
                    input.channel_type.to_string(),
                    &input.integration,
                    input.default_warehouse_id.map(|w| w.to_string()),
                    &tags_json,
                    &metadata_json,
                    &now_str,
                    &now_str,
                ],
            )?;
            tx.query_row("SELECT * FROM channels WHERE id = ?", [&id_str], Self::row_to_channel)
        })
    }

    fn get(&self, id: ChannelId) -> Result<Option<Channel>> {
        let conn = self.conn()?;
        match conn.query_row(
            "SELECT * FROM channels WHERE id = ?",
            [id.to_string()],
            Self::row_to_channel,
        ) {
            Ok(c) => Ok(Some(c)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(map_db_error(e)),
        }
    }

    fn update(&self, id: ChannelId, input: UpdateChannel) -> Result<Channel> {
        let id_str = id.to_string();
        let now_str = Utc::now().to_rfc3339();

        with_immediate_transaction(&self.pool, |tx| {
            // Reject mutations on locked channels (reads stay allowed elsewhere).
            let locked: i32 = tx
                .query_row("SELECT api_locked FROM channels WHERE id = ?", [&id_str], |r| r.get(0))
                .optional()?
                .ok_or(rusqlite::Error::QueryReturnedNoRows)?;
            if locked != 0 {
                return Err(rusqlite::Error::ToSqlConversionFailure(Box::new(
                    CommerceError::Conflict("channel is API-locked".into()),
                )));
            }

            let mut sets = vec!["updated_at = ?".to_string()];
            let mut params: Vec<Box<dyn rusqlite::types::ToSql>> = vec![Box::new(now_str.clone())];

            if let Some(ref name) = input.name {
                sets.push("name = ?".into());
                params.push(Box::new(name.clone()));
            }
            if let Some(ref integration) = input.integration {
                sets.push("integration = ?".into());
                params.push(Box::new(integration.clone()));
            }
            if let Some(status) = input.status {
                sets.push("status = ?".into());
                params.push(Box::new(status.to_string()));
            }
            if let Some(wh) = input.default_warehouse_id {
                sets.push("default_warehouse_id = ?".into());
                params.push(Box::new(wh.to_string()));
            }
            if let Some(ref tags) = input.tags {
                sets.push("tags = ?".into());
                params.push(Box::new(serde_json::to_string(tags).map_err(Self::json_err)?));
            }
            if let Some(ref metadata) = input.metadata {
                sets.push("metadata = ?".into());
                params.push(Box::new(serde_json::to_string(metadata).map_err(Self::json_err)?));
            }

            let sql = format!("UPDATE channels SET {} WHERE id = ?", sets.join(", "));
            params.push(Box::new(id_str.clone()));
            let param_refs: Vec<&dyn rusqlite::types::ToSql> =
                params.iter().map(|p| p.as_ref()).collect();
            tx.execute(&sql, param_refs.as_slice())?;

            tx.query_row("SELECT * FROM channels WHERE id = ?", [&id_str], Self::row_to_channel)
        })
    }

    fn list(&self, filter: ChannelFilter) -> Result<Vec<Channel>> {
        let conn = self.conn()?;
        let mut sql = "SELECT * FROM channels WHERE status != 'deleted'".to_string();
        let mut params: Vec<Box<dyn rusqlite::types::ToSql>> = vec![];

        if let Some(t) = filter.channel_type {
            sql.push_str(" AND channel_type = ?");
            params.push(Box::new(t.to_string()));
        }
        if let Some(s) = filter.status {
            sql.push_str(" AND status = ?");
            params.push(Box::new(s.to_string()));
        }
        if let Some(ref integration) = filter.integration {
            sql.push_str(" AND integration = ?");
            params.push(Box::new(integration.clone()));
        }
        if let Some(locked) = filter.api_locked {
            sql.push_str(" AND api_locked = ?");
            params.push(Box::new(locked as i32));
        }
        sql.push_str(" ORDER BY created_at DESC");
        crate::sqlite::append_limit_offset(&mut sql, filter.limit, filter.offset);

        let param_refs: Vec<&dyn rusqlite::types::ToSql> =
            params.iter().map(|p| p.as_ref()).collect();
        let mut stmt = conn.prepare(&sql).map_err(map_db_error)?;
        let rows = stmt
            .query_map(param_refs.as_slice(), Self::row_to_channel)
            .map_err(map_db_error)?
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(map_db_error)?;
        Ok(rows)
    }

    fn delete(&self, id: ChannelId) -> Result<()> {
        let id_str = id.to_string();
        with_immediate_transaction(&self.pool, |tx| {
            let locked: i32 = tx
                .query_row("SELECT api_locked FROM channels WHERE id = ?", [&id_str], |r| r.get(0))
                .optional()?
                .ok_or(rusqlite::Error::QueryReturnedNoRows)?;
            if locked != 0 {
                return Err(rusqlite::Error::ToSqlConversionFailure(Box::new(
                    CommerceError::Conflict("channel is API-locked".into()),
                )));
            }
            tx.execute("UPDATE channels SET status = 'deleted' WHERE id = ?", [&id_str])?;
            Ok(())
        })
    }

    fn set_lock(&self, id: ChannelId, locked: bool) -> Result<Channel> {
        let id_str = id.to_string();
        let now_str = Utc::now().to_rfc3339();
        with_immediate_transaction(&self.pool, |tx| {
            tx.execute(
                "UPDATE channels SET api_locked = ?, updated_at = ? WHERE id = ?",
                rusqlite::params![locked as i32, &now_str, &id_str],
            )?;
            tx.query_row("SELECT * FROM channels WHERE id = ?", [&id_str], Self::row_to_channel)
        })
    }

    fn sync_products(&self, id: ChannelId, items: Vec<ChannelProductSyncItem>) -> Result<u64> {
        let id_str = id.to_string();
        let now_str = Utc::now().to_rfc3339();
        with_immediate_transaction(&self.pool, |tx| {
            let mut affected: u64 = 0;
            for item in &items {
                if item.delete {
                    affected += tx.execute(
                        "DELETE FROM channel_product_mappings WHERE channel_id = ? AND channel_sku = ?",
                        rusqlite::params![&id_str, &item.channel_sku],
                    )? as u64;
                } else {
                    let product_id = item.product_id.ok_or_else(|| {
                        rusqlite::Error::ToSqlConversionFailure(Box::new(
                            CommerceError::ValidationError(
                                "product_id is required when delete is false".into(),
                            ),
                        ))
                    })?;
                    let internal_sku = item.internal_sku.clone().unwrap_or_default();
                    affected += tx.execute(
                        "INSERT INTO channel_product_mappings (channel_id, channel_sku, product_id, internal_sku, created_at, updated_at)
                         VALUES (?, ?, ?, ?, ?, ?)
                         ON CONFLICT(channel_id, channel_sku) DO UPDATE SET
                            product_id = excluded.product_id,
                            internal_sku = excluded.internal_sku,
                            updated_at = excluded.updated_at",
                        rusqlite::params![
                            &id_str,
                            &item.channel_sku,
                            product_id.to_string(),
                            &internal_sku,
                            &now_str,
                            &now_str,
                        ],
                    )? as u64;
                }
            }
            Ok(affected)
        })
    }

    fn list_product_mappings(&self, id: ChannelId) -> Result<Vec<ChannelProductMapping>> {
        let conn = self.conn()?;
        let mut stmt = conn
            .prepare(
                "SELECT * FROM channel_product_mappings WHERE channel_id = ? ORDER BY channel_sku",
            )
            .map_err(map_db_error)?;
        let rows = stmt
            .query_map([id.to_string()], Self::row_to_mapping)
            .map_err(map_db_error)?
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(map_db_error)?;
        Ok(rows)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::DatabaseConfig;
    use crate::sqlite::SqliteDatabase;
    use stateset_core::ProductId;

    fn test_repo() -> SqliteChannelRepository {
        let db = SqliteDatabase::new(&DatabaseConfig::in_memory()).expect("in-memory db");
        SqliteChannelRepository::new(db.pool().clone())
    }

    fn new_channel(repo: &SqliteChannelRepository, name: &str, t: ChannelType) -> Channel {
        repo.create(CreateChannel {
            name: name.into(),
            channel_type: t,
            integration: Some("shopify".into()),
            default_warehouse_id: None,
            tags: vec!["retail".into()],
            metadata: serde_json::json!({"k":"v"}),
        })
        .expect("create channel")
    }

    #[test]
    fn create_and_get() {
        let repo = test_repo();
        let c = new_channel(&repo, "Shopify US", ChannelType::SalesChannel);
        assert_eq!(c.name, "Shopify US");
        assert!(!c.api_locked);
        let fetched = repo.get(c.id).expect("get").expect("found");
        assert_eq!(fetched.id, c.id);
        assert_eq!(fetched.tags, vec!["retail".to_string()]);
    }

    #[test]
    fn lock_blocks_update_and_delete() {
        let repo = test_repo();
        let c = new_channel(&repo, "Locked", ChannelType::SalesChannel);
        repo.set_lock(c.id, true).expect("lock");
        let upd = repo.update(c.id, UpdateChannel { name: Some("x".into()), ..Default::default() });
        assert!(upd.is_err(), "update on locked channel must fail");
        assert!(repo.delete(c.id).is_err(), "delete on locked channel must fail");
        repo.set_lock(c.id, false).expect("unlock");
        assert!(
            repo.update(c.id, UpdateChannel { name: Some("ok".into()), ..Default::default() })
                .is_ok()
        );
    }

    #[test]
    fn list_filters_and_excludes_deleted() {
        let repo = test_repo();
        new_channel(&repo, "A", ChannelType::SalesChannel);
        let b = new_channel(&repo, "B", ChannelType::FulfillmentChannel);
        let all = repo.list(ChannelFilter::default()).expect("list");
        assert_eq!(all.len(), 2);
        let only_fc = repo
            .list(ChannelFilter {
                channel_type: Some(ChannelType::FulfillmentChannel),
                ..Default::default()
            })
            .expect("list fc");
        assert_eq!(only_fc.len(), 1);
        repo.delete(b.id).expect("delete");
        assert_eq!(repo.list(ChannelFilter::default()).expect("list").len(), 1);
    }

    #[test]
    fn sync_products_upsert_and_delete() {
        let repo = test_repo();
        let c = new_channel(&repo, "Sync", ChannelType::SalesChannel);
        let pid = ProductId::new();
        let n = repo
            .sync_products(
                c.id,
                vec![ChannelProductSyncItem {
                    channel_sku: "EXT-1".into(),
                    product_id: Some(pid),
                    internal_sku: Some("SKU-1".into()),
                    delete: false,
                }],
            )
            .expect("sync");
        assert_eq!(n, 1);
        let mappings = repo.list_product_mappings(c.id).expect("mappings");
        assert_eq!(mappings.len(), 1);
        assert_eq!(mappings[0].internal_sku, "SKU-1");

        // delete it
        repo.sync_products(
            c.id,
            vec![ChannelProductSyncItem {
                channel_sku: "EXT-1".into(),
                product_id: None,
                internal_sku: None,
                delete: true,
            }],
        )
        .expect("sync delete");
        assert_eq!(repo.list_product_mappings(c.id).expect("mappings").len(), 0);
    }

    #[test]
    fn sync_requires_product_when_not_deleting() {
        let repo = test_repo();
        let c = new_channel(&repo, "Sync2", ChannelType::SalesChannel);
        let res = repo.sync_products(
            c.id,
            vec![ChannelProductSyncItem {
                channel_sku: "EXT-9".into(),
                product_id: None,
                internal_sku: None,
                delete: false,
            }],
        );
        assert!(res.is_err());
    }
}