making strides in the device and link domain setup
This commit is contained in:
67
packages/logic/domains/device/controller.ts
Normal file
67
packages/logic/domains/device/controller.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { errAsync, ResultAsync } from "neverthrow";
|
||||
import { db } from "@pkg/db";
|
||||
import { type Err } from "@pkg/result";
|
||||
import { FlowExecCtx } from "@core/flow.execution.context";
|
||||
import { CreateDevice, Device, DeviceStatus, UpdateDevice } from "./data";
|
||||
import { DeviceRepository } from "./repository";
|
||||
import { deviceErrors } from "./errors";
|
||||
|
||||
export class DeviceController {
|
||||
constructor(private repo: DeviceRepository) {}
|
||||
|
||||
list(fctx: FlowExecCtx): ResultAsync<Device[], Err> {
|
||||
return this.repo.list(fctx);
|
||||
}
|
||||
|
||||
getById(fctx: FlowExecCtx, id: number): ResultAsync<Device, Err> {
|
||||
return this.repo.getById(fctx, id);
|
||||
}
|
||||
|
||||
create(fctx: FlowExecCtx, data: CreateDevice): ResultAsync<Device, Err> {
|
||||
return this.repo.create(fctx, data);
|
||||
}
|
||||
|
||||
update(
|
||||
fctx: FlowExecCtx,
|
||||
id: number,
|
||||
data: UpdateDevice,
|
||||
): ResultAsync<Device, Err> {
|
||||
return this.repo.update(fctx, id, data);
|
||||
}
|
||||
|
||||
delete(fctx: FlowExecCtx, id: number): ResultAsync<boolean, Err> {
|
||||
return this.repo.delete(fctx, id);
|
||||
}
|
||||
|
||||
setStatus(
|
||||
fctx: FlowExecCtx,
|
||||
id: number,
|
||||
status: DeviceStatus,
|
||||
): ResultAsync<Device, Err> {
|
||||
return this.repo.setStatus(fctx, id, status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a device as busy for an incoming session.
|
||||
* Only succeeds if the device is currently online.
|
||||
*/
|
||||
allocate(fctx: FlowExecCtx, id: number): ResultAsync<Device, Err> {
|
||||
return this.repo.getById(fctx, id).andThen((dev) => {
|
||||
if (dev.status !== DeviceStatus.ONLINE) {
|
||||
return errAsync(deviceErrors.deviceNotAvailable(fctx, id));
|
||||
}
|
||||
return this.repo.setStatus(fctx, id, DeviceStatus.BUSY);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Release a device back to online after a session ends.
|
||||
*/
|
||||
release(fctx: FlowExecCtx, id: number): ResultAsync<Device, Err> {
|
||||
return this.repo.setStatus(fctx, id, DeviceStatus.ONLINE);
|
||||
}
|
||||
}
|
||||
|
||||
export function getDeviceController(): DeviceController {
|
||||
return new DeviceController(new DeviceRepository(db));
|
||||
}
|
||||
48
packages/logic/domains/device/data.ts
Normal file
48
packages/logic/domains/device/data.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import * as v from "valibot";
|
||||
|
||||
export enum DeviceStatus {
|
||||
ONLINE = "online",
|
||||
OFFLINE = "offline",
|
||||
BUSY = "busy",
|
||||
ERROR = "error",
|
||||
}
|
||||
|
||||
export const deviceStatusSchema = v.picklist(["online", "offline", "busy", "error"]);
|
||||
export type DeviceStatusValue = v.InferOutput<typeof deviceStatusSchema>;
|
||||
|
||||
export const deviceSchema = v.object({
|
||||
id: v.number(),
|
||||
title: v.string(),
|
||||
version: v.string(),
|
||||
status: deviceStatusSchema,
|
||||
isActive: v.boolean(),
|
||||
containerId: v.nullable(v.string()),
|
||||
host: v.string(),
|
||||
wsPort: v.nullable(v.string()),
|
||||
createdAt: v.date(),
|
||||
updatedAt: v.date(),
|
||||
});
|
||||
export type Device = v.InferOutput<typeof deviceSchema>;
|
||||
|
||||
export const createDeviceSchema = v.object({
|
||||
title: v.pipe(v.string(), v.minLength(1)),
|
||||
version: v.pipe(v.string(), v.minLength(1)),
|
||||
host: v.pipe(v.string(), v.minLength(1)),
|
||||
containerId: v.optional(v.string()),
|
||||
wsPort: v.optional(v.string()),
|
||||
isActive: v.optional(v.boolean()),
|
||||
});
|
||||
export type CreateDevice = v.InferOutput<typeof createDeviceSchema>;
|
||||
|
||||
export const updateDeviceSchema = v.partial(
|
||||
v.object({
|
||||
title: v.string(),
|
||||
version: v.string(),
|
||||
host: v.string(),
|
||||
containerId: v.nullable(v.string()),
|
||||
wsPort: v.nullable(v.string()),
|
||||
isActive: v.boolean(),
|
||||
status: deviceStatusSchema,
|
||||
}),
|
||||
);
|
||||
export type UpdateDevice = v.InferOutput<typeof updateDeviceSchema>;
|
||||
69
packages/logic/domains/device/errors.ts
Normal file
69
packages/logic/domains/device/errors.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { FlowExecCtx } from "@/core/flow.execution.context";
|
||||
import { ERROR_CODES, type Err } from "@pkg/result";
|
||||
import { getError } from "@pkg/logger";
|
||||
|
||||
export const deviceErrors = {
|
||||
dbError: (fctx: FlowExecCtx, detail: string): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "Database operation failed",
|
||||
description: "Please try again later",
|
||||
detail,
|
||||
}),
|
||||
|
||||
deviceNotFound: (fctx: FlowExecCtx, id: number): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.NOT_FOUND,
|
||||
message: "Device not found",
|
||||
description: "The requested device does not exist",
|
||||
detail: `No device found with ID: ${id}`,
|
||||
}),
|
||||
|
||||
listFailed: (fctx: FlowExecCtx, detail: string): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "Failed to list devices",
|
||||
description: "Try again later",
|
||||
detail,
|
||||
}),
|
||||
|
||||
createFailed: (fctx: FlowExecCtx, detail: string): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "Failed to create device",
|
||||
description: "Try again later",
|
||||
detail,
|
||||
}),
|
||||
|
||||
updateFailed: (fctx: FlowExecCtx, detail: string): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "Failed to update device",
|
||||
description: "Try again later",
|
||||
detail,
|
||||
}),
|
||||
|
||||
deleteFailed: (fctx: FlowExecCtx, detail: string): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "Failed to delete device",
|
||||
description: "Try again later",
|
||||
detail,
|
||||
}),
|
||||
|
||||
deviceNotAvailable: (fctx: FlowExecCtx, id: number): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.NOT_ALLOWED,
|
||||
message: "Device is not available",
|
||||
description: "The device is currently busy or offline",
|
||||
detail: `Device ${id} cannot be allocated in its current state`,
|
||||
actionable: true,
|
||||
}),
|
||||
};
|
||||
137
packages/logic/domains/device/repository.ts
Normal file
137
packages/logic/domains/device/repository.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
import { ResultAsync, errAsync, okAsync } from "neverthrow";
|
||||
import { FlowExecCtx } from "@core/flow.execution.context";
|
||||
import { Database, asc, eq } from "@pkg/db";
|
||||
import { device } from "@pkg/db/schema";
|
||||
import { type Err } from "@pkg/result";
|
||||
import { logger } from "@pkg/logger";
|
||||
import { traceResultAsync } from "@core/observability";
|
||||
import { CreateDevice, Device, DeviceStatus, UpdateDevice } from "./data";
|
||||
import { deviceErrors } from "./errors";
|
||||
|
||||
export class DeviceRepository {
|
||||
constructor(private db: Database) {}
|
||||
|
||||
list(fctx: FlowExecCtx): ResultAsync<Device[], Err> {
|
||||
return traceResultAsync({
|
||||
name: "device.list",
|
||||
fctx,
|
||||
fn: () =>
|
||||
ResultAsync.fromPromise(
|
||||
this.db.select().from(device).orderBy(asc(device.createdAt)),
|
||||
(e) =>
|
||||
deviceErrors.listFailed(
|
||||
fctx,
|
||||
e instanceof Error ? e.message : String(e),
|
||||
),
|
||||
).map((rows) => rows as Device[]),
|
||||
});
|
||||
}
|
||||
|
||||
getById(fctx: FlowExecCtx, id: number): ResultAsync<Device, Err> {
|
||||
return traceResultAsync({
|
||||
name: "device.getById",
|
||||
fctx,
|
||||
fn: () =>
|
||||
ResultAsync.fromPromise(
|
||||
this.db.query.device.findFirst({ where: eq(device.id, id) }),
|
||||
(e) =>
|
||||
deviceErrors.dbError(
|
||||
fctx,
|
||||
e instanceof Error ? e.message : String(e),
|
||||
),
|
||||
).andThen((row) => {
|
||||
if (!row) return errAsync(deviceErrors.deviceNotFound(fctx, id));
|
||||
return okAsync(row as Device);
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
create(fctx: FlowExecCtx, data: CreateDevice): ResultAsync<Device, Err> {
|
||||
logger.info("Creating device", { ...fctx, host: data.host });
|
||||
|
||||
return traceResultAsync({
|
||||
name: "device.create",
|
||||
fctx,
|
||||
fn: () =>
|
||||
ResultAsync.fromPromise(
|
||||
this.db
|
||||
.insert(device)
|
||||
.values({
|
||||
title: data.title,
|
||||
version: data.version,
|
||||
host: data.host,
|
||||
containerId: data.containerId ?? null,
|
||||
wsPort: data.wsPort ?? null,
|
||||
status: DeviceStatus.OFFLINE,
|
||||
isActive: data.isActive ?? false,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.returning()
|
||||
.execute(),
|
||||
(e) =>
|
||||
deviceErrors.createFailed(
|
||||
fctx,
|
||||
e instanceof Error ? e.message : String(e),
|
||||
),
|
||||
).map((rows) => rows[0] as Device),
|
||||
});
|
||||
}
|
||||
|
||||
update(
|
||||
fctx: FlowExecCtx,
|
||||
id: number,
|
||||
updates: UpdateDevice,
|
||||
): ResultAsync<Device, Err> {
|
||||
return traceResultAsync({
|
||||
name: "device.update",
|
||||
fctx,
|
||||
fn: () =>
|
||||
this.getById(fctx, id).andThen(() =>
|
||||
ResultAsync.fromPromise(
|
||||
this.db
|
||||
.update(device)
|
||||
.set({ ...updates, updatedAt: new Date() })
|
||||
.where(eq(device.id, id))
|
||||
.returning()
|
||||
.execute(),
|
||||
(e) =>
|
||||
deviceErrors.updateFailed(
|
||||
fctx,
|
||||
e instanceof Error ? e.message : String(e),
|
||||
),
|
||||
).andThen((rows) => {
|
||||
if (!rows[0])
|
||||
return errAsync(deviceErrors.deviceNotFound(fctx, id));
|
||||
return okAsync(rows[0] as Device);
|
||||
}),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
delete(fctx: FlowExecCtx, id: number): ResultAsync<boolean, Err> {
|
||||
return traceResultAsync({
|
||||
name: "device.delete",
|
||||
fctx,
|
||||
fn: () =>
|
||||
this.getById(fctx, id).andThen(() =>
|
||||
ResultAsync.fromPromise(
|
||||
this.db.delete(device).where(eq(device.id, id)).execute(),
|
||||
(e) =>
|
||||
deviceErrors.deleteFailed(
|
||||
fctx,
|
||||
e instanceof Error ? e.message : String(e),
|
||||
),
|
||||
).map(() => true),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
setStatus(
|
||||
fctx: FlowExecCtx,
|
||||
id: number,
|
||||
status: DeviceStatus,
|
||||
): ResultAsync<Device, Err> {
|
||||
return this.update(fctx, id, { status });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user