Skip to content

Commit 9925a18

Browse files
committed
pay difference on territory switch
1 parent d8aa1ea commit 9925a18

File tree

4 files changed

+63
-24
lines changed

4 files changed

+63
-24
lines changed

api/paidAction/itemUpdate.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ export const paymentMethods = [
1212
PAID_ACTION_PAYMENT_METHODS.PESSIMISTIC
1313
]
1414

15-
export async function getCost ({ id, boost = 0, uploadIds, bio }, { me, models }) {
15+
// TODO: cleanup
16+
export async function getCost ({ subName, id, boost = 0, uploadIds, bio }, { me, models }) {
1617
// the only reason updating items costs anything is when it has new uploads
17-
// or more boost
18+
// or more boost or a more expensive sub
1819
const old = await models.item.findUnique({ where: { id: parseInt(id) } })
1920
const { totalFeesMsats } = await uploadFees(uploadIds, { models, me })
20-
const cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost)
21+
let cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost)
22+
if (old.subName !== subName) {
23+
const oldSub = await models.sub.findUnique({ where: { name: old.subName } })
24+
const newSub = await models.sub.findUnique({ where: { name: subName } })
25+
const oldCost = oldSub?.baseCost ?? 0
26+
const newCost = newSub?.baseCost ?? 0
27+
cost += satsToMsats(Math.max(0, newCost - oldCost))
28+
}
2129

2230
if (cost > 0 && old.invoiceActionState && old.invoiceActionState !== 'PAID') {
2331
throw new Error('creation invoice not paid')

api/resolvers/item.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1479,13 +1479,14 @@ export const updateItem = async (parent, { sub: subName, forward, hash, hmac, ..
14791479
throw new GqlInputError('item does not belong to you')
14801480
}
14811481

1482-
const differentSub = subName && old.subName !== subName
1483-
if (differentSub) {
1484-
const sub = await models.sub.findUnique({ where: { name: subName } })
1485-
if (sub.baseCost > old.sub.baseCost) {
1486-
throw new GqlInputError('cannot change to a more expensive sub')
1487-
}
1488-
}
1482+
// TODO: cleanup
1483+
// const differentSub = subName && old.subName !== subName
1484+
// if (differentSub) {
1485+
// const sub = await models.sub.findUnique({ where: { name: subName } })
1486+
// if (sub.baseCost > old.sub.baseCost) {
1487+
// // throw new GqlInputError('cannot change to a more expensive sub')
1488+
// }
1489+
// }
14891490

14901491
// in case they lied about their existing boost
14911492
await validateSchema(advSchema, { boost: item.boost }, { models, me, existingBoost: old.boost })

components/item-history.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { timeSince } from '@/lib/time'
22
import styles from './item.module.css'
33
import Text from './text'
44
import { Dropdown } from 'react-bootstrap'
5-
import { useRouter } from 'next/router'
5+
// import { useRouter } from 'next/router'
66
import { useShowModal } from './modal'
77

88
export function OldItem ({ version }) {
@@ -19,8 +19,9 @@ export function OldItem ({ version }) {
1919
)
2020
}
2121

22+
// TODO: cleanup
2223
export default function HistoryDropdown ({ item }) {
23-
const router = useRouter()
24+
// const router = useRouter()
2425
const showModal = useShowModal()
2526

2627
const lastEdited = new Date(item.oldVersions[0].cloneDiedAt)

pages/items/[id]/edit.js

+41-12
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,49 @@ import JobForm from '@/components/job-form'
77
import { PollForm } from '@/components/poll-form'
88
import { BountyForm } from '@/components/bounty-form'
99
import { useState } from 'react'
10-
import { useQuery } from '@apollo/client'
10+
import { gql, useQuery } from '@apollo/client'
1111
import { useRouter } from 'next/router'
1212
import PageLoading from '@/components/page-loading'
13-
import { FeeButtonProvider } from '@/components/fee-button'
13+
import { FeeButtonProvider, postCommentBaseLineItems } from '@/components/fee-button'
1414
import SubSelect from '@/components/sub-select'
1515
import useCanShadowEdit from '@/components/use-can-edit'
16+
import { useMe } from '@/components/me'
1617

1718
export const getServerSideProps = getGetServerSideProps({
1819
query: ITEM,
1920
notFound: data => !data.item
2021
})
2122

23+
// TODO: cleanup
24+
const SUB_QUERY = gql`
25+
query Sub($name: String!) {
26+
sub(name: $name) {
27+
name
28+
baseCost
29+
}
30+
}
31+
`
32+
2233
export default function PostEdit ({ ssrData }) {
34+
const { me } = useMe()
2335
const router = useRouter()
2436
const { data } = useQuery(ITEM, { variables: { id: router.query.id } })
2537
if (!data && !ssrData) return <PageLoading />
2638

2739
const { item } = data || ssrData
2840
const [sub, setSub] = useState(item.subName)
2941

42+
// TODO: cleanup
43+
const { data: oldSubData } = useQuery(SUB_QUERY, {
44+
variables: { name: item.subName },
45+
skip: !item.subName
46+
})
47+
48+
const { data: newSubData } = useQuery(SUB_QUERY, {
49+
variables: { name: sub },
50+
skip: !sub
51+
})
52+
3053
const [,, shadowEditThreshold] = useCanShadowEdit(item)
3154

3255
let FormType = DiscussionForm
@@ -45,20 +68,26 @@ export default function PostEdit ({ ssrData }) {
4568
itemType = 'BOUNTY'
4669
}
4770

48-
const existingBoostLineItem = item.boost
49-
? {
50-
existingBoost: {
51-
label: 'old boost',
52-
term: `- ${item.boost}`,
53-
op: '-',
54-
modifier: cost => cost - item.boost
71+
function editLineItems (oldSub, newSub) {
72+
const existingBoostLineItem = item.boost
73+
? {
74+
existingBoost: {
75+
label: 'old boost',
76+
term: `- ${item.boost}`,
77+
op: '-',
78+
modifier: cost => cost - item.boost
79+
}
5580
}
56-
}
57-
: undefined
81+
: undefined
82+
return {
83+
...(item.subName !== newSub?.name ? postCommentBaseLineItems({ baseCost: Math.max(0, newSub?.baseCost - oldSub?.baseCost), me: !!me }) : undefined),
84+
...existingBoostLineItem
85+
}
86+
}
5887

5988
return (
6089
<CenterLayout sub={sub}>
61-
<FeeButtonProvider baseLineItems={existingBoostLineItem}>
90+
<FeeButtonProvider baseLineItems={editLineItems(oldSubData?.sub, newSubData?.sub)}>
6291
<FormType item={item} shadowEditThreshold={shadowEditThreshold}>
6392
{!item.isJob &&
6493
<SubSelect

0 commit comments

Comments
 (0)