feat: add bulk import endpoint
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user