mirror of
https://github.com/certimate-go/certimate.git
synced 2026-06-22 21:05:48 +08:00
refactor: clean code
This commit is contained in:
parent
16c0ef01d2
commit
2e6f1e886e
@ -13,12 +13,7 @@ import Empty from "@/components/Empty";
|
||||
import Show from "@/components/Show";
|
||||
import { CERTIFICATE_SOURCES, type CertificateModel } from "@/domain/certificate";
|
||||
import { useAppSettings } from "@/hooks";
|
||||
import {
|
||||
get as getCertificate,
|
||||
list as listCertificates,
|
||||
type ListRequest as listCertificatesRequest,
|
||||
remove as removeCertificate,
|
||||
} from "@/repository/certificate";
|
||||
import { get as getCertificate, list as listCertificates, remove as removeCertificate } from "@/repository/certificate";
|
||||
import { getErrMsg } from "@/utils/error";
|
||||
|
||||
const CertificateList = () => {
|
||||
@ -244,7 +239,7 @@ const CertificateList = () => {
|
||||
|
||||
return listCertificates({
|
||||
keyword: filters["keyword"] as string,
|
||||
state: filters["state"] as listCertificatesRequest["state"],
|
||||
state: filters["state"] as Parameters<typeof listCertificates>[0]["state"],
|
||||
sort: sort,
|
||||
page: page,
|
||||
perPage: pageSize,
|
||||
|
||||
@ -3,12 +3,14 @@ import dayjs from "dayjs";
|
||||
import { type AccessModel } from "@/domain/access";
|
||||
import { COLLECTION_NAME_ACCESS, getPocketBase } from "./_pocketbase";
|
||||
|
||||
const _commonFields = ["id", "name", "provider", "reserve", "created", "updated", "deleted"];
|
||||
|
||||
export const list = async () => {
|
||||
const list = await getPocketBase()
|
||||
.collection(COLLECTION_NAME_ACCESS)
|
||||
.getFullList<AccessModel>({
|
||||
batch: 65535,
|
||||
fields: ["id", "name", "provider", "reserve", "created", "updated", "deleted"].join(","),
|
||||
fields: [..._commonFields].join(","),
|
||||
filter: "deleted=null",
|
||||
sort: "-created",
|
||||
requestKey: null,
|
||||
|
||||
@ -3,54 +3,53 @@ import dayjs from "dayjs";
|
||||
import { type CertificateModel } from "@/domain/certificate";
|
||||
import { COLLECTION_NAME_CERTIFICATE, getPocketBase } from "./_pocketbase";
|
||||
|
||||
export type ListRequest = {
|
||||
const _commonFields = [
|
||||
"id",
|
||||
"source",
|
||||
"subjectAltNames",
|
||||
"serialNumber",
|
||||
"issuerOrg",
|
||||
"keyAlgorithm",
|
||||
"validityNotBefore",
|
||||
"validityNotAfter",
|
||||
"validityInterval",
|
||||
"isRenewed",
|
||||
"isRevoked",
|
||||
"workflowRef",
|
||||
"created",
|
||||
"updated",
|
||||
"deleted",
|
||||
];
|
||||
const _expandFields = ["expand.workflowRef.id", "expand.workflowRef.name", "expand.workflowRef.description"];
|
||||
|
||||
export const list = async ({
|
||||
keyword,
|
||||
state,
|
||||
sort = "-created",
|
||||
page = 1,
|
||||
perPage = 10,
|
||||
}: {
|
||||
keyword?: string;
|
||||
state?: "expiringSoon" | "expired";
|
||||
sort?: string;
|
||||
page?: number;
|
||||
perPage?: number;
|
||||
};
|
||||
|
||||
export const list = async (request: ListRequest) => {
|
||||
}) => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
const filters: string[] = ["deleted=null"];
|
||||
if (request.keyword) {
|
||||
filters.push(pb.filter("(id={:keyword} || serialNumber={:keyword} || subjectAltNames~{:keyword})", { keyword: request.keyword }));
|
||||
if (keyword) {
|
||||
filters.push(pb.filter("(id={:keyword} || serialNumber={:keyword} || subjectAltNames~{:keyword})", { keyword: keyword }));
|
||||
}
|
||||
if (request.state === "expiringSoon") {
|
||||
if (state === "expiringSoon") {
|
||||
filters.push(pb.filter("validityNotAfter<{:expiredAt} && validityNotAfter>@now", { expiredAt: dayjs().add(20, "d").toDate() }));
|
||||
} else if (request.state === "expired") {
|
||||
} else if (state === "expired") {
|
||||
filters.push(pb.filter("validityNotAfter<={:expiredAt}", { expiredAt: new Date() }));
|
||||
}
|
||||
|
||||
const sort = request.sort || "-created";
|
||||
|
||||
const page = request.page || 1;
|
||||
const perPage = request.perPage || 10;
|
||||
|
||||
return pb.collection(COLLECTION_NAME_CERTIFICATE).getList<CertificateModel>(page, perPage, {
|
||||
expand: ["workflowRef"].join(","),
|
||||
fields: [
|
||||
"id",
|
||||
"source",
|
||||
"subjectAltNames",
|
||||
"serialNumber",
|
||||
"issuerOrg",
|
||||
"keyAlgorithm",
|
||||
"validityNotBefore",
|
||||
"validityNotAfter",
|
||||
"validityInterval",
|
||||
"isRenewed",
|
||||
"isRevoked",
|
||||
"workflowRef",
|
||||
"created",
|
||||
"updated",
|
||||
"deleted",
|
||||
"expand.workflowRef.id",
|
||||
"expand.workflowRef.name",
|
||||
"expand.workflowRef.description",
|
||||
].join(","),
|
||||
fields: [..._commonFields, ..._expandFields].join(","),
|
||||
filter: filters.join(" && "),
|
||||
sort: sort,
|
||||
requestKey: null,
|
||||
@ -62,26 +61,7 @@ export const listByWorkflowRunId = async (workflowRunId: string) => {
|
||||
|
||||
const list = await pb.collection(COLLECTION_NAME_CERTIFICATE).getFullList<CertificateModel>({
|
||||
batch: 65535,
|
||||
fields: [
|
||||
"id",
|
||||
"source",
|
||||
"subjectAltNames",
|
||||
"serialNumber",
|
||||
"issuerOrg",
|
||||
"keyAlgorithm",
|
||||
"validityNotBefore",
|
||||
"validityNotAfter",
|
||||
"validityInterval",
|
||||
"isRenewed",
|
||||
"isRevoked",
|
||||
"workflowRef",
|
||||
"created",
|
||||
"updated",
|
||||
"deleted",
|
||||
"expand.workflowRef.id",
|
||||
"expand.workflowRef.name",
|
||||
"expand.workflowRef.description",
|
||||
].join(","),
|
||||
fields: [..._commonFields, ..._expandFields].join(","),
|
||||
filter: pb.filter("workflowRunRef={:workflowRunId}", { workflowRunId }),
|
||||
sort: "created",
|
||||
requestKey: null,
|
||||
@ -98,7 +78,7 @@ export const get = async (id: string) => {
|
||||
.collection(COLLECTION_NAME_CERTIFICATE)
|
||||
.getOne<CertificateModel>(id, {
|
||||
expand: ["workflowRef"].join(","),
|
||||
fields: ["*", "expand.workflowRef.id", "expand.workflowRef.name", "expand.workflowRef.description"].join(","),
|
||||
fields: ["*", ..._expandFields].join(","),
|
||||
requestKey: null,
|
||||
});
|
||||
};
|
||||
|
||||
@ -3,55 +3,59 @@ import { type RecordSubscription } from "pocketbase";
|
||||
import { type WorkflowModel } from "@/domain/workflow";
|
||||
import { COLLECTION_NAME_WORKFLOW, getPocketBase } from "./_pocketbase";
|
||||
|
||||
export type ListRequest = {
|
||||
const _commonFields = [
|
||||
"id",
|
||||
"name",
|
||||
"description",
|
||||
"trigger",
|
||||
"triggerCron",
|
||||
"enabled",
|
||||
"hasDraft",
|
||||
"hasContent",
|
||||
"lastRunRef",
|
||||
"lastRunStatus",
|
||||
"lastRunTime",
|
||||
"created",
|
||||
"updated",
|
||||
"deleted",
|
||||
];
|
||||
const _expandFields = [
|
||||
"expand.lastRunRef.id",
|
||||
"expand.lastRunRef.status",
|
||||
"expand.lastRunRef.trigger",
|
||||
"expand.lastRunRef.startedAt",
|
||||
"expand.lastRunRef.endedAt",
|
||||
"expand.lastRunRef.error",
|
||||
];
|
||||
|
||||
export const list = async ({
|
||||
keyword,
|
||||
enabled,
|
||||
sort = "-created",
|
||||
page = 1,
|
||||
perPage = 10,
|
||||
expand = false,
|
||||
}: {
|
||||
keyword?: string;
|
||||
enabled?: boolean;
|
||||
sort?: string;
|
||||
page?: number;
|
||||
perPage?: number;
|
||||
expand?: boolean;
|
||||
};
|
||||
|
||||
export const list = async (request: ListRequest) => {
|
||||
}) => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
const filters: string[] = [];
|
||||
if (request.keyword) {
|
||||
filters.push(pb.filter("(id={:keyword} || name~{:keyword})", { keyword: request.keyword }));
|
||||
if (keyword) {
|
||||
filters.push(pb.filter("(id={:keyword} || name~{:keyword})", { keyword: keyword }));
|
||||
}
|
||||
if (request.enabled != null) {
|
||||
filters.push(pb.filter("enabled={:enabled}", { enabled: request.enabled }));
|
||||
if (enabled != null) {
|
||||
filters.push(pb.filter("enabled={:enabled}", { enabled: enabled }));
|
||||
}
|
||||
|
||||
const sort = request.sort || "-created";
|
||||
|
||||
const page = request.page || 1;
|
||||
const perPage = request.perPage || 10;
|
||||
|
||||
return await pb.collection(COLLECTION_NAME_WORKFLOW).getList<WorkflowModel>(page, perPage, {
|
||||
expand: request.expand ? ["lastRunRef"].join(",") : void 0,
|
||||
fields: [
|
||||
"id",
|
||||
"name",
|
||||
"description",
|
||||
"trigger",
|
||||
"triggerCron",
|
||||
"enabled",
|
||||
"hasDraft",
|
||||
"hasContent",
|
||||
"lastRunRef",
|
||||
"lastRunStatus",
|
||||
"lastRunTime",
|
||||
"created",
|
||||
"updated",
|
||||
"deleted",
|
||||
"expand.lastRunRef.id",
|
||||
"expand.lastRunRef.status",
|
||||
"expand.lastRunRef.trigger",
|
||||
"expand.lastRunRef.startedAt",
|
||||
"expand.lastRunRef.endedAt",
|
||||
"expand.lastRunRef.error",
|
||||
].join(","),
|
||||
expand: expand ? ["lastRunRef"].join(",") : void 0,
|
||||
fields: [..._commonFields, ..._expandFields].join(","),
|
||||
filter: filters.join(" && "),
|
||||
sort: sort,
|
||||
requestKey: null,
|
||||
@ -63,15 +67,7 @@ export const get = async (id: string) => {
|
||||
.collection(COLLECTION_NAME_WORKFLOW)
|
||||
.getOne<WorkflowModel>(id, {
|
||||
expand: ["lastRunRef"].join(","),
|
||||
fields: [
|
||||
"*",
|
||||
"expand.lastRunRef.id",
|
||||
"expand.lastRunRef.status",
|
||||
"expand.lastRunRef.trigger",
|
||||
"expand.lastRunRef.startedAt",
|
||||
"expand.lastRunRef.endedAt",
|
||||
"expand.lastRunRef.error",
|
||||
].join(","),
|
||||
fields: ["*", ..._expandFields].join(","),
|
||||
requestKey: null,
|
||||
});
|
||||
};
|
||||
|
||||
@ -4,39 +4,30 @@ import { type WorkflowRunModel } from "@/domain/workflowRun";
|
||||
|
||||
import { COLLECTION_NAME_WORKFLOW_RUN, getPocketBase } from "./_pocketbase";
|
||||
|
||||
export type ListRequest = {
|
||||
const _commonFields = ["id", "status", "trigger", "startedAt", "endedAt", "error", "created", "updated", "deleted"];
|
||||
const _expandFields = ["expand.workflowRef.id", "expand.workflowRef.name", "expand.workflowRef.description"];
|
||||
|
||||
export const list = async ({
|
||||
workflowId,
|
||||
page = 1,
|
||||
perPage = 10,
|
||||
expand = false,
|
||||
}: {
|
||||
workflowId?: string;
|
||||
page?: number;
|
||||
perPage?: number;
|
||||
expand?: boolean;
|
||||
};
|
||||
|
||||
export const list = async (request: ListRequest) => {
|
||||
}) => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
const filters: string[] = [];
|
||||
if (request.workflowId) {
|
||||
filters.push(pb.filter("workflowRef={:workflowId}", { workflowId: request.workflowId }));
|
||||
if (workflowId) {
|
||||
filters.push(pb.filter("workflowRef={:workflowId}", { workflowId: workflowId }));
|
||||
}
|
||||
|
||||
const page = request.page || 1;
|
||||
const perPage = request.perPage || 10;
|
||||
return await pb.collection(COLLECTION_NAME_WORKFLOW_RUN).getList<WorkflowRunModel>(page, perPage, {
|
||||
expand: request.expand ? ["workflowRef"].join(",") : void 0,
|
||||
fields: [
|
||||
"id",
|
||||
"status",
|
||||
"trigger",
|
||||
"startedAt",
|
||||
"endedAt",
|
||||
"error",
|
||||
"created",
|
||||
"updated",
|
||||
"deleted",
|
||||
"expand.workflowRef.id",
|
||||
"expand.workflowRef.name",
|
||||
"expand.workflowRef.description",
|
||||
].join(","),
|
||||
expand: expand ? ["workflowRef"].join(",") : void 0,
|
||||
fields: [..._commonFields, ..._expandFields].join(","),
|
||||
filter: filters.join(" && "),
|
||||
sort: "-created",
|
||||
requestKey: null,
|
||||
@ -48,7 +39,7 @@ export const get = async (id: string) => {
|
||||
.collection(COLLECTION_NAME_WORKFLOW_RUN)
|
||||
.getOne<WorkflowRunModel>(id, {
|
||||
expand: ["workflowRef"].join(","),
|
||||
fields: ["*", "expand.workflowRef.id", "expand.workflowRef.name", "expand.workflowRef.description"].join(","),
|
||||
fields: ["*", ..._expandFields].join(","),
|
||||
requestKey: null,
|
||||
});
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user