Changes to deactivate/reactivate products (#89)

This commit is contained in:
rleed 2024-11-04 05:59:04 -03:00 committed by GitHub
parent cf82ed478d
commit 83c94e94db
5 changed files with 41 additions and 13 deletions

10
crud.py
View file

@ -292,8 +292,8 @@ async def create_product(merchant_id: str, data: PartialProduct) -> Product:
await db.execute(
f"""
INSERT INTO nostrmarket.products
(merchant_id, id, stall_id, name, price, quantity, pending, event_id, event_created_at, image_urls, category_list, meta)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
(merchant_id, id, stall_id, name, price, quantity, active, pending, event_id, event_created_at, image_urls, category_list, meta)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO NOTHING
""",
(
@ -303,6 +303,7 @@ async def create_product(merchant_id: str, data: PartialProduct) -> Product:
data.name,
data.price,
data.quantity,
data.active,
data.pending,
data.event_id,
data.event_created_at,
@ -321,13 +322,14 @@ async def update_product(merchant_id: str, product: Product) -> Product:
await db.execute(
f"""
UPDATE nostrmarket.products set name = ?, price = ?, quantity = ?, pending = ?, event_id =?, event_created_at = ?, image_urls = ?, category_list = ?, meta = ?
UPDATE nostrmarket.products set name = ?, price = ?, quantity = ?, active = ?, pending = ?, event_id =?, event_created_at = ?, image_urls = ?, category_list = ?, meta = ?
WHERE merchant_id = ? AND id = ?
""",
(
product.name,
product.price,
product.quantity,
product.active,
product.pending,
product.event_id,
product.event_created_at,
@ -385,7 +387,7 @@ async def get_products_by_ids(
q = ",".join(["?"] * len(product_ids))
rows = await db.fetchall(
f"""
SELECT id, stall_id, name, price, quantity, category_list, meta
SELECT id, stall_id, name, price, quantity, active, category_list, meta
FROM nostrmarket.products
WHERE merchant_id = ? AND pending = false AND id IN ({q})
""",

View file

@ -173,3 +173,8 @@ async def m004_add_merchant_timestamp(db):
await db.execute(
f"ALTER TABLE nostrmarket.merchants ADD COLUMN time TIMESTAMP;"
)
async def m005_update_product_activation(db):
await db.execute(
"ALTER TABLE nostrmarket.products ADD COLUMN active BOOLEAN NOT NULL DEFAULT true;"
)

View file

@ -236,6 +236,7 @@ class PartialProduct(BaseModel):
images: List[str] = []
price: float
quantity: int
active: bool = True
pending: bool = False
config: ProductConfig = ProductConfig()
@ -257,10 +258,12 @@ class Product(PartialProduct, Nostrable):
"currency": self.config.currency,
"price": self.price,
"quantity": self.quantity,
"active": self.active,
"shipping": [dict(s) for s in self.config.shipping or []]
}
categories = [["t", tag] for tag in self.categories]
if self.active:
event = NostrEvent(
pubkey=pubkey,
created_at=round(time.time()),
@ -271,6 +274,8 @@ class Product(PartialProduct, Nostrable):
event.id = event.event_id
return event
else:
return self.to_nostr_delete_event(pubkey)
def to_nostr_delete_event(self, pubkey: str) -> NostrEvent:
delete_event = NostrEvent(

View file

@ -100,6 +100,16 @@
<q-td auto-width>
<q-btn size="sm" color="primary" dense @click="editProduct(props.row)" icon="edit" />
</q-td>
<q-td auto-width>
<q-toggle
@input="updateProduct({ ...props.row, active: props.row.active })"
size="xs"
checked-icon="check"
v-model="props.row.active"
color="green"
unchecked-icon="clear"
/>
</q-td>
<q-td key="id" :props="props"> {{props.row.id}} </q-td>
<q-td key="name" :props="props"> {{shortLabel(props.row.name)}} </q-td>

View file

@ -40,6 +40,12 @@ async function stallDetails(path) {
label: '',
field: ''
},
{
name: 'activate',
align: 'left',
label: '',
field: ''
},
{
name: 'id',