supported apps domain + some refactor of data types redundancy
This commit is contained in:
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user