Skip to content

Commit

Permalink
feat: add capability filter (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
thezzisu authored Jun 12, 2024
1 parent e67e925 commit 21a373c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .yarn/versions/9acd0a68.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
releases:
"@aoi-js/frontend": patch
"@aoi-js/server": patch
28 changes: 20 additions & 8 deletions apps/frontend/src/pages/admin/user.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
<VCardTitle class="d-flex align-center">
<div>Users</div>
<VSpacer />
<VTextField
v-model="newSearch"
@keydown.enter="search = newSearch"
label="Search"
hide-details
density="compact"
/>
<div class="u-grid u-grid-flow-col u-grid-rows-1 u-gap-2 u-flex-1">
<CapabilityInput
v-model="searchCapability"
:bits="userBits"
hide-details
density="compact"
/>
<VTextField
v-model="newSearch"
@keydown.enter="search = newSearch"
label="Search"
hide-details
density="compact"
/>
</div>
</VCardTitle>
<VDataTableServer
:headers="headers"
Expand Down Expand Up @@ -95,6 +103,7 @@ const headers = [
] as const
const search = useRouteQuery('search', '')
const searchCapability = useRouteQuery('capability', '')
const newSearch = ref(search.value)
const {
Expand All @@ -112,7 +121,10 @@ const {
tags?: string[]
}>(
`admin/user`,
computed(() => ({ search: search.value || undefined }))
computed(() => ({
search: search.value || undefined,
capability: searchCapability.value || undefined
}))
)
const dialog = ref(false)
Expand Down
22 changes: 19 additions & 3 deletions apps/frontend/src/pages/org/[orgId]/admin/member.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
<VCard variant="flat">
<VCardTitle class="d-flex justify-space-between align-center">
<div>{{ t('term.members') }}</div>
<div class="flex-grow-1 u-max-w-64">
<VSpacer />
<div class="u-grid u-grid-flow-col u-grid-rows-1 u-gap-2 u-flex-1">
<CapabilityInput
v-model="searchCapability"
:bits="orgBits"
hide-details
density="compact"
/>
<UserIdInput
v-model="newMember"
:label="t('term.user-id')"
density="compact"
append-icon="mdi-plus"
@click:append="addMember"
hide-details
/>
</div>
</VCardTitle>
Expand Down Expand Up @@ -61,7 +69,8 @@
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouteQuery } from '@vueuse/router'
import { computed, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import AoiGravatar from '@/components/aoi/AoiGravatar.vue'
Expand All @@ -85,6 +94,8 @@ const headers = [
{ title: t('term.actions'), key: '_actions' }
] as const
const searchCapability = useRouteQuery('capability', '')
const {
page,
itemsPerPage,
Expand All @@ -98,7 +109,12 @@ const {
}
}
capability: string
}>(`org/${props.orgId}/admin/member`, {})
}>(
`org/${props.orgId}/admin/member`,
computed(() => ({
capability: searchCapability.value || undefined
}))
)
async function deleteMember(userId: string) {
await http.delete(`org/${props.orgId}/admin/member/${userId}`)
Expand Down
10 changes: 7 additions & 3 deletions apps/server/src/routes/admin/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const adminUserRoutes = defineRoutes(async (s) => {
page: T.Integer({ minimum: 1, default: 1 }),
perPage: T.Integer({ enum: [15, 30, 50, 100] }),
count: T.Boolean({ default: false }),
search: T.Optional(T.String({ minLength: 1 }))
search: T.Optional(T.String({ minLength: 1 })),
capability: T.Optional(T.String())
}),
response: {
200: T.PaginationResult(T.Any())
Expand All @@ -25,9 +26,12 @@ export const adminUserRoutes = defineRoutes(async (s) => {
},
async (req) => {
const filter: Document = {}
if (req.query.search) {
filter['profile.name'] = { $regex: req.query.search }
if (req.query.search) filter['profile.name'] = { $regex: req.query.search }
if (req.query.capability && +req.query.capability) {
const capability = new BSON.Long(req.query.capability)
filter.$expr = { $eq: [{ $bitAnd: ['$capability', capability] }, capability] }
}

const { items, total } = await findPaginated(
users,
req.query.page,
Expand Down
16 changes: 13 additions & 3 deletions apps/server/src/routes/org/admin/member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const orgAdminMemberRoutes = defineRoutes(async (s) => {
querystring: T.Object({
page: T.Integer({ minimum: 1, default: 1 }),
perPage: T.Integer({ enum: [15, 30, 50, 100] }),
count: T.Boolean({ default: false })
count: T.Boolean({ default: false }),
capability: T.Optional(T.String())
}),
response: {
200: T.PaginationResult(T.Any())
Expand All @@ -28,14 +29,23 @@ export const orgAdminMemberRoutes = defineRoutes(async (s) => {
},
async (req) => {
const ctx = req.inject(kOrgContext)
const capability = new BSON.Long(req.query.capability ?? 0)
let count = 0
if (req.query.count) {
count = await orgMemberships.countDocuments({ orgId: ctx._orgId })
count = await orgMemberships.countDocuments({
orgId: ctx._orgId,
$expr: { $eq: [{ $bitAnd: ['$capability', capability] }, capability] }
})
}
const skip = paginationSkip(req.query.page, req.query.perPage)
const members = await orgMemberships
.aggregate([
{ $match: { orgId: ctx._orgId } },
{
$match: {
orgId: ctx._orgId,
$expr: { $eq: [{ $bitAnd: ['$capability', capability] }, capability] }
}
},
{ $skip: skip },
{ $limit: req.query.perPage },
{
Expand Down

0 comments on commit 21a373c

Please sign in to comment.