Skip to content

Commit

Permalink
fix the http route as well
Browse files Browse the repository at this point in the history
  • Loading branch information
pinocchio-life-like committed Aug 5, 2024
1 parent 47666fa commit 154919d
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions server/src/controllers/item/importFromBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function generateAWSHeaders(
}

export const importFromBucket = async (c) => {
const { directory } = await c.req.query();
const { directory, ownerId } = await c.req.query();

const endpoint = c.env.BUCKET_ENDPOINT;
const bucket = c.env.BUCKET_NAME;
Expand Down Expand Up @@ -105,17 +105,13 @@ export const importFromBucket = async (c) => {
.filter((item) => item.Key[0].startsWith(`${directory}/`))
.map((item) => item.Key[0]);

// console.log('File names in backcountry directory:', fileNames);

// Sort file names to get the latest one
const latestFileName = fileNames.sort().reverse()[0];

if (!latestFileName) {
throw new Error('No files found in the backcountry directory');
}

// console.log('Latest file name:', latestFileName);

// Generate AWS Headers for fetching the latest file
const fileHeaders = generateAWSHeaders(
`${endpoint}/${bucket}/${latestFileName}`,
Expand Down Expand Up @@ -143,19 +139,51 @@ export const importFromBucket = async (c) => {
return c.json({ error: 'Error fetching file', details: fileData });
}

let parsedCSVData = null;
const itemsToInsert = [];

await new Promise((resolve, reject) => {
Papa.parse(fileData, {
header: true,
complete: (results) => {
try {
for (const [index, item] of results.data.entries()) {
if (
index === results.data.length - 1 &&
Object.values(item).every((value) => value === '')
) {
continue;
}

Papa.parse(fileData, {
header: true,
complete: function (results) {
parsedCSVData = results.data;
},
error: function (error) {
console.error('Error parsing CSV file:', error);
},
itemsToInsert.push({
name: item.name,
weight: item.claimed_weight || 0,
quantity: item.quantity || 1,
unit: item.claimed_weight_unit || 'g',
type: 'Essentials',
ownerId,
});
}
resolve(null);
} catch (error) {
console.error('Error processing CSV data:', error);
reject(error);
}
},
error: (error) => {
console.error('Error parsing CSV file:', error);
reject(error);
},
});
});

return c.json({ message: 'File content logged', data: parsedCSVData });
const insertedItems = await bulkAddItemsGlobalService(
itemsToInsert,
c.executionCtx,
);
return c.json({
message: 'Items inserted successfully',
data: insertedItems,
});
} catch (err) {
console.error('Error:', err);
return c.json({ error: 'An error occurred' });
Expand Down

0 comments on commit 154919d

Please sign in to comment.