Handle update merchandise quantity to 0 and < 0

When quantity = 0 remove the merchandise item from the form_list.

When quantity < 0 continue with the update which will fail on the new constraint
This commit is contained in:
2020-10-28 23:40:38 -04:00
parent e831a925f5
commit e482e7764d
5 changed files with 15 additions and 8 deletions

View File

@@ -2,7 +2,6 @@ use anyhow::{Error, Result};
use async_trait::async_trait;
use chrono::prelude::*;
use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::postgres::PgPool;
use sqlx::types::Json;
use tracing::instrument;
@@ -207,13 +206,16 @@ impl MerchandiseList {
"UPDATE
merchandise_lists
SET
form_list =
jsonb_set(
form_list = CASE
WHEN quantity::int + $4 = 0
THEN form_list - elem_index::int
ELSE jsonb_set(
form_list,
array[elem_index::text, 'quantity'],
to_jsonb(quantity::int + $4),
true
)
END
FROM (
SELECT
pos - 1 as elem_index,