Skip to content

Commit

Permalink
Refactor Asset Model Import Formatting (#6388)
Browse files Browse the repository at this point in the history
* Refactor Asset Model Import Formatting

* fixes to warranty_amc_end_of_validity

* Fix asset import file for cypress
  • Loading branch information
Ashesh3 committed Oct 4, 2023
1 parent 5a7804e commit 410a7d5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
Binary file modified cypress/fixtures/sampleAsset.xlsx
Binary file not shown.
45 changes: 33 additions & 12 deletions src/Common/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ export const XLSXAssetImportSchema = {
Class: {
prop: "asset_class",
type: String,
oneOf: ["HL7MONITOR", "ONVIF"],
oneOf: ["HL7MONITOR", "ONVIF", "VENTILATOR", ""],
},
Description: { prop: "description", type: String },
"Working Status": {
Expand All @@ -908,7 +908,7 @@ export const XLSXAssetImportSchema = {
} else if (status === "NOT WORKING") {
return false;
} else {
throw new Error("Invalid Working Status");
throw new Error("Invalid Working Status: " + status);
}
},
required: true,
Expand All @@ -917,6 +917,7 @@ export const XLSXAssetImportSchema = {
prop: "not_working_reason",
type: String,
},
"Serial Number": { prop: "serial_number", type: String },
"QR Code ID": { prop: "qr_code_id", type: String },
Manufacturer: { prop: "manufacturer", type: String },
"Vendor Name": { prop: "vendor_name", type: String },
Expand All @@ -925,10 +926,11 @@ export const XLSXAssetImportSchema = {
prop: "support_email",
type: String,
parse: (email: string) => {
if (!email) return null;
const isValid = /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/.test(email);

if (!isValid) {
throw new Error("Invalid Support Email");
throw new Error("Invalid Support Email: " + email);
}

return email;
Expand All @@ -938,23 +940,38 @@ export const XLSXAssetImportSchema = {
prop: "support_phone",
type: String,
parse: (phone: number | string) => {
phone = "+91" + String(phone);
if (!PhoneNumberValidator(["support"])(phone) === undefined) {
throw new Error("Invalid Support Phone Number");
phone = String(phone);
if (phone.length === 10 && !phone.startsWith("1800")) {
phone = "+91" + phone;
}
if (phone.startsWith("91") && phone.length === 12) {
phone = "+" + phone;
}
if (phone.startsWith("+911800")) {
phone = "1800" + phone.slice(6);
}
if (
PhoneNumberValidator(["mobile", "landline", "support"])(phone) !==
undefined
) {
throw new Error("Invalid Support Phone Number: " + phone);
}

return phone ? phone : undefined;
},
required: true,
},
"Warrenty End Date": {
"Warranty End Date": {
prop: "warranty_amc_end_of_validity",
type: String,
parse: (date: string) => {
const parsed = new Date(date);
if (!date) return null;
const parts = date.split("-");
const reformattedDateStr = `${parts[2]}-${parts[1]}-${parts[0]}`;
const parsed = new Date(reformattedDateStr);

if (String(parsed) === "Invalid Date") {
throw new Error("Invalid Warrenty End Date");
throw new Error("Invalid Warranty End Date:" + date);
}

return dateQueryString(parsed);
Expand All @@ -964,10 +981,13 @@ export const XLSXAssetImportSchema = {
prop: "last_serviced_on",
type: String,
parse: (date: string) => {
const parsed = new Date(date);
if (!date) return null;
const parts = date.split("-");
const reformattedDateStr = `${parts[2]}-${parts[1]}-${parts[0]}`;
const parsed = new Date(reformattedDateStr);

if (String(parsed) === "Invalid Date") {
throw new Error("Invalid Last Service Date");
throw new Error("Invalid Last Service Date:" + date);
}

return dateQueryString(parsed);
Expand All @@ -981,13 +1001,14 @@ export const XLSXAssetImportSchema = {
prop: "local_ip_address",
type: String,
parse: (ip: string) => {
if (!ip) return null;
const isValid =
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(
ip
);

if (!isValid) {
throw new Error("Invalid Config IP Address");
throw new Error("Invalid Config IP Address: " + ip);
}

return ip;
Expand Down
20 changes: 12 additions & 8 deletions src/Components/Assets/AssetImportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ const AssetImportModal = ({ open, onClose, facility }: Props) => {
}
}
}
} catch (e) {
} catch (e: any) {
setPreview(undefined);
console.log(e);
Notification.Error({
msg: "Invalid file",
msg: "Invalid file: " + e.message,
});
setSelectedFile(undefined);
}
Expand All @@ -113,7 +113,7 @@ const AssetImportModal = ({ open, onClose, facility }: Props) => {
let error = false;

for (const asset of preview || []) {
const asset_data = JSON.stringify({
const asset_data: any = {
name: asset.name,
asset_type: asset.asset_type,
asset_class: asset.asset_class,
Expand All @@ -129,11 +129,15 @@ const AssetImportModal = ({ open, onClose, facility }: Props) => {
qr_code_id: asset.qr_code_id,
manufacturer: asset.manufacturer,
meta: { ...asset.meta },
warranty_amc_end_of_validity: asset.warranty_amc_end_of_validity,
last_serviced_on: asset.last_serviced_on,
note: asset.notes,
cancelToken: { promise: {} },
});
};

if (asset.last_serviced_on)
asset_data["last_serviced_on"] = asset.last_serviced_on;

if (asset.warranty_amc_end_of_validity)
asset_data["warranty_amc_end_of_validity"] =
asset.warranty_amc_end_of_validity;

const response = await fetch("/api/v1/asset/", {
method: "POST",
Expand All @@ -142,7 +146,7 @@ const AssetImportModal = ({ open, onClose, facility }: Props) => {
Authorization:
"Bearer " + localStorage.getItem(LocalStorageKeys.accessToken),
},
body: asset_data,
body: JSON.stringify(asset_data),
});
const data = await response.json();
if (response.status !== 201) {
Expand Down

0 comments on commit 410a7d5

Please sign in to comment.