Skip to content

Instantly share code, notes, and snippets.

@tifandotme
Last active October 10, 2025 04:35
Show Gist options
  • Select an option

  • Save tifandotme/d5757a1cb183254083f2e01d2264e251 to your computer and use it in GitHub Desktop.

Select an option

Save tifandotme/d5757a1cb183254083f2e01d2264e251 to your computer and use it in GitHub Desktop.
window.restoreBatch = function(eventData, companyId) {
const HealthParameter = { whiteSpot: "whiteSpot", tubulesLipid: "tubulesLipid", tubulesShape: "tubulesShape", thc: "thc", chromatophore: "chromatophore", hepatoColor: "hepatoColor" };
const samples = eventData.samples.map((s) => ({
uuid: s.uuid,
name: s.name,
farmId: s.farm_id || "",
cycleId: s.cycle_id || "",
pondId: s.pond_id || "",
DOC: s.doc || "",
shrimpsCount: s.shrimps_count?.toString() || "10",
sampleDate: s.sample_date || "",
createdDate: new Date().toISOString(),
createdBy: "restored",
parameters: s.parameters || {
whiteSpot: false,
tubulesLipid: false,
tubulesShape: false,
thc: false,
chromatophore: false,
hepatoColor: false
}
}));
const batch = {
uuid: eventData.batch_uuid,
name: eventData.batch_name,
companyId,
status: eventData.batch_status,
createdDate: eventData.created_date,
updatedDate: eventData.updated_date,
createdBy: "restored",
savedToCloud: eventData.saved_to_cloud,
lastSavedToCloud: eventData.last_saved_to_cloud || undefined,
samples
};
const imagesBySampleUuid = {};
for (const sample of eventData.samples) {
const sampleImages = {};
const imagesByShrimp = {};
for (const img of sample.images) {
const shrimpNum = img.shrimp_number;
const param = img.health_parameter;
if (!shrimpNum || !param) continue;
if (!imagesByShrimp[shrimpNum]) imagesByShrimp[shrimpNum] = {
whiteSpot: [],
tubulesLipid: [],
tubulesShape: [],
thc: [],
chromatophore: [],
hepatoColor: []
};
imagesByShrimp[shrimpNum][param].push(img);
}
for (const [shrimpNumStr, params] of Object.entries(imagesByShrimp)) {
const shrimpNum = Number(shrimpNumStr);
sampleImages[shrimpNum] = {
whiteSpot: params.whiteSpot.map((img, index) => ({
id: index + 1,
uuid: img.uuid,
uploadUrl: img.upload_url ? new URL(img.upload_url).pathname : null,
uploaded: img.uploaded,
size: img.size,
createdDate: img.created_date,
createdBy: "restored",
savedToDisk: true,
displayName: img.name,
picNumber: index + 1
})),
tubulesLipid: params.tubulesLipid.map((img, index) => ({
id: index + 1,
uuid: img.uuid,
uploadUrl: img.upload_url ? new URL(img.upload_url).pathname : null,
uploaded: img.uploaded,
size: img.size,
createdDate: img.created_date,
createdBy: "restored",
savedToDisk: true,
displayName: img.name,
picNumber: index + 1
})),
tubulesShape: params.tubulesShape.map((img, index) => ({
id: index + 1,
uuid: img.uuid,
uploadUrl: img.upload_url ? new URL(img.upload_url).pathname : null,
uploaded: img.uploaded,
size: img.size,
createdDate: img.created_date,
createdBy: "restored",
savedToDisk: true,
displayName: img.name,
picNumber: index + 1
})),
thc: params.thc.map((img, index) => ({
id: index + 1,
uuid: img.uuid,
uploadUrl: img.upload_url ? new URL(img.upload_url).pathname : null,
uploaded: img.uploaded,
size: img.size,
createdDate: img.created_date,
createdBy: "restored",
savedToDisk: true,
displayName: img.name,
picNumber: index + 1
})),
chromatophore: params.chromatophore.map((img, index) => ({
id: index + 1,
uuid: img.uuid,
uploadUrl: img.upload_url ? new URL(img.upload_url).pathname : null,
uploaded: img.uploaded,
size: img.size,
createdDate: img.created_date,
createdBy: "restored",
savedToDisk: true,
displayName: img.name,
picNumber: index + 1
})),
hepatoColor: params.hepatoColor.map((img, index) => ({
id: index + 1,
uuid: img.uuid,
uploadUrl: img.upload_url ? new URL(img.upload_url).pathname : null,
uploaded: img.uploaded,
size: img.size,
createdDate: img.created_date,
createdBy: "restored",
savedToDisk: true,
displayName: img.name,
picNumber: index + 1
}))
};
}
imagesBySampleUuid[sample.uuid] = sampleImages;
}
const existing = JSON.parse(localStorage.getItem("aquasense:health-batch-store") || '{"state":{}}');
const existingBatches = existing.state.batchesByUuid || {};
const existingImages = existing.state.imagesBySampleUuid || {};
const newBatches = { ...existingBatches, [eventData.batch_uuid]: batch };
const newImages = { ...existingImages, ...imagesBySampleUuid };
localStorage.setItem("aquasense:health-batch-store", JSON.stringify({
state: {
batchesByUuid: newBatches,
imagesBySampleUuid: newImages
},
version: 18
}));
window.location.reload();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment