Added bills upload & list

This commit is contained in:
Patrick Fic
2022-05-03 17:30:24 -07:00
parent 8d851c52b2
commit b7be304520
14 changed files with 226 additions and 6081 deletions

View File

@@ -0,0 +1,26 @@
import { Request, Response, NextFunction } from "express";
export default function BillRequestValidator(
req: Request,
res: Response,
next: NextFunction
) {
const vendor: string = (req.body.vendor || "").trim();
const invoice_number: string = (req.body.invoice_number || "").trim();
const ro_number: string = (req.body.ro_number || "").trim();
// if (vendor === "") {
// res.status(400).json({ error: "No vendor name has been specified." });
// return;
// }
// if (invoice_number === "") {
// res.status(400).json({ error: "No invoice number has been specified." });
// return;
// }
if (ro_number === "") {
res.status(400).json({ error: "No RO Number has been specified." });
return;
} else {
next();
}
}

50
bills/billsListMedia.ts Normal file
View File

@@ -0,0 +1,50 @@
import { Request, Response } from "express";
import fs from "fs-extra";
import path from "path";
import GenerateThumbnail from "../util/generateThumbnail";
import MediaFile from "../util/interfaces/MediaFile";
import GenerateUrl from "../util/MediaUrlGen";
import { PathToRoBillsFolder, PathToRoFolder } from "../util/pathGenerators";
import { FolderPaths } from "../util/serverInit";
/** @description Bills will use the hierarchy of PDFs stored under the Job first, and then the Bills folder. */
export async function BillsListMedia(req: Request, res: Response) {
const ro_number: string = (req.body.ro_number || "").trim();
const vendor: string = (req.body.vendor || "").trim();
const invoice_number: string = (req.body.invoice_number || "").trim();
//Ensure all directories exist.
await fs.ensureDir(PathToRoBillsFolder(ro_number));
const filesList: fs.Dirent[] = (
await fs.readdir(PathToRoBillsFolder(ro_number), {
withFileTypes: true,
})
).filter((f) => f.isFile() && !/(^|\/)\.[^\/\.]/g.test(f.name));
const ret: MediaFile[] = await Promise.all(
filesList.map(async (file) => {
const relativeThumbPath: string = await GenerateThumbnail(
path.join(PathToRoBillsFolder(ro_number), file.name)
);
return {
src: GenerateUrl([
FolderPaths.StaticPath,
FolderPaths.JobsFolder,
ro_number,
FolderPaths.BillsSubDir,
file.name,
]),
thumb: GenerateUrl([
FolderPaths.StaticPath,
FolderPaths.JobsFolder,
ro_number,
FolderPaths.BillsSubDir,
relativeThumbPath,
]),
};
})
);
res.json(ret);
}

78
bills/billsUploadMedia.ts Normal file
View File

@@ -0,0 +1,78 @@
import dotenv from "dotenv";
import { Request, Response } from "express";
import fs from "fs-extra";
import multer from "multer";
import path, { resolve } from "path";
import { logger } from "../server";
import GenerateThumbnail from "../util/generateThumbnail";
import generateUniqueFilename from "../util/generateUniqueFilename";
import {
PathToRoBillsFolder,
PathToVendorBillsFile,
} from "../util/pathGenerators";
import { BillsListMedia } from "./billsListMedia";
dotenv.config({
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`),
});
export const BillsMediaUploadMulter = multer({
storage: multer.diskStorage({
destination: function (req, file, cb) {
const ro_number: string = (req.body.ro_number || "").trim();
const DestinationFolder: string = PathToRoBillsFolder(ro_number);
fs.ensureDirSync(DestinationFolder);
cb(null, DestinationFolder);
},
filename: function (req, file, cb) {
logger.info("Uploading file: ", path.basename(file.originalname));
cb(null, generateUniqueFilename(file));
},
}),
});
export async function BillsUploadMedia(req: Request, res: Response) {
try {
if (!req.files) {
res.send({
status: false,
message: "No file uploaded",
});
} else {
const thumbnailGenerationQueue: Promise<string>[] = [];
//for each file.path, generate the thumbnail.
(req.files as Express.Multer.File[]).forEach((file) => {
thumbnailGenerationQueue.push(GenerateThumbnail(file.path));
});
await Promise.all(thumbnailGenerationQueue);
//Check to see if it should be duplicated to the vendor.
if (process.env.DUPLICATE_BILL_TO_VENDOR) {
const copyQueue: Promise<void>[] = [];
//for each file.path, generate the thumbnail.
(req.files as Express.Multer.File[]).forEach((file) => {
const vendor: string = (req.body.vendor || "").trim();
const invoice_number: string = (req.body.invoice_number || "").trim();
copyQueue.push(
(async () => {
const target: string = path.join(
PathToVendorBillsFile(vendor),
invoice_number + path.extname(file.filename)
);
await fs.ensureDir(path.dirname(target));
await fs.copyFile(file.path, target);
})()
);
//Copy Queue is not awaited - we don't care if it finishes before we serve up the thumbnails.
});
}
BillsListMedia(req, res);
}
} catch (err) {
res.status(500).send(err);
}
}