now creating dewars from spreadsheet
This commit is contained in:
@ -17,12 +17,6 @@ import { SpreadsheetService, ShipmentsService, DewarsService, ApiError } from '.
|
||||
import * as ExcelJS from 'exceljs';
|
||||
import { saveAs } from 'file-saver';
|
||||
|
||||
const HeaderMapping = {
|
||||
'dewarname': 'dewar_name',
|
||||
'trackingnumber': 'tracking_number',
|
||||
'status': 'status'
|
||||
};
|
||||
|
||||
const SpreadsheetTable = ({
|
||||
raw_data,
|
||||
errors,
|
||||
@ -50,6 +44,7 @@ const SpreadsheetTable = ({
|
||||
dewar_name: '',
|
||||
tracking_number: 'UNKNOWN',
|
||||
status: 'In preparation',
|
||||
pucks: []
|
||||
};
|
||||
|
||||
const [newDewar, setNewDewar] = useState(initialNewDewarState);
|
||||
@ -77,7 +72,6 @@ const SpreadsheetTable = ({
|
||||
|
||||
useEffect(() => {
|
||||
const initialNonEditableCells = new Set();
|
||||
|
||||
raw_data.forEach((row, rowIndex) => {
|
||||
headers.forEach((_, colIndex) => {
|
||||
const key = `${row.row_num}-${headers[colIndex]}`;
|
||||
@ -86,7 +80,6 @@ const SpreadsheetTable = ({
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
setNonEditableCells(initialNonEditableCells);
|
||||
}, [raw_data, headers, errorMap]);
|
||||
|
||||
@ -148,35 +141,7 @@ const SpreadsheetTable = ({
|
||||
'dewarname': 0,
|
||||
'puckname': 1,
|
||||
'pucktype': 2,
|
||||
'crystalname': 3,
|
||||
'positioninpuck': 4,
|
||||
'priority': 5,
|
||||
'comments': 6,
|
||||
'directory': 7,
|
||||
'proteinname': 8,
|
||||
'oscillation': 9,
|
||||
'aperture': 10,
|
||||
'exposure': 11,
|
||||
'totalrange': 12,
|
||||
'transmission': 13,
|
||||
'dose': 14,
|
||||
'targetresolution': 15,
|
||||
'datacollectiontype': 16,
|
||||
'processingpipeline': 17,
|
||||
'spacegroupnumber': 18,
|
||||
'cellparameters': 19,
|
||||
'rescutkey': 20,
|
||||
'rescutvalue': 21,
|
||||
'userresolution': 22,
|
||||
'pdbid': 23,
|
||||
'autoprocfull': 24,
|
||||
'procfull': 25,
|
||||
'adpenabled': 26,
|
||||
'noano': 27,
|
||||
'ffcscampaign': 28,
|
||||
'trustedhigh': 29,
|
||||
'autoprocextraparams': 30,
|
||||
'chiphiangles': 31,
|
||||
// Add other fields as needed
|
||||
};
|
||||
|
||||
const createDewarsFromSheet = async (data, contactPerson, returnAddress) => {
|
||||
@ -185,7 +150,13 @@ const SpreadsheetTable = ({
|
||||
return null;
|
||||
}
|
||||
|
||||
const dewars = new Map(); // Use a Map to prevent duplicates
|
||||
const dewars = new Map();
|
||||
|
||||
const dewarNameIdx = fieldToCol['dewarname'];
|
||||
const puckNameIdx = fieldToCol['puckname'];
|
||||
const puckTypeIdx = fieldToCol['pucktype'];
|
||||
|
||||
let puckPositionInDewar = 1;
|
||||
|
||||
for (const row of data) {
|
||||
if (!row.data) {
|
||||
@ -193,46 +164,74 @@ const SpreadsheetTable = ({
|
||||
continue;
|
||||
}
|
||||
|
||||
const dewarNameIdx = fieldToCol['dewarname'];
|
||||
const dewarName = row.data[dewarNameIdx]?.trim();
|
||||
const dewarName = typeof row.data[dewarNameIdx] === 'string' ? row.data[dewarNameIdx].trim() : null;
|
||||
const puckName = typeof row.data[puckNameIdx] === 'string' ? row.data[puckNameIdx].trim() : null;
|
||||
const puckType = typeof row.data[puckTypeIdx] === 'string' ? row.data[puckTypeIdx] : 'Unipuck';
|
||||
|
||||
if (dewarName && !dewars.has(dewarName)) {
|
||||
const newDewarToPost = {
|
||||
...initialNewDewarState,
|
||||
dewar_name: dewarName,
|
||||
tracking_number: row.data[fieldToCol['trackingnumber']] || "UNKNOWN",
|
||||
status: row.data[fieldToCol['status']] || "In preparation",
|
||||
contact_person_id: contactPerson.id,
|
||||
return_address_id: returnAddress.id,
|
||||
};
|
||||
console.log(`Processing Dewar: ${dewarName}, Puck: ${puckName}, Type: ${puckType}`);
|
||||
|
||||
try {
|
||||
const createdDewar = await DewarsService.createDewarDewarsPost(newDewarToPost);
|
||||
if (createdDewar && selectedShipment) {
|
||||
const updatedShipment = await ShipmentsService.addDewarToShipmentShipmentsShipmentIdAddDewarPost(
|
||||
selectedShipment.id,
|
||||
createdDewar.id
|
||||
);
|
||||
dewars.set(dewarName, updatedShipment); // Track the added dewar
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error adding dewar for row: ${row.row_num}`, error);
|
||||
if (error instanceof ApiError && error.body) {
|
||||
console.error('Validation errors:', error.body.detail);
|
||||
} else {
|
||||
console.error('Unexpected error:', error);
|
||||
}
|
||||
if (dewarName) {
|
||||
let dewar;
|
||||
if (!dewars.has(dewarName)) {
|
||||
dewar = {
|
||||
...initialNewDewarState,
|
||||
dewar_name: dewarName,
|
||||
contact_person_id: contactPerson.id,
|
||||
return_address_id: returnAddress.id,
|
||||
pucks: []
|
||||
};
|
||||
dewars.set(dewarName, dewar);
|
||||
puckPositionInDewar = 1;
|
||||
console.log(`Created new dewar: ${dewarName}`);
|
||||
} else {
|
||||
dewar = dewars.get(dewarName);
|
||||
puckPositionInDewar++;
|
||||
console.log(`Found existing dewar: ${dewarName}`);
|
||||
}
|
||||
} else if (!dewarName) {
|
||||
|
||||
const puck = {
|
||||
puck_name: puckName || 'test', // Fixed puck name
|
||||
puck_type: puckType || 'Unipuck', // Fixed puck type
|
||||
puck_position_in_dewar: puckPositionInDewar
|
||||
};
|
||||
dewar.pucks.push(puck);
|
||||
|
||||
console.log(`Added puck: ${JSON.stringify(puck)}`);
|
||||
} else {
|
||||
console.error('Dewar name is missing in the row');
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(dewars.values());
|
||||
const dewarsArray = Array.from(dewars.values());
|
||||
for (const dewar of dewarsArray) {
|
||||
try {
|
||||
// Call to create the dewar
|
||||
const createdDewar = await DewarsService.createDewarDewarsPost(dewar);
|
||||
console.log(`Created dewar: ${createdDewar.id}`);
|
||||
|
||||
// Add dewar to the shipment if created successfully
|
||||
if (createdDewar && selectedShipment) {
|
||||
await ShipmentsService.addDewarToShipmentShipmentsShipmentIdAddDewarPost(
|
||||
selectedShipment.id,
|
||||
createdDewar.id
|
||||
);
|
||||
console.log(`Added dewar to shipment: ${createdDewar.id}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error adding dewar`, error);
|
||||
if (error instanceof ApiError && error.body) {
|
||||
console.error('Validation errors:', error.body.detail);
|
||||
} else {
|
||||
console.error('Unexpected error:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dewarsArray;
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (isSubmitting) return; // Prevent multiple submissions
|
||||
if (isSubmitting) return;
|
||||
if (!headers || headers.length === 0) {
|
||||
console.error('Cannot submit, headers are not defined or empty');
|
||||
return;
|
||||
|
Reference in New Issue
Block a user