---
# System prepended metadata

title: Custom Spec & Add To Cart

---

# Custom Spec & Add To Cart

## Tables

**carts**

| Name                 | type          | Desc                                                 |
| -------------------- | ------------- |:---------------------------------------------------- |
| user_id              | int           | index, can be null if anonymous, required if         |
| status               | varchar(255)  | draft, checkout, waiting_payment, ordered, completed |
| total_price          | decimal(15,2) |                                                      |
| selected_items_count | int           |                                                      |


**cart_items**

| Name            | Type          | Desc           |
|:--------------- |:------------- |:-------------- |
| cart_id         | int           | index          |
| variant_page_id | int           | index          |
| title_id        | varchar(255)  |                |
| title_en        | varchar(255)  |                |
| product_sku_id  | int           | index          |
| is_custom_spec  | boolean       | default: false |
| spec_set        | jsonb         | index          |
| is_custom_qty   | boolean       | default: false |
| qty             | int           |                |
| price           | decimal(15,2) |                |
| price_each      | decimal(15,2) |                |
| final_price     | decimal(15,2) |                |
| margin          | decimal(5,2)  |                |
| extra_margin    | decimal(5,2)  |                |
| discount        | decimal(5,2)  |                |
| artwork_url     | varchar(255)  |                |
| notes           | text          |                |
| selected        | boolean       | default: true |

**product_sku**


| Name                | Type  | Desc  |
| ------------------- | ----- | ----- |
| ....                | ....  | ....  |
| sku_spec_properties | jsonb | index |



## endpoint
- Calculate Pricing
**`post web/v1/pricings`**
```json=
// request
{
    "variant_page_id": 1,
    "is_custom_spec": true | false,
    "spec_set": [
        {
            "spec_key_id": 1, // Bahan
            "spec_key_name": "key_name",
            "spec_value_id": 2,
            "spec_value_name": "value_name",
        },
        {
            "spec_key_id": 1, // Corner
            "spec_value_id": 2
        },
        {
            "spec_key_id": 1, // Size
            "spec_value_id": NULL,
            "custom": {
                "length": 7,
                "width": 5
            }
        }
    ],
    "is_custom_qty": true | false,
    "custom_qty": 110 // Only if user input custom qty
}

// Logic
- Harus hitung untuk semua quantities + custom_qty
- Harus hitung untuk plan user tersebut

// Response
// -> Lihat price simulation
[
    {
        "qty": 1,
        "prices": [
            {
                "plan": "normal",
                "price": 10000,
                "price_each": 1000,
                "discount": 10.02,
                "product_sku_id": 1,
                "save_percentage": 1
            }
        ]
    }
]
```
- Add to Cart
**`post web/v1/carts`**
```json=
// request
{
    "variant_page_id": 1,
    "is_custom_spec": true | false,
    "spec_set": [
        {
            "spec_key_id": 1, // Bahan
            "spec_value_id": 2
        },
        {
            "spec_key_id": 1, // Corner
            "spec_value_id": 2
        },
        {
            "spec_key_id": 1, // Size
            "spec_value_id": NULL,
            "custom": { // optional
                "length": 7,
                "width": 5
            }
        }
    ],
    "is_custom_qty": true | false,
    "qty": 110,
    "artwork_url": "artwork_url"
}
```


```sql=
-- Logic
-- Find / Create SKU
SELECT *
FROM product_sku_specs AS spec
    INNER JOIN product_skus AS sku
WHERE sku.product_variant_id = ?
    AND (spec.spec_key_id = ? AND spec.spec_value_id = ?)
    AND (spec.spec_key_id = ? AND spec.spec_value_id = ?)
    
SELECT *
FROM product_sku_specs AS spec
    INNER JOIN product_skus AS sku
WHERE sku.product_variant_id = ?
    AND (spec.spec_key_id = ? AND spec.spec_value_id = ?)
    AND (spec.spec_key_id = ? AND spec.spec_value_id = NULL AND spec.custom = ?)
```
```jsonb
// response
{
     //object cart
    "cart_id": 1,   
    ...
    "summary": [
        "item_name": "item_name",
        "final_price": 1000
    ]
}
```

- get cart
**`get /web/v1/carts/:cartId`**

```json=
// response 
{
     //object cart
    "cart_id": 1,   
    ...

}
```

- update qty
**`patch /web/v1/carts/:cartId/cart-items/:cartItemId/quantities`**
```json=
// request
// include recalculate price,
// include update total_price on carts
{
    "qty": 1    
}

// response
{
    "cart_item_id": 1,
    "variant_page_quantity_id": 1,
    "qty": 1,
    "price": 1000,
    "price_each": 100,
    "final_price": 1000,
    "margin": 1,
    "extra_margin": 1,
    "discount": 1,
    "cart": {
        "total_price": 1000,
    }
}
```

- update artwork
**`patch /web/v1/carts/:cartId/cart-items/:cartItemId/artwprk`**

```json=
// request
{
    "artwork_url": "url"
}

// response
{
    // object cart_item
}
```

- delete cart item
**`delete /web/v1/carts/:cartId/cart-items/:cartItemId`**

```json=
// include recalculate and update total_price on carts
// response 
{
    // object cart
}
```
