allow territories to be renamed

This commit is contained in:
keyan 2024-01-11 17:45:17 -06:00
parent cd2979b022
commit 1dae33312f
5 changed files with 36 additions and 42 deletions

View File

@ -1,5 +1,5 @@
import { GraphQLError } from 'graphql'
import serialize, { serializeInvoicable } from './serial'
import { serializeInvoicable } from './serial'
import { TERRITORY_COST_MONTHLY, TERRITORY_COST_ONCE, TERRITORY_COST_YEARLY } from '../../lib/constants'
import { datePivot } from '../../lib/time'
import { ssValidate, territorySchema } from '../../lib/validate'
@ -124,17 +124,9 @@ export default {
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
}
// XXX this is because we did the wrong thing and used the subName as a primary key
const old = await models.sub.findUnique({
where: {
name: data.name,
userId: me.id
}
})
await ssValidate(territorySchema, data, { models, me })
if (old) {
if (data.oldName) {
return await updateSub(parent, data, { me, models, lnd, hash, hmac })
} else {
return await createSub(parent, data, { me, models, lnd, hash, hmac })
@ -265,19 +257,25 @@ async function createSub (parent, data, { me, models, lnd, hash, hmac }) {
}
}
async function updateSub (parent, { name, ...data }, { me, models, lnd, hash, hmac }) {
async function updateSub (parent, { oldName, ...data }, { me, models, lnd, hash, hmac }) {
// prevent modification of billingType
delete data.billingType
try {
const results = await serialize(models,
// update 'em
const results = await models.$transaction([
models.sub.update({
data,
where: {
name
name: oldName,
userId: me.id
}
}))
}),
models.$queryRaw`
UPDATE pgboss.job
SET data = ${JSON.stringify({ subName: data.name })}::JSONB
WHERE name = 'territoryBilling'
AND data->>'subName' = ${oldName}`
])
return results[0]
} catch (error) {

View File

@ -8,7 +8,7 @@ export default gql`
}
extend type Mutation {
upsertSub(name: String!, desc: String, baseCost: Int!,
upsertSub(oldName: String, name: String!, desc: String, baseCost: Int!,
postTypes: [String!]!, allowFreebies: Boolean!,
billingType: String!, billingAutoRenew: Boolean!,
moderated: Boolean!, hash: String, hmac: String): Sub

View File

@ -15,10 +15,10 @@ export default function TerritoryForm ({ sub }) {
const me = useMe()
const [upsertSub] = useMutation(
gql`
mutation upsertSub($name: String!, $desc: String, $baseCost: Int!,
mutation upsertSub($oldName: String, $name: String!, $desc: String, $baseCost: Int!,
$postTypes: [String!]!, $allowFreebies: Boolean!, $billingType: String!,
$billingAutoRenew: Boolean!, $moderated: Boolean!, $hash: String, $hmac: String) {
upsertSub(name: $name, desc: $desc, baseCost: $baseCost,
upsertSub(oldName: $oldName, name: $name, desc: $desc, baseCost: $baseCost,
postTypes: $postTypes, allowFreebies: $allowFreebies, billingType: $billingType,
billingAutoRenew: $billingAutoRenew, moderated: $moderated, hash: $hash, hmac: $hmac) {
name
@ -29,7 +29,7 @@ export default function TerritoryForm ({ sub }) {
const onSubmit = useCallback(
async ({ ...variables }) => {
const { error } = await upsertSub({
variables
variables: { oldName: sub?.name, ...variables }
})
if (error) {
@ -40,9 +40,9 @@ export default function TerritoryForm ({ sub }) {
client.cache.modify({
fields: {
subs (existing = []) {
console.log('existing', existing, variables.name)
const filtered = existing.filter(s => s.name !== variables.name && s.name !== sub?.name)
return [
...existing,
...filtered,
{ __typename: 'Sub', name: variables.name }]
}
}
@ -72,24 +72,15 @@ export default function TerritoryForm ({ sub }) {
onSubmit={onSubmit}
className='mb-5'
storageKeyPrefix={sub ? undefined : 'territory'}
>
{sub?.name
? <Input
label={<>name <small className='text-muted ms-2'>read only</small></>}
name='name'
readOnly
prepend={<InputGroup.Text className='text-monospace'>~</InputGroup.Text>}
className='text-muted'
/>
: <Input
label='name'
name='name'
required
autoFocus
clear
maxLength={32}
prepend={<InputGroup.Text className='text-monospace'>~</InputGroup.Text>}
/>}
> <Input
label='name'
name='name'
required
autoFocus
clear
maxLength={32}
prepend={<InputGroup.Text className='text-monospace'>~</InputGroup.Text>}
/>
<MarkdownInput
label='description'
name='desc'

View File

@ -0,0 +1,5 @@
-- DropForeignKey
ALTER TABLE "Item" DROP CONSTRAINT "Item_subName_fkey";
-- AddForeignKey
ALTER TABLE "Item" ADD CONSTRAINT "Item_subName_fkey" FOREIGN KEY ("subName") REFERENCES "Sub"("name") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -313,7 +313,7 @@ model Item {
pin Pin? @relation(fields: [pinId], references: [id])
root Item? @relation("RootDescendant", fields: [rootId], references: [id])
descendants Item[] @relation("RootDescendant")
sub Sub? @relation(fields: [subName], references: [name])
sub Sub? @relation(fields: [subName], references: [name], onDelete: Cascade, onUpdate: Cascade)
user User @relation("UserItems", fields: [userId], references: [id], onDelete: Cascade)
actions ItemAct[]
mentions Mention[]
@ -447,7 +447,7 @@ model SubAct {
msats BigInt
type SubActType
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
sub Sub @relation(fields: [subName], references: [name], onDelete: Cascade)
sub Sub @relation(fields: [subName], references: [name], onDelete: Cascade, onUpdate: Cascade)
@@index([userId])
@@index([userId, type])
@ -462,7 +462,7 @@ model MuteSub {
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
subName String @db.Citext
userId Int
sub Sub @relation(fields: [subName], references: [name], onDelete: Cascade)
sub Sub @relation(fields: [subName], references: [name], onDelete: Cascade, onUpdate: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@id([userId, subName])