Skip to content

Commit

Permalink
big fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Sep 19, 2024
1 parent 9babcf2 commit 2525f03
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 125 deletions.
19 changes: 12 additions & 7 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ async def create_tpos(data: CreateTposData) -> Tpos:
tpos_id = urlsafe_short_hash()
tpos = Tpos(id=tpos_id, **data.dict())
await db.execute(
insert_query("tpos.pos", data),
data.dict(),
insert_query("tpos.pos", tpos),
tpos.dict(),
)
return tpos


async def get_tpos(tpos_id: str) -> Optional[Tpos]:
row = await db.fetchone("SELECT * FROM tpos.pos WHERE id = :id", {"id": tpos_id})
print(row)
return Tpos(**row) if row else None


Expand All @@ -39,13 +40,15 @@ async def start_lnurlcharge(tpos_id: str):

now = await get_current_timestamp()
withdraw_time_seconds = (
tpos.withdrawbtwn * 60 if tpos.withdrawtimeopt != "secs" else tpos.withdrawbtwn
tpos.withdraw_between * 60
if tpos.withdraw_time_option != "secs"
else tpos.withdraw_between
)
assert (
now - tpos.withdrawtime > withdraw_time_seconds
now - tpos.withdraw_time > withdraw_time_seconds
), f"""
Last withdraw was made too recently, please try again in
{int(withdraw_time_seconds - (now - tpos.withdrawtime))} secs
{int(withdraw_time_seconds - (now - tpos.withdraw_time))} secs
"""

token = urlsafe_short_hash()
Expand Down Expand Up @@ -87,9 +90,11 @@ async def get_clean_tpos(tpos_id: str) -> Optional[TposClean]:
async def update_tpos_withdraw(data: Tpos, tpos_id: str) -> Tpos:
# Calculate the time between withdrawals in seconds
now = await get_current_timestamp()
time_elapsed = now - data.withdrawtime
time_elapsed = now - data.withdraw_time
withdraw_time_seconds = (
data.withdrawbtwn * 60 if data.withdrawtimeopt != "secs" else data.withdrawbtwn
data.withdraw_between * 60
if data.withdraw_time_option != "secs"
else data.withdraw_between
)

logger.debug(f"Time between: {time_elapsed} seconds")
Expand Down
25 changes: 25 additions & 0 deletions migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,28 @@ async def m009_tax_inclusive(db):
"ALTER TABLE tpos.pos ADD COLUMN tax_inclusive BOOL NOT NULL DEFAULT true;"
)
await db.execute("ALTER TABLE tpos.pos ADD COLUMN tax_default FLOAT DEFAULT 0;")


async def m010_rename_tpos_withdraw_columns(db):
"""
Add rename tpos withdraw columns
"""
await db.execute(
"""
CREATE TABLE tpos.pos_backup AS
SELECT
id, name, currency, items, wallet, tax_inclusive,
tax_default, tip_wallet, tip_options,
withdrawtime AS withdraw_time,
withdrawbtwn AS withdraw_between,
withdrawlimit AS withdraw_limit,
withdrawamt AS withdraw_amount,
withdrawtimeopt AS withdraw_time_option,
withdrawpremium AS withdraw_premium,
withdrawpindisabled AS withdraw_pin_disabled,
withdrawpin AS withdraw_pin
FROM tpos.pos
"""
)
await db.execute("DROP TABLE tpos.pos")
await db.execute("ALTER TABLE tpos.pos_backup RENAME TO pos")
50 changes: 26 additions & 24 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,47 @@ class CreateTposData(BaseModel):
wallet: Optional[str]
name: Optional[str]
currency: Optional[str]
tip_options: str = Field("[]")
tip_wallet: str = Field("")
withdrawlimit: int = Field(None, ge=1)
withdrawpin: int = Field(None, ge=1)
withdrawamt: int = Field(None, ge=0)
withdrawtime: int = Field(0)
withdrawtimeopt: Optional[str]
withdrawbtwn: int = Field(10, ge=1)
withdrawpremium: float = Field(None)
withdrawpindisabled: bool = Field(False)
tax_inclusive: bool = Field(True)
tax_default: float = Field(None)
tip_options: str = Field("[]")
tip_wallet: str = Field("")
withdraw_time: int = Field(0)
withdraw_between: int = Field(10, ge=1)
withdraw_limit: Optional[int] = Field(None, ge=1)
withdraw_pin: Optional[int] = Field(None, ge=1)
withdraw_amount: Optional[int] = Field(None, ge=0)
withdraw_time_option: Optional[str] = Field(None)
withdraw_premium: Optional[float] = Field(None)
withdraw_pin_disabled: bool = Field(False)


