problem: can't set product start time

This commit is contained in:
gsovereignty
2024-08-21 19:14:05 +03:00
parent c94d00a30a
commit a02ae03561
4 changed files with 60 additions and 14 deletions

View File

@@ -16,9 +16,9 @@
export let product: Product; export let product: Product;
export let rocket: Rocket; export let rocket: Rocket;
let price: number = 0; let price: number = 0;
let max: number = 0; let max: number = 0;
let validAfter: number = 0;
let o = false; let o = false;
@@ -33,12 +33,18 @@
if (rocket.Event.author.pubkey != author.pubkey) { if (rocket.Event.author.pubkey != author.pubkey) {
throw new Error(`${author.pubkey} is not the creator of this rocket`); throw new Error(`${author.pubkey} is not the creator of this rocket`);
} }
let event = rocket.UpsertProduct(product.ID(), price, max); console.log(37, validAfter);
event.ndk = $ndk let event = rocket.UpsertProduct(product.ID(), price, max, validAfter);
event.publish().then((x) => { event.ndk = $ndk;
console.log(x); event
o = false .publish()
}).catch(()=>{ console.log("failed to publish", event.rawEvent())}); .then((x) => {
console.log(x);
o = false;
})
.catch(() => {
console.log('failed to publish', event.rawEvent());
});
} }
</script> </script>
@@ -74,6 +80,15 @@
class="col-span-3" class="col-span-3"
/> />
</div> </div>
<div class="grid grid-cols-4 items-center gap-4">
<Label for="validAfter" class="text-right">Valid After</Label>
<Input
bind:value={validAfter}
id="validAfter"
placeholder="Cannot be purchased before this time (unix)"
class="col-span-3"
/>
</div>
</div> </div>
<Todo text={['validate input is a number']} /> <Todo text={['validate input is a number']} />
<Dialog.Footer> <Dialog.Footer>

View File

@@ -39,7 +39,6 @@
} }
function checkNewZaps() { function checkNewZaps() {
console.log(39);
const currentTime = Date.now() / 1000; const currentTime = Date.now() / 1000;
const recentZaps = Array.from(unratifiedZaps.values()).filter( const recentZaps = Array.from(unratifiedZaps.values()).filter(
(zap) => (zap) =>

View File

@@ -23,9 +23,24 @@
if (!product.Validate()) { if (!product.Validate()) {
throw new Error('this should not happen'); throw new Error('this should not happen');
} }
//productFromRocket = rocket.Products().get(product.ID());
}); });
function disabled(productFromRocket: RocketProduct): boolean {
let disabled = false;
if (!productFromRocket?.ValidNow()) {
disabled = true;
}
if (
productFromRocket.MaxPurchases() > 0 &&
productFromRocket.MaxPurchases() - zapsForThisProduct(productFromRocket!).size == 0
) {
disabled = true;
}
return disabled;
// productFromRocket.MaxPurchases() > 0 &&
// productFromRocket.MaxPurchases() - zapsForThisProduct(productFromRocket).size == 0 && !productFromRocket.ValidNow()
}
function zapsForThisProduct(product: RocketProduct): Map<string, ZapPurchase> { function zapsForThisProduct(product: RocketProduct): Map<string, ZapPurchase> {
let m = new Map<string, ZapPurchase>(); let m = new Map<string, ZapPurchase>();
if (unratifiedZaps) { if (unratifiedZaps) {
@@ -84,8 +99,7 @@
</div> </div>
{/if} {/if}
<PayNow <PayNow
disabled={productFromRocket.MaxPurchases() > 0 && disabled={disabled(productFromRocket)}
productFromRocket.MaxPurchases() - zapsForThisProduct(productFromRocket).size == 0}
{product} {product}
rocketProduct={rocket.Products().get(product.ID())} rocketProduct={rocket.Products().get(product.ID())}
{rocket} {rocket}
@@ -102,7 +116,13 @@
'max purchases', 'max purchases',
productFromRocket.MaxPurchases(), productFromRocket.MaxPurchases(),
'price', 'price',
productFromRocket.Price() productFromRocket.Price(),
'valid after',
productFromRocket.ValidAfter(),
'seconds till valid',
productFromRocket.TimeTillValid(),
'validNow',
productFromRocket.ValidNow()
); );
} }
}}>print product</a }}>print product</a

View File

@@ -384,7 +384,7 @@ export class Rocket {
} }
return event; return event;
} }
UpsertProduct(id: string, price: number, maxSales?: number): NDKEvent { UpsertProduct(id: string, price: number, maxSales?: number, validAfter?: number): NDKEvent {
this.PrepareForUpdate(id); this.PrepareForUpdate(id);
let event = new NDKEvent(this.Event.ndk, this.Event.rawEvent()); let event = new NDKEvent(this.Event.ndk, this.Event.rawEvent());
event.created_at = Math.floor(new Date().getTime() / 1000); event.created_at = Math.floor(new Date().getTime() / 1000);
@@ -394,9 +394,14 @@ export class Rocket {
if (existingProduct) { if (existingProduct) {
purchases = existingProduct.PurchasesJSON(); purchases = existingProduct.PurchasesJSON();
} }
let _validAfter = event.created_at;
if (validAfter) {
_validAfter = validAfter;
}
console.log(401, _validAfter);
event.tags.push([ event.tags.push([
'product', 'product',
`${id}:${price}:${event.created_at}:${maxSales}`, `${id}:${price}:${_validAfter}:${maxSales}`,
'wss://relay.nostrocket.org', 'wss://relay.nostrocket.org',
purchases purchases
]); ]);
@@ -571,6 +576,13 @@ export class RocketProduct {
ValidAfter(): number { ValidAfter(): number {
return parseInt(this.tag[1].split(':')[2], 10); return parseInt(this.tag[1].split(':')[2], 10);
} }
TimeTillValid(): number {
return this.ValidAfter() - Math.floor(new Date().getTime() / 1000);
}
ValidNow(): boolean {
let valid = this.TimeTillValid() < 1;
return valid;
}
MaxPurchases(): number { MaxPurchases(): number {
return parseInt(this.tag[1].split(':')[3], 10); return parseInt(this.tag[1].split(':')[3], 10);
} }