mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-08 11:37:25 +00:00
add utils
This commit is contained in:
parent
85d6274e87
commit
08705f708a
7 changed files with 96 additions and 11 deletions
|
|
@ -27,7 +27,7 @@ export async function POST(request: NextRequest) {
|
|||
const s3 = new S3({
|
||||
signatureVersion: "v4",
|
||||
region: process.env.PRESIGN_AWS_REGION,
|
||||
accessKeyId: process.env.PRSIGN_AWS_ACCESS_KEY,
|
||||
accessKeyId: process.env.PRESIGN_AWS_ACCESS_KEY,
|
||||
secretAccessKey: process.env.PRESIGN_AWS_SECRET_KEY,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -42,8 +42,8 @@ function Nav({ userImage }: { userImage: string }) {
|
|||
<div className="hidden md:block">
|
||||
<div className="ml-10 flex items-baseline space-x-4">
|
||||
{makeLink("/home", "Home")}
|
||||
{makeLink("/due-considerations", "Due Considerations")}
|
||||
{makeLink("/eco-spreadsheet", "Eco Spreadsheet")}
|
||||
{/* {makeLink("/due-considerations", "Due Considerations")} */}
|
||||
{/* {makeLink("/eco-spreadsheet", "Eco Spreadsheet")} */}
|
||||
{makeLink("/help", "Help")}
|
||||
<div className="flex-grow"></div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ export const reportType: [string, ...string[]] = [
|
|||
"U_VALUE_CALCULATOR_REPORT",
|
||||
"OVERWRITING_U_VALUE_DECLARATION_FORM",
|
||||
"OSMOSIS_CONDITION_PAS_2035_REPORT",
|
||||
"DOMNA_CONDITION_PAS_2035_REPORT",
|
||||
];
|
||||
|
||||
const reportTypeEnum = pgEnum("report_type", reportType);
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ const descriptions: Record<ReportType, string> = {
|
|||
OVERWRITING_U_VALUE_DECLARATION_FORM: "Signed form for overwriting U-values",
|
||||
OSMOSIS_CONDITION_PAS_2035_REPORT:
|
||||
"Osmosis-generated PAS 2035 Condition Report",
|
||||
DOMNA_CONDITION_PAS_2035_REPORT:
|
||||
"Domna-generated PAS 2035 Condition Report"
|
||||
};
|
||||
|
||||
export const DocumentSection = ({
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@ export const DocumentsTable: React.FC<Props> = ({
|
|||
const handleUpload = () => {
|
||||
// Handle the upload logic here
|
||||
console.log("Upload button clicked");
|
||||
console.log("Junte was here");
|
||||
};
|
||||
|
||||
// We split out the various document types. Filter all of the quidos pre-site notes
|
||||
|
|
@ -70,8 +71,8 @@ export const DocumentsTable: React.FC<Props> = ({
|
|||
(doc) => doc.documentType === "QUIDOS_PRESITE_NOTE"
|
||||
);
|
||||
|
||||
const osmosisConditionReport = documents.filter(
|
||||
(doc) => doc.documentType === "OSMOSIS_CONDITION_PAS_2035_REPORT"
|
||||
const domnaConditionReport = documents.filter(
|
||||
(doc) => doc.documentType === "DOMNA_CONDITION_PAS_2035_REPORT"
|
||||
);
|
||||
|
||||
const floors = documents.filter((doc) => doc.documentType === "FLOOR_PLAN");
|
||||
|
|
@ -97,9 +98,9 @@ export const DocumentsTable: React.FC<Props> = ({
|
|||
|
||||
<DocumentSection
|
||||
title="Condition Report"
|
||||
docs={osmosisConditionReport}
|
||||
docs={domnaConditionReport}
|
||||
sectionKey="condition"
|
||||
documentType="OSMOSIS_CONDITION_PAS_2035_REPORT"
|
||||
documentType="DOMNA_CONDITION_PAS_2035_REPORT"
|
||||
fileTypes=".pdf"
|
||||
/>
|
||||
|
||||
|
|
|
|||
|
|
@ -24,6 +24,31 @@ const titles: Record<ReportType, string> = {
|
|||
QUIDOS_PRESITE_NOTE: "RdSAP Summary Report",
|
||||
};
|
||||
|
||||
async function generatePresignedUrls({
|
||||
key,
|
||||
bucket,
|
||||
contentType,
|
||||
expiresInSeconds,
|
||||
}: {
|
||||
key: string;
|
||||
bucket: string;
|
||||
contentType: string;
|
||||
expiresInSeconds: number;
|
||||
}) {
|
||||
const body = JSON.stringify({ key, bucket, contentType, expiresInSeconds });
|
||||
console.log("bucket is ", bucket);
|
||||
const presignedResponse = await fetch("/api/upload/s3_bucket_presigned_url", {
|
||||
method: "POST",
|
||||
body,
|
||||
});
|
||||
|
||||
if (!presignedResponse.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
|
||||
return presignedResponse.json();
|
||||
}
|
||||
|
||||
export const UploadModal = ({
|
||||
open,
|
||||
onClose,
|
||||
|
|
@ -33,6 +58,20 @@ export const UploadModal = ({
|
|||
const [uploadFiles, setUploadFiles] = useState<File[]>([]);
|
||||
const [buttonDisabled, setButtonDisabled] = useState(true);
|
||||
|
||||
async function handleS3Upload() {
|
||||
console.log("Get Presigned url in a specific bucket location")
|
||||
|
||||
const { url } = await generatePresignedUrls({
|
||||
key: "foo/test/trololol", // path in bucket
|
||||
bucket: process.env.RETROFIT_ENERGY_ASSESSMENTS_BUCKET || "back up", //s3 bucket location
|
||||
// bucket: "retrofit-energy-assessments-dev", //s3 bucket location
|
||||
contentType: "application/pdf",
|
||||
expiresInSeconds: 5 * 60,
|
||||
});
|
||||
console.log("URl is ", url);
|
||||
onClose(); //probably khalim call back to update the front end properl
|
||||
}
|
||||
|
||||
function handleInputOnChange(e: React.ChangeEvent<HTMLInputElement>) {
|
||||
if (e.target.files) {
|
||||
const filesArray = Array.from(e.target.files);
|
||||
|
|
@ -86,10 +125,7 @@ export const UploadModal = ({
|
|||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
console.log("Uploading for", documentType);
|
||||
onClose();
|
||||
}}
|
||||
onClick={handleS3Upload}
|
||||
disabled={buttonDisabled}
|
||||
>
|
||||
Upload
|
||||
|
|
|
|||
45
src/app/utils/s3.ts
Normal file
45
src/app/utils/s3.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// src/utils/s3.ts
|
||||
import S3 from "aws-sdk/clients/s3";
|
||||
|
||||
|
||||
|
||||
// Condig to setup a s3 instance
|
||||
type S3Config = {
|
||||
region?: string;
|
||||
accessKeyId?: string;
|
||||
secretAccessKey?: string;
|
||||
signatureVersion?: string;
|
||||
};
|
||||
|
||||
export function createS3Client(config?: S3Config) {
|
||||
return new S3({
|
||||
region: config?.region ?? process.env.PRESIGN_AWS_REGION,
|
||||
accessKeyId: config?.accessKeyId ?? process.env.PRESIGN_AWS_ACCESS_KEY,
|
||||
secretAccessKey: config?.secretAccessKey ?? process.env.PRESIGN_AWS_SECRET_KEY,
|
||||
signatureVersion: config?.signatureVersion ?? "v4",
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Get presigned url from s3
|
||||
export type PresignGetOptions = {
|
||||
bucket: string;
|
||||
key: string;
|
||||
expiresInSeconds?: number; // default 300
|
||||
responseContentType?: string;
|
||||
responseContentDisposition?: string; // e.g. 'attachment; filename="file.csv"'
|
||||
};
|
||||
|
||||
/** Presign a GET URL using an existing S3 instance (aws-sdk v2). */
|
||||
export async function presignGetUrl(
|
||||
s3: S3,
|
||||
{ bucket, key, expiresInSeconds = 300, responseContentType, responseContentDisposition }: PresignGetOptions
|
||||
): Promise<string> {
|
||||
return (s3 as any).getSignedUrlPromise("getObject", {
|
||||
Bucket: bucket,
|
||||
Key: key,
|
||||
Expires: expiresInSeconds,
|
||||
ResponseContentType: responseContentType,
|
||||
ResponseContentDisposition: responseContentDisposition,
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue