Skip to content

Commit

Permalink
Merge branch 'develop' into asset_changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijv256 authored Oct 3, 2023
2 parents 610780d + 8589460 commit d4f5079
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 200 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
32 changes: 12 additions & 20 deletions src/Common/hooks/useRangePagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ interface Props {
bounds: DateRange;
perPage: number;
slots?: number;
snapToLatest?: boolean;
reverse?: boolean;
defaultEnd?: boolean;
}

const useRangePagination = ({ bounds, perPage, ...props }: Props) => {
const [currentRange, setCurrentRange] = useState(
getInitialBounds(bounds, perPage, props.snapToLatest)
getInitialBounds(bounds, perPage, props.defaultEnd)
);

useEffect(() => {
setCurrentRange(getInitialBounds(bounds, perPage, props.snapToLatest));
}, [bounds, perPage, props.snapToLatest]);
setCurrentRange(getInitialBounds(bounds, perPage, props.defaultEnd));
}, [bounds, perPage, props.defaultEnd]);

const next = () => {
const { end } = currentRange;
Expand Down Expand Up @@ -63,24 +62,17 @@ const useRangePagination = ({ bounds, perPage, ...props }: Props) => {
}

const slots: DateRange[] = [];
const { start, end } = currentRange;
const { start } = currentRange;
const delta = perPage / props.slots;

for (let i = 0; i < props.slots; i++) {
if (props.snapToLatest) {
slots.push({
start: new Date(end.valueOf() - delta * (i - 1)),
end: new Date(end.valueOf() - delta * i),
});
} else {
slots.push({
start: new Date(start.valueOf() + delta * i),
end: new Date(start.valueOf() + delta * (i + 1)),
});
}
slots.push({
start: new Date(start.valueOf() + delta * i),
end: new Date(start.valueOf() + delta * (i + 1)),
});
}

return props.reverse ? slots.reverse() : slots;
return slots;
}, [currentRange, props.slots, perPage]);

return {
Expand All @@ -98,15 +90,15 @@ export default useRangePagination;
const getInitialBounds = (
bounds: DateRange,
perPage: number,
snapToLatest?: boolean
defaultEnd?: boolean
) => {
const deltaBounds = bounds.end.valueOf() - bounds.start.valueOf();

if (deltaBounds < perPage) {
return bounds;
}

if (snapToLatest) {
if (defaultEnd) {
return {
start: new Date(bounds.end.valueOf() - perPage),
end: bounds.end,
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 @@ -82,11 +82,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 @@ -110,7 +110,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 @@ -126,11 +126,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 @@ -139,7 +143,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
Loading

0 comments on commit d4f5079

Please sign in to comment.