data limit

This commit is contained in:
jusax23 2023-03-25 16:19:41 +01:00
parent b28ac9c901
commit 6f322f0150
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
3 changed files with 33 additions and 2 deletions

View file

@ -109,7 +109,7 @@ export const changeViewable: Act = {
aws("error", "existence"); aws("error", "existence");
} }
} }
} };
export const createRoom: Act = { export const createRoom: Act = {
state: STATE.client, state: STATE.client,

View file

@ -2,7 +2,7 @@ import { and, coalesce, eq, insert, max, plus, remove, select, update } from "db
import { checkSelfTag } from "../../server/outbagURL.js"; import { checkSelfTag } from "../../server/outbagURL.js";
import { Act, Client, STATE } from "../user.js"; import { Act, Client, STATE } from "../user.js";
import { db, listCategories, listItems, listProducts } from "../../sys/db.js"; import { db, listCategories, listItems, listProducts } from "../../sys/db.js";
import { isCategoryInRoom, isItemInRoom, isProductInRoom } from "../helper.js" import { isCategoryInRoom, isItemInRoom, isProductInRoom, isRoomDataFull } from "../helper.js"
export const getCategories: Act = { export const getCategories: Act = {
state: STATE.client | STATE.remote, state: STATE.client | STATE.remote,
@ -63,6 +63,7 @@ export const addCategory: Act = {
aws("error", "existence"); aws("error", "existence");
return; return;
} }
if (await isRoomDataFull(roomID)) return void aws("error", "limit");
let req = await insert( let req = await insert(
listCategories.roomID, listCategories.roomID,
listCategories.title, listCategories.title,
@ -260,6 +261,7 @@ export const addProduct: Act = {
aws("error", "existence"); aws("error", "existence");
return; return;
} }
if (await isRoomDataFull(roomID)) return void aws("error", "limit");
if (!isCategoryInRoom(roomID, data.listCatID)) return void aws("error", "existence"); if (!isCategoryInRoom(roomID, data.listCatID)) return void aws("error", "existence");
if (data.parent > -1 && !isProductInRoom(roomID, data.parent)) return void aws("error", "existence"); if (data.parent > -1 && !isProductInRoom(roomID, data.parent)) return void aws("error", "existence");
let req = await insert( let req = await insert(
@ -436,6 +438,7 @@ export const addItem: Act = {
aws("error", "existence"); aws("error", "existence");
return; return;
} }
if (await isRoomDataFull(roomID)) return void aws("error", "limit");
if (data.listCatID > -1 && !isCategoryInRoom(roomID, data.listCatID)) return void aws("error", "existence"); if (data.listCatID > -1 && !isCategoryInRoom(roomID, data.listCatID)) return void aws("error", "existence");
if (data.listProdID > -1 && !isProductInRoom(roomID, data.listProdID)) return void aws("error", "existence"); if (data.listProdID > -1 && !isProductInRoom(roomID, data.listProdID)) return void aws("error", "existence");
let req = await insert( let req = await insert(

View file

@ -19,6 +19,34 @@ export const isRoomFull = async (roomID: number) => {
return req[0][currentCount] >= req[0][maxCount]; return req[0][currentCount] >= req[0][maxCount];
}; };
export const isRoomDataFull = async (roomID: number) => {
let currentCatCount = alias(
select([count(listCategories.listCatID)], listCategories)
.where(eq(listCategories.roomID, roomID)),
"currentCatCount"
) as any;
let currentProdCount = alias(
select([count(listProducts.listProdID)], listProducts)
.where(eq(listProducts.roomID, roomID)),
"currentProdCount"
) as any;
let currentItemCount = alias(
select([count(listItems.listItemID)], listItems)
.where(eq(listItems.roomID, roomID)),
"currentItemCount"
) as any;
let maxCount = alias(
select([accounts.maxRoomSize],
innerJoinOn(accounts, rooms, eq(accounts.accID, rooms.owner)))
.where(eq(rooms.roomID, roomID)),
"maxCount"
) as any;
let req = await select([currentCatCount, currentProdCount, currentItemCount, maxCount], null)
.query(db);
if (maxCount == -1) return false;
return req[0][currentCatCount] + req[0][currentProdCount] + req[0][currentItemCount] >= req[0][maxCount];
};
export const canCreateRoom = async (accID: number) => { export const canCreateRoom = async (accID: number) => {
let currentCount = alias( let currentCount = alias(
select([count(rooms.roomID)], rooms) select([count(rooms.roomID)], rooms)