feat: activate/deactivate merchant

This commit is contained in:
Vlad Stan 2023-07-03 12:40:42 +03:00
parent b89d761b04
commit 21a77d643f
4 changed files with 86 additions and 7 deletions

View file

@ -398,8 +398,7 @@ async def _handle_outgoing_dms(
async def _handle_incoming_structured_dm(merchant: Merchant, dm: DirectMessage, json_data) -> Optional[str]:
try:
if dm.type == DirectMessageType.CUSTOMER_ORDER.value:
# and merchant.config.active:
if dm.type == DirectMessageType.CUSTOMER_ORDER.value and merchant.config.active:
json_data["public_key"] = dm.public_key
json_data["merchant_public_key"] = merchant.public_key
json_data["event_id"] = dm.event_id

View file

@ -61,6 +61,48 @@ const merchant = async () => {
toggleMerchantKeys: function (value) {
this.showKeys = value
},
toggleMerchantState: async function () {
const merchant = await this.getMerchant()
if (!merchant) {
this.$q.notify({
timeout: 5000,
type: 'warning',
message: "Cannot fetch merchant!"
})
return
}
const message = merchant.config.active ?
'New orders will not be processed. Are you sure you want to deactivate?' :
merchant.config.restore_in_progress ?
'Merchant restore from nostr in progress. Please wait!! ' +
'Activating now can lead to duplicate order processing. Click "OK" if you want to activate anyway?' :
'Are you sure you want activate this merchant?'
LNbits.utils
.confirmDialog(message)
.onOk(async () => {
await this.toggleMerchant()
})
},
toggleMerchant: async function () {
try {
const { data } = await LNbits.api.request(
'PUT',
`/nostrmarket/api/v1/merchant/${this.merchant.id}/toggle`,
this.g.user.wallets[0].adminkey,
)
const state = data.config.active ? 'activated' : 'disabled'
this.merchant = data
this.$q.notify({
type: 'positive',
message: `'Merchant ${state}`,
timeout: 5000
})
} catch (error) {
console.warn(error)
LNbits.utils.notifyApiError(error)
}
},
handleMerchantDeleted: function () {
this.merchant = null
this.shippingZones = []
@ -75,7 +117,7 @@ const merchant = async () => {
public_key: pubkey,
config: {}
}
const {data} = await LNbits.api.request(
const { data } = await LNbits.api.request(
'POST',
'/nostrmarket/api/v1/merchant',
this.g.user.wallets[0].adminkey,
@ -93,12 +135,13 @@ const merchant = async () => {
},
getMerchant: async function () {
try {
const {data} = await LNbits.api.request(
const { data } = await LNbits.api.request(
'GET',
'/nostrmarket/api/v1/merchant',
this.g.user.wallets[0].inkey
)
this.merchant = data
return data
} catch (error) {
LNbits.utils.notifyApiError(error)
}

View file

@ -6,7 +6,7 @@
<q-card>
<q-card-section>
<div class="row">
<div class="col-2">
<div class="col-3">
<merchant-details
:merchant-id="merchant.id"
:inkey="g.user.wallets[0].inkey"
@ -15,7 +15,17 @@
@merchant-deleted="handleMerchantDeleted"
></merchant-details>
</div>
<div class="col-8"></div>
<div class="col-2">
<q-toggle
@input="toggleMerchantState()"
size="md"
checked-icon="check"
v-model="merchant.config.active"
color="green"
unchecked-icon="clear"
/> <span v-text="merchant.config.active ? 'Active': 'Deactivated'"></span>
</div>
<div class="col-5"></div>
<div class="col-2">
<shipping-zones
:inkey="g.user.wallets[0].inkey"

View file

@ -140,7 +140,7 @@ async def api_get_merchant(
merchant = await touch_merchant(wallet.wallet.user, merchant.id)
last_dm_time = await get_last_direct_messages_time(merchant.id)
merchant.config.restore_in_progress = (merchant.time - last_dm_time) < 60
merchant.config.restore_in_progress = (merchant.time - last_dm_time) < 30
return merchant
except Exception as ex:
@ -209,6 +209,33 @@ async def api_republish_merchant(
detail="Cannot get merchant",
)
@nostrmarket_ext.put("/api/v1/merchant/{merchant_id}/toggle")
async def api_republish_merchant(
merchant_id: str,
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> Merchant:
try:
merchant = await get_merchant_for_user(wallet.wallet.user)
assert merchant, "Merchant cannot be found"
assert merchant.id == merchant_id, "Wrong merchant ID"
merchant.config.active = not merchant.config.active
await update_merchant(wallet.wallet.user, merchant.id, merchant.config)
return merchant
except AssertionError as ex:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=str(ex),
)
except Exception as ex:
logger.warning(ex)
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail="Cannot get merchant",
)
@nostrmarket_ext.delete("/api/v1/merchant/{merchant_id}/nostr")
async def api_delete_merchant(