refactor(api): narrow import-export procedure contexts
This commit is contained in:
@@ -4,8 +4,10 @@ import { z } from "zod";
|
|||||||
import type { TRPCContext } from "../trpc.js";
|
import type { TRPCContext } from "../trpc.js";
|
||||||
import { requirePermission } from "../trpc.js";
|
import { requirePermission } from "../trpc.js";
|
||||||
|
|
||||||
type ImportExportProcedureContext = Pick<TRPCContext, "db"> & {
|
type ImportExportReadContext = Pick<TRPCContext, "db">;
|
||||||
permissions?: Set<PermissionKey>;
|
|
||||||
|
type ImportExportMutationContext = ImportExportReadContext & {
|
||||||
|
permissions: Set<PermissionKey>;
|
||||||
};
|
};
|
||||||
|
|
||||||
type ImportRow = Record<string, string>;
|
type ImportRow = Record<string, string>;
|
||||||
@@ -33,7 +35,7 @@ function buildCsv(headers: unknown[], rows: unknown[][]) {
|
|||||||
return [headers.map(escapeCsvValue).join(","), ...rows.map((row) => row.map(escapeCsvValue).join(","))].join("\n");
|
return [headers.map(escapeCsvValue).join(","), ...rows.map((row) => row.map(escapeCsvValue).join(","))].join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function exportResourcesCsv(ctx: ImportExportProcedureContext) {
|
export async function exportResourcesCsv(ctx: ImportExportReadContext) {
|
||||||
const [resources, globalBlueprints] = await Promise.all([
|
const [resources, globalBlueprints] = await Promise.all([
|
||||||
ctx.db.resource.findMany({
|
ctx.db.resource.findMany({
|
||||||
where: { isActive: true },
|
where: { isActive: true },
|
||||||
@@ -78,7 +80,7 @@ export async function exportResourcesCsv(ctx: ImportExportProcedureContext) {
|
|||||||
return buildCsv(headers, rows);
|
return buildCsv(headers, rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function exportProjectsCsv(ctx: ImportExportProcedureContext) {
|
export async function exportProjectsCsv(ctx: ImportExportReadContext) {
|
||||||
const [projects, globalBlueprints] = await Promise.all([
|
const [projects, globalBlueprints] = await Promise.all([
|
||||||
ctx.db.project.findMany({ orderBy: { shortCode: "asc" } }),
|
ctx.db.project.findMany({ orderBy: { shortCode: "asc" } }),
|
||||||
ctx.db.blueprint.findMany({
|
ctx.db.blueprint.findMany({
|
||||||
@@ -120,7 +122,7 @@ export async function exportProjectsCsv(ctx: ImportExportProcedureContext) {
|
|||||||
return buildCsv(headers, rows);
|
return buildCsv(headers, rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function importResourceRow(ctx: ImportExportProcedureContext, row: ImportRow) {
|
async function importResourceRow(ctx: ImportExportMutationContext, row: ImportRow) {
|
||||||
const existing = await ctx.db.resource.findFirst({
|
const existing = await ctx.db.resource.findFirst({
|
||||||
where: { eid: row["eid"] ?? "" },
|
where: { eid: row["eid"] ?? "" },
|
||||||
});
|
});
|
||||||
@@ -142,11 +144,8 @@ async function importResourceRow(ctx: ImportExportProcedureContext, row: ImportR
|
|||||||
return { updated: true, error: null };
|
return { updated: true, error: null };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function importCsv(ctx: ImportExportProcedureContext, input: ImportCsvInput) {
|
export async function importCsv(ctx: ImportExportMutationContext, input: ImportCsvInput) {
|
||||||
requirePermission(
|
requirePermission(ctx, PermissionKey.IMPORT_DATA);
|
||||||
{ permissions: ctx.permissions ?? new Set<PermissionKey>() },
|
|
||||||
PermissionKey.IMPORT_DATA,
|
|
||||||
);
|
|
||||||
|
|
||||||
const results = {
|
const results = {
|
||||||
total: input.rows.length,
|
total: input.rows.length,
|
||||||
|
|||||||
Reference in New Issue
Block a user