supported apps domain + some refactor of data types redundancy

This commit is contained in:
user
2026-03-28 16:19:24 +02:00
parent 6639bcd799
commit 671a712b08
26 changed files with 2052 additions and 169 deletions

View File

@@ -3,18 +3,24 @@ import { nanoid } from "nanoid";
import { db } from "@pkg/db";
import { type Err } from "@pkg/result";
import { FlowExecCtx } from "@core/flow.execution.context";
import { CreateLink, Link, LinkStatus, LinkWithDevice, UpdateLink } from "./data";
import {
CreateLink,
Link,
LinkStatus,
LinkWithDevice,
UpdateLink,
} from "./data";
import { LinkRepository } from "./repository";
import { linkErrors } from "./errors";
export class LinkController {
constructor(private repo: LinkRepository) {}
list(fctx: FlowExecCtx): ResultAsync<Link[], Err> {
list(fctx: FlowExecCtx): ResultAsync<LinkWithDevice[], Err> {
return this.repo.list(fctx);
}
getById(fctx: FlowExecCtx, id: number): ResultAsync<Link, Err> {
getById(fctx: FlowExecCtx, id: number): ResultAsync<LinkWithDevice, Err> {
return this.repo.getById(fctx, id);
}

View File

@@ -1,5 +1,6 @@
import * as v from "valibot";
import { deviceSchema } from "@domains/device/data";
import { supportedAppSchema } from "@domains/supported-app/data";
export enum LinkStatus {
ACTIVE = "active",
@@ -15,9 +16,8 @@ export const linkSchema = v.object({
id: v.number(),
token: v.string(),
status: linkStatusSchema,
appName: v.string(),
appPackage: v.string(),
linkedDeviceId: v.nullable(v.number()),
supportedAppId: v.number(),
expiresAt: v.nullable(v.date()),
lastAccessedAt: v.nullable(v.date()),
createdAt: v.date(),
@@ -28,14 +28,14 @@ export type Link = v.InferOutput<typeof linkSchema>;
export const linkWithDeviceSchema = v.object({
...linkSchema.entries,
device: v.nullable(deviceSchema),
supportedApp: v.nullable(supportedAppSchema),
});
export type LinkWithDevice = v.InferOutput<typeof linkWithDeviceSchema>;
export const createLinkSchema = v.object({
token: v.pipe(v.string(), v.minLength(1)),
appName: v.pipe(v.string(), v.minLength(1)),
appPackage: v.pipe(v.string(), v.minLength(1)),
linkedDeviceId: v.number(),
supportedAppId: v.number(),
expiresAt: v.optional(v.nullable(v.date())),
});
export type CreateLink = v.InferOutput<typeof createLinkSchema>;
@@ -43,9 +43,8 @@ export type CreateLink = v.InferOutput<typeof createLinkSchema>;
export const updateLinkSchema = v.partial(
v.object({
status: linkStatusSchema,
appName: v.string(),
appPackage: v.string(),
linkedDeviceId: v.nullable(v.number()),
supportedAppId: v.number(),
expiresAt: v.nullable(v.date()),
lastAccessedAt: v.nullable(v.date()),
}),

View File

@@ -1,6 +1,6 @@
import { ResultAsync, errAsync, okAsync } from "neverthrow";
import { FlowExecCtx } from "@core/flow.execution.context";
import { Database, asc, eq } from "@pkg/db";
import { Database, eq } from "@pkg/db";
import { link } from "@pkg/db/schema";
import { type Err } from "@pkg/result";
import { logger } from "@pkg/logger";
@@ -11,29 +11,41 @@ import { linkErrors } from "./errors";
export class LinkRepository {
constructor(private db: Database) {}
list(fctx: FlowExecCtx): ResultAsync<Link[], Err> {
list(fctx: FlowExecCtx): ResultAsync<LinkWithDevice[], Err> {
return traceResultAsync({
name: "link.list",
fctx,
fn: () =>
ResultAsync.fromPromise(
this.db.select().from(link).orderBy(asc(link.createdAt)),
this.db.query.link.findMany({
orderBy: (link, { asc }) => [asc(link.createdAt)],
with: {
device: true,
supportedApp: true,
},
}),
(e) =>
linkErrors.listFailed(
fctx,
e instanceof Error ? e.message : String(e),
),
).map((rows) => rows as Link[]),
).map((rows) => rows as LinkWithDevice[]),
});
}
getById(fctx: FlowExecCtx, id: number): ResultAsync<Link, Err> {
getById(fctx: FlowExecCtx, id: number): ResultAsync<LinkWithDevice, Err> {
return traceResultAsync({
name: "link.getById",
fctx,
fn: () =>
ResultAsync.fromPromise(
this.db.query.link.findFirst({ where: eq(link.id, id) }),
this.db.query.link.findFirst({
where: eq(link.id, id),
with: {
device: true,
supportedApp: true,
},
}),
(e) =>
linkErrors.dbError(
fctx,
@@ -41,7 +53,7 @@ export class LinkRepository {
),
).andThen((row) => {
if (!row) return errAsync(linkErrors.linkNotFound(fctx, id));
return okAsync(row as Link);
return okAsync(row as LinkWithDevice);
}),
});
}
@@ -54,7 +66,7 @@ export class LinkRepository {
ResultAsync.fromPromise(
this.db.query.link.findFirst({
where: eq(link.token, token),
with: { device: true },
with: { device: true, supportedApp: true },
}),
(e) =>
linkErrors.dbError(
@@ -81,9 +93,8 @@ export class LinkRepository {
.values({
token: data.token,
status: "active",
appName: data.appName,
appPackage: data.appPackage,
linkedDeviceId: data.linkedDeviceId ?? null,
supportedAppId: data.supportedAppId,
expiresAt: data.expiresAt ?? null,
createdAt: new Date(),
updatedAt: new Date(),

View File

@@ -0,0 +1,45 @@
import { db } from "@pkg/db";
import { type Err } from "@pkg/result";
import { FlowExecCtx } from "@core/flow.execution.context";
import { ResultAsync } from "neverthrow";
import {
CreateSupportedApp,
SupportedApp,
UpdateSupportedApp,
} from "./data";
import { SupportedAppRepository } from "./repository";
export class SupportedAppController {
constructor(private repo: SupportedAppRepository) {}
list(fctx: FlowExecCtx): ResultAsync<SupportedApp[], Err> {
return this.repo.list(fctx);
}
getById(fctx: FlowExecCtx, id: number): ResultAsync<SupportedApp, Err> {
return this.repo.getById(fctx, id);
}
create(
fctx: FlowExecCtx,
data: CreateSupportedApp,
): ResultAsync<SupportedApp, Err> {
return this.repo.create(fctx, data);
}
update(
fctx: FlowExecCtx,
id: number,
data: UpdateSupportedApp,
): ResultAsync<SupportedApp, Err> {
return this.repo.update(fctx, id, data);
}
delete(fctx: FlowExecCtx, id: number): ResultAsync<boolean, Err> {
return this.repo.delete(fctx, id);
}
}
export function getSupportedAppController(): SupportedAppController {
return new SupportedAppController(new SupportedAppRepository(db));
}

View File

@@ -0,0 +1,28 @@
import * as v from "valibot";
export const supportedAppSchema = v.object({
id: v.number(),
title: v.string(),
packageName: v.string(),
createdAt: v.date(),
updatedAt: v.date(),
});
export type SupportedApp = v.InferOutput<typeof supportedAppSchema>;
export const createSupportedAppSchema = v.object({
title: v.pipe(v.string(), v.minLength(1)),
packageName: v.pipe(v.string(), v.minLength(1)),
});
export type CreateSupportedApp = v.InferOutput<
typeof createSupportedAppSchema
>;
export const updateSupportedAppSchema = v.partial(
v.object({
title: v.string(),
packageName: v.string(),
}),
);
export type UpdateSupportedApp = v.InferOutput<
typeof updateSupportedAppSchema
>;

View File

@@ -0,0 +1,59 @@
import { FlowExecCtx } from "@/core/flow.execution.context";
import { ERROR_CODES, type Err } from "@pkg/result";
import { getError } from "@pkg/logger";
export const supportedAppErrors = {
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,
}),
supportedAppNotFound: (fctx: FlowExecCtx, id: number): Err =>
getError({
flowId: fctx.flowId,
code: ERROR_CODES.NOT_FOUND,
message: "Supported app not found",
description: "The requested supported app does not exist",
detail: `No supported app found with ID: ${id}`,
}),
listFailed: (fctx: FlowExecCtx, detail: string): Err =>
getError({
flowId: fctx.flowId,
code: ERROR_CODES.DATABASE_ERROR,
message: "Failed to list supported apps",
description: "Try again later",
detail,
}),
createFailed: (fctx: FlowExecCtx, detail: string): Err =>
getError({
flowId: fctx.flowId,
code: ERROR_CODES.DATABASE_ERROR,
message: "Failed to create supported app",
description: "Try again later",
detail,
}),
updateFailed: (fctx: FlowExecCtx, detail: string): Err =>
getError({
flowId: fctx.flowId,
code: ERROR_CODES.DATABASE_ERROR,
message: "Failed to update supported app",
description: "Try again later",
detail,
}),
deleteFailed: (fctx: FlowExecCtx, detail: string): Err =>
getError({
flowId: fctx.flowId,
code: ERROR_CODES.DATABASE_ERROR,
message: "Failed to delete supported app",
description: "Try again later",
detail,
}),
};

View File

@@ -0,0 +1,149 @@
import { ResultAsync, errAsync, okAsync } from "neverthrow";
import { FlowExecCtx } from "@core/flow.execution.context";
import { Database, asc, eq } from "@pkg/db";
import { supportedApp } from "@pkg/db/schema";
import { type Err } from "@pkg/result";
import { logger } from "@pkg/logger";
import { traceResultAsync } from "@core/observability";
import {
CreateSupportedApp,
SupportedApp,
UpdateSupportedApp,
} from "./data";
import { supportedAppErrors } from "./errors";
export class SupportedAppRepository {
constructor(private db: Database) {}
list(fctx: FlowExecCtx): ResultAsync<SupportedApp[], Err> {
return traceResultAsync({
name: "supportedApp.list",
fctx,
fn: () =>
ResultAsync.fromPromise(
this.db
.select()
.from(supportedApp)
.orderBy(asc(supportedApp.createdAt)),
(e) =>
supportedAppErrors.listFailed(
fctx,
e instanceof Error ? e.message : String(e),
),
).map((rows) => rows as SupportedApp[]),
});
}
getById(fctx: FlowExecCtx, id: number): ResultAsync<SupportedApp, Err> {
return traceResultAsync({
name: "supportedApp.getById",
fctx,
fn: () =>
ResultAsync.fromPromise(
this.db.query.supportedApp.findFirst({
where: eq(supportedApp.id, id),
}),
(e) =>
supportedAppErrors.dbError(
fctx,
e instanceof Error ? e.message : String(e),
),
).andThen((row) => {
if (!row) {
return errAsync(
supportedAppErrors.supportedAppNotFound(fctx, id),
);
}
return okAsync(row as SupportedApp);
}),
});
}
create(
fctx: FlowExecCtx,
data: CreateSupportedApp,
): ResultAsync<SupportedApp, Err> {
logger.info("Creating supported app", { ...fctx, title: data.title });
return traceResultAsync({
name: "supportedApp.create",
fctx,
fn: () =>
ResultAsync.fromPromise(
this.db
.insert(supportedApp)
.values({
title: data.title,
packageName: data.packageName,
createdAt: new Date(),
updatedAt: new Date(),
})
.returning()
.execute(),
(e) =>
supportedAppErrors.createFailed(
fctx,
e instanceof Error ? e.message : String(e),
),
).map((rows) => rows[0] as SupportedApp),
});
}
update(
fctx: FlowExecCtx,
id: number,
updates: UpdateSupportedApp,
): ResultAsync<SupportedApp, Err> {
return traceResultAsync({
name: "supportedApp.update",
fctx,
fn: () =>
this.getById(fctx, id).andThen(() =>
ResultAsync.fromPromise(
this.db
.update(supportedApp)
.set({ ...updates, updatedAt: new Date() })
.where(eq(supportedApp.id, id))
.returning()
.execute(),
(e) =>
supportedAppErrors.updateFailed(
fctx,
e instanceof Error ? e.message : String(e),
),
).andThen((rows) => {
if (!rows[0]) {
return errAsync(
supportedAppErrors.supportedAppNotFound(
fctx,
id,
),
);
}
return okAsync(rows[0] as SupportedApp);
}),
),
});
}
delete(fctx: FlowExecCtx, id: number): ResultAsync<boolean, Err> {
return traceResultAsync({
name: "supportedApp.delete",
fctx,
fn: () =>
this.getById(fctx, id).andThen(() =>
ResultAsync.fromPromise(
this.db
.delete(supportedApp)
.where(eq(supportedApp.id, id))
.execute(),
(e) =>
supportedAppErrors.deleteFailed(
fctx,
e instanceof Error ? e.message : String(e),
),
).map(() => true),
),
});
}
}