Skip to content

Commit

Permalink
fix: serial badge actions (#1254)
Browse files Browse the repository at this point in the history
* fix: run revoke/reinstate serially, and hide revoked badges with Other reason

* refactor: add status reason to governance badge
  • Loading branch information
1emu authored Sep 11, 2023
1 parent b158ba7 commit cef989a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
21 changes: 21 additions & 0 deletions src/entities/Badges/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export enum BadgeStatusReason {
TenureEnded = 'tenure ended',
Minted = 'Badge minted by user',
BurnedByUser = 'Badge burned by user',
Other = 'other',
}

export type Badge = {
name: string
description: string
image: string
status: BadgeStatus
statusReason: BadgeStatusReason
isPastBadge: boolean
createdAt: number
transactionHash: string
Expand Down Expand Up @@ -55,11 +57,20 @@ export function isBadgeStatus(value: string | null | undefined): boolean {
return !!value && new Set<string>(Object.values(BadgeStatus)).has(value)
}

export function isBadgeStatusReason(value: string | null | undefined): boolean {
return !!value && new Set<string>(Object.values(BadgeStatusReason)).has(value)
}

export function toBadgeStatus(value: string | null | undefined): BadgeStatus {
if (isBadgeStatus(value)) return value as BadgeStatus
else throw new Error(`Invalid BadgeStatus ${value}`)
}

export function toBadgeStatusReason(value: string | null | undefined): BadgeStatusReason {
if (isBadgeStatusReason(value)) return value as BadgeStatusReason
else throw new Error(`Invalid BadgeStatusReason ${value}`)
}

export function isPastBadge(badge: OtterspaceBadge) {
const status = toBadgeStatus(badge.status)
const expiresAt = badge.spec.metadata.expiresAt
Expand All @@ -77,6 +88,7 @@ export function toGovernanceBadge(otterspaceBadge: OtterspaceBadge) {
createdAt: otterspaceBadge.createdAt,
image: getIpfsHttpsLink(image),
status: toBadgeStatus(otterspaceBadge.status),
statusReason: toBadgeStatusReason(otterspaceBadge.statusReason),
isPastBadge: isPastBadge(otterspaceBadge),
transactionHash: otterspaceBadge.transactionHash || '',
}
Expand Down Expand Up @@ -105,3 +117,12 @@ export function toOtterspaceRevokeReason(
): OtterspaceRevokeReason {
return isOtterspaceRevokeReason(value) ? (value as OtterspaceRevokeReason) : orElse(value)
}

export function shouldDisplayBadge(badge: Badge) {
return (
badge.status !== BadgeStatus.Burned &&
!(badge.status === BadgeStatus.Revoked && badge.statusReason === BadgeStatusReason.Other) &&
badge.name &&
badge.name.length > 0
)
}
48 changes: 23 additions & 25 deletions src/services/BadgesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
OtterspaceRevokeReason,
RevokeOrReinstateResult,
UserBadges,
shouldDisplayBadge,
toGovernanceBadge,
} from '../entities/Badges/types'
import {
Expand Down Expand Up @@ -54,13 +55,11 @@ export class BadgesService {
for (const otterspaceBadge of otterspaceBadges) {
try {
const badge = toGovernanceBadge(otterspaceBadge)
if (badge.status !== BadgeStatus.Burned) {
if (otterspaceBadge.spec.metadata) {
if (badge.isPastBadge) {
expiredBadges.push(badge)
} else {
currentBadges.push(badge)
}
if (shouldDisplayBadge(badge)) {
if (badge.isPastBadge) {
expiredBadges.push(badge)
} else {
currentBadges.push(badge)
}
}
} catch (error) {
Expand Down Expand Up @@ -196,37 +195,36 @@ export class BadgesService {
return []
}

const actionResults = await Promise.all<RevokeOrReinstateResult>(
badgeOwnerships.map(async (badgeOwnership) => {
const trimmedId = trimOtterspaceId(badgeOwnership.id)
const actionResults: RevokeOrReinstateResult[] = []

if (trimmedId === '') {
return {
status: ActionStatus.Failed,
address: badgeOwnership.address,
badgeId: badgeOwnership.id,
error: ErrorReason.InvalidBadgeId,
}
}
for (const badgeOwnership of badgeOwnerships) {
const trimmedId = trimOtterspaceId(badgeOwnership.id)

if (trimmedId === '') {
actionResults.push({
status: ActionStatus.Failed,
address: badgeOwnership.address,
badgeId: badgeOwnership.id,
error: ErrorReason.InvalidBadgeId,
})
} else {
try {
await action(trimmedId)
return {
actionResults.push({
status: ActionStatus.Success,
address: badgeOwnership.address,
badgeId: trimmedId,
}
// eslint-disable-next-line
})
} catch (error: any) {
return {
actionResults.push({
status: ActionStatus.Failed,
address: badgeOwnership.address,
badgeId: trimmedId,
error: JSON.stringify(error?.reason || error),
}
})
}
})
)
}
}

return actionResults
}
Expand Down

0 comments on commit cef989a

Please sign in to comment.