This commit is contained in:
jusax23 2023-03-24 21:59:51 +01:00
parent e45be71606
commit b0dc1672fc
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
3 changed files with 156 additions and 21 deletions

View file

@ -2,6 +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, listProducts } from "../../sys/db.js"; import { db, listCategories, listProducts } from "../../sys/db.js";
import { isCategoryInRoom, isProductInRoom } from "../helper.js"
export const getCategories: Act = { export const getCategories: Act = {
state: STATE.client | STATE.remote, state: STATE.client | STATE.remote,
@ -90,7 +91,7 @@ export const changeCategory: Act = {
data: { data: {
room: "string", room: "string",
server: "string", server: "string",
catID: "number", listCatID: "number",
title: "string-256", title: "string-256",
color: "string-32" color: "string-32"
}, },
@ -111,7 +112,7 @@ export const changeCategory: Act = {
.set(listCategories.color, data.color) .set(listCategories.color, data.color)
.where(and( .where(and(
eq(listCategories.roomID, roomID), eq(listCategories.roomID, roomID),
eq(listCategories.listCatID, data.catID) eq(listCategories.listCatID, data.listCatID)
)) ))
.query(db); .query(db);
if (req.affectedRows > 0) aws("ok", ""); if (req.affectedRows > 0) aws("ok", "");
@ -125,7 +126,7 @@ export const changeCategoryWeights: Act = {
data: { data: {
room: "string", room: "string",
server: "string", server: "string",
catIDs: "array-number", listCatIDs: "array-number",
weights: "array-number", weights: "array-number",
}, },
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
@ -140,15 +141,15 @@ export const changeCategoryWeights: Act = {
aws("error", "existence"); aws("error", "existence");
return; return;
} }
if (data.weights.length != data.catIDs.length) return void aws("error", "data"); if (data.weights.length != data.listCatIDs.length) return void aws("error", "data");
let affacted = 0; let affacted = 0;
for (let i = 0; i < data.catIDs.length; i++) { for (let i = 0; i < data.listCatIDs.length; i++) {
const catID = data.catIDs[i]; const listCatID = data.listCatIDs[i];
let req = await update(listCategories) let req = await update(listCategories)
.set(listCategories.weight, data.title) .set(listCategories.weight, data.title)
.where(and( .where(and(
eq(listCategories.roomID, roomID), eq(listCategories.roomID, roomID),
eq(listCategories.listCatID, catID) eq(listCategories.listCatID, listCatID)
)) ))
.query(db); .query(db);
affacted += req.affectedRows; affacted += req.affectedRows;
@ -164,7 +165,7 @@ export const deleteCategory: Act = {
data: { data: {
room: "string", room: "string",
server: "string", server: "string",
catID: "number", listCatID: "number",
}, },
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
if (!checkSelfTag(data.server)) { if (!checkSelfTag(data.server)) {
@ -181,7 +182,7 @@ export const deleteCategory: Act = {
let req = await remove(listCategories) let req = await remove(listCategories)
.where(and( .where(and(
eq(listCategories.roomID, roomID), eq(listCategories.roomID, roomID),
eq(listCategories.listCatID, data.catID) eq(listCategories.listCatID, data.listCatID)
)) ))
.query(db); .query(db);
if (req.affectedRows > 0) aws("ok", ""); if (req.affectedRows > 0) aws("ok", "");
@ -233,3 +234,124 @@ export const getProducts: Act = {
} }
}; };
export const addProduct: Act = {
state: STATE.client | STATE.remote,
right: 0,
data: {
room: "string",
server: "string",
title: "string-256",
description: "string-4096",
listCatID: "number",
defUnit: "number",
defValue: "string-256",
ean: "string-64"
},
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
if (!checkSelfTag(data.server)) {
if (client.state != STATE.client) return void aws("error", "right");
let resp = await client.pass(data.server, "addProduct", data);
aws(resp.state, resp.data);
return;
}
let roomID = await client.isInRoom(data.room);
if (roomID == -1) {
aws("error", "existence");
return;
}
if (!isCategoryInRoom(roomID, data.listCatID)) return void aws("error", "existence");
let req = await insert(
listProducts.roomID,
listProducts.title,
listProducts.description,
listProducts.category,
listProducts.defUnit,
listProducts.defValue,
listProducts.ean,
).add(
roomID,
data.title,
data.description,
data.listCatID,
data.defUnit,
data.defValue,
data.ean
).query(db);
if (req.affectedRows > 0) aws("ok", {
listProdID: req.insertId
});
else aws("error", "existence");
}
};
export const changeProduct: Act = {
state: STATE.client | STATE.remote,
right: 0,
data: {
room: "string",
server: "string",
listProdID: "number",
title: "string-256",
description: "string-4096",
listCatID: "number",
defUnit: "number",
defValue: "string-256",
ean: "string-64"
},
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
if (!checkSelfTag(data.server)) {
if (client.state != STATE.client) return void aws("error", "right");
let resp = await client.pass(data.server, "changeProduct", data);
aws(resp.state, resp.data);
return;
}
let roomID = await client.isInRoom(data.room);
if (roomID == -1) {
aws("error", "existence");
return;
}
if (!isCategoryInRoom(roomID, data.listCatID)) return void aws("error", "existence");
if (!isProductInRoom(roomID, data.listProdID)) return void aws("error", "existence");
let req = await update(listProducts)
.set(listProducts.title, data.title)
.set(listProducts.description, data.description)
.set(listProducts.category, data.listCatID)
.set(listProducts.defUnit, data.defUnit)
.set(listProducts.defValue, data.defValue)
.set(listProducts.ean, data.ean)
.where(eq(listProducts.listProdID, data.listProdID))
.query(db);
if (req.affectedRows > 0) aws("ok", "");
else aws("error", "existence");
}
};
export const deleteProduct: Act = {
state: STATE.client | STATE.remote,
right: 0,
data: {
room: "string",
server: "string",
listProdID: "number"
},
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
if (!checkSelfTag(data.server)) {
if (client.state != STATE.client) return void aws("error", "right");
let resp = await client.pass(data.server, "deleteProduct", data);
aws(resp.state, resp.data);
return;
}
let roomID = await client.isInRoom(data.room);
if (roomID == -1) {
aws("error", "existence");
return;
}
if (!isCategoryInRoom(roomID, data.listCatID)) return void aws("error", "existence");
if (!isProductInRoom(roomID, data.listProdID)) return void aws("error", "existence");
let req = await remove(listProducts)
.where(eq(listProducts.listProdID, data.listProdID))
.query(db);
if (req.affectedRows > 0) aws("ok", "");
else aws("error", "existence");
}
};

View file

@ -1,5 +1,5 @@
import { alias, eq, count, select, naturalJoin, innerJoinOn } from "dblang"; import { alias, eq, count, select, innerJoinOn } from "dblang";
import { accounts, db, roomMembers, rooms } from "../sys/db.js"; import { accounts, db, listCategories, listProducts, roomMembers, rooms } from "../sys/db.js";
export const isRoomFull = async (roomID: number) => { export const isRoomFull = async (roomID: number) => {
let currentCount = alias( let currentCount = alias(
@ -34,4 +34,17 @@ export const canCreateRoom = async (accID: number) => {
.query(db); .query(db);
if (maxCount == -1) return true; if (maxCount == -1) return true;
return req[0][currentCount] < req[0][maxCount]; return req[0][currentCount] < req[0][maxCount];
} };
export const isCategoryInRoom = async (roomID: number, catID: number) => {
let res = await select([listCategories.roomID], listCategories)
.where(eq(listCategories.listCatID, catID))
.query(db);
return res.length > 0 && res[0][listCategories.roomID] == roomID;
};
export const isProductInRoom = async (roomID: number, prodID: number) => {
let res = await select([listProducts.roomID], listProducts)
.where(eq(listProducts.listProdID, prodID))
.query(db);
return res.length > 0 && res[0][listProducts.roomID] == roomID;
};

View file

@ -201,8 +201,8 @@ listProducts.addAttributes({
onUpdate: onAction.cascade onUpdate: onAction.cascade
} }
}, },
title: { type: TEXT }, title: { type: VARCHAR(256) },
description: { type: TEXT }, description: { type: VARCHAR(4096) },
category: { category: {
type: INT, type: INT,
foreignKey: { foreignKey: {
@ -212,8 +212,8 @@ listProducts.addAttributes({
} }
}, },
defUnit: { type: SMALLINT }, defUnit: { type: SMALLINT },
defValue: { type: TEXT }, defValue: { type: VARCHAR(256) },
ean: { type: INT, notNull: false } ean: { type: VARCHAR(64), notNull: false }
}); });
listProducts.addAttribute("parent", INT, { listProducts.addAttribute("parent", INT, {
foreignKey: { foreignKey: {