@frigear-nu/users 3.1.6 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.d.mts +16 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +6 -2
- package/dist/runtime/app/pages/admin.vue +68 -1
- package/dist/runtime/app/plugins/terms-gate.client.js +17 -2
- package/dist/runtime/app/tabs/Overview.vue +2 -1
- package/dist/runtime/app/utils/admin-user.d.ts +5 -0
- package/dist/runtime/app/widgets/NewUsers.vue +36 -24
- package/dist/runtime/app/widgets/NewUsersStat.vue +20 -2
- package/dist/runtime/i18n/lang/da.json +13 -2
- package/dist/runtime/i18n/lang/en.json +13 -2
- package/dist/runtime/server/routes/admin/stats.get.js +9 -5
- package/dist/runtime/server/routes/admin/users.get.js +7 -39
- package/dist/runtime/server/utils/admin-list.d.ts +45 -0
- package/dist/runtime/server/utils/admin-list.js +54 -0
- package/dist/runtime/server/utils/admin.d.ts +2 -0
- package/dist/runtime/server/utils/admin.js +1 -0
- package/dist/runtime/utils/api.d.ts +1 -1
- package/dist/runtime/utils/api.js +1 -1
- package/dist/runtime/utils/trend.d.ts +9 -0
- package/dist/runtime/utils/trend.js +5 -0
- package/dist/types.d.mts +1 -1
- package/package.json +5 -5
package/dist/module.d.mts
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
2
|
import { H3Event } from 'h3';
|
|
3
|
+
import { SQL } from 'drizzle-orm';
|
|
3
4
|
|
|
5
|
+
type UsersMembershipFilterContext = {
|
|
6
|
+
event: H3Event;
|
|
7
|
+
now: Date;
|
|
8
|
+
status?: 'active' | 'expired' | 'none';
|
|
9
|
+
plan?: string;
|
|
10
|
+
condition?: SQL;
|
|
11
|
+
plans: {
|
|
12
|
+
value: string;
|
|
13
|
+
label: string;
|
|
14
|
+
}[];
|
|
15
|
+
exportUrl?: string;
|
|
16
|
+
};
|
|
4
17
|
/**
|
|
5
18
|
* Context of `users:deleted`: a staff member deleted the user (`DELETE /api/admin/users/:id`). Modules that keep rows
|
|
6
19
|
* about a user without a foreign key (wristbands, ...) remove them here. `event` is the request.
|
|
@@ -165,6 +178,8 @@ type UserTermsAcceptanceDto = {
|
|
|
165
178
|
|
|
166
179
|
declare module 'nitropack' {
|
|
167
180
|
interface NitroRuntimeHooks {
|
|
181
|
+
/** Supplies the optional membership filters for the admin users list. */
|
|
182
|
+
'users:admin:membership-filter': (ctx: UsersMembershipFilterContext) => void | Promise<void>;
|
|
168
183
|
/** A user was deleted (after their row is gone): remove what you keep about them. */
|
|
169
184
|
'users:deleted': (ctx: UserDeletedContext) => void | Promise<void>;
|
|
170
185
|
/** A user accepted the current terms (`users.terms`), on the registration form or at the prompt; the history row is written. */
|
|
@@ -191,4 +206,4 @@ declare module 'nuxt/schema' {
|
|
|
191
206
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
192
207
|
|
|
193
208
|
export { _default as default };
|
|
194
|
-
export type { AdminUserDto, ModuleOptions, ProfileDto, PublicModuleOptions, UserDeletedContext, UserTermsAcceptanceDto, UserTermsStatusDto, UsersPublicTerms, UsersTermsAcceptedContext, UsersTermsSource };
|
|
209
|
+
export type { AdminUserDto, ModuleOptions, ProfileDto, PublicModuleOptions, UserDeletedContext, UserTermsAcceptanceDto, UserTermsStatusDto, UsersMembershipFilterContext, UsersPublicTerms, UsersTermsAcceptedContext, UsersTermsSource };
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -263,15 +263,19 @@ const module$1 = defineNuxtModule({
|
|
|
263
263
|
title: "New users",
|
|
264
264
|
titleKey: "users.admin.dashboard.newUsersStat",
|
|
265
265
|
order: 10,
|
|
266
|
-
size: { cols: 1, maxCols: 2 }
|
|
266
|
+
size: { cols: 1, maxCols: 2 },
|
|
267
|
+
// Shown to staff who may read the numbers (the route checks it too).
|
|
268
|
+
permission: usersAccess.permissions.list
|
|
267
269
|
}, "top");
|
|
268
270
|
extendAdminDashboard(nuxt, {
|
|
269
271
|
id: "users-new-users",
|
|
270
272
|
component: resolver.resolve("./runtime/app/widgets/NewUsers.vue"),
|
|
271
273
|
title: "Latest sign-ups",
|
|
274
|
+
icon: "i-lucide-users",
|
|
272
275
|
titleKey: "users.admin.dashboard.newUsers",
|
|
273
276
|
order: 20,
|
|
274
|
-
size: { cols: 2, minCols: 1, maxCols: 4, rows: 2 }
|
|
277
|
+
size: { cols: 2, minCols: 1, maxCols: 4, rows: 2 },
|
|
278
|
+
permission: usersAccess.permissions.list
|
|
275
279
|
});
|
|
276
280
|
}
|
|
277
281
|
}
|
|
@@ -10,7 +10,7 @@ const format = useFormat();
|
|
|
10
10
|
const actions = useUserActions();
|
|
11
11
|
const { page, pageSize, search, sort: sortState, filters, query, filtered, reset } = useTableQuery({
|
|
12
12
|
sort: { id: "createdAt", desc: true },
|
|
13
|
-
filters: { verified: "all", role: "all", mediaOptOut: "all" }
|
|
13
|
+
filters: { verified: "all", role: "all", mediaOptOut: "all", membership: "all", membershipPlan: "*" }
|
|
14
14
|
});
|
|
15
15
|
const sort = useTableSort(null, sortState);
|
|
16
16
|
const { sorting } = sort;
|
|
@@ -19,6 +19,17 @@ const { data, status, error, refresh } = await useFetch(usersApi.admin.users, {
|
|
|
19
19
|
query
|
|
20
20
|
});
|
|
21
21
|
const rows = computed(() => data.value?.items ?? []);
|
|
22
|
+
const membershipExportHref = computed(() => {
|
|
23
|
+
const url = data.value?.membershipExportUrl;
|
|
24
|
+
if (!url) return void 0;
|
|
25
|
+
const { page: _page, pageSize: _pageSize, ...filterQuery } = query.value;
|
|
26
|
+
const params = new URLSearchParams();
|
|
27
|
+
for (const [key, value] of Object.entries(filterQuery)) {
|
|
28
|
+
if (value !== void 0 && value !== "") params.set(key, String(value));
|
|
29
|
+
}
|
|
30
|
+
const search2 = params.toString();
|
|
31
|
+
return search2 ? `${url}?${search2}` : url;
|
|
32
|
+
});
|
|
22
33
|
const hasRoles = computed(() => data.value?.hasRoles ?? false);
|
|
23
34
|
const hasSubscriptions = computed(() => data.value?.hasSubscriptions ?? false);
|
|
24
35
|
const roles = computed(() => data.value?.roles ?? []);
|
|
@@ -26,9 +37,29 @@ const forbidden = computed(() => [401, 403].includes(error.value?.statusCode ??
|
|
|
26
37
|
const roleLabels = computed(() => new Map(roles.value.map((role) => [role.value, role.label])));
|
|
27
38
|
const roleLabel = (value) => roleLabels.value.get(value) ?? value;
|
|
28
39
|
const roleFilterItems = computed(() => [{ label: t("admin.filters.allRoles", "All roles"), value: "all" }, ...roles.value.map(({ value, label }) => ({ value, label }))]);
|
|
40
|
+
const membershipOptions = computed(() => [
|
|
41
|
+
{ label: t("admin.filters.anyMembership", "Any membership status"), value: "all" },
|
|
42
|
+
{ label: t("admin.filters.activeMember", "Active membership"), value: "active" },
|
|
43
|
+
{ label: t("admin.filters.expiredMember", "Expired membership"), value: "expired" },
|
|
44
|
+
{ label: t("admin.filters.noMembership", "No membership"), value: "none" }
|
|
45
|
+
]);
|
|
46
|
+
const membershipPlanItems = computed(() => data.value?.membershipPlans ?? []);
|
|
47
|
+
const membershipPlanOptions = computed(() => [
|
|
48
|
+
{ label: t("admin.filters.allPlans", "All plans"), value: "*" },
|
|
49
|
+
...membershipPlanItems.value
|
|
50
|
+
]);
|
|
29
51
|
const pending = computed(() => status.value === "pending");
|
|
30
52
|
const loading = computed(() => pending.value && !data.value);
|
|
31
53
|
const refreshing = computed(() => pending.value && !!data.value);
|
|
54
|
+
function showMembers() {
|
|
55
|
+
reset();
|
|
56
|
+
filters.membership = "active";
|
|
57
|
+
page.value = 1;
|
|
58
|
+
}
|
|
59
|
+
function selectMembership(value) {
|
|
60
|
+
filters.membership = value;
|
|
61
|
+
if (value === "none") filters.membershipPlan = "*";
|
|
62
|
+
}
|
|
32
63
|
const StatusBadge = resolveComponent("StatusBadge");
|
|
33
64
|
const TableIdentity = resolveComponent("TableIdentity");
|
|
34
65
|
const RowMenu = resolveComponent("RowMenu");
|
|
@@ -103,6 +134,8 @@ const addUser = () => actions.create();
|
|
|
103
134
|
<PageHeading :title="t('admin.title', 'Users')">
|
|
104
135
|
<template #actions>
|
|
105
136
|
<TooltipButton
|
|
137
|
+
:kbds="['meta', 'enter']"
|
|
138
|
+
shortcut-level="page"
|
|
106
139
|
icon="i-lucide-plus"
|
|
107
140
|
:label="t('admin.create', 'Add user')"
|
|
108
141
|
:tooltip="t('admin.createButtonHint', 'Create an account, with a welcome email if you choose')"
|
|
@@ -147,6 +180,40 @@ const addUser = () => actions.create();
|
|
|
147
180
|
class="w-48"
|
|
148
181
|
icon="i-lucide-shield"
|
|
149
182
|
/>
|
|
183
|
+
<UButton
|
|
184
|
+
v-if="hasSubscriptions"
|
|
185
|
+
color="neutral"
|
|
186
|
+
:variant="filters.membership === 'active' ? 'soft' : 'outline'"
|
|
187
|
+
icon="i-lucide-badge-check"
|
|
188
|
+
:label="t('admin.filters.members', 'Members')"
|
|
189
|
+
@click="showMembers"
|
|
190
|
+
/>
|
|
191
|
+
<USelect
|
|
192
|
+
v-if="hasSubscriptions"
|
|
193
|
+
:model-value="filters.membership"
|
|
194
|
+
:items="membershipOptions"
|
|
195
|
+
:aria-label="t('admin.filters.membershipStatus', 'Membership status')"
|
|
196
|
+
class="w-48"
|
|
197
|
+
@update:model-value="selectMembership"
|
|
198
|
+
/>
|
|
199
|
+
<USelect
|
|
200
|
+
v-if="hasSubscriptions && membershipPlanItems.length && filters.membership !== 'none'"
|
|
201
|
+
v-model="filters.membershipPlan"
|
|
202
|
+
:items="membershipPlanOptions"
|
|
203
|
+
:aria-label="t('admin.filters.membershipPlan', 'Membership plan')"
|
|
204
|
+
class="w-40"
|
|
205
|
+
/>
|
|
206
|
+
<TooltipButton
|
|
207
|
+
v-if="membershipExportHref && filters.membership !== 'none'"
|
|
208
|
+
:to="membershipExportHref"
|
|
209
|
+
external
|
|
210
|
+
download
|
|
211
|
+
variant="outline"
|
|
212
|
+
color="neutral"
|
|
213
|
+
icon="i-lucide-download"
|
|
214
|
+
:label="t('admin.exportMembers', 'Export members')"
|
|
215
|
+
:tooltip="t('admin.exportMembersHint', 'Download all memberships matching the filters as CSV')"
|
|
216
|
+
/>
|
|
150
217
|
<UButton
|
|
151
218
|
v-if="filtered"
|
|
152
219
|
variant="ghost"
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { watch } from "vue";
|
|
2
2
|
import { usersApi } from "../../utils/api.js";
|
|
3
3
|
import UsersTermsGate from "../components/UsersTermsGate.vue";
|
|
4
|
-
import { $fetch, defineNuxtPlugin, useLocaleLink, useOverlay, useRouter, useRuntimeConfig, useUserSession } from "#imports";
|
|
4
|
+
import { $fetch, defineNuxtPlugin, useCookie, useLocaleLink, useOverlay, useRouter, useRuntimeConfig, useUserSession } from "#imports";
|
|
5
|
+
const TERMS_OK_COOKIE = "users-terms-ok";
|
|
6
|
+
const TERMS_OK_MAX_AGE = 60 * 60 * 24;
|
|
5
7
|
export default defineNuxtPlugin({
|
|
6
8
|
name: "users:terms-gate",
|
|
7
9
|
setup(nuxtApp) {
|
|
@@ -13,6 +15,11 @@ export default defineNuxtPlugin({
|
|
|
13
15
|
const router = useRouter();
|
|
14
16
|
const { isActive } = useLocaleLink();
|
|
15
17
|
const gate = useOverlay().create(UsersTermsGate, { destroyOnClose: true });
|
|
18
|
+
const termsOk = useCookie(TERMS_OK_COOKIE, { maxAge: TERMS_OK_MAX_AGE, path: "/", sameSite: "lax" });
|
|
19
|
+
const okKey = (id) => `${String(id)}:${terms.version}`;
|
|
20
|
+
const remember = (id, next) => {
|
|
21
|
+
termsOk.value = next?.upToDate ? okKey(id) : null;
|
|
22
|
+
};
|
|
16
23
|
let status = null;
|
|
17
24
|
let open = false;
|
|
18
25
|
let checked;
|
|
@@ -23,7 +30,10 @@ export default defineNuxtPlugin({
|
|
|
23
30
|
open = true;
|
|
24
31
|
void gate.open({ status }).then((next) => {
|
|
25
32
|
open = false;
|
|
26
|
-
if (next)
|
|
33
|
+
if (next) {
|
|
34
|
+
status = next;
|
|
35
|
+
if (checked !== void 0) remember(checked, next);
|
|
36
|
+
}
|
|
27
37
|
});
|
|
28
38
|
} else if (!needed && open) {
|
|
29
39
|
open = false;
|
|
@@ -39,8 +49,13 @@ export default defineNuxtPlugin({
|
|
|
39
49
|
}
|
|
40
50
|
if (checked === id) return sync();
|
|
41
51
|
checked = id;
|
|
52
|
+
if (termsOk.value === okKey(id)) {
|
|
53
|
+
status = null;
|
|
54
|
+
return sync();
|
|
55
|
+
}
|
|
42
56
|
try {
|
|
43
57
|
status = await $fetch(usersApi.me.terms);
|
|
58
|
+
remember(id, status);
|
|
44
59
|
} catch {
|
|
45
60
|
status = null;
|
|
46
61
|
checked = void 0;
|
|
@@ -171,9 +171,10 @@ async function save(event) {
|
|
|
171
171
|
</UForm>
|
|
172
172
|
<template #footer>
|
|
173
173
|
<div class="flex justify-end">
|
|
174
|
-
<
|
|
174
|
+
<TooltipButton
|
|
175
175
|
type="submit"
|
|
176
176
|
form="user-profile-form"
|
|
177
|
+
:kbds="['meta', 'enter']"
|
|
177
178
|
:loading="saving"
|
|
178
179
|
:disabled="!dirty || !user.can.update"
|
|
179
180
|
:label="t('admin.detail.saveProfile', 'Save profile')"
|
|
@@ -23,6 +23,11 @@ export type UsersPage = {
|
|
|
23
23
|
pageSize: number;
|
|
24
24
|
hasRoles: boolean;
|
|
25
25
|
hasSubscriptions: boolean;
|
|
26
|
+
membershipPlans?: {
|
|
27
|
+
value: string;
|
|
28
|
+
label: string;
|
|
29
|
+
}[];
|
|
30
|
+
membershipExportUrl?: string;
|
|
26
31
|
roles: UserRoleOption[];
|
|
27
32
|
};
|
|
28
33
|
/** The shape of `GET /api/admin/users/:id`. */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<script setup>
|
|
2
2
|
import { usersApi } from "../../utils/api";
|
|
3
|
-
import { statusColor, useFormat, useLazyFetch, useLocaleLink, useModuleT, useRuntimeConfig } from "#imports";
|
|
3
|
+
import { computed, statusColor, useFormat, useLazyFetch, useLocaleLink, useModuleT, useRuntimeConfig } from "#imports";
|
|
4
4
|
defineOptions({ name: "UsersNewUsersWidget" });
|
|
5
5
|
defineProps({
|
|
6
6
|
framed: { type: Boolean, required: false }
|
|
@@ -10,7 +10,8 @@ const format = useFormat();
|
|
|
10
10
|
const { localize } = useLocaleLink();
|
|
11
11
|
const adminPage = useRuntimeConfig().public.users?.pages?.admin;
|
|
12
12
|
const listPath = adminPage ? localize(adminPage.replace(/\/+$/, "")) : void 0;
|
|
13
|
-
const { data
|
|
13
|
+
const { data, status } = useLazyFetch(usersApi.admin.new, { key: "admin-users-new", default: () => [] });
|
|
14
|
+
const users = computed(() => data.value?.slice(0, 6) ?? []);
|
|
14
15
|
</script>
|
|
15
16
|
|
|
16
17
|
<template>
|
|
@@ -26,29 +27,40 @@ const { data: users, status } = useLazyFetch(usersApi.admin.new, { key: "admin-u
|
|
|
26
27
|
<li
|
|
27
28
|
v-for="user in users"
|
|
28
29
|
:key="user.id"
|
|
29
|
-
class="flex items-center
|
|
30
|
+
class="flex items-center gap-3"
|
|
30
31
|
>
|
|
31
|
-
<
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
<
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
32
|
+
<UAvatar
|
|
33
|
+
:src="user.avatar ?? void 0"
|
|
34
|
+
:alt="user.name"
|
|
35
|
+
size="sm"
|
|
36
|
+
/>
|
|
37
|
+
<div class="min-w-0 flex-1">
|
|
38
|
+
<div class="flex min-w-0 items-center gap-2">
|
|
39
|
+
<NuxtLink
|
|
40
|
+
v-if="listPath"
|
|
41
|
+
:to="`${listPath}/${user.id}`"
|
|
42
|
+
class="text-highlighted truncate font-medium hover:underline"
|
|
43
|
+
>{{ user.name }}</NuxtLink>
|
|
44
|
+
<span
|
|
45
|
+
v-else
|
|
46
|
+
class="text-highlighted truncate font-medium"
|
|
47
|
+
>{{ user.name }}</span>
|
|
48
|
+
<UBadge
|
|
49
|
+
v-if="user.subscription"
|
|
50
|
+
size="sm"
|
|
51
|
+
variant="subtle"
|
|
52
|
+
:color="statusColor(user.subscription.status)"
|
|
53
|
+
:label="user.subscription.planKey ?? user.subscription.status"
|
|
54
|
+
/>
|
|
55
|
+
</div>
|
|
56
|
+
<p class="text-muted truncate text-xs">
|
|
57
|
+
{{ user.email }}
|
|
58
|
+
</p>
|
|
59
|
+
</div>
|
|
60
|
+
<time
|
|
61
|
+
class="text-muted shrink-0 text-xs tabular-nums"
|
|
62
|
+
:datetime="user.createdAt"
|
|
63
|
+
>{{ format.shortDate(user.createdAt) }}</time>
|
|
52
64
|
</li>
|
|
53
65
|
</ListCard>
|
|
54
66
|
</template>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<script setup>
|
|
2
2
|
import { usersApi } from "../../utils/api";
|
|
3
|
-
import {
|
|
3
|
+
import { usersTrend } from "../../utils/trend";
|
|
4
|
+
import { computed, useLazyFetch, useLocaleLink, useModuleT, useRuntimeConfig } from "#imports";
|
|
4
5
|
defineOptions({ name: "UsersNewUsersStat" });
|
|
5
6
|
defineProps({
|
|
6
7
|
framed: { type: Boolean, required: false }
|
|
@@ -9,6 +10,20 @@ const t = useModuleT("users");
|
|
|
9
10
|
const { localize } = useLocaleLink();
|
|
10
11
|
const adminPage = useRuntimeConfig().public.users?.pages?.admin;
|
|
11
12
|
const { data: stats, status } = useLazyFetch(usersApi.admin.stats, { key: "admin-users-stats" });
|
|
13
|
+
const trend = computed(() => {
|
|
14
|
+
if (!stats.value || stats.value.previous === void 0) return void 0;
|
|
15
|
+
const { direction, delta, percent } = usersTrend(stats.value.newUsers, stats.value.previous);
|
|
16
|
+
return {
|
|
17
|
+
trend: direction === "flat" ? "neutral" : direction,
|
|
18
|
+
delta: percent === null ? `${delta > 0 ? "+" : ""}${delta}` : `${percent > 0 ? "+" : ""}${percent}%`
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
const hint = computed(() => {
|
|
22
|
+
if (!stats.value) return void 0;
|
|
23
|
+
const total = t("admin.stats.inTotal", "{total} in total", { total: stats.value.total });
|
|
24
|
+
if (stats.value.previous === void 0) return total;
|
|
25
|
+
return `${t("admin.stats.versus", "vs. {previous} before", { previous: stats.value.previous })} \xB7 ${total}`;
|
|
26
|
+
});
|
|
12
27
|
</script>
|
|
13
28
|
|
|
14
29
|
<template>
|
|
@@ -16,8 +31,11 @@ const { data: stats, status } = useLazyFetch(usersApi.admin.stats, { key: "admin
|
|
|
16
31
|
:label="framed ? void 0 : t('admin.stats.newUsers', 'New users')"
|
|
17
32
|
icon="i-lucide-user-plus"
|
|
18
33
|
:value="stats?.newUsers"
|
|
19
|
-
:hint="
|
|
34
|
+
:hint="hint"
|
|
35
|
+
:delta="trend?.delta"
|
|
36
|
+
:trend="trend?.trend"
|
|
20
37
|
:to="adminPage ? localize(adminPage) : void 0"
|
|
21
38
|
:loading="status === 'pending' && !stats"
|
|
39
|
+
:empty="t('admin.newUsers.error', 'Could not load the new users.')"
|
|
22
40
|
/>
|
|
23
41
|
</template>
|
|
@@ -38,9 +38,19 @@
|
|
|
38
38
|
"unverified": "Ikke verificeret",
|
|
39
39
|
"role": "Præcis rolle",
|
|
40
40
|
"clear": "Ryd filtre",
|
|
41
|
-
"allRoles": "Alle roller"
|
|
41
|
+
"allRoles": "Alle roller",
|
|
42
|
+
"members": "Medlemmer",
|
|
43
|
+
"membershipStatus": "Medlemsstatus",
|
|
44
|
+
"membershipPlan": "Medlemsplan",
|
|
45
|
+
"anyMembership": "Alle medlemsstatusser",
|
|
46
|
+
"activeMember": "Aktivt medlemskab",
|
|
47
|
+
"expiredMember": "Udløbet medlemskab",
|
|
48
|
+
"noMembership": "Intet medlemskab",
|
|
49
|
+
"allPlans": "Alle planer"
|
|
42
50
|
},
|
|
43
51
|
"subscription": "Medlemskab",
|
|
52
|
+
"exportMembers": "Eksportér medlemmer",
|
|
53
|
+
"exportMembersHint": "Download alle medlemskaber, der passer til filtrene, som CSV",
|
|
44
54
|
"detail": {
|
|
45
55
|
"profile": "Profil",
|
|
46
56
|
"account": "Konto",
|
|
@@ -64,7 +74,8 @@
|
|
|
64
74
|
},
|
|
65
75
|
"stats": {
|
|
66
76
|
"newUsers": "Nye brugere",
|
|
67
|
-
"
|
|
77
|
+
"inTotal": "{total} i alt",
|
|
78
|
+
"versus": "mod {previous} før"
|
|
68
79
|
},
|
|
69
80
|
"createHint": "De kan logge ind med det link, vi sender dem på e-mail.",
|
|
70
81
|
"createSubmit": "Tilføj bruger",
|
|
@@ -38,9 +38,19 @@
|
|
|
38
38
|
"unverified": "Unverified",
|
|
39
39
|
"role": "Exact role",
|
|
40
40
|
"clear": "Clear filters",
|
|
41
|
-
"allRoles": "All roles"
|
|
41
|
+
"allRoles": "All roles",
|
|
42
|
+
"members": "Members",
|
|
43
|
+
"membershipStatus": "Membership status",
|
|
44
|
+
"membershipPlan": "Membership plan",
|
|
45
|
+
"anyMembership": "Any membership status",
|
|
46
|
+
"activeMember": "Active membership",
|
|
47
|
+
"expiredMember": "Expired membership",
|
|
48
|
+
"noMembership": "No membership",
|
|
49
|
+
"allPlans": "All plans"
|
|
42
50
|
},
|
|
43
51
|
"subscription": "Membership",
|
|
52
|
+
"exportMembers": "Export members",
|
|
53
|
+
"exportMembersHint": "Download all memberships matching the filters as CSV",
|
|
44
54
|
"detail": {
|
|
45
55
|
"profile": "Profile",
|
|
46
56
|
"account": "Account",
|
|
@@ -64,7 +74,8 @@
|
|
|
64
74
|
},
|
|
65
75
|
"stats": {
|
|
66
76
|
"newUsers": "New users",
|
|
67
|
-
"
|
|
77
|
+
"inTotal": "{total} in total",
|
|
78
|
+
"versus": "vs. {previous} before"
|
|
68
79
|
},
|
|
69
80
|
"createHint": "They can sign in with the link we email them.",
|
|
70
81
|
"createSubmit": "Add user",
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { db, schema } from "@nuxthub/db";
|
|
2
|
-
import { count, gte } from "drizzle-orm";
|
|
2
|
+
import { and, count, gte, lt } from "drizzle-orm";
|
|
3
3
|
const DAYS = 30;
|
|
4
|
+
const DAY_MS = 24 * 60 * 60 * 1e3;
|
|
4
5
|
export default defineEventHandler(async (event) => {
|
|
5
6
|
await authorizeUsersAdmin(event, "list");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
7
|
+
const now = Date.now();
|
|
8
|
+
const since = new Date(now - DAYS * DAY_MS);
|
|
9
|
+
const before = new Date(now - 2 * DAYS * DAY_MS);
|
|
10
|
+
const [[total], [recent], [previous]] = await Promise.all([
|
|
8
11
|
db.select({ value: count() }).from(schema.users),
|
|
9
|
-
db.select({ value: count() }).from(schema.users).where(gte(schema.users.createdAt, since))
|
|
12
|
+
db.select({ value: count() }).from(schema.users).where(gte(schema.users.createdAt, since)),
|
|
13
|
+
db.select({ value: count() }).from(schema.users).where(and(gte(schema.users.createdAt, before), lt(schema.users.createdAt, since)))
|
|
10
14
|
]);
|
|
11
|
-
return { total: total?.value ?? 0, newUsers: recent?.value ?? 0, days: DAYS };
|
|
15
|
+
return { total: total?.value ?? 0, newUsers: recent?.value ?? 0, previous: previous?.value ?? 0, days: DAYS };
|
|
12
16
|
});
|
|
@@ -1,45 +1,11 @@
|
|
|
1
1
|
import { db, schema } from "@nuxthub/db";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { USER_MEDIA_OPT_OUTS, normalizeUserPeopleNumber } from "../../../utils/profile.js";
|
|
6
|
-
const querySchema = pageQuery({
|
|
7
|
-
sort: ["id", "name", "email", "role", "createdAt", "lastLoginAt"],
|
|
8
|
-
defaultSort: "createdAt",
|
|
9
|
-
filters: {
|
|
10
|
-
role: z.string().trim().min(1).max(60).optional(),
|
|
11
|
-
verified: z.enum(["true", "false"]).optional(),
|
|
12
|
-
// Users who opted out of photos (`photo`) or of tagging (`tag`).
|
|
13
|
-
mediaOptOut: z.enum(USER_MEDIA_OPT_OUTS).optional()
|
|
14
|
-
}
|
|
15
|
-
});
|
|
2
|
+
import { asc, count, inArray } from "drizzle-orm";
|
|
3
|
+
import { pageRange, paginate } from "@frigear-nu/module-kit/server";
|
|
4
|
+
import { usersAdminListQuery, usersAdminListQuerySchema } from "../../utils/admin-list.js";
|
|
16
5
|
export default defineEventHandler(async (event) => {
|
|
17
6
|
await authorizeUsersAdmin(event, "list");
|
|
18
|
-
const query = await getValidatedQuery(event,
|
|
19
|
-
const userRoles =
|
|
20
|
-
const conditions = [];
|
|
21
|
-
if (query.q) {
|
|
22
|
-
const peopleNumber = normalizeUserPeopleNumber(query.q);
|
|
23
|
-
const byPeopleNumber = peopleNumber ? eq(schema.users.peopleNumber, peopleNumber) : void 0;
|
|
24
|
-
conditions.push(or(searchContains(schema.users.searchText, query.q), likeContains(schema.users.email, query.q), byPeopleNumber));
|
|
25
|
-
}
|
|
26
|
-
if (query.mediaOptOut) conditions.push(eq(query.mediaOptOut === "photo" ? schema.users.photoOptOut : schema.users.tagOptOut, true));
|
|
27
|
-
if (query.role && userRoles) {
|
|
28
|
-
conditions.push(sql`exists (select 1 from ${userRoles} where ${userRoles.userId} = ${schema.users.id} and ${userRoles.role} = ${query.role})`);
|
|
29
|
-
}
|
|
30
|
-
if (query.verified) {
|
|
31
|
-
conditions.push(query.verified === "true" ? isNotNull(schema.users.emailVerifiedAt) : isNull(schema.users.emailVerifiedAt));
|
|
32
|
-
}
|
|
33
|
-
const where = conditions.length ? and(...conditions) : void 0;
|
|
34
|
-
const firstRole = userRoles ? sql`(select min(${userRoles.role}) from ${userRoles} where ${userRoles.userId} = ${schema.users.id})` : void 0;
|
|
35
|
-
const orderBy = orderByQuery({
|
|
36
|
-
id: schema.users.id,
|
|
37
|
-
name: schema.users.name,
|
|
38
|
-
email: schema.users.email,
|
|
39
|
-
role: firstRole,
|
|
40
|
-
createdAt: schema.users.createdAt,
|
|
41
|
-
lastLoginAt: schema.users.lastLoginAt
|
|
42
|
-
}, query, { tiebreaker: schema.users.id });
|
|
7
|
+
const query = await getValidatedQuery(event, usersAdminListQuerySchema.parse);
|
|
8
|
+
const { where, orderBy, userRoles, membership } = await usersAdminListQuery(event, query);
|
|
43
9
|
const { limit, offset } = pageRange(query);
|
|
44
10
|
const [rows, [totals]] = await Promise.all([
|
|
45
11
|
db.select().from(schema.users).where(where).orderBy(...orderBy).limit(limit).offset(offset),
|
|
@@ -63,6 +29,8 @@ export default defineEventHandler(async (event) => {
|
|
|
63
29
|
}),
|
|
64
30
|
hasRoles: !!userRoles,
|
|
65
31
|
hasSubscriptions: !!schema.subscriptions,
|
|
32
|
+
membershipPlans: membership.plans,
|
|
33
|
+
membershipExportUrl: membership.exportUrl,
|
|
66
34
|
roles: userRoles ? usersStoredRoleOptions() : []
|
|
67
35
|
};
|
|
68
36
|
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { H3Event } from 'h3';
|
|
2
|
+
import type { SQL } from 'drizzle-orm';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import type { UsersMembershipFilterContext } from '../../../types.js';
|
|
5
|
+
export declare const usersAdminListQuerySchema: z.ZodObject<{
|
|
6
|
+
page: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
7
|
+
pageSize: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
8
|
+
q: z.ZodOptional<z.ZodString>;
|
|
9
|
+
sort: z.ZodDefault<z.ZodEnum<{
|
|
10
|
+
name: "name";
|
|
11
|
+
role: "role";
|
|
12
|
+
email: "email";
|
|
13
|
+
id: "id";
|
|
14
|
+
createdAt: "createdAt";
|
|
15
|
+
lastLoginAt: "lastLoginAt";
|
|
16
|
+
}>>;
|
|
17
|
+
order: z.ZodDefault<z.ZodEnum<{
|
|
18
|
+
asc: "asc";
|
|
19
|
+
desc: "desc";
|
|
20
|
+
}>>;
|
|
21
|
+
role: z.ZodOptional<z.ZodString>;
|
|
22
|
+
verified: z.ZodOptional<z.ZodEnum<{
|
|
23
|
+
true: "true";
|
|
24
|
+
false: "false";
|
|
25
|
+
}>>;
|
|
26
|
+
membership: z.ZodOptional<z.ZodEnum<{
|
|
27
|
+
none: "none";
|
|
28
|
+
active: "active";
|
|
29
|
+
expired: "expired";
|
|
30
|
+
}>>;
|
|
31
|
+
membershipPlan: z.ZodOptional<z.ZodString>;
|
|
32
|
+
mediaOptOut: z.ZodOptional<z.ZodEnum<{
|
|
33
|
+
photo: "photo";
|
|
34
|
+
tag: "tag";
|
|
35
|
+
}>>;
|
|
36
|
+
}, z.core.$strip>;
|
|
37
|
+
export declare function usersAdminListQuery(event: H3Event, query: z.output<typeof usersAdminListQuerySchema>): Promise<{
|
|
38
|
+
where: SQL<unknown> | undefined;
|
|
39
|
+
orderBy: SQL<unknown>[];
|
|
40
|
+
userRoles: {
|
|
41
|
+
userId: import("drizzle-orm").AnyColumn;
|
|
42
|
+
role: import("drizzle-orm").AnyColumn;
|
|
43
|
+
} | undefined;
|
|
44
|
+
membership: UsersMembershipFilterContext;
|
|
45
|
+
}>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { schema } from "@nuxthub/db";
|
|
2
|
+
import { and, eq, isNotNull, isNull, or, sql } from "drizzle-orm";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { likeContains, orderByQuery, pageQuery, searchContains } from "@frigear-nu/module-kit/server";
|
|
5
|
+
import { USER_MEDIA_OPT_OUTS, normalizeUserPeopleNumber } from "../../utils/profile.js";
|
|
6
|
+
import { usersRoleAssignments } from "./roles.js";
|
|
7
|
+
export const usersAdminListQuerySchema = pageQuery({
|
|
8
|
+
sort: ["id", "name", "email", "role", "createdAt", "lastLoginAt"],
|
|
9
|
+
defaultSort: "createdAt",
|
|
10
|
+
filters: {
|
|
11
|
+
role: z.string().trim().min(1).max(60).optional(),
|
|
12
|
+
verified: z.enum(["true", "false"]).optional(),
|
|
13
|
+
membership: z.enum(["active", "expired", "none"]).optional(),
|
|
14
|
+
membershipPlan: z.string().trim().min(1).max(200).optional(),
|
|
15
|
+
// Users who opted out of photos (`photo`) or of tagging (`tag`).
|
|
16
|
+
mediaOptOut: z.enum(USER_MEDIA_OPT_OUTS).optional()
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
export async function usersAdminListQuery(event, query) {
|
|
20
|
+
const userRoles = usersRoleAssignments();
|
|
21
|
+
const conditions = [];
|
|
22
|
+
if (query.q) {
|
|
23
|
+
const peopleNumber = normalizeUserPeopleNumber(query.q);
|
|
24
|
+
const byPeopleNumber = peopleNumber ? eq(schema.users.peopleNumber, peopleNumber) : void 0;
|
|
25
|
+
conditions.push(or(searchContains(schema.users.searchText, query.q), likeContains(schema.users.email, query.q), byPeopleNumber));
|
|
26
|
+
}
|
|
27
|
+
if (query.mediaOptOut) conditions.push(eq(query.mediaOptOut === "photo" ? schema.users.photoOptOut : schema.users.tagOptOut, true));
|
|
28
|
+
if (query.role && userRoles) {
|
|
29
|
+
conditions.push(sql`exists (select 1 from ${userRoles} where ${userRoles.userId} = ${schema.users.id} and ${userRoles.role} = ${query.role})`);
|
|
30
|
+
}
|
|
31
|
+
if (query.verified) {
|
|
32
|
+
conditions.push(query.verified === "true" ? isNotNull(schema.users.emailVerifiedAt) : isNull(schema.users.emailVerifiedAt));
|
|
33
|
+
}
|
|
34
|
+
const membership = {
|
|
35
|
+
event,
|
|
36
|
+
now: /* @__PURE__ */ new Date(),
|
|
37
|
+
status: query.membership,
|
|
38
|
+
plan: query.membershipPlan,
|
|
39
|
+
plans: []
|
|
40
|
+
};
|
|
41
|
+
await useNitroApp().hooks.callHook("users:admin:membership-filter", membership);
|
|
42
|
+
if (membership.status || membership.plan) conditions.push(membership.condition ?? sql`1 = 0`);
|
|
43
|
+
const where = conditions.length ? and(...conditions) : void 0;
|
|
44
|
+
const firstRole = userRoles ? sql`(select min(${userRoles.role}) from ${userRoles} where ${userRoles.userId} = ${schema.users.id})` : void 0;
|
|
45
|
+
const orderBy = orderByQuery({
|
|
46
|
+
id: schema.users.id,
|
|
47
|
+
name: schema.users.name,
|
|
48
|
+
email: schema.users.email,
|
|
49
|
+
role: firstRole,
|
|
50
|
+
createdAt: schema.users.createdAt,
|
|
51
|
+
lastLoginAt: schema.users.lastLoginAt
|
|
52
|
+
}, query, { tiebreaker: schema.users.id });
|
|
53
|
+
return { where, orderBy, userRoles, membership };
|
|
54
|
+
}
|
|
@@ -18,6 +18,8 @@ export declare const USERS_PERMISSIONS: {
|
|
|
18
18
|
};
|
|
19
19
|
/** The permission to read one user (`GET /api/admin/users/:id` and the routes other modules add below it). */
|
|
20
20
|
export declare const USERS_READ_PERMISSION: "users:read";
|
|
21
|
+
/** Reading contact details for an existing user requires this permission. */
|
|
22
|
+
export declare const USERS_PII_PERMISSION: "users:pii";
|
|
21
23
|
/** The permission for the actions that touch someone's sign-in (email, sign-in links). Sessions only. */
|
|
22
24
|
export declare const USERS_MANAGE_PERMISSION: "users:manage";
|
|
23
25
|
/** A stored user, as `loadUsersAdminTarget` returns it. */
|
|
@@ -11,6 +11,7 @@ export const USERS_PERMISSIONS = {
|
|
|
11
11
|
manage: usersAccess.permissions.manage
|
|
12
12
|
};
|
|
13
13
|
export const USERS_READ_PERMISSION = USERS_PERMISSIONS.view;
|
|
14
|
+
export const USERS_PII_PERMISSION = usersAccess.permissions.pii;
|
|
14
15
|
export const USERS_MANAGE_PERMISSION = USERS_PERMISSIONS.manage;
|
|
15
16
|
export async function authorizeUsersAdmin(event, action) {
|
|
16
17
|
return (await authorize(event, USERS_PERMISSIONS[action])).user;
|
|
@@ -52,7 +52,7 @@ export declare const usersApi: {
|
|
|
52
52
|
readonly avatar: (id: number | string) => string;
|
|
53
53
|
/** `GET`: the newest sign-ups of the last 30 days (dashboard). */
|
|
54
54
|
readonly new: "/api/admin/users/new";
|
|
55
|
-
/** `GET`: `{ total, newUsers, days }` (dashboard). */
|
|
55
|
+
/** `GET`: `UsersAdminStatsDto`, `{ total, newUsers, previous, days }` (dashboard; `previous` is the period before). */
|
|
56
56
|
readonly stats: "/api/admin/users/stats";
|
|
57
57
|
};
|
|
58
58
|
};
|
|
@@ -50,7 +50,7 @@ export const usersApi = {
|
|
|
50
50
|
avatar: (id) => `${adminBase}/${encodeURIComponent(String(id))}/avatar`,
|
|
51
51
|
/** `GET`: the newest sign-ups of the last 30 days (dashboard). */
|
|
52
52
|
new: `${adminBase}/new`,
|
|
53
|
-
/** `GET`: `{ total, newUsers, days }` (dashboard). */
|
|
53
|
+
/** `GET`: `UsersAdminStatsDto`, `{ total, newUsers, previous, days }` (dashboard; `previous` is the period before). */
|
|
54
54
|
stats: `${adminBase}/stats`
|
|
55
55
|
}
|
|
56
56
|
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** How a count moved against the period before: the direction, the change and, when there is a base, the percentage. */
|
|
2
|
+
export type UsersTrend = {
|
|
3
|
+
direction: 'up' | 'down' | 'flat';
|
|
4
|
+
delta: number;
|
|
5
|
+
/** Rounded percentage change; `null` when the period before had none (no base to compare with). */
|
|
6
|
+
percent: number | null;
|
|
7
|
+
};
|
|
8
|
+
/** The trend of `current` against `previous`, for the sign-ups stat of the admin dashboard. */
|
|
9
|
+
export declare function usersTrend(current: number, previous: number): UsersTrend;
|
package/dist/types.d.mts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { default } from './module.mjs'
|
|
2
2
|
|
|
3
|
-
export { type AdminUserDto, type ModuleOptions, type ProfileDto, type PublicModuleOptions, type UserDeletedContext, type UserTermsAcceptanceDto, type UserTermsStatusDto, type UsersPublicTerms, type UsersTermsAcceptedContext, type UsersTermsSource } from './module.mjs'
|
|
3
|
+
export { type AdminUserDto, type ModuleOptions, type ProfileDto, type PublicModuleOptions, type UserDeletedContext, type UserTermsAcceptanceDto, type UserTermsStatusDto, type UsersMembershipFilterContext, type UsersPublicTerms, type UsersTermsAcceptedContext, type UsersTermsSource } from './module.mjs'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frigear-nu/users",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "The user entity for the main frigear app: the users table, admin user management and self-service profile, email and password changes. Sign-in lives in @frigear-nu/auth.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"frigear-module",
|
|
@@ -38,11 +38,11 @@
|
|
|
38
38
|
"playground"
|
|
39
39
|
],
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@frigear-nu/auth": "^0.
|
|
41
|
+
"@frigear-nu/auth": "^0.7.0",
|
|
42
42
|
"@frigear-nu/mcp": "^0.5.3",
|
|
43
43
|
"@frigear-nu/module-kit": "^1.3.13",
|
|
44
|
-
"@frigear-nu/notify": "^0.
|
|
45
|
-
"@frigear-nu/ui": "^0.
|
|
44
|
+
"@frigear-nu/notify": "^0.4.0",
|
|
45
|
+
"@frigear-nu/ui": "^0.3.0",
|
|
46
46
|
"@libsql/client": "^0.18.0",
|
|
47
47
|
"@nuxt/kit": "^4.6.4",
|
|
48
48
|
"drizzle-kit": "^0.31.11",
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
80
|
"@electric-sql/pglite": "^0.5.16",
|
|
81
|
-
"@frigear-nu/admin": "^0.
|
|
81
|
+
"@frigear-nu/admin": "^0.6.0",
|
|
82
82
|
"@frigear-nu/email-templates": "^0.1.6",
|
|
83
83
|
"@frigear-nu/menu": "^0.4.2",
|
|
84
84
|
"@frigear-nu/widget": "^0.4.4",
|