2023-04-26 01:18:05 +00:00
|
|
|
import { getUsedResources } from "./pve.js";
|
|
|
|
import { getUserConfig, getResourceConfig } from "./db.js";
|
2023-04-24 21:28:58 +00:00
|
|
|
|
2023-05-17 21:38:11 +00:00
|
|
|
export async function getAllocatedResources(req, username) {
|
2023-04-24 21:28:58 +00:00
|
|
|
let dbResources = getResourceConfig();
|
|
|
|
let used = await getUsedResources(req, dbResources);
|
|
|
|
let max = getUserConfig(username).resources.max;
|
2023-04-26 01:18:05 +00:00
|
|
|
let avail = {};
|
2023-04-24 21:28:58 +00:00
|
|
|
Object.keys(max).forEach((k) => {
|
|
|
|
avail[k] = max[k] - used[k];
|
|
|
|
});
|
2023-05-17 21:38:11 +00:00
|
|
|
return { used: used, max: max, avail: avail, units: dbResources };
|
2023-04-24 21:28:58 +00:00
|
|
|
}
|
|
|
|
|
2023-05-17 21:38:11 +00:00
|
|
|
export async function approveResources(req, username, request) {
|
2023-04-24 21:28:58 +00:00
|
|
|
|
|
|
|
let avail = (await getAllocatedResources(req, username)).avail;
|
|
|
|
|
|
|
|
let approved = true;
|
|
|
|
Object.keys(request).forEach((key) => {
|
|
|
|
if (!(key in avail)) {
|
|
|
|
approved = false;
|
|
|
|
}
|
2023-05-18 14:58:44 +00:00
|
|
|
else if (isNaN(avail[key]) || isNaN(request[key])) {
|
2023-04-24 21:28:58 +00:00
|
|
|
approved = false;
|
|
|
|
}
|
2023-05-18 14:58:44 +00:00
|
|
|
else if (avail[key] - request[key] < 0) {
|
|
|
|
approved = false;
|
|
|
|
}
|
2023-04-24 21:28:58 +00:00
|
|
|
});
|
|
|
|
return approved;
|
2023-04-26 01:18:05 +00:00
|
|
|
}
|