Skip to content

Commit

Permalink
cleanup; better query; better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Soxasora committed Feb 21, 2025
1 parent 8c5897b commit fa7725b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
19 changes: 10 additions & 9 deletions api/paidAction/itemUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ export const paymentMethods = [
PAID_ACTION_PAYMENT_METHODS.PESSIMISTIC
]

async function getBaseCostDifference (oldSubName, newSubName, { models }) {
if (oldSubName === newSubName) return 0
const oldSub = await models.sub.findUnique({ where: { name: oldSubName }, select: { baseCost: true } })
const newSub = await models.sub.findUnique({ where: { name: newSubName }, select: { baseCost: true } })
return Math.max(0, (newSub?.baseCost ?? 0) - (oldSub?.baseCost ?? 0))
}

export async function getCost ({ subName, id, boost = 0, uploadIds, bio }, { me, models }) {
// the only reason updating items costs anything is when it has new uploads
// or more boost or is switch to a more expensive sub
// or more boost or is switching to a more expensive sub
const old = await models.item.findUnique({ where: { id: parseInt(id) } })
const { totalFeesMsats } = await uploadFees(uploadIds, { models, me })
let cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost)
if (old.subName !== subName) {
const oldSub = await models.sub.findUnique({ where: { name: old.subName } })
const newSub = await models.sub.findUnique({ where: { name: subName } })
const oldCost = oldSub?.baseCost ?? 0
const newCost = newSub?.baseCost ?? 0
cost += satsToMsats(Math.max(0, newCost - oldCost)) // user will pay just the difference
}
const baseCostDifference = await getBaseCostDifference(old.subName, subName, { models })
const cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost) + satsToMsats(baseCostDifference)

if (cost > 0 && old.invoiceActionState && old.invoiceActionState !== 'PAID') {
throw new Error('creation invoice not paid')
Expand Down
1 change: 1 addition & 0 deletions fragments/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const ITEM_FIELDS = gql`
meMuteSub
meSubscription
nsfw
baseCost
replyCost
}
otsHash
Expand Down
30 changes: 15 additions & 15 deletions pages/items/[id]/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export const getServerSideProps = getGetServerSideProps({
notFound: data => !data.item
})

// TODO: cleanup
const SUB_QUERY = gql`
const SUB_BASECOST = gql`
query Sub($name: String!) {
sub(name: $name) {
name
Expand All @@ -39,16 +38,8 @@ export default function PostEdit ({ ssrData }) {
const { me } = useMe()
const [sub, setSub] = useState(item.subName)

// TODO: cleanup
const { data: oldSubData } = useQuery(SUB_QUERY, {
variables: { name: item.subName },
skip: !item.subName
})

const { data: newSubData } = useQuery(SUB_QUERY, {
variables: { name: sub },
skip: !sub
})
// we need to fetch the new sub to calculate the cost difference
const { data: newSubData } = useQuery(SUB_BASECOST, { variables: { name: sub } })

const [,, editThreshold] = useCanEdit(item)

Expand All @@ -68,7 +59,7 @@ export default function PostEdit ({ ssrData }) {
itemType = 'BOUNTY'
}

function editLineItems (oldSub, newSub) {
const editLineItems = (newSub) => {
const existingBoostLineItem = item.boost
? {
existingBoost: {
Expand All @@ -79,15 +70,24 @@ export default function PostEdit ({ ssrData }) {
}
}
: undefined

const isSwitchingSub = item.subName !== newSub?.name
const subCostDifference = isSwitchingSub && {
...postCommentBaseLineItems({
baseCost: Math.max(0, (newSub?.baseCost ?? 0) - (item?.sub?.baseCost ?? 0)),
me: !!me
})
}

return {
...(item.subName !== newSub?.name ? postCommentBaseLineItems({ baseCost: Math.max(0, newSub?.baseCost - oldSub?.baseCost), me: !!me }) : undefined),
...subCostDifference,
...existingBoostLineItem
}
}

return (
<CenterLayout sub={sub}>
<FeeButtonProvider baseLineItems={editLineItems(oldSubData?.sub, newSubData?.sub)}>
<FeeButtonProvider baseLineItems={editLineItems(newSubData?.sub)}>
<FormType item={item} editThreshold={editThreshold}>
{!item.isJob &&
<SubSelect
Expand Down

0 comments on commit fa7725b

Please sign in to comment.