Ignore if sub belongs to user during existence check (#904)
* Ignore if sub belongs to user during existence check * Remove code no longer needed * Fix territory edit Territory edits were broken because validation failed for existing territories and if you edit an territory, it obviously already exists. This commit fixes this by ignoring the territory that we're currently editing. * Fix existence check using stale cache --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
This commit is contained in:
parent
5065b32bde
commit
501885cfa0
|
@ -212,7 +212,7 @@ export default {
|
|||
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
|
||||
}
|
||||
|
||||
await ssValidate(territorySchema, data, { models, me })
|
||||
await ssValidate(territorySchema, data, { models, me, sub: { name: data.oldName } })
|
||||
|
||||
if (data.oldName) {
|
||||
return await updateSub(parent, data, { me, models, lnd, hash, hmac })
|
||||
|
|
|
@ -86,7 +86,7 @@ export default function TerritoryForm ({ sub }) {
|
|||
moderated: sub?.moderated || false,
|
||||
nsfw: sub?.nsfw || false
|
||||
}}
|
||||
schema={territorySchema({ client, me })}
|
||||
schema={territorySchema({ client, me, sub })}
|
||||
invoiceable
|
||||
onSubmit={onSubmit}
|
||||
className='mb-5'
|
||||
|
|
|
@ -183,7 +183,7 @@ async function usernameExists (name, { client, models }) {
|
|||
return !!user
|
||||
}
|
||||
|
||||
async function subExists (name, { client, models, me }) {
|
||||
async function subExists (name, { client, models, me, filter }) {
|
||||
if (!client && !models) {
|
||||
throw new Error('cannot check for territory')
|
||||
}
|
||||
|
@ -191,13 +191,13 @@ async function subExists (name, { client, models, me }) {
|
|||
let sub
|
||||
// apollo client
|
||||
if (client) {
|
||||
const { data } = await client.query({ query: SUB, variables: { sub: name } })
|
||||
const { data } = await client.query({ query: SUB, variables: { sub: name }, fetchPolicy: 'no-cache' })
|
||||
sub = data?.sub
|
||||
} else {
|
||||
sub = await models.sub.findUnique({ where: { name } })
|
||||
}
|
||||
|
||||
return !!sub && sub.userId !== Number(me?.id)
|
||||
return !!sub && (!filter || filter(sub))
|
||||
}
|
||||
|
||||
async function subActive (name, { client, models, me }) {
|
||||
|
@ -277,22 +277,19 @@ export function advPostSchemaMembers ({ me, existingBoost = 0, ...args }) {
|
|||
}
|
||||
|
||||
export function subSelectSchemaMembers (args) {
|
||||
// for subSelectSchemaMembers we want to filter out me
|
||||
// because we want to allow the user to select their own territory
|
||||
const { me, ...filteredArgs } = args
|
||||
return {
|
||||
sub: string().required('required').test({
|
||||
name: 'sub',
|
||||
test: async sub => {
|
||||
if (!sub || !sub.length) return false
|
||||
return await subExists(sub, filteredArgs)
|
||||
return await subExists(sub, args)
|
||||
},
|
||||
message: 'pick valid territory'
|
||||
}).test({
|
||||
name: 'sub',
|
||||
test: async sub => {
|
||||
if (!sub || !sub.length) return false
|
||||
return await subActive(sub, filteredArgs)
|
||||
return await subActive(sub, args)
|
||||
},
|
||||
message: 'territory is not active'
|
||||
})
|
||||
|
@ -414,7 +411,15 @@ export function territorySchema (args) {
|
|||
name: 'name',
|
||||
test: async name => {
|
||||
if (!name || !name.length) return false
|
||||
return !(await subExists(name, args))
|
||||
const edit = !!args.sub
|
||||
let exists
|
||||
if (edit) {
|
||||
// ignore the sub we are currently editing
|
||||
exists = await subExists(name, { ...args, filter: sub => sub.name !== args.sub.name })
|
||||
} else {
|
||||
exists = await subExists(name, args)
|
||||
}
|
||||
return !exists
|
||||
},
|
||||
message: 'taken'
|
||||
}),
|
||||
|
|
Loading…
Reference in New Issue