Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+127
@@ -0,0 +1,127 @@
|
||||
import { FormulaComponentLog, FormulaLog, Prisma } from "@toolctrl/db/prisma-browser";
|
||||
import { FiltersDto } from "~/application/dtos/data/FiltersDto";
|
||||
import { db } from "~/utils/db.server";
|
||||
|
||||
export type FormulaLogWithDetails = FormulaLog & {
|
||||
formula: { id: string; name: string };
|
||||
user: { id: string; email: string } | null;
|
||||
tenant: { id: string; name: string } | null;
|
||||
components: FormulaComponentLog[];
|
||||
};
|
||||
export async function getFormulaLogs({
|
||||
id,
|
||||
pagination,
|
||||
filters,
|
||||
}: {
|
||||
id: string | undefined;
|
||||
pagination: { pageSize: number; page: number };
|
||||
filters: FiltersDto;
|
||||
}): Promise<{ items: FormulaLogWithDetails[]; total: number }> {
|
||||
let where: Prisma.FormulaLogWhereInput = {
|
||||
formulaId: id,
|
||||
};
|
||||
const statusFilter = filters.properties.find((filter) => filter.name === "status")?.value;
|
||||
if (statusFilter) {
|
||||
if (statusFilter === "success") {
|
||||
where = { ...where, error: null, result: { not: "" } };
|
||||
} else if (statusFilter === "error") {
|
||||
where = { ...where, error: { not: null } };
|
||||
} else if (statusFilter === "empty") {
|
||||
where = { ...where, result: "", error: null };
|
||||
}
|
||||
}
|
||||
const hasRowIdFilter = filters.properties.find((filter) => filter.name === "hasRowId")?.value;
|
||||
if (hasRowIdFilter) {
|
||||
where = { ...where, components: { some: { rowId: hasRowIdFilter } } };
|
||||
}
|
||||
filters.properties
|
||||
.filter((f) => !f.manual && f.value)
|
||||
.forEach((filter) => {
|
||||
where = { ...where, [filter.name]: filter.value };
|
||||
});
|
||||
const items = await db.formulaLog.findMany({
|
||||
take: pagination.pageSize,
|
||||
skip: pagination.pageSize * (pagination.page - 1),
|
||||
where,
|
||||
include: {
|
||||
formula: { select: { id: true, name: true } },
|
||||
user: { select: { id: true, email: true } },
|
||||
tenant: { select: { id: true, name: true } },
|
||||
components: true,
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
const total = await db.formulaLog.count({ where });
|
||||
return {
|
||||
items,
|
||||
total,
|
||||
};
|
||||
}
|
||||
|
||||
export async function countLogs(ids: string[]): Promise<{ formulaId: string; count: number }[]> {
|
||||
return (
|
||||
await db.formulaLog.groupBy({
|
||||
by: ["formulaId"],
|
||||
where: { formulaId: { in: ids } },
|
||||
_count: { formulaId: true },
|
||||
})
|
||||
).map((item) => ({ formulaId: item.formulaId, count: item._count.formulaId }));
|
||||
}
|
||||
|
||||
export async function createFormulaLog(data: {
|
||||
formulaId: string;
|
||||
userId: string | null;
|
||||
tenantId: string | null;
|
||||
originalTrigger: string;
|
||||
triggeredBy: string;
|
||||
expresion: string;
|
||||
result: string;
|
||||
error: string | null;
|
||||
rowValueId: string | null;
|
||||
components: {
|
||||
order: number;
|
||||
type: string;
|
||||
value: string;
|
||||
rowId: string | null;
|
||||
}[];
|
||||
}) {
|
||||
return await db.formulaLog.create({
|
||||
data: {
|
||||
formulaId: data.formulaId,
|
||||
userId: data.userId,
|
||||
tenantId: data.tenantId,
|
||||
originalTrigger: data.originalTrigger,
|
||||
triggeredBy: data.triggeredBy,
|
||||
expression: data.expresion,
|
||||
duration: 0,
|
||||
result: data.result,
|
||||
error: data.error,
|
||||
rowValueId: data.rowValueId,
|
||||
components: {
|
||||
create: data.components.map((component) => ({
|
||||
order: component.order,
|
||||
type: component.type,
|
||||
value: component.value,
|
||||
rowId: component.rowId,
|
||||
})),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateFormulaLog(
|
||||
id: string,
|
||||
data: {
|
||||
duration: number;
|
||||
result: string;
|
||||
error: string | null;
|
||||
}
|
||||
) {
|
||||
return await db.formulaLog.update({
|
||||
where: { id },
|
||||
data: {
|
||||
result: data.result,
|
||||
error: data.error,
|
||||
},
|
||||
});
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
import { Entity, Formula, FormulaComponent, Prisma, Property } from "@toolctrl/db/prisma-browser";
|
||||
import { db } from "~/utils/db.server";
|
||||
|
||||
export type FormulaWithDetails = Formula & {
|
||||
components: FormulaComponent[];
|
||||
inProperties: (Property & { entity: Entity })[];
|
||||
};
|
||||
|
||||
export async function getAllFormulas(): Promise<FormulaWithDetails[]> {
|
||||
return await db.formula.findMany({
|
||||
include: {
|
||||
components: { orderBy: { order: "asc" } },
|
||||
inProperties: { include: { entity: true } },
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAllFormulasIdsAndNames(): Promise<{ id: string; name: string }[]> {
|
||||
return await db.formula.findMany({
|
||||
select: { id: true, name: true },
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAllFormulasInIds(ids: string[]): Promise<FormulaWithDetails[]> {
|
||||
return await db.formula.findMany({
|
||||
where: { id: { in: ids } },
|
||||
include: {
|
||||
components: { orderBy: { order: "asc" } },
|
||||
inProperties: { include: { entity: true } },
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getFormula(id: string): Promise<FormulaWithDetails | null> {
|
||||
return await db.formula.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
components: { orderBy: { order: "asc" } },
|
||||
inProperties: { include: { entity: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getFormulaWithLogs(id: string): Promise<FormulaWithDetails | null> {
|
||||
return await db.formula.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
components: { orderBy: { order: "asc" } },
|
||||
inProperties: { include: { entity: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export type CreateFormulaDto = {
|
||||
name: string;
|
||||
description: string | null;
|
||||
resultAs: string;
|
||||
calculationTrigger: string;
|
||||
withLogs: boolean;
|
||||
components: {
|
||||
order: number;
|
||||
type: string;
|
||||
value: string;
|
||||
}[];
|
||||
}
|
||||
export async function createFormula(data: CreateFormulaDto): Promise<Formula> {
|
||||
return await db.formula.create({
|
||||
data: {
|
||||
name: data.name,
|
||||
description: data.description,
|
||||
resultAs: data.resultAs,
|
||||
calculationTrigger: data.calculationTrigger,
|
||||
withLogs: data.withLogs,
|
||||
components: {
|
||||
create: data.components.map((component) => ({
|
||||
order: component.order,
|
||||
type: component.type,
|
||||
value: component.value,
|
||||
})),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateFormula(
|
||||
id: string,
|
||||
data: {
|
||||
name?: string;
|
||||
description?: string | null;
|
||||
resultAs?: string;
|
||||
calculationTrigger?: string;
|
||||
withLogs?: boolean;
|
||||
components?: {
|
||||
order: number;
|
||||
type: string;
|
||||
value: string;
|
||||
}[];
|
||||
}
|
||||
): Promise<Formula> {
|
||||
const update: Prisma.FormulaUpdateInput = {
|
||||
name: data.name,
|
||||
description: data.description,
|
||||
resultAs: data.resultAs,
|
||||
calculationTrigger: data.calculationTrigger,
|
||||
withLogs: data.withLogs,
|
||||
};
|
||||
if (data.components) {
|
||||
update.components = {
|
||||
deleteMany: {},
|
||||
create: data.components,
|
||||
};
|
||||
}
|
||||
return await db.formula.update({
|
||||
where: { id },
|
||||
data: update,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteFormula(id: string): Promise<Formula> {
|
||||
return await db.formula.delete({ where: { id } });
|
||||
}
|
||||
Reference in new issue
Block a user