@shivaedev/admin 0.2.0 → 0.4.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 +43 -3
- package/dist/client/UsersTable.d.ts +11 -0
- package/dist/client/UsersTable.d.ts.map +1 -0
- package/dist/client/UsersTable.js +25 -0
- package/dist/client/UsersTable.js.map +1 -0
- package/dist/client/UsersView.d.ts +13 -0
- package/dist/client/UsersView.d.ts.map +1 -0
- package/dist/client/UsersView.js +35 -0
- package/dist/client/UsersView.js.map +1 -0
- package/dist/jobs/keys.d.ts +8 -0
- package/dist/jobs/keys.d.ts.map +1 -0
- package/dist/jobs/keys.js +10 -0
- package/dist/jobs/keys.js.map +1 -0
- package/dist/jobs/page.d.ts +35 -35
- package/dist/jobs/page.d.ts.map +1 -1
- package/dist/jobs/page.js +11 -5
- package/dist/jobs/page.js.map +1 -1
- package/dist/jobs/retryPage.d.ts +241 -0
- package/dist/jobs/retryPage.d.ts.map +1 -0
- package/dist/jobs/retryPage.js +36 -0
- package/dist/jobs/retryPage.js.map +1 -0
- package/dist/server/jobsHandlers.d.ts +45 -2
- package/dist/server/jobsHandlers.d.ts.map +1 -1
- package/dist/server/jobsHandlers.js +12 -9
- package/dist/server/jobsHandlers.js.map +1 -1
- package/dist/server/jobsHandlersWithRetry.d.ts +6 -0
- package/dist/server/jobsHandlersWithRetry.d.ts.map +1 -0
- package/dist/server/jobsHandlersWithRetry.js +22 -0
- package/dist/server/jobsHandlersWithRetry.js.map +1 -0
- package/dist/server/usersHandlers.d.ts +2 -0
- package/dist/server/usersHandlers.d.ts.map +1 -0
- package/dist/server/usersHandlers.js +13 -0
- package/dist/server/usersHandlers.js.map +1 -0
- package/dist/users/page.d.ts +80 -0
- package/dist/users/page.d.ts.map +1 -0
- package/dist/users/page.js +31 -0
- package/dist/users/page.js.map +1 -0
- package/package.json +17 -6
- package/src/client/UsersTable.tsx +102 -0
- package/src/client/UsersView.ts +71 -0
- package/src/jobs/keys.ts +10 -0
- package/src/jobs/page.ts +11 -5
- package/src/jobs/retryPage.ts +39 -0
- package/src/server/jobsHandlers.ts +21 -14
- package/src/server/jobsHandlersWithRetry.ts +27 -0
- package/src/server/usersHandlers.ts +14 -0
- package/src/users/page.ts +32 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/admin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Explicit admin page contracts and a protected native Effect RPC registry",
|
|
5
5
|
"homepage": "https://github.com/ShivaeDev/platform/tree/main/packages/admin#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -34,26 +34,37 @@
|
|
|
34
34
|
"dist",
|
|
35
35
|
"src",
|
|
36
36
|
"!src/**/*.test.ts",
|
|
37
|
+
"!src/**/*.test.tsx",
|
|
37
38
|
"!src/**/*.spec.ts",
|
|
39
|
+
"!src/**/*.spec.tsx",
|
|
38
40
|
"!src/**/test-support",
|
|
39
41
|
"README.md"
|
|
40
42
|
],
|
|
41
43
|
"dependencies": {
|
|
42
|
-
"@shivaedev/effect-contract": "0.
|
|
44
|
+
"@shivaedev/effect-contract": "0.16.0",
|
|
43
45
|
"@shivaedev/effect-pg-boss": "0.19.0",
|
|
44
|
-
"@shivaedev/
|
|
46
|
+
"@shivaedev/effect-react": "0.18.1",
|
|
47
|
+
"@shivaedev/platform": "0.33.0"
|
|
45
48
|
},
|
|
46
49
|
"devDependencies": {
|
|
50
|
+
"@effect/atom-react": "4.0.5",
|
|
47
51
|
"@shivaedev/effect-test-sql": "0.9.3",
|
|
48
|
-
"@shivaedev/quality": "0.
|
|
49
|
-
"@shivaedev/test-story": "0.20.
|
|
52
|
+
"@shivaedev/quality": "0.41.1",
|
|
53
|
+
"@shivaedev/test-story": "0.20.1",
|
|
50
54
|
"@types/node": "24.19.2",
|
|
55
|
+
"@types/react": "19.3.0",
|
|
56
|
+
"@types/react-dom": "19.3.0",
|
|
51
57
|
"effect": "4.0.5",
|
|
58
|
+
"happy-dom": "21.0.3",
|
|
59
|
+
"react": "19.3.1",
|
|
60
|
+
"react-dom": "19.3.1",
|
|
52
61
|
"typescript": "7.0.2",
|
|
53
62
|
"vitest": "5.0.5"
|
|
54
63
|
},
|
|
55
64
|
"peerDependencies": {
|
|
56
|
-
"effect": "4.0.5"
|
|
65
|
+
"@effect/atom-react": "4.0.5",
|
|
66
|
+
"effect": "4.0.5",
|
|
67
|
+
"react": "19.3.1"
|
|
57
68
|
},
|
|
58
69
|
"engines": {
|
|
59
70
|
"node": ">=24"
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { DateTime } from "effect";
|
|
2
|
+
import { type AppUser, type ChangeRoleInput, UserRole } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
3
|
+
import type { UsersList } from "#users/page.ts";
|
|
4
|
+
|
|
5
|
+
interface RoleControls {
|
|
6
|
+
readonly dispatch: (input: typeof ChangeRoleInput.Type) => void;
|
|
7
|
+
readonly pending: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function UserHeaders() {
|
|
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({
|
|
38
|
+
user,
|
|
39
|
+
canChangeRoles,
|
|
40
|
+
dispatch,
|
|
41
|
+
pending,
|
|
42
|
+
}: RoleControls & {
|
|
43
|
+
readonly user: typeof AppUser.Type;
|
|
44
|
+
readonly canChangeRoles: boolean;
|
|
45
|
+
}) {
|
|
46
|
+
return (
|
|
47
|
+
<div>
|
|
48
|
+
<p>{user.roles.join(", ") || "No roles"}</p>
|
|
49
|
+
{canChangeRoles
|
|
50
|
+
? UserRole.literals.map((role) => <RoleCheckbox dispatch={dispatch} key={role} pending={pending} role={role} user={user} />)
|
|
51
|
+
: null}
|
|
52
|
+
</div>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function UserRow({
|
|
57
|
+
user,
|
|
58
|
+
...controls
|
|
59
|
+
}: RoleControls & {
|
|
60
|
+
readonly user: typeof AppUser.Type;
|
|
61
|
+
readonly canChangeRoles: boolean;
|
|
62
|
+
}) {
|
|
63
|
+
return (
|
|
64
|
+
<tr className="border-b">
|
|
65
|
+
<td className="p-3">{user.name}</td>
|
|
66
|
+
<td className="p-3">{user.email}</td>
|
|
67
|
+
<td className="p-3">
|
|
68
|
+
<UserRoles {...controls} user={user} />
|
|
69
|
+
</td>
|
|
70
|
+
<td className="whitespace-nowrap p-3">
|
|
71
|
+
<time dateTime={DateTime.formatIso(user.createdAt)}>{DateTime.formatIso(user.createdAt)}</time>
|
|
72
|
+
</td>
|
|
73
|
+
</tr>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function UserRows({ data, ...controls }: RoleControls & { readonly data: typeof UsersList.Type }) {
|
|
78
|
+
return (
|
|
79
|
+
<div className="overflow-x-auto rounded-md border">
|
|
80
|
+
<table className="w-full text-left text-sm">
|
|
81
|
+
<caption className="sr-only">App users and roles</caption>
|
|
82
|
+
<thead className="bg-muted">
|
|
83
|
+
<UserHeaders />
|
|
84
|
+
</thead>
|
|
85
|
+
<tbody>
|
|
86
|
+
{data.users.map((user) => (
|
|
87
|
+
<UserRow {...controls} canChangeRoles={data.canChangeRoles} key={user.id} user={user} />
|
|
88
|
+
))}
|
|
89
|
+
</tbody>
|
|
90
|
+
</table>
|
|
91
|
+
</div>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function UsersTable({ data, ...controls }: RoleControls & { readonly data: typeof UsersList.Type }) {
|
|
96
|
+
return (
|
|
97
|
+
<div className="space-y-4">
|
|
98
|
+
{data.canChangeRoles ? null : <p>Only owners can change roles.</p>}
|
|
99
|
+
{data.users.length === 0 ? <p>No users.</p> : <UserRows {...controls} data={data} />}
|
|
100
|
+
</div>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Cause, Option, Predicate } from "effect";
|
|
2
|
+
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
4
|
+
import { createElement, useState } from "react";
|
|
5
|
+
import { useAction, useQuery } from "@shivaedev/effect-react/client/resultState.ts";
|
|
6
|
+
import type { ChangeRoleInput, UserListInput } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
7
|
+
import type { UsersList } from "#users/page.ts";
|
|
8
|
+
import { UsersTable } from "./UsersTable.tsx";
|
|
9
|
+
|
|
10
|
+
export interface UsersViewProps {
|
|
11
|
+
readonly changeRole: Atom.AtomResultFn<typeof ChangeRoleInput.Type, void, unknown>;
|
|
12
|
+
readonly list: (input: typeof UserListInput.Type) => Atom.Atom<AsyncResult.AsyncResult<typeof UsersList.Type, unknown>>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function failure(cause: Cause.Cause<unknown>): string {
|
|
16
|
+
const error = Option.getOrUndefined(Cause.findErrorOption(cause));
|
|
17
|
+
if (Predicate.isTagged(error, "LastOwner")) {
|
|
18
|
+
return "The last owner cannot be demoted. Grant owner to another user first.";
|
|
19
|
+
}
|
|
20
|
+
if (Predicate.isTagged(error, "Forbidden")) {
|
|
21
|
+
return "Owner role required to change roles.";
|
|
22
|
+
}
|
|
23
|
+
return "Could not change the role. Try again.";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function UsersView({ list, changeRole }: UsersViewProps) {
|
|
27
|
+
const [cursors, setCursors] = useState<readonly string[]>([]);
|
|
28
|
+
const cursor = cursors.at(-1);
|
|
29
|
+
const state = useQuery(list({ ...(cursor === undefined ? {} : { cursor }), limit: 20 }));
|
|
30
|
+
const action = useAction(changeRole);
|
|
31
|
+
const data = Option.getOrUndefined(state.data);
|
|
32
|
+
return createElement(
|
|
33
|
+
"section",
|
|
34
|
+
{ "aria-label": "App users", className: "space-y-4" },
|
|
35
|
+
createElement(
|
|
36
|
+
"button",
|
|
37
|
+
{ className: "rounded-md border px-3 py-2 disabled:opacity-50", disabled: state.pending, onClick: state.refresh, type: "button" },
|
|
38
|
+
"Refresh users",
|
|
39
|
+
),
|
|
40
|
+
state.pending ? createElement("p", { role: "status" }, "Loading users…") : null,
|
|
41
|
+
Option.isSome(state.cause) ? createElement("p", { role: "alert" }, "Could not load users. Try refreshing.") : null,
|
|
42
|
+
Option.isSome(action.cause) ? createElement("p", { role: "alert" }, failure(action.cause.value)) : null,
|
|
43
|
+
data === undefined ? null : createElement(UsersTable, { data, dispatch: action.dispatch, pending: action.pending }),
|
|
44
|
+
data === undefined
|
|
45
|
+
? null
|
|
46
|
+
: createElement(
|
|
47
|
+
"nav",
|
|
48
|
+
{ "aria-label": "User pages", className: "flex gap-4" },
|
|
49
|
+
createElement(
|
|
50
|
+
"button",
|
|
51
|
+
{
|
|
52
|
+
className: "rounded-md border px-3 py-2 disabled:opacity-50",
|
|
53
|
+
disabled: cursors.length === 0 || state.pending,
|
|
54
|
+
onClick: () => setCursors(cursors.slice(0, -1)),
|
|
55
|
+
type: "button",
|
|
56
|
+
},
|
|
57
|
+
"Previous users",
|
|
58
|
+
),
|
|
59
|
+
createElement(
|
|
60
|
+
"button",
|
|
61
|
+
{
|
|
62
|
+
className: "rounded-md border px-3 py-2 disabled:opacity-50",
|
|
63
|
+
disabled: Option.isNone(data.next) || state.pending,
|
|
64
|
+
onClick: () => Option.map(data.next, (next) => setCursors([...cursors, next])),
|
|
65
|
+
type: "button",
|
|
66
|
+
},
|
|
67
|
+
"Next users",
|
|
68
|
+
),
|
|
69
|
+
),
|
|
70
|
+
);
|
|
71
|
+
}
|
package/src/jobs/keys.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { collection } from "@shivaedev/effect-contract/keys.ts";
|
|
3
|
+
|
|
4
|
+
export const jobsKeys = {
|
|
5
|
+
counts: collection("admin.jobs.counts", Schema.String),
|
|
6
|
+
deadLettered: collection("admin.jobs.deadLettered", Schema.String),
|
|
7
|
+
failed: collection("admin.jobs.failed", Schema.String),
|
|
8
|
+
health: collection("admin.jobs.health", Schema.String),
|
|
9
|
+
job: (queue: string) => collection(`admin.jobs.job.${queue}`, Schema.String),
|
|
10
|
+
};
|
package/src/jobs/page.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Schema } from "effect";
|
|
|
2
2
|
import { contract } from "@shivaedev/effect-contract/contract.ts";
|
|
3
3
|
import { query } from "@shivaedev/effect-contract/operation.ts";
|
|
4
4
|
import { adminPage } from "#page.ts";
|
|
5
|
+
import { jobsKeys } from "./keys.ts";
|
|
5
6
|
import { deadLetteredSchema, JobsFailures, JobsHealth, type JobsQueue, jobPage, jobSchema, PageFields, QueueCounts } from "./schemas.ts";
|
|
6
7
|
|
|
7
8
|
export function jobsPage<const TQueues extends readonly JobsQueue[]>(...queues: TQueues) {
|
|
@@ -10,23 +11,28 @@ export function jobsPage<const TQueues extends readonly JobsQueue[]>(...queues:
|
|
|
10
11
|
const page = adminPage({
|
|
11
12
|
contract: contract("admin.jobs", {
|
|
12
13
|
queries: [
|
|
13
|
-
query("health", { reads: () => [], rejections: JobsFailures, success: JobsHealth }),
|
|
14
|
-
query("counts", {
|
|
14
|
+
query("health", { reads: () => [jobsKeys.health.list], rejections: JobsFailures, success: JobsHealth }),
|
|
15
|
+
query("counts", {
|
|
16
|
+
payload: { queue: Schema.String },
|
|
17
|
+
reads: ({ queue }) => [jobsKeys.counts.item(queue)],
|
|
18
|
+
rejections: JobsFailures,
|
|
19
|
+
success: QueueCounts,
|
|
20
|
+
}),
|
|
15
21
|
query("failed", {
|
|
16
22
|
payload: { ...PageFields, queue: Schema.String },
|
|
17
|
-
reads: () => [],
|
|
23
|
+
reads: ({ queue }) => [jobsKeys.failed.item(queue)],
|
|
18
24
|
rejections: JobsFailures,
|
|
19
25
|
success: jobPage(Job),
|
|
20
26
|
}),
|
|
21
27
|
query("deadLettered", {
|
|
22
28
|
payload: { ...PageFields, queue: Schema.String },
|
|
23
|
-
reads: () => [],
|
|
29
|
+
reads: ({ queue }) => [jobsKeys.deadLettered.item(queue)],
|
|
24
30
|
rejections: JobsFailures,
|
|
25
31
|
success: jobPage(DeadLettered),
|
|
26
32
|
}),
|
|
27
33
|
query("job", {
|
|
28
34
|
payload: { id: Schema.String, queue: Schema.String },
|
|
29
|
-
reads: () => [],
|
|
35
|
+
reads: ({ queue, id }) => [jobsKeys.job(queue).item(id)],
|
|
30
36
|
rejections: JobsFailures,
|
|
31
37
|
success: Schema.OptionFromNullOr(Job),
|
|
32
38
|
}),
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { contract } from "@shivaedev/effect-contract/contract.ts";
|
|
3
|
+
import { command } from "@shivaedev/effect-contract/operation.ts";
|
|
4
|
+
import { adminPage } from "#page.ts";
|
|
5
|
+
import { jobsKeys } from "./keys.ts";
|
|
6
|
+
import { jobsPage } from "./page.ts";
|
|
7
|
+
import { JobsFailures, type JobsQueue } from "./schemas.ts";
|
|
8
|
+
|
|
9
|
+
export class JobsNotFailed extends Schema.TaggedError<JobsNotFailed>()("JobsNotFailed", {
|
|
10
|
+
id: Schema.String,
|
|
11
|
+
queue: Schema.String,
|
|
12
|
+
}) {}
|
|
13
|
+
|
|
14
|
+
export function jobsPageWithRetry<const TQueues extends readonly JobsQueue[]>(...queues: TQueues) {
|
|
15
|
+
const page = jobsPage(...queues);
|
|
16
|
+
return Object.assign(
|
|
17
|
+
adminPage({
|
|
18
|
+
contract: contract("admin.jobs", {
|
|
19
|
+
commands: [
|
|
20
|
+
command("retry", {
|
|
21
|
+
invalidates: ({ queue, id }) => [
|
|
22
|
+
jobsKeys.health.list,
|
|
23
|
+
jobsKeys.counts.item(queue),
|
|
24
|
+
jobsKeys.failed.item(queue),
|
|
25
|
+
jobsKeys.deadLettered.item(queue),
|
|
26
|
+
jobsKeys.job(queue).item(id),
|
|
27
|
+
],
|
|
28
|
+
payload: { id: Schema.String, queue: Schema.String },
|
|
29
|
+
rejections: { ...JobsFailures, JobsNotFailed },
|
|
30
|
+
}),
|
|
31
|
+
],
|
|
32
|
+
queries: page.contract.declaration.queries,
|
|
33
|
+
}),
|
|
34
|
+
id: page.id,
|
|
35
|
+
route: page.route,
|
|
36
|
+
}),
|
|
37
|
+
{ queues },
|
|
38
|
+
);
|
|
39
|
+
}
|
|
@@ -4,7 +4,7 @@ import type { PgBossService } from "@shivaedev/effect-pg-boss/server/service.ts"
|
|
|
4
4
|
import type { jobsPage } from "#jobs/page.ts";
|
|
5
5
|
import { JobsPayloadInvalid, type JobsQueue, JobsQueueUnregistered, JobsUnavailable } from "#jobs/schemas.ts";
|
|
6
6
|
|
|
7
|
-
function
|
|
7
|
+
export function jobsFailures<A, R>(effect: Effect.Effect<A, PgBossError | PgBossPayloadError, R>) {
|
|
8
8
|
return effect.pipe(
|
|
9
9
|
Effect.mapError((error) =>
|
|
10
10
|
error._tag === "PgBossPayloadError" ? new JobsPayloadInvalid({ queue: error.queue }) : new JobsUnavailable({ operation: error.operation }),
|
|
@@ -12,8 +12,8 @@ function failures<A, R>(effect: Effect.Effect<A, PgBossError | PgBossPayloadErro
|
|
|
12
12
|
);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export function
|
|
16
|
-
page:
|
|
15
|
+
export function jobsReadHandlers<const TQueues extends readonly JobsQueue[], TService>(
|
|
16
|
+
page: { readonly queues: TQueues },
|
|
17
17
|
service: Context.Service<TService, PgBossService>,
|
|
18
18
|
) {
|
|
19
19
|
function registered(name: string) {
|
|
@@ -21,29 +21,29 @@ export function jobsHandlers<const TQueues extends readonly JobsQueue[], TServic
|
|
|
21
21
|
const queue = queues.find((candidate) => candidate.name === name);
|
|
22
22
|
return queue === undefined ? Effect.fail(new JobsQueueUnregistered({ queue: name })) : Effect.succeed(queue);
|
|
23
23
|
}
|
|
24
|
-
return
|
|
25
|
-
"admin.jobs.counts": ({ queue }) =>
|
|
24
|
+
return {
|
|
25
|
+
"admin.jobs.counts": ({ queue }: { readonly queue: string }) =>
|
|
26
26
|
Effect.gen(function* () {
|
|
27
27
|
const definition = yield* registered(queue);
|
|
28
28
|
const jobs = yield* service;
|
|
29
|
-
return yield*
|
|
29
|
+
return yield* jobsFailures(jobs.counts(definition));
|
|
30
30
|
}),
|
|
31
|
-
"admin.jobs.deadLettered": ({ queue, ...options }) =>
|
|
31
|
+
"admin.jobs.deadLettered": ({ queue, ...options }: { readonly queue: string; readonly limit: number; readonly cursor?: string }) =>
|
|
32
32
|
Effect.gen(function* () {
|
|
33
33
|
const definition = yield* registered(queue);
|
|
34
34
|
const jobs = yield* service;
|
|
35
|
-
return yield*
|
|
35
|
+
return yield* jobsFailures(jobs.deadLetteredJobs<string, TQueues[number]["schema"]>(definition, options));
|
|
36
36
|
}),
|
|
37
|
-
"admin.jobs.failed": ({ queue, ...options }) =>
|
|
37
|
+
"admin.jobs.failed": ({ queue, ...options }: { readonly queue: string; readonly limit: number; readonly cursor?: string }) =>
|
|
38
38
|
Effect.gen(function* () {
|
|
39
39
|
const definition = yield* registered(queue);
|
|
40
40
|
const jobs = yield* service;
|
|
41
|
-
return yield*
|
|
41
|
+
return yield* jobsFailures(jobs.failedJobs<string, TQueues[number]["schema"]>(definition, options));
|
|
42
42
|
}),
|
|
43
43
|
"admin.jobs.health": () =>
|
|
44
44
|
Effect.gen(function* () {
|
|
45
45
|
const jobs = yield* service;
|
|
46
|
-
const health = yield*
|
|
46
|
+
const health = yield* jobsFailures(jobs.health);
|
|
47
47
|
const visible = health.jobs.filter((queue) => page.queues.some((candidate) => candidate.name === queue.name));
|
|
48
48
|
return {
|
|
49
49
|
activeTotal: visible.reduce((sum, queue) => sum + queue.activeCount, 0),
|
|
@@ -54,11 +54,11 @@ export function jobsHandlers<const TQueues extends readonly JobsQueue[], TServic
|
|
|
54
54
|
readyTotal: visible.reduce((sum, queue) => sum + queue.readyCount, 0),
|
|
55
55
|
};
|
|
56
56
|
}),
|
|
57
|
-
"admin.jobs.job": ({ id, queue }) =>
|
|
57
|
+
"admin.jobs.job": ({ id, queue }: { readonly id: string; readonly queue: string }) =>
|
|
58
58
|
Effect.gen(function* () {
|
|
59
59
|
const definition = yield* registered(queue);
|
|
60
60
|
const jobs = yield* service;
|
|
61
|
-
return Option.map(yield*
|
|
61
|
+
return Option.map(yield* jobsFailures(jobs.getJob<string, TQueues[number]["schema"]>(definition, id)), (job) => ({
|
|
62
62
|
attempt: job.startedOn === null ? 0 : job.retryCount + 1,
|
|
63
63
|
completedOn: Option.map(Option.fromNullishOr(job.completedOn), DateTime.makeUnsafe),
|
|
64
64
|
createdOn: DateTime.makeUnsafe(job.createdOn),
|
|
@@ -72,5 +72,12 @@ export function jobsHandlers<const TQueues extends readonly JobsQueue[], TServic
|
|
|
72
72
|
state: job.state,
|
|
73
73
|
}));
|
|
74
74
|
}),
|
|
75
|
-
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function jobsHandlers<const TQueues extends readonly JobsQueue[], TService>(
|
|
79
|
+
page: ReturnType<typeof jobsPage<TQueues>>,
|
|
80
|
+
service: Context.Service<TService, PgBossService>,
|
|
81
|
+
) {
|
|
82
|
+
return page.contract.application.toLayer(jobsReadHandlers(page, service));
|
|
76
83
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Context, Effect, Option } from "effect";
|
|
2
|
+
import type { PgBossService } from "@shivaedev/effect-pg-boss/server/service.ts";
|
|
3
|
+
import { JobsNotFailed, type jobsPageWithRetry } from "#jobs/retryPage.ts";
|
|
4
|
+
import { type JobsQueue, JobsQueueUnregistered } from "#jobs/schemas.ts";
|
|
5
|
+
import { jobsFailures, jobsReadHandlers } from "./jobsHandlers.ts";
|
|
6
|
+
|
|
7
|
+
export function jobsHandlersWithRetry<const TQueues extends readonly JobsQueue[], TService>(
|
|
8
|
+
page: ReturnType<typeof jobsPageWithRetry<TQueues>>,
|
|
9
|
+
service: Context.Service<TService, PgBossService>,
|
|
10
|
+
) {
|
|
11
|
+
return page.contract.application.toLayer({
|
|
12
|
+
...jobsReadHandlers(page, service),
|
|
13
|
+
"admin.jobs.retry": ({ queue, id }) =>
|
|
14
|
+
Effect.gen(function* () {
|
|
15
|
+
const definition = page.queues.find((candidate) => candidate.name === queue);
|
|
16
|
+
if (definition === undefined) {
|
|
17
|
+
return yield* Effect.fail(new JobsQueueUnregistered({ queue }));
|
|
18
|
+
}
|
|
19
|
+
const jobs = yield* service;
|
|
20
|
+
const job = yield* jobsFailures(jobs.getJob<string, TQueues[number]["schema"]>(definition, id));
|
|
21
|
+
if (Option.isNone(job) || job.value.state !== "failed") {
|
|
22
|
+
return yield* Effect.fail(new JobsNotFailed({ id, queue }));
|
|
23
|
+
}
|
|
24
|
+
yield* jobsFailures(jobs.retry(definition, id));
|
|
25
|
+
}),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { hasRole } from "@shivaedev/platform/rpc/hasRole.ts";
|
|
3
|
+
import { SessionUser } from "@shivaedev/platform/rpc/identity.ts";
|
|
4
|
+
import { changeUserRole, listUsers } from "@shivaedev/platform/server/better-auth/users.ts";
|
|
5
|
+
import { UsersPage } from "#users/page.ts";
|
|
6
|
+
|
|
7
|
+
export const usersHandlers = UsersPage.contract.application.toLayer({
|
|
8
|
+
"admin.users.changeRole": changeUserRole,
|
|
9
|
+
"admin.users.list": (input) =>
|
|
10
|
+
Effect.gen(function* () {
|
|
11
|
+
const user = yield* SessionUser;
|
|
12
|
+
return { ...(yield* listUsers(input)), canChangeRoles: hasRole(user, "owner") };
|
|
13
|
+
}),
|
|
14
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { contract } from "@shivaedev/effect-contract/contract.ts";
|
|
3
|
+
import { AuthUnavailable, Forbidden, NotFound } from "@shivaedev/effect-contract/errors/taxonomy.ts";
|
|
4
|
+
import { collection } from "@shivaedev/effect-contract/keys.ts";
|
|
5
|
+
import { command, query } from "@shivaedev/effect-contract/operation.ts";
|
|
6
|
+
import { ChangeRoleInput, LastOwner, UserList, UserListInput } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
7
|
+
import { adminPage } from "#page.ts";
|
|
8
|
+
|
|
9
|
+
const users = collection("admin.users", Schema.String);
|
|
10
|
+
|
|
11
|
+
export const UsersList = Schema.Struct({ ...UserList.fields, canChangeRoles: Schema.Boolean });
|
|
12
|
+
export const UsersPage = adminPage({
|
|
13
|
+
contract: contract("admin.users", {
|
|
14
|
+
commands: [
|
|
15
|
+
command("changeRole", {
|
|
16
|
+
invalidates: () => [users.list],
|
|
17
|
+
payload: ChangeRoleInput.fields,
|
|
18
|
+
rejections: { AuthUnavailable, Forbidden, LastOwner, NotFound },
|
|
19
|
+
}),
|
|
20
|
+
],
|
|
21
|
+
queries: [
|
|
22
|
+
query("list", {
|
|
23
|
+
payload: UserListInput.fields,
|
|
24
|
+
reads: () => [users.list],
|
|
25
|
+
rejections: { AuthUnavailable },
|
|
26
|
+
success: UsersList,
|
|
27
|
+
}),
|
|
28
|
+
],
|
|
29
|
+
}),
|
|
30
|
+
id: "users",
|
|
31
|
+
route: { path: "/admin/users", title: "Users" },
|
|
32
|
+
});
|