/
delete-group.ts
71 lines (59 loc) · 1.97 KB
/
delete-group.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { isModId } from 'common/envs/constants'
import { run } from 'common/supabase/utils'
import { log } from 'shared/log'
import {
createSupabaseClient,
createSupabaseDirectClient,
} from 'shared/supabase/init'
import { APIError, type AuthedUser } from './helpers/endpoint'
export const deleteGroup = async (
props: { id: string } | { slug: string },
auth: AuthedUser
) => {
const db = createSupabaseClient()
const pg = createSupabaseDirectClient()
const q = db.from('groups').select()
if ('id' in props) {
q.eq('id', props.id)
} else {
q.eq('slug', props.slug)
}
const { data: groups } = await run(q)
if (groups.length == 0) {
throw new APIError(404, 'Group not found')
}
const group = groups[0]
log(
`delete group ${group.name} ${group.slug} initiated by ${auth.uid}`,
group
)
const id = group.id
if (!isModId(auth.uid)) {
const requester = await pg.oneOrNone(
'select role from group_members where group_id = $1 and member_id = $2',
[id, auth.uid]
)
if (requester?.role !== 'admin') {
throw new APIError(403, 'You do not have permission to delete this group')
}
}
// fail if there are contracts tagged with this group
// we could just untag contracts like in scripts/deleteGroup.ts
// but I don't trust the mods. I'm forcing them to manually untag or retag contracts to make them reckon with the responsibility of what deleting a group means.
const { count: contractCount } = await pg.one(
`select count(*) from group_contracts where group_id = $1`,
[id]
)
if (contractCount > 0) {
throw new APIError(
400,
`Only topics with no questions can be deleted. There are still ${contractCount} questions tagged with this topic.`
)
}
await pg.tx(async (tx) => {
log('removing group members')
await tx.none('delete from group_members where group_id = $1', [id])
log('deleting group ', id)
await tx.none('delete from groups where id = $1', [id])
})
}