class TposClean(BaseModel):
id: str
name: str
currency: str
tip_options: Optional[str]
withdrawlimit: Optional[int]
withdrawamt: int
withdrawtime: int
withdrawtimeopt: Optional[str]
withdrawbtwn: int
withdrawpremium: Optional[float]
withdrawpindisabled: Optional[bool]
items: Optional[str]
tax_inclusive: bool
tax_default: Optional[float]
tax_default: Optional[float] = None
withdraw_time: int
withdraw_between: int
withdraw_limit: Optional[int] = None
withdraw_amount: Optional[int] = None
withdraw_time_option: Optional[str] = None
withdraw_premium: Optional[float] = None
withdraw_pin_disabled: Optional[bool] = None
items: Optional[str] = None
tip_options: Optional[str] = None

@property
def withdrawamtposs(self) -> int:
return self.withdrawlimit - self.withdrawamt if self.withdrawlimit else 0
def withdraw_maximum(self) -> int:
if not self.withdraw_amount or not self.withdraw_limit:
return 0
return self.withdraw_limit - self.withdraw_amount


class Tpos(TposClean, BaseModel):
wallet: str
tip_wallet: Optional[str]
withdrawpin: Optional[int]
tip_wallet: Optional[str] = None
withdraw_pin: Optional[int] = None


class LnurlCharge(BaseModel):
Expand Down
16 changes: 8 additions & 8 deletions static/js/tpos.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const tposJS = async () => {
return {
tposId: tpos.id,
currency: tpos.currency,
withdrawamtposs: tpos.withdrawamtposs,
atmPremium: tpos.withdrawpremium / 100,
withdrawpinopen: withdrawpinopen,
atmPremium: tpos.withdraw_premium / 100,
withdraw_maximum: withdraw_maximum,
withdraw_pin_open: withdraw_pin_open,
tip_options: null,
exchangeRate: null,
stack: [],
Expand Down Expand Up @@ -262,12 +262,12 @@ const tposJS = async () => {
if (this.atmPremium > 0) {
this.exchangeRate = this.exchangeRate / (1 + this.atmPremium)
}
if (this.withdrawpinopen != 0) {
this.atmPin = this.withdrawpinopen
if (this.withdraw_pin_open != 0) {
this.atmPin = this.withdraw_pin_open
this.atmSubmit()
return
}
if (this.withdrawamtposs > 0) {
if (this.withdraw_maximum > 0) {
this.atmBox = true
}
},
Expand All @@ -289,7 +289,7 @@ const tposJS = async () => {
atmGetWithdraw: function () {
self = this
var dialog = this.invoiceDialog
if (this.sat > this.withdrawamtposs) {
if (this.sat > this.withdraw_maximum) {
this.$q.notify({
type: 'negative',
message: 'Amount exceeds the maximum withdrawal limit.'
Expand Down Expand Up @@ -710,7 +710,7 @@ const tposJS = async () => {
created: function () {
var getRates = this.getRates
getRates()
this.pinDisabled = tpos.withdrawpindisabled
this.pinDisabled = tpos.withdraw_pin_disabled
this.taxInclusive = tpos.tax_inclusive
this.taxDefault = tpos.tax_default

Expand Down
Loading

0 comments on commit 2525f03

Please sign in to comment.