mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-30 12:55:02 +00:00
Merge pull request #47 from Hestia-Homes/costs-migration
Costs migration
This commit is contained in:
commit
bc78511547
1 changed files with 19 additions and 7 deletions
|
|
@ -288,8 +288,7 @@ async function uploadCsvToS3({
|
|||
console.error(error);
|
||||
throw new Error("Upload failed.");
|
||||
}
|
||||
|
||||
console.log("S3 got the stuff");
|
||||
console.log("File uploaded successfully");
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
|
|
@ -332,16 +331,29 @@ function generateS3Keys(userId: string, portfolioId: string) {
|
|||
|
||||
type GenericObject = Record<string, any>;
|
||||
|
||||
const convertToCSV = <T extends GenericObject>(data: T[]): string => {
|
||||
// Get headers (keys from the first object)
|
||||
const convertToCSV = <T extends Record<string, any>>(data: T[]): string => {
|
||||
if (data.length === 0) return "";
|
||||
|
||||
const headers = Object.keys(data[0]) as (keyof T)[];
|
||||
|
||||
// Create CSV rows
|
||||
const escape = (value: any): string => {
|
||||
if (value == null) return "";
|
||||
|
||||
const str = String(value);
|
||||
|
||||
// Check if field contains special characters
|
||||
if (/[",\n]/.test(str)) {
|
||||
// Escape double quotes and wrap in quotes
|
||||
return `"${str.replace(/"/g, '""')}"`;
|
||||
}
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
const rows = data.map((row) =>
|
||||
headers.map((header) => row[header]).join(",")
|
||||
headers.map((header) => escape(row[header])).join(",")
|
||||
);
|
||||
|
||||
// Combine headers and rows into CSV string
|
||||
return [headers.join(","), ...rows].join("\n");
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue