From 6f322f0150cc17d954e2eec549d2379b3f86e12f Mon Sep 17 00:00:00 2001 From: jusax23 Date: Sat, 25 Mar 2023 16:19:41 +0100 Subject: [PATCH] data limit --- src/api/acts/client.ts | 2 +- src/api/acts/roomContent.ts | 5 ++++- src/api/helper.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/api/acts/client.ts b/src/api/acts/client.ts index aa6df78..33c2a6b 100644 --- a/src/api/acts/client.ts +++ b/src/api/acts/client.ts @@ -109,7 +109,7 @@ export const changeViewable: Act = { aws("error", "existence"); } } -} +}; export const createRoom: Act = { state: STATE.client, diff --git a/src/api/acts/roomContent.ts b/src/api/acts/roomContent.ts index 5ccd59e..f78e3af 100644 --- a/src/api/acts/roomContent.ts +++ b/src/api/acts/roomContent.ts @@ -2,7 +2,7 @@ import { and, coalesce, eq, insert, max, plus, remove, select, update } from "db import { checkSelfTag } from "../../server/outbagURL.js"; import { Act, Client, STATE } from "../user.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 = { state: STATE.client | STATE.remote, @@ -63,6 +63,7 @@ export const addCategory: Act = { aws("error", "existence"); return; } + if (await isRoomDataFull(roomID)) return void aws("error", "limit"); let req = await insert( listCategories.roomID, listCategories.title, @@ -260,6 +261,7 @@ export const addProduct: Act = { aws("error", "existence"); return; } + if (await isRoomDataFull(roomID)) return void aws("error", "limit"); if (!isCategoryInRoom(roomID, data.listCatID)) return void aws("error", "existence"); if (data.parent > -1 && !isProductInRoom(roomID, data.parent)) return void aws("error", "existence"); let req = await insert( @@ -436,6 +438,7 @@ export const addItem: Act = { aws("error", "existence"); 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.listProdID > -1 && !isProductInRoom(roomID, data.listProdID)) return void aws("error", "existence"); let req = await insert( diff --git a/src/api/helper.ts b/src/api/helper.ts index 0ef58c7..36bd4a0 100644 --- a/src/api/helper.ts +++ b/src/api/helper.ts @@ -19,6 +19,34 @@ export const isRoomFull = async (roomID: number) => { 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) => { let currentCount = alias( select([count(rooms.roomID)], rooms)