@shivaedev/admin 0.6.0 → 0.8.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/README.md +23 -12
- package/dist/client/UsersTable.d.ts +5 -4
- package/dist/client/UsersTable.d.ts.map +1 -1
- package/dist/client/UsersTable.js +13 -14
- package/dist/client/UsersTable.js.map +1 -1
- package/dist/client/UsersView.d.ts +4 -3
- package/dist/client/UsersView.d.ts.map +1 -1
- package/dist/client/UsersView.js +5 -5
- package/dist/client/UsersView.js.map +1 -1
- package/dist/jobs/entry.d.ts +12 -0
- package/dist/jobs/entry.d.ts.map +1 -0
- package/dist/jobs/entry.js +5 -0
- package/dist/jobs/entry.js.map +1 -0
- package/dist/jobs/keys.d.ts +5 -5
- package/dist/jobs/keys.js +1 -1
- package/dist/jobs/keys.js.map +1 -1
- package/dist/jobs/page.d.ts +36 -29
- package/dist/jobs/page.d.ts.map +1 -1
- package/dist/jobs/page.js +19 -4
- package/dist/jobs/page.js.map +1 -1
- package/dist/jobs/retryPage.d.ts +51 -44
- package/dist/jobs/retryPage.d.ts.map +1 -1
- package/dist/jobs/retryPage.js +2 -0
- package/dist/jobs/retryPage.js.map +1 -1
- package/dist/jobs/schemas.d.ts +2 -2
- package/dist/jobs/schemas.d.ts.map +1 -1
- package/dist/jobs/schemas.js.map +1 -1
- package/dist/page.d.ts +9 -5
- package/dist/page.d.ts.map +1 -1
- package/dist/page.js +6 -2
- package/dist/page.js.map +1 -1
- package/dist/permissions.d.ts +26 -5
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +6 -2
- package/dist/permissions.js.map +1 -1
- package/dist/server/jobsHandlers.d.ts +8 -7
- package/dist/server/jobsHandlers.d.ts.map +1 -1
- package/dist/server/jobsHandlers.js +2 -1
- package/dist/server/jobsHandlers.js.map +1 -1
- package/dist/server/jobsHandlersWithRetry.d.ts +2 -2
- package/dist/server/jobsHandlersWithRetry.d.ts.map +1 -1
- package/dist/server/jobsHandlersWithRetry.js +5 -2
- package/dist/server/jobsHandlersWithRetry.js.map +1 -1
- package/dist/server/users.d.ts +32 -10
- package/dist/server/users.d.ts.map +1 -1
- package/dist/server/users.js +17 -15
- package/dist/server/users.js.map +1 -1
- package/dist/server/usersHandlers.d.ts +3 -1
- package/dist/server/usersHandlers.d.ts.map +1 -1
- package/dist/server/usersHandlers.js +10 -9
- package/dist/server/usersHandlers.js.map +1 -1
- package/dist/users/page.d.ts +22 -15
- package/dist/users/page.d.ts.map +1 -1
- package/dist/users/page.js +31 -24
- package/dist/users/page.js.map +1 -1
- package/dist/users/roles.d.ts +8 -19
- package/dist/users/roles.d.ts.map +1 -1
- package/dist/users/roles.js +2 -6
- package/dist/users/roles.js.map +1 -1
- package/package.json +12 -11
- package/src/client/UsersTable.tsx +48 -51
- package/src/client/UsersView.ts +8 -7
- package/src/jobs/entry.ts +15 -0
- package/src/jobs/keys.ts +1 -1
- package/src/jobs/page.ts +21 -6
- package/src/jobs/retryPage.ts +5 -2
- package/src/jobs/schemas.ts +2 -2
- package/src/page.ts +13 -5
- package/src/permissions.ts +5 -2
- package/src/server/jobsHandlers.ts +8 -7
- package/src/server/jobsHandlersWithRetry.ts +8 -5
- package/src/server/users.ts +24 -15
- package/src/server/usersHandlers.ts +16 -10
- package/src/users/page.ts +32 -24
- package/src/users/roles.ts +6 -9
|
@@ -1,71 +1,58 @@
|
|
|
1
1
|
import { DateTime } from "effect";
|
|
2
2
|
import type { UsersList } from "#users/page.ts";
|
|
3
|
-
import {
|
|
3
|
+
import type { AppUser, ChangeRoleInput } from "#users/roles.ts";
|
|
4
4
|
|
|
5
|
-
interface RoleControls {
|
|
6
|
-
readonly dispatch: (input:
|
|
5
|
+
interface RoleControls<TRole extends string> {
|
|
6
|
+
readonly dispatch: (input: ChangeRoleInput<TRole>) => void;
|
|
7
7
|
readonly pending: boolean;
|
|
8
|
+
readonly roles: readonly TRole[];
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
function
|
|
11
|
-
return (
|
|
12
|
-
<tr>
|
|
13
|
-
{["Name", "Email", "Roles", "Signed up"].map((label) => (
|
|
14
|
-
<th className="whitespace-nowrap p-3" key={label} scope="col">
|
|
15
|
-
{label}
|
|
16
|
-
</th>
|
|
17
|
-
))}
|
|
18
|
-
</tr>
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function RoleCheckbox({ user, role, pending, dispatch }: RoleControls & { readonly user: typeof AppUser.Type; readonly role: typeof UserRole.Type }) {
|
|
23
|
-
return (
|
|
24
|
-
<label className="mr-3 inline-flex gap-1">
|
|
25
|
-
<input
|
|
26
|
-
aria-label={`${role} for ${user.email}`}
|
|
27
|
-
checked={user.roles.includes(role)}
|
|
28
|
-
disabled={pending}
|
|
29
|
-
onChange={(event) => dispatch({ grant: event.target.checked, role, userId: user.id })}
|
|
30
|
-
type="checkbox"
|
|
31
|
-
/>
|
|
32
|
-
{role}
|
|
33
|
-
</label>
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function UserRoles({
|
|
11
|
+
function UserRole<TRole extends string>({
|
|
38
12
|
user,
|
|
39
13
|
canChangeRoles,
|
|
40
|
-
|
|
14
|
+
roles,
|
|
41
15
|
pending,
|
|
42
|
-
|
|
16
|
+
dispatch,
|
|
17
|
+
}: RoleControls<TRole> & {
|
|
43
18
|
readonly user: typeof AppUser.Type;
|
|
44
19
|
readonly canChangeRoles: boolean;
|
|
45
20
|
}) {
|
|
46
|
-
return (
|
|
47
|
-
<
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
21
|
+
return canChangeRoles ? (
|
|
22
|
+
<select
|
|
23
|
+
aria-label={`Role for ${user.email}`}
|
|
24
|
+
className="rounded-md border bg-background p-2"
|
|
25
|
+
disabled={pending}
|
|
26
|
+
onChange={(event) => {
|
|
27
|
+
const role = roles.find((candidate) => candidate === event.target.value);
|
|
28
|
+
if (role !== undefined) {
|
|
29
|
+
dispatch({ role, userId: user.id });
|
|
30
|
+
}
|
|
31
|
+
}}
|
|
32
|
+
value={user.role}
|
|
33
|
+
>
|
|
34
|
+
{roles.map((role) => (
|
|
35
|
+
<option key={role} value={role}>
|
|
36
|
+
{role}
|
|
37
|
+
</option>
|
|
38
|
+
))}
|
|
39
|
+
</select>
|
|
40
|
+
) : (
|
|
41
|
+
user.role
|
|
53
42
|
);
|
|
54
43
|
}
|
|
55
44
|
|
|
56
|
-
function UserRow({
|
|
45
|
+
function UserRow<TRole extends string>({
|
|
57
46
|
user,
|
|
47
|
+
canChangeRoles,
|
|
58
48
|
...controls
|
|
59
|
-
}: RoleControls & {
|
|
60
|
-
readonly user: typeof AppUser.Type;
|
|
61
|
-
readonly canChangeRoles: boolean;
|
|
62
|
-
}) {
|
|
49
|
+
}: RoleControls<TRole> & { readonly user: typeof AppUser.Type; readonly canChangeRoles: boolean }) {
|
|
63
50
|
return (
|
|
64
51
|
<tr className="border-b">
|
|
65
52
|
<td className="p-3">{user.name}</td>
|
|
66
53
|
<td className="p-3">{user.email}</td>
|
|
67
54
|
<td className="p-3">
|
|
68
|
-
<
|
|
55
|
+
<UserRole {...controls} canChangeRoles={canChangeRoles} user={user} />
|
|
69
56
|
</td>
|
|
70
57
|
<td className="whitespace-nowrap p-3">
|
|
71
58
|
<time dateTime={DateTime.formatIso(user.createdAt)}>{DateTime.formatIso(user.createdAt)}</time>
|
|
@@ -73,8 +60,7 @@ function UserRow({
|
|
|
73
60
|
</tr>
|
|
74
61
|
);
|
|
75
62
|
}
|
|
76
|
-
|
|
77
|
-
function UserRows({ data, ...controls }: RoleControls & { readonly data: typeof UsersList.Type }) {
|
|
63
|
+
function UserRows<TRole extends string>({ data, ...controls }: RoleControls<TRole> & { readonly data: typeof UsersList.Type }) {
|
|
78
64
|
return (
|
|
79
65
|
<div className="overflow-x-auto rounded-md border">
|
|
80
66
|
<table className="w-full text-left text-sm">
|
|
@@ -91,12 +77,23 @@ function UserRows({ data, ...controls }: RoleControls & { readonly data: typeof
|
|
|
91
77
|
</div>
|
|
92
78
|
);
|
|
93
79
|
}
|
|
94
|
-
|
|
95
|
-
export function UsersTable({ data, ...controls }: RoleControls & { readonly data: typeof UsersList.Type }) {
|
|
80
|
+
export function UsersTable<TRole extends string>({ data, ...controls }: RoleControls<TRole> & { readonly data: typeof UsersList.Type }) {
|
|
96
81
|
return (
|
|
97
82
|
<div className="space-y-4">
|
|
98
|
-
{data.canChangeRoles ? null : <p>
|
|
83
|
+
{data.canChangeRoles ? null : <p>Permission to assign roles is required to change roles.</p>}
|
|
99
84
|
{data.users.length === 0 ? <p>No users.</p> : <UserRows {...controls} data={data} />}
|
|
100
85
|
</div>
|
|
101
86
|
);
|
|
102
87
|
}
|
|
88
|
+
|
|
89
|
+
function UserHeaders() {
|
|
90
|
+
return (
|
|
91
|
+
<tr>
|
|
92
|
+
{["Name", "Email", "Role", "Signed up"].map((label) => (
|
|
93
|
+
<th className="whitespace-nowrap p-3" key={label} scope="col">
|
|
94
|
+
{label}
|
|
95
|
+
</th>
|
|
96
|
+
))}
|
|
97
|
+
</tr>
|
|
98
|
+
);
|
|
99
|
+
}
|
package/src/client/UsersView.ts
CHANGED
|
@@ -7,23 +7,24 @@ import type { UsersList } from "#users/page.ts";
|
|
|
7
7
|
import type { ChangeRoleInput, UserListInput } from "#users/roles.ts";
|
|
8
8
|
import { UsersTable } from "./UsersTable.tsx";
|
|
9
9
|
|
|
10
|
-
export interface UsersViewProps {
|
|
11
|
-
readonly changeRole: Atom.AtomResultFn<
|
|
10
|
+
export interface UsersViewProps<TRole extends string> {
|
|
11
|
+
readonly changeRole: Atom.AtomResultFn<ChangeRoleInput<TRole>, void, unknown>;
|
|
12
12
|
readonly list: (input: typeof UserListInput.Type) => Atom.Atom<AsyncResult.AsyncResult<typeof UsersList.Type, unknown>>;
|
|
13
|
+
readonly roles: readonly TRole[];
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
function failure(cause: Cause.Cause<unknown>): string {
|
|
16
17
|
const error = Option.getOrUndefined(Cause.findErrorOption(cause));
|
|
17
|
-
if (Predicate.isTagged(error, "
|
|
18
|
-
return "The last
|
|
18
|
+
if (Predicate.isTagged(error, "LastRoleAssigner")) {
|
|
19
|
+
return "The last user who can assign roles cannot be removed. Assign another user permission to change roles first.";
|
|
19
20
|
}
|
|
20
21
|
if (Predicate.isTagged(error, "Forbidden")) {
|
|
21
|
-
return "
|
|
22
|
+
return "Permission required to assign roles.";
|
|
22
23
|
}
|
|
23
24
|
return "Could not change the role. Try again.";
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
export function UsersView({ list, changeRole }: UsersViewProps) {
|
|
27
|
+
export function UsersView<TRole extends string>({ list, changeRole, roles }: UsersViewProps<TRole>) {
|
|
27
28
|
const [cursors, setCursors] = useState<readonly string[]>([]);
|
|
28
29
|
const cursor = cursors.at(-1);
|
|
29
30
|
const state = useQuery(list({ ...(cursor === undefined ? {} : { cursor }), limit: 20 }));
|
|
@@ -40,7 +41,7 @@ export function UsersView({ list, changeRole }: UsersViewProps) {
|
|
|
40
41
|
state.pending ? createElement("p", { role: "status" }, "Loading users…") : null,
|
|
41
42
|
Option.isSome(state.cause) ? createElement("p", { role: "alert" }, "Could not load users. Try refreshing.") : null,
|
|
42
43
|
Option.isSome(action.cause) ? createElement("p", { role: "alert" }, failure(action.cause.value)) : null,
|
|
43
|
-
data === undefined ? null : createElement(UsersTable
|
|
44
|
+
data === undefined ? null : createElement(UsersTable<TRole>, { data, dispatch: action.dispatch, pending: action.pending, roles }),
|
|
44
45
|
data === undefined
|
|
45
46
|
? null
|
|
46
47
|
: createElement(
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import type { JobsQueue } from "./schemas.ts";
|
|
3
|
+
|
|
4
|
+
export type JobsEntry = JobsQueue | { readonly _tag: "ScheduleDefinition"; readonly name: string };
|
|
5
|
+
|
|
6
|
+
export type JobsPayload<TEntry extends JobsEntry> = [TEntry] extends [never]
|
|
7
|
+
? typeof Schema.Never
|
|
8
|
+
: TEntry extends JobsQueue
|
|
9
|
+
? TEntry["schema"]
|
|
10
|
+
: typeof Schema.Null;
|
|
11
|
+
|
|
12
|
+
export function jobsQueue<TEntry extends JobsEntry>(entry: TEntry): { readonly name: string; readonly schema: JobsPayload<TEntry> };
|
|
13
|
+
export function jobsQueue(entry: JobsEntry) {
|
|
14
|
+
return { name: entry.name, schema: "schema" in entry ? entry.schema : Schema.Null };
|
|
15
|
+
}
|
package/src/jobs/keys.ts
CHANGED
package/src/jobs/page.ts
CHANGED
|
@@ -1,37 +1,51 @@
|
|
|
1
|
-
import { Schema } from "effect";
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
2
|
import { contract } from "@shivaedev/contract/contract.ts";
|
|
3
3
|
import { query } from "@shivaedev/contract/operation.ts";
|
|
4
|
+
import { SessionUser } from "@shivaedev/identity/identity.ts";
|
|
5
|
+
import { hasPermission } from "@shivaedev/identity/permission.ts";
|
|
4
6
|
import { adminPage } from "#page.ts";
|
|
7
|
+
import { JobsPermission } from "#permissions.ts";
|
|
8
|
+
import { type JobsEntry, type JobsPayload, jobsQueue } from "./entry.ts";
|
|
5
9
|
import { jobsKeys } from "./keys.ts";
|
|
6
|
-
import { deadLetteredSchema, JobsFailures, JobsHealth,
|
|
10
|
+
import { deadLetteredSchema, JobsFailures, JobsHealth, jobPage, jobSchema, PageFields, QueueCounts } from "./schemas.ts";
|
|
7
11
|
|
|
8
|
-
export function jobsPage<const TQueues extends readonly
|
|
9
|
-
const
|
|
10
|
-
const
|
|
12
|
+
export function jobsPage<const TQueues extends readonly JobsEntry[]>(...queues: TQueues) {
|
|
13
|
+
const definitions = queues.map((queue: TQueues[number]) => jobsQueue(queue));
|
|
14
|
+
const Job = Schema.Union(definitions.map((queue) => jobSchema<JobsPayload<TQueues[number]>>(queue)));
|
|
15
|
+
const DeadLettered = Schema.Union(definitions.map((queue) => deadLetteredSchema<JobsPayload<TQueues[number]>>(queue)));
|
|
11
16
|
const page = adminPage({
|
|
12
17
|
contract: contract("admin.jobs", {
|
|
13
18
|
queries: [
|
|
14
|
-
query("health", {
|
|
19
|
+
query("health", {
|
|
20
|
+
readPolicy: () => Effect.map(SessionUser, (user) => hasPermission(user, JobsPermission.permission)),
|
|
21
|
+
reads: () => [jobsKeys.health.list],
|
|
22
|
+
rejections: JobsFailures,
|
|
23
|
+
success: JobsHealth,
|
|
24
|
+
}),
|
|
15
25
|
query("counts", {
|
|
16
26
|
payload: { queue: Schema.String },
|
|
27
|
+
readPolicy: () => Effect.map(SessionUser, (user) => hasPermission(user, JobsPermission.permission)),
|
|
17
28
|
reads: ({ queue }) => [jobsKeys.counts.item(queue)],
|
|
18
29
|
rejections: JobsFailures,
|
|
19
30
|
success: QueueCounts,
|
|
20
31
|
}),
|
|
21
32
|
query("failed", {
|
|
22
33
|
payload: { ...PageFields, queue: Schema.String },
|
|
34
|
+
readPolicy: () => Effect.map(SessionUser, (user) => hasPermission(user, JobsPermission.permission)),
|
|
23
35
|
reads: ({ queue }) => [jobsKeys.failed.item(queue)],
|
|
24
36
|
rejections: JobsFailures,
|
|
25
37
|
success: jobPage(Job),
|
|
26
38
|
}),
|
|
27
39
|
query("deadLettered", {
|
|
28
40
|
payload: { ...PageFields, queue: Schema.String },
|
|
41
|
+
readPolicy: () => Effect.map(SessionUser, (user) => hasPermission(user, JobsPermission.permission)),
|
|
29
42
|
reads: ({ queue }) => [jobsKeys.deadLettered.item(queue)],
|
|
30
43
|
rejections: JobsFailures,
|
|
31
44
|
success: jobPage(DeadLettered),
|
|
32
45
|
}),
|
|
33
46
|
query("job", {
|
|
34
47
|
payload: { id: Schema.String, queue: Schema.String },
|
|
48
|
+
readPolicy: () => Effect.map(SessionUser, (user) => hasPermission(user, JobsPermission.permission)),
|
|
35
49
|
reads: ({ queue, id }) => [jobsKeys.job(queue).item(id)],
|
|
36
50
|
rejections: JobsFailures,
|
|
37
51
|
success: Schema.OptionFromNullOr(Job),
|
|
@@ -39,6 +53,7 @@ export function jobsPage<const TQueues extends readonly JobsQueue[]>(...queues:
|
|
|
39
53
|
],
|
|
40
54
|
}),
|
|
41
55
|
id: "jobs",
|
|
56
|
+
permission: JobsPermission,
|
|
42
57
|
route: { path: "/admin/jobs", title: "Jobs" },
|
|
43
58
|
});
|
|
44
59
|
return Object.assign(page, { queues });
|
package/src/jobs/retryPage.ts
CHANGED
|
@@ -2,16 +2,18 @@ import { Schema } from "effect";
|
|
|
2
2
|
import { contract } from "@shivaedev/contract/contract.ts";
|
|
3
3
|
import { command } from "@shivaedev/contract/operation.ts";
|
|
4
4
|
import { adminPage } from "#page.ts";
|
|
5
|
+
import { JobsPermission } from "#permissions.ts";
|
|
6
|
+
import type { JobsEntry } from "./entry.ts";
|
|
5
7
|
import { jobsKeys } from "./keys.ts";
|
|
6
8
|
import { jobsPage } from "./page.ts";
|
|
7
|
-
import { JobsFailures
|
|
9
|
+
import { JobsFailures } from "./schemas.ts";
|
|
8
10
|
|
|
9
11
|
export class JobsNotFailed extends Schema.TaggedError<JobsNotFailed>()("JobsNotFailed", {
|
|
10
12
|
id: Schema.String,
|
|
11
13
|
queue: Schema.String,
|
|
12
14
|
}) {}
|
|
13
15
|
|
|
14
|
-
export function jobsPageWithRetry<const TQueues extends readonly
|
|
16
|
+
export function jobsPageWithRetry<const TQueues extends readonly JobsEntry[]>(...queues: TQueues) {
|
|
15
17
|
const page = jobsPage(...queues);
|
|
16
18
|
return Object.assign(
|
|
17
19
|
adminPage({
|
|
@@ -32,6 +34,7 @@ export function jobsPageWithRetry<const TQueues extends readonly JobsQueue[]>(..
|
|
|
32
34
|
queries: page.contract.declaration.queries,
|
|
33
35
|
}),
|
|
34
36
|
id: page.id,
|
|
37
|
+
permission: JobsPermission,
|
|
35
38
|
route: page.route,
|
|
36
39
|
}),
|
|
37
40
|
{ queues },
|
package/src/jobs/schemas.ts
CHANGED
|
@@ -58,11 +58,11 @@ export const JobFields = {
|
|
|
58
58
|
|
|
59
59
|
export const PageFields = { cursor: Schema.optionalKey(Schema.String), limit: Schema.Number };
|
|
60
60
|
|
|
61
|
-
export function jobSchema<TPayload extends
|
|
61
|
+
export function jobSchema<TPayload extends Schema.Top>(queue: { readonly name: string; readonly schema: TPayload }) {
|
|
62
62
|
return Schema.Struct({ ...JobFields, data: queue.schema, queue: Schema.Literal(queue.name) });
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
export function deadLetteredSchema<TPayload extends
|
|
65
|
+
export function deadLetteredSchema<TPayload extends Schema.Top>(queue: { readonly name: string; readonly schema: TPayload }) {
|
|
66
66
|
const name: string = `${queue.name}-dlq`;
|
|
67
67
|
return Schema.Struct({
|
|
68
68
|
...JobFields,
|
package/src/page.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type { Rpc, RpcGroup } from "effect/rpc";
|
|
1
|
+
import type { Rpc, RpcGroup, RpcMiddleware } from "effect/rpc";
|
|
2
|
+
import type { Permission } from "@shivaedev/access/permission.ts";
|
|
2
3
|
import type { Contract } from "@shivaedev/contract/contract.ts";
|
|
3
4
|
import type { CommandShape, QueryShape, StreamShape } from "@shivaedev/contract/operation.ts";
|
|
4
|
-
import { AdminPermission } from "#permissions.ts";
|
|
5
5
|
|
|
6
6
|
export interface AdminRoute {
|
|
7
7
|
readonly path: string;
|
|
8
|
+
readonly permission: Permission;
|
|
8
9
|
readonly title: string;
|
|
9
10
|
}
|
|
10
11
|
|
|
@@ -27,13 +28,20 @@ export function adminPage<
|
|
|
27
28
|
const TCommands extends readonly CommandShape[],
|
|
28
29
|
TRpcs extends Rpc.Any,
|
|
29
30
|
const TStreams extends readonly StreamShape[],
|
|
31
|
+
TMiddleware extends RpcMiddleware.AnyService & { readonly permission: Permission },
|
|
30
32
|
>(page: {
|
|
31
33
|
readonly id: TId;
|
|
32
|
-
readonly route: AdminRoute
|
|
34
|
+
readonly route: Omit<AdminRoute, "permission">;
|
|
35
|
+
readonly permission: TMiddleware;
|
|
33
36
|
readonly contract: Contract<TName, TQueries, TCommands, TRpcs, TStreams>;
|
|
34
37
|
}): AdminPage & {
|
|
35
38
|
readonly id: TId;
|
|
36
|
-
readonly contract: Contract<TName, TQueries, TCommands, Rpc.AddMiddleware<TRpcs,
|
|
39
|
+
readonly contract: Contract<TName, TQueries, TCommands, Rpc.AddMiddleware<TRpcs, TMiddleware>, TStreams>;
|
|
37
40
|
} {
|
|
38
|
-
return {
|
|
41
|
+
return {
|
|
42
|
+
...page,
|
|
43
|
+
route: { ...page.route, permission: page.permission.permission },
|
|
44
|
+
[AdminPageTypeId]: true as const,
|
|
45
|
+
contract: page.contract.middleware(page.permission),
|
|
46
|
+
};
|
|
39
47
|
}
|
package/src/permissions.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { permission } from "@shivaedev/access/permission.ts";
|
|
2
2
|
import { permissionMiddleware } from "@shivaedev/identity/permission.ts";
|
|
3
3
|
|
|
4
|
-
export const
|
|
5
|
-
export
|
|
4
|
+
export const ReadUsers = permission("users.read");
|
|
5
|
+
export const AssignRoles = permission("roles.assign");
|
|
6
|
+
export const ReadJobs = permission("jobs.read");
|
|
7
|
+
export class UsersPermission extends permissionMiddleware<UsersPermission>()("@shivaedev/admin/UsersPermission", ReadUsers) {}
|
|
8
|
+
export class JobsPermission extends permissionMiddleware<JobsPermission>()("@shivaedev/admin/JobsPermission", ReadJobs) {}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { type Context, DateTime, Effect, Option } from "effect";
|
|
2
2
|
import type { PgBossError, PgBossPayloadError } from "@shivaedev/jobs/server/error.ts";
|
|
3
3
|
import type { PgBossService } from "@shivaedev/jobs/server/service.ts";
|
|
4
|
+
import { type JobsEntry, type JobsPayload, jobsQueue } from "#jobs/entry.ts";
|
|
4
5
|
import type { jobsPage } from "#jobs/page.ts";
|
|
5
|
-
import { JobsPayloadInvalid,
|
|
6
|
+
import { JobsPayloadInvalid, JobsQueueUnregistered, JobsUnavailable } from "#jobs/schemas.ts";
|
|
6
7
|
|
|
7
8
|
export function jobsFailures<A, R>(effect: Effect.Effect<A, PgBossError | PgBossPayloadError, R>) {
|
|
8
9
|
return effect.pipe(
|
|
@@ -12,14 +13,14 @@ export function jobsFailures<A, R>(effect: Effect.Effect<A, PgBossError | PgBoss
|
|
|
12
13
|
);
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
export function jobsReadHandlers<const TQueues extends readonly
|
|
16
|
+
export function jobsReadHandlers<const TQueues extends readonly JobsEntry[], TService>(
|
|
16
17
|
page: { readonly queues: TQueues },
|
|
17
18
|
service: Context.Service<TService, PgBossService>,
|
|
18
19
|
) {
|
|
19
20
|
function registered(name: string) {
|
|
20
21
|
const queues: readonly TQueues[number][] = page.queues;
|
|
21
22
|
const queue = queues.find((candidate) => candidate.name === name);
|
|
22
|
-
return queue === undefined ? Effect.fail(new JobsQueueUnregistered({ queue: name })) : Effect.succeed(queue);
|
|
23
|
+
return queue === undefined ? Effect.fail(new JobsQueueUnregistered({ queue: name })) : Effect.succeed(jobsQueue(queue));
|
|
23
24
|
}
|
|
24
25
|
return {
|
|
25
26
|
"admin.jobs.counts": ({ queue }: { readonly queue: string }) =>
|
|
@@ -32,13 +33,13 @@ export function jobsReadHandlers<const TQueues extends readonly JobsQueue[], TSe
|
|
|
32
33
|
Effect.gen(function* () {
|
|
33
34
|
const definition = yield* registered(queue);
|
|
34
35
|
const jobs = yield* service;
|
|
35
|
-
return yield* jobsFailures(jobs.deadLetteredJobs<string, TQueues[number]
|
|
36
|
+
return yield* jobsFailures(jobs.deadLetteredJobs<string, JobsPayload<TQueues[number]>>(definition, options));
|
|
36
37
|
}),
|
|
37
38
|
"admin.jobs.failed": ({ queue, ...options }: { readonly queue: string; readonly limit: number; readonly cursor?: string }) =>
|
|
38
39
|
Effect.gen(function* () {
|
|
39
40
|
const definition = yield* registered(queue);
|
|
40
41
|
const jobs = yield* service;
|
|
41
|
-
return yield* jobsFailures(jobs.failedJobs<string, TQueues[number]
|
|
42
|
+
return yield* jobsFailures(jobs.failedJobs<string, JobsPayload<TQueues[number]>>(definition, options));
|
|
42
43
|
}),
|
|
43
44
|
"admin.jobs.health": () =>
|
|
44
45
|
Effect.gen(function* () {
|
|
@@ -58,7 +59,7 @@ export function jobsReadHandlers<const TQueues extends readonly JobsQueue[], TSe
|
|
|
58
59
|
Effect.gen(function* () {
|
|
59
60
|
const definition = yield* registered(queue);
|
|
60
61
|
const jobs = yield* service;
|
|
61
|
-
return Option.map(yield* jobsFailures(jobs.getJob<string, TQueues[number]
|
|
62
|
+
return Option.map(yield* jobsFailures(jobs.getJob<string, JobsPayload<TQueues[number]>>(definition, id)), (job) => ({
|
|
62
63
|
attempt: job.startedOn === null ? 0 : job.retryCount + 1,
|
|
63
64
|
completedOn: Option.map(Option.fromNullishOr(job.completedOn), DateTime.makeUnsafe),
|
|
64
65
|
createdOn: DateTime.makeUnsafe(job.createdOn),
|
|
@@ -75,7 +76,7 @@ export function jobsReadHandlers<const TQueues extends readonly JobsQueue[], TSe
|
|
|
75
76
|
};
|
|
76
77
|
}
|
|
77
78
|
|
|
78
|
-
export function jobsHandlers<const TQueues extends readonly
|
|
79
|
+
export function jobsHandlers<const TQueues extends readonly JobsEntry[], TService>(
|
|
79
80
|
page: ReturnType<typeof jobsPage<TQueues>>,
|
|
80
81
|
service: Context.Service<TService, PgBossService>,
|
|
81
82
|
) {
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { type Context, Effect, Option } from "effect";
|
|
2
2
|
import type { PgBossService } from "@shivaedev/jobs/server/service.ts";
|
|
3
|
+
import { type JobsEntry, type JobsPayload, jobsQueue } from "#jobs/entry.ts";
|
|
3
4
|
import { JobsNotFailed, type jobsPageWithRetry } from "#jobs/retryPage.ts";
|
|
4
|
-
import {
|
|
5
|
+
import { JobsQueueUnregistered } from "#jobs/schemas.ts";
|
|
5
6
|
import { jobsFailures, jobsReadHandlers } from "./jobsHandlers.ts";
|
|
6
7
|
|
|
7
|
-
export function jobsHandlersWithRetry<const TQueues extends readonly
|
|
8
|
+
export function jobsHandlersWithRetry<const TQueues extends readonly JobsEntry[], TService>(
|
|
8
9
|
page: ReturnType<typeof jobsPageWithRetry<TQueues>>,
|
|
9
10
|
service: Context.Service<TService, PgBossService>,
|
|
10
11
|
) {
|
|
@@ -12,12 +13,14 @@ export function jobsHandlersWithRetry<const TQueues extends readonly JobsQueue[]
|
|
|
12
13
|
...jobsReadHandlers(page, service),
|
|
13
14
|
"admin.jobs.retry": ({ queue, id }) =>
|
|
14
15
|
Effect.gen(function* () {
|
|
15
|
-
const
|
|
16
|
-
|
|
16
|
+
const entries: readonly TQueues[number][] = page.queues;
|
|
17
|
+
const entry = entries.find((candidate) => candidate.name === queue);
|
|
18
|
+
if (entry === undefined) {
|
|
17
19
|
return yield* Effect.fail(new JobsQueueUnregistered({ queue }));
|
|
18
20
|
}
|
|
21
|
+
const definition = jobsQueue(entry);
|
|
19
22
|
const jobs = yield* service;
|
|
20
|
-
const job = yield* jobsFailures(jobs.getJob<string, TQueues[number]
|
|
23
|
+
const job = yield* jobsFailures(jobs.getJob<string, JobsPayload<TQueues[number]>>(definition, id));
|
|
21
24
|
if (Option.isNone(job) || job.value.state !== "failed") {
|
|
22
25
|
return yield* Effect.fail(new JobsNotFailed({ id, queue }));
|
|
23
26
|
}
|
package/src/server/users.ts
CHANGED
|
@@ -1,42 +1,51 @@
|
|
|
1
1
|
import { DateTime, Effect, Option, Schema } from "effect";
|
|
2
2
|
import { SqlClient, SqlSchema } from "effect/sql";
|
|
3
|
+
import type { roleSet } from "@shivaedev/access/roleSet.ts";
|
|
3
4
|
import { AuthUnavailable, NotFound } from "@shivaedev/errors/taxonomy.ts";
|
|
4
5
|
import { SessionUser } from "@shivaedev/identity/identity.ts";
|
|
5
6
|
import { requirePermission } from "@shivaedev/identity/permission.ts";
|
|
6
7
|
import { invalidateOnCommit, transact } from "@shivaedev/sql/transact.ts";
|
|
7
|
-
import {
|
|
8
|
+
import { AssignRoles, ReadUsers } from "#permissions.ts";
|
|
9
|
+
import { type ChangeRoleInput, LastRoleAssigner, type UserListInput } from "#users/roles.ts";
|
|
8
10
|
|
|
9
11
|
const StoredUser = Schema.Struct({
|
|
10
12
|
createdAt: Schema.Date,
|
|
11
13
|
email: Schema.String,
|
|
12
14
|
id: Schema.String,
|
|
13
15
|
name: Schema.String,
|
|
14
|
-
role: Schema.
|
|
16
|
+
role: Schema.String,
|
|
15
17
|
});
|
|
16
18
|
function unavailable() {
|
|
17
19
|
return new AuthUnavailable({ message: "Could not access app users" });
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
export const listUsers = Effect.fn("Platform.listUsers")(function*
|
|
22
|
+
export const listUsers = Effect.fn("Platform.listUsers")(function* <TRole extends string>(
|
|
23
|
+
access: Pick<ReturnType<typeof roleSet<TRole>>, "Role">,
|
|
24
|
+
input: typeof UserListInput.Type,
|
|
25
|
+
) {
|
|
26
|
+
yield* requirePermission(ReadUsers);
|
|
21
27
|
const sql = yield* SqlClient.SqlClient;
|
|
22
28
|
const read = SqlSchema.findAll({
|
|
23
29
|
execute: () => sql`select id, name, email, role, "createdAt" from "user"
|
|
24
30
|
where id > ${input.cursor ?? ""} order by id limit ${input.limit + 1}`,
|
|
25
31
|
Request: Schema.Void,
|
|
26
|
-
Result: StoredUser,
|
|
32
|
+
Result: Schema.Struct({ ...StoredUser.fields, role: access.Role }),
|
|
27
33
|
});
|
|
28
34
|
const rows = yield* read(undefined).pipe(Effect.mapError(unavailable));
|
|
29
35
|
const users = rows.slice(0, input.limit).map(({ role, createdAt, ...user }) => ({
|
|
30
36
|
...user,
|
|
31
37
|
createdAt: DateTime.makeUnsafe(createdAt),
|
|
32
|
-
|
|
38
|
+
role,
|
|
33
39
|
}));
|
|
34
40
|
return { next: rows.length > input.limit ? Option.fromUndefinedOr(users.at(-1)?.id) : Option.none<string>(), users };
|
|
35
41
|
});
|
|
36
42
|
|
|
37
|
-
export const changeUserRole = Effect.fn("Platform.changeUserRole")(function*
|
|
43
|
+
export const changeUserRole = Effect.fn("Platform.changeUserRole")(function* <TRole extends string>(
|
|
44
|
+
access: Pick<ReturnType<typeof roleSet<TRole>>, "Role" | "hasPermission">,
|
|
45
|
+
input: ChangeRoleInput<TRole>,
|
|
46
|
+
) {
|
|
38
47
|
const actor = yield* SessionUser;
|
|
39
|
-
yield* requirePermission(
|
|
48
|
+
yield* requirePermission(AssignRoles);
|
|
40
49
|
const sql = yield* SqlClient.SqlClient;
|
|
41
50
|
const changed = yield* Effect.gen(function* () {
|
|
42
51
|
yield* sql`select pg_advisory_xact_lock(hashtext('platform.auth.user-roles'))`;
|
|
@@ -45,18 +54,18 @@ export const changeUserRole = Effect.fn("Platform.changeUserRole")(function* (in
|
|
|
45
54
|
if (target === undefined) {
|
|
46
55
|
return yield* Effect.fail(new NotFound({ message: "User not found" }));
|
|
47
56
|
}
|
|
48
|
-
const
|
|
49
|
-
if (
|
|
57
|
+
const next = yield* Schema.decodeUnknownEffect(access.Role)(input.role).pipe(Effect.mapError(unavailable));
|
|
58
|
+
if (target.role === next) {
|
|
50
59
|
return false;
|
|
51
60
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
const assigners = access.Role.literals.filter((role) => access.hasPermission(role, AssignRoles));
|
|
62
|
+
if (assigners.some((role) => role === target.role) && !access.hasPermission(next, AssignRoles)) {
|
|
63
|
+
const holders = yield* sql<{ readonly id: string }>`select id from "user" where role in ${sql.in(assigners)}`;
|
|
64
|
+
if (holders.length === 1) {
|
|
65
|
+
return yield* Effect.fail(new LastRoleAssigner({ userId: input.userId }));
|
|
56
66
|
}
|
|
57
67
|
}
|
|
58
|
-
|
|
59
|
-
yield* sql`update "user" set role = ${next.join(",")}, "updatedAt" = now() where id = ${input.userId}`;
|
|
68
|
+
yield* sql`update "user" set role = ${next}, "updatedAt" = now() where id = ${input.userId}`;
|
|
60
69
|
yield* invalidateOnCommit(["admin.users"]);
|
|
61
70
|
return true;
|
|
62
71
|
}).pipe(transact({ onSqlError: unavailable }));
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
+
import type { roleSet } from "@shivaedev/access/roleSet.ts";
|
|
2
3
|
import { SessionUser } from "@shivaedev/identity/identity.ts";
|
|
3
4
|
import { hasPermission } from "@shivaedev/identity/permission.ts";
|
|
5
|
+
import { AssignRoles } from "#permissions.ts";
|
|
4
6
|
import { changeUserRole, listUsers } from "#server/users.ts";
|
|
5
|
-
import {
|
|
6
|
-
import { ChangeUserRoles } from "#users/roles.ts";
|
|
7
|
+
import type { usersPage } from "#users/page.ts";
|
|
7
8
|
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
export function usersHandlers<const TRole extends string>(
|
|
10
|
+
page: ReturnType<typeof usersPage<TRole>>,
|
|
11
|
+
access: Pick<ReturnType<typeof roleSet<TRole>>, "Role" | "hasPermission">,
|
|
12
|
+
) {
|
|
13
|
+
return page.contract.application.toLayer({
|
|
14
|
+
"admin.users.changeRole": (input) => changeUserRole(access, input),
|
|
15
|
+
"admin.users.list": (input) =>
|
|
16
|
+
Effect.gen(function* () {
|
|
17
|
+
const user = yield* SessionUser;
|
|
18
|
+
return { ...(yield* listUsers(access, input)), canChangeRoles: hasPermission(user, AssignRoles) };
|
|
19
|
+
}),
|
|
20
|
+
});
|
|
21
|
+
}
|
package/src/users/page.ts
CHANGED
|
@@ -1,32 +1,40 @@
|
|
|
1
|
-
import { Schema } from "effect";
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import type { roleSet } from "@shivaedev/access/roleSet.ts";
|
|
3
|
+
import { collection } from "@shivaedev/changes/keys.ts";
|
|
2
4
|
import { contract } from "@shivaedev/contract/contract.ts";
|
|
3
|
-
import { collection } from "@shivaedev/contract/keys.ts";
|
|
4
5
|
import { command, query } from "@shivaedev/contract/operation.ts";
|
|
5
6
|
import { AuthUnavailable, Forbidden, NotFound } from "@shivaedev/errors/taxonomy.ts";
|
|
7
|
+
import { SessionUser } from "@shivaedev/identity/identity.ts";
|
|
8
|
+
import { hasPermission } from "@shivaedev/identity/permission.ts";
|
|
6
9
|
import { adminPage } from "#page.ts";
|
|
7
|
-
import {
|
|
10
|
+
import { UsersPermission } from "#permissions.ts";
|
|
11
|
+
import { LastRoleAssigner, UserList, UserListInput } from "#users/roles.ts";
|
|
8
12
|
|
|
9
13
|
const users = collection("admin.users", Schema.String);
|
|
10
14
|
|
|
11
15
|
export const UsersList = Schema.Struct({ ...UserList.fields, canChangeRoles: Schema.Boolean });
|
|
12
|
-
export const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
16
|
+
export function usersPage<const TRole extends string>(access: Pick<ReturnType<typeof roleSet<TRole>>, "Role">) {
|
|
17
|
+
return adminPage({
|
|
18
|
+
contract: contract("admin.users", {
|
|
19
|
+
commands: [
|
|
20
|
+
command("changeRole", {
|
|
21
|
+
invalidates: () => [users.list],
|
|
22
|
+
payload: { role: access.Role, userId: Schema.String },
|
|
23
|
+
rejections: { AuthUnavailable, Forbidden, LastRoleAssigner, NotFound },
|
|
24
|
+
}),
|
|
25
|
+
],
|
|
26
|
+
queries: [
|
|
27
|
+
query("list", {
|
|
28
|
+
payload: UserListInput.fields,
|
|
29
|
+
readPolicy: () => Effect.map(SessionUser, (user) => hasPermission(user, UsersPermission.permission)),
|
|
30
|
+
reads: () => [users.list],
|
|
31
|
+
rejections: { AuthUnavailable, Forbidden },
|
|
32
|
+
success: UsersList,
|
|
33
|
+
}),
|
|
34
|
+
],
|
|
35
|
+
}),
|
|
36
|
+
id: "users",
|
|
37
|
+
permission: UsersPermission,
|
|
38
|
+
route: { path: "/admin/users", title: "Users" },
|
|
39
|
+
});
|
|
40
|
+
}
|