DocSpace-client/packages/common/store/CurrentQuotaStore.js

230 lines
5.1 KiB
JavaScript
Raw Normal View History

2022-09-14 06:55:22 +00:00
import { makeAutoObservable } from "mobx";
2022-09-14 06:55:22 +00:00
import api from "../api";
import { PortalFeaturesLimitations } from "../constants";
2023-03-10 14:09:03 +00:00
import toastr from "@docspace/components/toast/toastr";
2022-09-20 15:15:38 +00:00
const MANAGER = "manager";
const TOTAL_SIZE = "total_size";
const FILE_SIZE = "file_size";
const ROOM = "room";
const USERS = "users";
const USERS_IN_ROOM = "usersInRoom";
const COUNT_FOR_SHOWING_BAR = 2;
const PERCENTAGE_FOR_SHOWING_BAR = 90;
2022-09-14 06:55:22 +00:00
class QuotasStore {
currentPortalQuota = {};
currentPortalQuotaFeatures = [];
2022-09-14 06:55:22 +00:00
isLoaded = false;
constructor() {
makeAutoObservable(this);
}
init = async () => {
if (this.isLoaded) return;
await this.setPortalQuota();
};
setIsLoaded = (isLoaded) => {
this.isLoaded = isLoaded;
};
get isFreeTariff() {
2022-09-26 08:19:10 +00:00
return this.currentPortalQuota.free;
2022-09-14 06:55:22 +00:00
}
get currentPlanCost() {
if (this.currentPortalQuota.price) return this.currentPortalQuota.price;
else return { value: 0, currencySymbol: "" };
}
get maxCountManagersByQuota() {
const result = this.currentPortalQuotaFeatures.find(
2022-09-20 15:15:38 +00:00
(obj) => obj.id === MANAGER
2022-09-14 06:55:22 +00:00
);
return result?.value;
2022-09-14 06:55:22 +00:00
}
get addedManagersCount() {
const result = this.currentPortalQuotaFeatures.find(
2022-09-20 15:15:38 +00:00
(obj) => obj.id === MANAGER
2022-09-14 06:55:22 +00:00
);
return result?.used?.value;
2022-09-14 06:55:22 +00:00
}
get maxTotalSizeByQuota() {
const result = this.currentPortalQuotaFeatures.find(
2022-09-20 15:15:38 +00:00
(obj) => obj.id === TOTAL_SIZE
2022-09-14 06:55:22 +00:00
);
if (!result?.value) return PortalFeaturesLimitations.Limitless;
2022-09-14 06:55:22 +00:00
return result?.value;
2022-09-14 06:55:22 +00:00
}
get usedTotalStorageSizeCount() {
const result = this.currentPortalQuotaFeatures.find(
2022-09-20 15:15:38 +00:00
(obj) => obj.id === TOTAL_SIZE
2022-09-14 06:55:22 +00:00
);
return result?.used?.value;
2022-09-14 06:55:22 +00:00
}
get maxFileSizeByQuota() {
const result = this.currentPortalQuotaFeatures.find(
2022-09-20 15:15:38 +00:00
(obj) => obj.id === FILE_SIZE
2022-09-14 06:55:22 +00:00
);
return result?.value;
2022-09-14 06:55:22 +00:00
}
get maxCountUsersByQuota() {
const result = this.currentPortalQuotaFeatures.find(
2022-09-20 15:15:38 +00:00
(obj) => obj.id === USERS
2022-09-14 06:55:22 +00:00
);
if (!result || !result?.value) return PortalFeaturesLimitations.Limitless;
return result?.value;
2022-09-14 06:55:22 +00:00
}
get maxCountRoomsByQuota() {
const result = this.currentPortalQuotaFeatures.find(
2022-09-20 15:15:38 +00:00
(obj) => obj.id === ROOM
2022-09-14 06:55:22 +00:00
);
if (!result || !result?.value) return PortalFeaturesLimitations.Limitless;
return result?.value;
2022-09-14 06:55:22 +00:00
}
get usedRoomsCount() {
const result = this.currentPortalQuotaFeatures.find(
2022-09-20 15:15:38 +00:00
(obj) => obj.id === ROOM
2022-09-14 06:55:22 +00:00
);
return result?.used?.value;
2022-09-14 06:55:22 +00:00
}
2022-09-16 07:45:10 +00:00
get isBrandingAndCustomizationAvailable() {
2022-09-14 06:55:22 +00:00
const result = this.currentPortalQuotaFeatures.find(
(obj) => obj.id === "whitelabel"
);
return result?.value;
2022-09-14 06:55:22 +00:00
}
2023-03-29 11:43:48 +00:00
get isOAuthAvailable() {
const result = this.currentPortalQuotaFeatures.find(
(obj) => obj.id === "oauth"
);
return result?.value;
}
2023-03-29 10:56:58 +00:00
get isThirdPartyAvailable() {
const result = this.currentPortalQuotaFeatures.find(
(obj) => obj.id === "thirdparty"
);
return result?.value;
}
2022-09-14 06:55:22 +00:00
get isSSOAvailable() {
const result = this.currentPortalQuotaFeatures.find(
(obj) => obj.id === "sso"
);
return result?.value;
2022-09-14 06:55:22 +00:00
}
get isRestoreAndAutoBackupAvailable() {
const result = this.currentPortalQuotaFeatures.find(
(obj) => obj.id === "restore"
);
return result?.value;
2022-09-14 06:55:22 +00:00
}
get isAuditAvailable() {
const result = this.currentPortalQuotaFeatures.find(
2022-09-20 15:15:38 +00:00
(obj) => obj.id === "audit"
2022-09-14 06:55:22 +00:00
);
return result?.value;
2022-09-14 06:55:22 +00:00
}
2022-09-14 14:17:40 +00:00
get currentTariffPlanTitle() {
2022-09-14 06:55:22 +00:00
return this.currentPortalQuota.title;
}
2022-09-14 11:13:21 +00:00
get quotaCharacteristics() {
const result = [];
this.currentPortalQuotaFeatures.forEach((elem) => {
elem.id === ROOM && result?.splice(0, 0, elem);
elem.id === MANAGER && result?.splice(1, 0, elem);
elem.id === TOTAL_SIZE && result?.splice(2, 0, elem);
2022-09-14 11:13:21 +00:00
});
return result;
}
get maxUsersCountInRoom() {
const result = this.currentPortalQuotaFeatures.find(
2022-09-20 15:15:38 +00:00
(obj) => obj.id === USERS_IN_ROOM
);
if (!result || !result?.value) return PortalFeaturesLimitations.Limitless;
return result?.value;
}
get showRoomQuotaBar() {
return (
this.maxCountRoomsByQuota - this.usedRoomsCount <=
COUNT_FOR_SHOWING_BAR &&
this.maxCountRoomsByQuota > 0 &&
this.maxCountRoomsByQuota >= this.usedRoomsCount
);
}
get showStorageQuotaBar() {
return (
(this.usedTotalStorageSizeCount / this.maxTotalSizeByQuota) * 100 >=
PERCENTAGE_FOR_SHOWING_BAR
);
}
get showUserQuotaBar() {
return (
this.addedManagersCount > 1 &&
this.maxCountManagersByQuota - this.addedManagersCount <=
COUNT_FOR_SHOWING_BAR &&
this.maxCountManagersByQuota >= this.addedManagersCount
);
}
setPortalQuotaValue = (res) => {
this.currentPortalQuota = res;
this.currentPortalQuotaFeatures = res.features;
};
2022-09-14 06:55:22 +00:00
setPortalQuota = async () => {
2023-03-10 14:09:03 +00:00
try {
const res = await api.portal.getPortalQuota();
if (!res) return;
this.setIsLoaded(true);
2022-09-14 06:55:22 +00:00
2023-03-10 14:09:03 +00:00
this.setPortalQuotaValue(res);
} catch (e) {
toastr.error(e);
}
2022-09-14 06:55:22 +00:00
};
}
export default QuotasStore;