Skip to content

Commit

Permalink
Tickets plugin: add banning feature (#1645)
Browse files Browse the repository at this point in the history
  • Loading branch information
aggre authored Nov 30, 2023
1 parent d9ff2ce commit 2bfa83e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 7 deletions.
4 changes: 4 additions & 0 deletions scripts/populate/cryptocafe.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ const populate = async (client) => {
},
],
},
{
key: 'banId',
value: [214, 215],
},
],
},
{
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/tickets/Id.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
import type { Membership } from '@plugins/memberships'
import type { Ticket } from '.'
import Id_ from './Id.svelte'
import type { BanningRules } from './utils/get-banning-rules'
const { ticket, membership, rpcUrl } = Astro.props as {
type Props = {
ticket: Ticket
membership: Membership
rpcUrl: string
ban: BanningRules
}
const { ticket, membership, rpcUrl, ban } = Astro.props
const sTokensId = Number(Astro.url.searchParams.get('id'))
---

Expand All @@ -18,4 +22,5 @@ const sTokensId = Number(Astro.url.searchParams.get('id'))
membership={membership}
sTokensId={sTokensId}
rpcUrl={rpcUrl}
ban={ban}
/>
16 changes: 14 additions & 2 deletions src/plugins/tickets/Id.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
import Modal from './Modal.svelte'
import { Strings } from './i18n'
import { expirationDatetime } from './utils/date'
import type { BanningRules } from './utils/get-banning-rules'
export let ticket: Ticket
export let membership: UndefinedOr<Membership>
export let sTokensId: UndefinedOr<number>
export let rpcUrl: string
export let ban: BanningRules
const isInvalidId = ban.id.includes(sTokensId ?? 0)
let benefits: UndefinedOr<TicketStatus[]>
let signer: UndefinedOr<Signer>
Expand Down Expand Up @@ -160,8 +164,16 @@

<section class="rounded-md bg-white p-6 text-black shadow">
<div class="mx-auto grid max-w-lg gap-8">
{#if !sTokensId || !membership}
<p>Error</p>
{#if !sTokensId || !membership || isInvalidId}
<p class="text-center font-bold text-dp-white-600">
{#if !membership}
Internal error
{:else if !sTokensId}
No ID specified
{:else if isInvalidId}
Invalid ID
{/if}
</p>
{:else}
<p>#{sTokensId}</p>

Expand Down
7 changes: 6 additions & 1 deletion src/plugins/tickets/Index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
import type { Membership } from '@plugins/memberships'
import type { Tickets } from '.'
import Index_ from './Index.svelte'
import type { BanningRules } from './utils/get-banning-rules'
const { tickets, memberships, propertyAddress, rpcUrl } = Astro.props as {
type Props = {
tickets: Tickets
memberships: Membership[]
propertyAddress: string
rpcUrl: string
ban: BanningRules
}
const { tickets, memberships, propertyAddress, rpcUrl, ban } = Astro.props
---

<Index_
Expand All @@ -17,4 +21,5 @@ const { tickets, memberships, propertyAddress, rpcUrl } = Astro.props as {
memberships={memberships}
propertyAddress={propertyAddress}
rpcUrl={rpcUrl}
ban={ban}
/>
8 changes: 6 additions & 2 deletions src/plugins/tickets/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import { client, clientsSTokens } from '@devprotocol/dev-kit'
import PQueue from 'p-queue'
import { reverse } from 'ramda'
import type { BanningRules } from './utils/get-banning-rules'
export let tickets: Tickets
export let memberships: UndefinedOr<Membership[]>
export let propertyAddress: string
export let rpcUrl: string
export let ban: BanningRules
let account: UndefinedOr<string>
let ownedTickets: UndefinedOr<TicketWithStatus[]>
Expand All @@ -40,9 +42,11 @@
detector(propertyAddress, _account),
)
console.log({ idList })
const filteredIdList = idList?.filter((i) => ban.id.includes(i) === false)
await whenDefined(idList, async (li) => {
console.log({ idList, filteredIdList })
await whenDefined(filteredIdList, async (li) => {
await Promise.all(
reverse(li).map(async (id) =>
queueTickets.add(async () => {
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/tickets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import tickets1 from './assets/tickets-1.jpg'
import tickets2 from './assets/tickets-2.jpg'
import tickets3 from './assets/tickets-3.jpg'
import readme from './README.md'
import { getBanningRules } from './utils/get-banning-rules'

export enum SlotType {
WeekdayTime = 'weekday-time',
Expand Down Expand Up @@ -63,6 +64,7 @@ export const getPagePaths = (async (
{ getPluginConfigById },
) => {
const tickets = getItems(options)
const ban = getBanningRules(options)

const memberships: UndefinedOr<Membership>[] = tickets.map((tk) => {
const [plg] = getPluginConfigById(tk.importedFrom.plugin)
Expand All @@ -78,7 +80,7 @@ export const getPagePaths = (async (
? [
{
paths: ['tickets'],
props: { tickets, memberships, propertyAddress, rpcUrl },
props: { tickets, memberships, propertyAddress, rpcUrl, ban },
component: Index,
},
...tickets.map((ticket, index) => ({
Expand All @@ -93,6 +95,7 @@ export const getPagePaths = (async (
membership: memberships[index],
propertyAddress,
rpcUrl,
ban,
signals: [ClubsPluginSignal.DisplayFullPage],
},
component: Id,
Expand Down
14 changes: 14 additions & 0 deletions src/plugins/tickets/utils/get-banning-rules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { ClubsPluginOptions } from '@devprotocol/clubs-core'
import type { UndefinedOr } from '@devprotocol/util-ts'

export type BanningRules = {
id: number[]
}

export const getBanningRules = (options: ClubsPluginOptions): BanningRules => {
const id =
(options?.find((opt) => opt.key === 'banId')?.value as UndefinedOr<
number[]
>) ?? []
return { id }
}

0 comments on commit 2bfa83e

Please sign in to comment.