feat: add bulk import endpoint

This commit is contained in:
2025-11-21 16:55:01 +05:30
parent bb51d62bed
commit f1b6f48a51
6 changed files with 148 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { CreatePermitInput, UpdatePermitInput } from "./permit.schema";
import {
bulkImport,
createPermit,
deletePermit,
getPermit,
@@ -10,6 +11,7 @@ import {
updatePermit,
} from "./permit.service";
import { PageQueryParams } from "../pagination";
import { csv2json } from "json-2-csv";
export async function createPermitHandler(
req: FastifyRequest,
@@ -117,3 +119,20 @@ export async function searchPermitByAddressHandler(
return err;
}
}
export async function bulkImportHandler(
req: FastifyRequest,
res: FastifyReply
) {
try {
const data = await req.file();
const csvString = (await data.toBuffer()).toString("utf-8");
const parsedCSV = csv2json(csvString, { delimiter: { eol: "\r\n" } });
const result = await bulkImport(parsedCSV, req.user);
return res.code(200).send(result);
} catch (err) {
return err;
}
}