supported apps domain + some refactor of data types redundancy
This commit is contained in:
45
packages/logic/domains/supported-app/controller.ts
Normal file
45
packages/logic/domains/supported-app/controller.ts
Normal 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));
|
||||
}
|
||||
28
packages/logic/domains/supported-app/data.ts
Normal file
28
packages/logic/domains/supported-app/data.ts
Normal 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
|
||||
>;
|
||||
59
packages/logic/domains/supported-app/errors.ts
Normal file
59
packages/logic/domains/supported-app/errors.ts
Normal 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,
|
||||
}),
|
||||
};
|
||||
149
packages/logic/domains/supported-app/repository.ts
Normal file
149
packages/logic/domains/supported-app/repository.ts
Normal 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),
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user