allow territories to be renamed
This commit is contained in:
parent
cd2979b022
commit
1dae33312f
@ -1,5 +1,5 @@
|
|||||||
import { GraphQLError } from 'graphql'
|
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 { TERRITORY_COST_MONTHLY, TERRITORY_COST_ONCE, TERRITORY_COST_YEARLY } from '../../lib/constants'
|
||||||
import { datePivot } from '../../lib/time'
|
import { datePivot } from '../../lib/time'
|
||||||
import { ssValidate, territorySchema } from '../../lib/validate'
|
import { ssValidate, territorySchema } from '../../lib/validate'
|
||||||
@ -124,17 +124,9 @@ export default {
|
|||||||
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
|
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 })
|
await ssValidate(territorySchema, data, { models, me })
|
||||||
|
|
||||||
if (old) {
|
if (data.oldName) {
|
||||||
return await updateSub(parent, data, { me, models, lnd, hash, hmac })
|
return await updateSub(parent, data, { me, models, lnd, hash, hmac })
|
||||||
} else {
|
} else {
|
||||||
return await createSub(parent, data, { me, models, lnd, hash, hmac })
|
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
|
// prevent modification of billingType
|
||||||
delete data.billingType
|
delete data.billingType
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const results = await serialize(models,
|
const results = await models.$transaction([
|
||||||
// update 'em
|
|
||||||
models.sub.update({
|
models.sub.update({
|
||||||
data,
|
data,
|
||||||
where: {
|
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]
|
return results[0]
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -8,7 +8,7 @@ export default gql`
|
|||||||
}
|
}
|
||||||
|
|
||||||
extend type Mutation {
|
extend type Mutation {
|
||||||
upsertSub(name: String!, desc: String, baseCost: Int!,
|
upsertSub(oldName: String, name: String!, desc: String, baseCost: Int!,
|
||||||
postTypes: [String!]!, allowFreebies: Boolean!,
|
postTypes: [String!]!, allowFreebies: Boolean!,
|
||||||
billingType: String!, billingAutoRenew: Boolean!,
|
billingType: String!, billingAutoRenew: Boolean!,
|
||||||
moderated: Boolean!, hash: String, hmac: String): Sub
|
moderated: Boolean!, hash: String, hmac: String): Sub
|
||||||
|
@ -15,10 +15,10 @@ export default function TerritoryForm ({ sub }) {
|
|||||||
const me = useMe()
|
const me = useMe()
|
||||||
const [upsertSub] = useMutation(
|
const [upsertSub] = useMutation(
|
||||||
gql`
|
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!,
|
$postTypes: [String!]!, $allowFreebies: Boolean!, $billingType: String!,
|
||||||
$billingAutoRenew: Boolean!, $moderated: Boolean!, $hash: String, $hmac: 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,
|
postTypes: $postTypes, allowFreebies: $allowFreebies, billingType: $billingType,
|
||||||
billingAutoRenew: $billingAutoRenew, moderated: $moderated, hash: $hash, hmac: $hmac) {
|
billingAutoRenew: $billingAutoRenew, moderated: $moderated, hash: $hash, hmac: $hmac) {
|
||||||
name
|
name
|
||||||
@ -29,7 +29,7 @@ export default function TerritoryForm ({ sub }) {
|
|||||||
const onSubmit = useCallback(
|
const onSubmit = useCallback(
|
||||||
async ({ ...variables }) => {
|
async ({ ...variables }) => {
|
||||||
const { error } = await upsertSub({
|
const { error } = await upsertSub({
|
||||||
variables
|
variables: { oldName: sub?.name, ...variables }
|
||||||
})
|
})
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
@ -40,9 +40,9 @@ export default function TerritoryForm ({ sub }) {
|
|||||||
client.cache.modify({
|
client.cache.modify({
|
||||||
fields: {
|
fields: {
|
||||||
subs (existing = []) {
|
subs (existing = []) {
|
||||||
console.log('existing', existing, variables.name)
|
const filtered = existing.filter(s => s.name !== variables.name && s.name !== sub?.name)
|
||||||
return [
|
return [
|
||||||
...existing,
|
...filtered,
|
||||||
{ __typename: 'Sub', name: variables.name }]
|
{ __typename: 'Sub', name: variables.name }]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -72,24 +72,15 @@ export default function TerritoryForm ({ sub }) {
|
|||||||
onSubmit={onSubmit}
|
onSubmit={onSubmit}
|
||||||
className='mb-5'
|
className='mb-5'
|
||||||
storageKeyPrefix={sub ? undefined : 'territory'}
|
storageKeyPrefix={sub ? undefined : 'territory'}
|
||||||
>
|
> <Input
|
||||||
{sub?.name
|
label='name'
|
||||||
? <Input
|
name='name'
|
||||||
label={<>name <small className='text-muted ms-2'>read only</small></>}
|
required
|
||||||
name='name'
|
autoFocus
|
||||||
readOnly
|
clear
|
||||||
prepend={<InputGroup.Text className='text-monospace'>~</InputGroup.Text>}
|
maxLength={32}
|
||||||
className='text-muted'
|
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
|
<MarkdownInput
|
||||||
label='description'
|
label='description'
|
||||||
name='desc'
|
name='desc'
|
||||||
|
@ -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;
|
@ -313,7 +313,7 @@ model Item {
|
|||||||
pin Pin? @relation(fields: [pinId], references: [id])
|
pin Pin? @relation(fields: [pinId], references: [id])
|
||||||
root Item? @relation("RootDescendant", fields: [rootId], references: [id])
|
root Item? @relation("RootDescendant", fields: [rootId], references: [id])
|
||||||
descendants Item[] @relation("RootDescendant")
|
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)
|
user User @relation("UserItems", fields: [userId], references: [id], onDelete: Cascade)
|
||||||
actions ItemAct[]
|
actions ItemAct[]
|
||||||
mentions Mention[]
|
mentions Mention[]
|
||||||
@ -447,7 +447,7 @@ model SubAct {
|
|||||||
msats BigInt
|
msats BigInt
|
||||||
type SubActType
|
type SubActType
|
||||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
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])
|
||||||
@@index([userId, type])
|
@@index([userId, type])
|
||||||
@ -462,7 +462,7 @@ model MuteSub {
|
|||||||
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
|
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
|
||||||
subName String @db.Citext
|
subName String @db.Citext
|
||||||
userId Int
|
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)
|
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
@@id([userId, subName])
|
@@id([userId, subName])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user